layouts.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. BlazeLayout.setRoot('body');
  2. const i18nTagToT9n = i18nTag => {
  3. // t9n/i18n tags are same now, see: https://github.com/softwarerero/meteor-accounts-t9n/pull/129
  4. // but we keep this conversion function here, to be aware that that they are different system.
  5. return i18nTag;
  6. };
  7. let alreadyCheck = 1;
  8. let isCheckDone = false;
  9. const validator = {
  10. set(obj, prop, value) {
  11. if (prop === 'state' && value !== 'signIn') {
  12. $('.at-form-authentication').hide();
  13. } else if (prop === 'state' && value === 'signIn') {
  14. $('.at-form-authentication').show();
  15. }
  16. // The default behavior to store the value
  17. obj[prop] = value;
  18. // Indicate success
  19. return true;
  20. },
  21. };
  22. // let isSettingDatabaseFctCallDone = false;
  23. Template.userFormsLayout.onCreated(function() {
  24. const templateInstance = this;
  25. templateInstance.currentSetting = new ReactiveVar();
  26. templateInstance.isLoading = new ReactiveVar(false);
  27. Meteor.subscribe('setting', {
  28. onReady() {
  29. templateInstance.currentSetting.set(Settings.findOne());
  30. let currSetting = templateInstance.currentSetting.curValue;
  31. let oidcBtnElt = $("#at-oidc");
  32. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  33. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  34. oidcBtnElt.html(htmlvalue);
  35. }
  36. // isSettingDatabaseFctCallDone = true;
  37. if(currSetting && currSetting !== undefined && currSetting.customLoginLogoImageUrl !== undefined)
  38. document.getElementById("isSettingDatabaseCallDone").style.display = 'none';
  39. else
  40. document.getElementById("isSettingDatabaseCallDone").style.display = 'block';
  41. return this.stop();
  42. },
  43. });
  44. Meteor.call('isPasswordLoginDisabled', (_, result) => {
  45. if (result) {
  46. $('.at-pwd-form').hide();
  47. }
  48. });
  49. });
  50. Template.userFormsLayout.onRendered(() => {
  51. AccountsTemplates.state.form.keys = new Proxy(
  52. AccountsTemplates.state.form.keys,
  53. validator,
  54. );
  55. const i18nTag = navigator.language;
  56. if (i18nTag) {
  57. T9n.setLanguage(i18nTagToT9n(i18nTag));
  58. }
  59. EscapeActions.executeAll();
  60. });
  61. Template.userFormsLayout.helpers({
  62. currentSetting() {
  63. return Template.instance().currentSetting.get();
  64. },
  65. // isSettingDatabaseCallDone(){
  66. // return isSettingDatabaseFctCallDone;
  67. // },
  68. isLegalNoticeLinkExist(){
  69. const currSet = Template.instance().currentSetting.get();
  70. if(currSet && currSet !== undefined && currSet != null){
  71. return currSet.legalNotice !== undefined && currSet.legalNotice.trim() != "";
  72. }
  73. else
  74. return false;
  75. },
  76. getLegalNoticeWithWritTraduction(){
  77. let spanLegalNoticeElt = $("#legalNoticeSpan");
  78. if(spanLegalNoticeElt != null && spanLegalNoticeElt != undefined){
  79. spanLegalNoticeElt.html(TAPi18n.__('acceptance_of_our_legalNotice', {}, T9n.getLanguage() || 'en'));
  80. }
  81. let atLinkLegalNoticeElt = $("#legalNoticeAtLink");
  82. if(atLinkLegalNoticeElt != null && atLinkLegalNoticeElt != undefined){
  83. atLinkLegalNoticeElt.html(TAPi18n.__('legalNotice', {}, T9n.getLanguage() || 'en'));
  84. }
  85. return true;
  86. },
  87. isLoading() {
  88. return Template.instance().isLoading.get();
  89. },
  90. afterBodyStart() {
  91. return currentSetting.customHTMLafterBodyStart;
  92. },
  93. beforeBodyEnd() {
  94. return currentSetting.customHTMLbeforeBodyEnd;
  95. },
  96. languages() {
  97. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  98. const tag = code;
  99. let name = lang.name;
  100. if (lang.name === 'br') {
  101. name = 'Brezhoneg';
  102. } else if (lang.name === 'ar-EG') {
  103. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  104. name = 'مَصرى';
  105. } else if (lang.name === 'de-CH') {
  106. name = 'Deutsch (Schweiz)';
  107. } else if (lang.name === 'de-AT') {
  108. name = 'Deutsch (Österreich)';
  109. } else if (lang.name === 'en-DE') {
  110. name = 'English (Germany)';
  111. } else if (lang.name === 'fa-IR') {
  112. // fa-IR = Persian (Iran)
  113. name = 'فارسی/پارسی (ایران‎)';
  114. } else if (lang.name === 'fr-BE') {
  115. name = 'Français (Belgique)';
  116. } else if (lang.name === 'fr-CA') {
  117. name = 'Français (Canada)';
  118. } else if (lang.name === 'fr-CH') {
  119. name = 'Français (Schweiz)';
  120. } else if (lang.name === 'gu-IN') {
  121. // gu-IN = Gurajati (India)
  122. name = 'ગુજરાતી';
  123. } else if (lang.name === 'hi-IN') {
  124. // hi-IN = Hindi (India)
  125. name = 'हिंदी (भारत)';
  126. } else if (lang.name === 'ig') {
  127. name = 'Igbo';
  128. } else if (lang.name === 'lv') {
  129. name = 'Latviešu';
  130. } else if (lang.name === 'latviešu valoda') {
  131. name = 'Latviešu';
  132. } else if (lang.name === 'ms-MY') {
  133. // ms-MY = Malay (Malaysia)
  134. name = 'بهاس ملايو';
  135. } else if (lang.name === 'en-IT') {
  136. name = 'English (Italy)';
  137. } else if (lang.name === 'el-GR') {
  138. // el-GR = Greek (Greece)
  139. name = 'Ελληνικά (Ελλάδα)';
  140. } else if (lang.name === 'Español') {
  141. name = 'español';
  142. } else if (lang.name === 'es_419') {
  143. name = 'español de América Latina';
  144. } else if (lang.name === 'es-419') {
  145. name = 'español de América Latina';
  146. } else if (lang.name === 'Español de América Latina') {
  147. name = 'español de América Latina';
  148. } else if (lang.name === 'es-LA') {
  149. name = 'español de América Latina';
  150. } else if (lang.name === 'Español de Argentina') {
  151. name = 'español de Argentina';
  152. } else if (lang.name === 'Español de Chile') {
  153. name = 'español de Chile';
  154. } else if (lang.name === 'Español de Colombia') {
  155. name = 'español de Colombia';
  156. } else if (lang.name === 'Español de México') {
  157. name = 'español de México';
  158. } else if (lang.name === 'es-PY') {
  159. name = 'español de Paraguayo';
  160. } else if (lang.name === 'Español de Paraguayo') {
  161. name = 'español de Paraguayo';
  162. } else if (lang.name === 'Español de Perú') {
  163. name = 'español de Perú';
  164. } else if (lang.name === 'Español de Puerto Rico') {
  165. name = 'español de Puerto Rico';
  166. } else if (lang.name === 'oc') {
  167. name = 'Occitan';
  168. } else if (lang.name === 'st') {
  169. name = 'Sãotomense';
  170. } else if (lang.name === '繁体中文(台湾)') {
  171. // Traditional Chinese (Taiwan)
  172. name = '繁體中文(台灣)';
  173. }
  174. return { tag, name };
  175. }).sort(function(a, b) {
  176. if (a.name === b.name) {
  177. return 0;
  178. } else {
  179. return a.name > b.name ? 1 : -1;
  180. }
  181. });
  182. },
  183. isCurrentLanguage() {
  184. const t9nTag = i18nTagToT9n(this.tag);
  185. const curLang = T9n.getLanguage() || 'en';
  186. return t9nTag === curLang;
  187. },
  188. });
  189. Template.userFormsLayout.events({
  190. 'change .js-userform-set-language'(event) {
  191. const i18nTag = $(event.currentTarget).val();
  192. T9n.setLanguage(i18nTagToT9n(i18nTag));
  193. event.preventDefault();
  194. },
  195. 'click #at-btn'(event, templateInstance) {
  196. if (FlowRouter.getRouteName() === 'atSignIn') {
  197. templateInstance.isLoading.set(true);
  198. authentication(event, templateInstance).then(() => {
  199. templateInstance.isLoading.set(false);
  200. });
  201. }
  202. isCheckDone = false;
  203. },
  204. 'click #at-signUp'(event, templateInstance){
  205. isCheckDone = false;
  206. },
  207. 'DOMSubtreeModified #at-oidc'(event){
  208. if(alreadyCheck <= 2){
  209. let currSetting = Settings.findOne();
  210. let oidcBtnElt = $("#at-oidc");
  211. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  212. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  213. if(alreadyCheck == 1){
  214. alreadyCheck++;
  215. oidcBtnElt.html("");
  216. }
  217. else{
  218. alreadyCheck++;
  219. oidcBtnElt.html(htmlvalue);
  220. }
  221. }
  222. }
  223. else{
  224. alreadyCheck = 1;
  225. }
  226. },
  227. 'DOMSubtreeModified .at-form'(event){
  228. if(alreadyCheck <= 2 && !isCheckDone){
  229. if(document.getElementById("at-oidc") != null){
  230. let currSetting = Settings.findOne();
  231. let oidcBtnElt = $("#at-oidc");
  232. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  233. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  234. if(alreadyCheck == 1){
  235. alreadyCheck++;
  236. oidcBtnElt.html("");
  237. }
  238. else{
  239. alreadyCheck++;
  240. isCheckDone = true;
  241. oidcBtnElt.html(htmlvalue);
  242. }
  243. }
  244. }
  245. }
  246. else{
  247. alreadyCheck = 1;
  248. }
  249. },
  250. });
  251. Template.defaultLayout.events({
  252. 'click .js-close-modal': () => {
  253. Modal.close();
  254. },
  255. });
  256. async function authentication(event, templateInstance) {
  257. const match = $('#at-field-username_and_email').val();
  258. const password = $('#at-field-password').val();
  259. if (!match || !password) return undefined;
  260. const result = await getAuthenticationMethod(
  261. templateInstance.currentSetting.get(),
  262. match,
  263. );
  264. if (result === 'password') return undefined;
  265. // Stop submit #at-pwd-form
  266. event.preventDefault();
  267. event.stopImmediatePropagation();
  268. switch (result) {
  269. case 'ldap':
  270. return new Promise(resolve => {
  271. Meteor.loginWithLDAP(match, password, function() {
  272. resolve(FlowRouter.go('/'));
  273. });
  274. });
  275. case 'saml':
  276. return new Promise(resolve => {
  277. const provider = Meteor.settings.public.SAML_PROVIDER;
  278. Meteor.loginWithSaml(
  279. {
  280. provider,
  281. },
  282. function() {
  283. resolve(FlowRouter.go('/'));
  284. },
  285. );
  286. });
  287. case 'cas':
  288. return new Promise(resolve => {
  289. Meteor.loginWithCas(match, password, function() {
  290. resolve(FlowRouter.go('/'));
  291. });
  292. });
  293. default:
  294. return undefined;
  295. }
  296. }
  297. function getAuthenticationMethod(
  298. { displayAuthenticationMethod, defaultAuthenticationMethod },
  299. match,
  300. ) {
  301. if (displayAuthenticationMethod) {
  302. return $('.select-authentication').val();
  303. }
  304. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  305. }
  306. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  307. return new Promise(resolve => {
  308. try {
  309. Meteor.subscribe('user-authenticationMethod', match, {
  310. onReady() {
  311. const user = Users.findOne();
  312. const authenticationMethod = user
  313. ? user.authenticationMethod
  314. : defaultAuthenticationMethod;
  315. resolve(authenticationMethod);
  316. },
  317. });
  318. } catch (error) {
  319. resolve(defaultAuthenticationMethod);
  320. }
  321. });
  322. }