layouts.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. Meteor.call('isDisableRegistration', (_, result) => {
  50. if (result) {
  51. $('.at-signup-link').hide();
  52. }
  53. });
  54. Meteor.call('isDisableForgotPassword', (_, result) => {
  55. if (result) {
  56. $('.at-pwd-link').hide();
  57. }
  58. });
  59. });
  60. Template.userFormsLayout.onRendered(() => {
  61. AccountsTemplates.state.form.keys = new Proxy(
  62. AccountsTemplates.state.form.keys,
  63. validator,
  64. );
  65. const i18nTag = navigator.language;
  66. if (i18nTag) {
  67. T9n.setLanguage(i18nTagToT9n(i18nTag));
  68. }
  69. EscapeActions.executeAll();
  70. });
  71. Template.userFormsLayout.helpers({
  72. currentSetting() {
  73. return Template.instance().currentSetting.get();
  74. },
  75. // isSettingDatabaseCallDone(){
  76. // return isSettingDatabaseFctCallDone;
  77. // },
  78. isLegalNoticeLinkExist(){
  79. const currSet = Template.instance().currentSetting.get();
  80. if(currSet && currSet !== undefined && currSet != null){
  81. return currSet.legalNotice !== undefined && currSet.legalNotice.trim() != "";
  82. }
  83. else
  84. return false;
  85. },
  86. getLegalNoticeWithWritTraduction(){
  87. let spanLegalNoticeElt = $("#legalNoticeSpan");
  88. if(spanLegalNoticeElt != null && spanLegalNoticeElt != undefined){
  89. spanLegalNoticeElt.html(TAPi18n.__('acceptance_of_our_legalNotice', {}, T9n.getLanguage() || 'en'));
  90. }
  91. let atLinkLegalNoticeElt = $("#legalNoticeAtLink");
  92. if(atLinkLegalNoticeElt != null && atLinkLegalNoticeElt != undefined){
  93. atLinkLegalNoticeElt.html(TAPi18n.__('legalNotice', {}, T9n.getLanguage() || 'en'));
  94. }
  95. return true;
  96. },
  97. isLoading() {
  98. return Template.instance().isLoading.get();
  99. },
  100. afterBodyStart() {
  101. return currentSetting.customHTMLafterBodyStart;
  102. },
  103. beforeBodyEnd() {
  104. return currentSetting.customHTMLbeforeBodyEnd;
  105. },
  106. languages() {
  107. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  108. const tag = code;
  109. let name = lang.name;
  110. if (lang.name === 'br') {
  111. name = 'Brezhoneg';
  112. } else if (lang.name === 'ar-EG') {
  113. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  114. name = 'مَصرى';
  115. } else if (lang.name === 'cs-CZ') {
  116. name = 'čeština (Česká republika)';
  117. } else if (lang.name === 'de-CH') {
  118. name = 'Deutsch (Schweiz)';
  119. } else if (lang.name === 'de-AT') {
  120. name = 'Deutsch (Österreich)';
  121. } else if (lang.name === 'en-BR') {
  122. name = 'English (Brazil)';
  123. } else if (lang.name === 'en-DE') {
  124. name = 'English (Germany)';
  125. } else if (lang.name === 'et-EE') {
  126. name = 'eesti keel (Eesti)';
  127. } else if (lang.name === 'fa-IR') {
  128. // fa-IR = Persian (Iran)
  129. name = 'فارسی/پارسی (ایران‎)';
  130. } else if (lang.name === 'fr-BE') {
  131. name = 'Français (Belgique)';
  132. } else if (lang.name === 'fr-CA') {
  133. name = 'Français (Canada)';
  134. } else if (lang.name === 'fr-CH') {
  135. name = 'Français (Schweiz)';
  136. } else if (lang.name === 'gu-IN') {
  137. // gu-IN = Gurajati (India)
  138. name = 'ગુજરાતી';
  139. } else if (lang.name === 'hi-IN') {
  140. // hi-IN = Hindi (India)
  141. name = 'हिंदी (भारत)';
  142. } else if (lang.name === 'ig') {
  143. name = 'Igbo';
  144. } else if (lang.name === 'lv') {
  145. name = 'Latviešu';
  146. } else if (lang.name === 'latviešu valoda') {
  147. name = 'Latviešu';
  148. } else if (lang.name === 'ms-MY') {
  149. // ms-MY = Malay (Malaysia)
  150. name = 'بهاس ملايو';
  151. } else if (lang.name === 'en-IT') {
  152. name = 'English (Italy)';
  153. } else if (lang.name === 'el-GR') {
  154. // el-GR = Greek (Greece)
  155. name = 'Ελληνικά (Ελλάδα)';
  156. } else if (lang.name === 'Español') {
  157. name = 'español';
  158. } else if (lang.name === 'es_419') {
  159. name = 'español de América Latina';
  160. } else if (lang.name === 'es-419') {
  161. name = 'español de América Latina';
  162. } else if (lang.name === 'Español de América Latina') {
  163. name = 'español de América Latina';
  164. } else if (lang.name === 'es-LA') {
  165. name = 'español de América Latina';
  166. } else if (lang.name === 'Español de Argentina') {
  167. name = 'español de Argentina';
  168. } else if (lang.name === 'Español de Chile') {
  169. name = 'español de Chile';
  170. } else if (lang.name === 'Español de Colombia') {
  171. name = 'español de Colombia';
  172. } else if (lang.name === 'Español de México') {
  173. name = 'español de México';
  174. } else if (lang.name === 'es-PY') {
  175. name = 'español de Paraguayo';
  176. } else if (lang.name === 'Español de Paraguayo') {
  177. name = 'español de Paraguayo';
  178. } else if (lang.name === 'Español de Perú') {
  179. name = 'español de Perú';
  180. } else if (lang.name === 'Español de Puerto Rico') {
  181. name = 'español de Puerto Rico';
  182. } else if (lang.name === 'gl-ES') {
  183. name = 'Galego (España)';
  184. } else if (lang.name === 'oc') {
  185. name = 'Occitan';
  186. } else if (lang.name === 'ru-UA') {
  187. name = 'Русский (Украина)';
  188. } else if (lang.name === 'st') {
  189. name = 'Sãotomense';
  190. } else if (lang.name === 'uk-UA') {
  191. name = 'українська (Україна)';
  192. } else if (lang.name === '繁体中文(台湾)') {
  193. // Traditional Chinese (Taiwan)
  194. name = '繁體中文(台灣)';
  195. }
  196. return { tag, name };
  197. }).sort(function(a, b) {
  198. if (a.name === b.name) {
  199. return 0;
  200. } else {
  201. return a.name > b.name ? 1 : -1;
  202. }
  203. });
  204. },
  205. isCurrentLanguage() {
  206. const t9nTag = i18nTagToT9n(this.tag);
  207. const curLang = T9n.getLanguage() || 'en';
  208. return t9nTag === curLang;
  209. },
  210. });
  211. Template.userFormsLayout.events({
  212. 'change .js-userform-set-language'(event) {
  213. const i18nTag = $(event.currentTarget).val();
  214. T9n.setLanguage(i18nTagToT9n(i18nTag));
  215. event.preventDefault();
  216. },
  217. 'click #at-btn'(event, templateInstance) {
  218. if (FlowRouter.getRouteName() === 'atSignIn') {
  219. templateInstance.isLoading.set(true);
  220. authentication(event, templateInstance).then(() => {
  221. templateInstance.isLoading.set(false);
  222. });
  223. }
  224. isCheckDone = false;
  225. },
  226. 'click #at-signUp'(event, templateInstance){
  227. isCheckDone = false;
  228. },
  229. 'DOMSubtreeModified #at-oidc'(event){
  230. if(alreadyCheck <= 2){
  231. let currSetting = Settings.findOne();
  232. let oidcBtnElt = $("#at-oidc");
  233. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  234. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  235. if(alreadyCheck == 1){
  236. alreadyCheck++;
  237. oidcBtnElt.html("");
  238. }
  239. else{
  240. alreadyCheck++;
  241. oidcBtnElt.html(htmlvalue);
  242. }
  243. }
  244. }
  245. else{
  246. alreadyCheck = 1;
  247. }
  248. },
  249. 'DOMSubtreeModified .at-form'(event){
  250. if(alreadyCheck <= 2 && !isCheckDone){
  251. if(document.getElementById("at-oidc") != null){
  252. let currSetting = Settings.findOne();
  253. let oidcBtnElt = $("#at-oidc");
  254. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  255. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  256. if(alreadyCheck == 1){
  257. alreadyCheck++;
  258. oidcBtnElt.html("");
  259. }
  260. else{
  261. alreadyCheck++;
  262. isCheckDone = true;
  263. oidcBtnElt.html(htmlvalue);
  264. }
  265. }
  266. }
  267. }
  268. else{
  269. alreadyCheck = 1;
  270. }
  271. },
  272. });
  273. Template.defaultLayout.events({
  274. 'click .js-close-modal': () => {
  275. Modal.close();
  276. },
  277. });
  278. async function authentication(event, templateInstance) {
  279. const match = $('#at-field-username_and_email').val();
  280. const password = $('#at-field-password').val();
  281. if (!match || !password) return undefined;
  282. const result = await getAuthenticationMethod(
  283. templateInstance.currentSetting.get(),
  284. match,
  285. );
  286. if (result === 'password') return undefined;
  287. // Stop submit #at-pwd-form
  288. event.preventDefault();
  289. event.stopImmediatePropagation();
  290. switch (result) {
  291. case 'ldap':
  292. return new Promise(resolve => {
  293. Meteor.loginWithLDAP(match, password, function() {
  294. resolve(FlowRouter.go('/'));
  295. });
  296. });
  297. case 'saml':
  298. return new Promise(resolve => {
  299. const provider = Meteor.settings.public.SAML_PROVIDER;
  300. Meteor.loginWithSaml(
  301. {
  302. provider,
  303. },
  304. function() {
  305. resolve(FlowRouter.go('/'));
  306. },
  307. );
  308. });
  309. case 'cas':
  310. return new Promise(resolve => {
  311. Meteor.loginWithCas(match, password, function() {
  312. resolve(FlowRouter.go('/'));
  313. });
  314. });
  315. default:
  316. return undefined;
  317. }
  318. }
  319. function getAuthenticationMethod(
  320. { displayAuthenticationMethod, defaultAuthenticationMethod },
  321. match,
  322. ) {
  323. if (displayAuthenticationMethod) {
  324. return $('.select-authentication').val();
  325. }
  326. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  327. }
  328. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  329. return new Promise(resolve => {
  330. try {
  331. Meteor.subscribe('user-authenticationMethod', match, {
  332. onReady() {
  333. const user = Users.findOne();
  334. const authenticationMethod = user
  335. ? user.authenticationMethod
  336. : defaultAuthenticationMethod;
  337. resolve(authenticationMethod);
  338. },
  339. });
  340. } catch (error) {
  341. resolve(defaultAuthenticationMethod);
  342. }
  343. });
  344. }