layouts.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. const validator = {
  8. set(obj, prop, value) {
  9. if (prop === 'state' && value !== 'signIn') {
  10. $('.at-form-authentication').hide();
  11. } else if (prop === 'state' && value === 'signIn') {
  12. $('.at-form-authentication').show();
  13. }
  14. // The default behavior to store the value
  15. obj[prop] = value;
  16. // Indicate success
  17. return true;
  18. },
  19. };
  20. Template.userFormsLayout.onCreated(function() {
  21. const templateInstance = this;
  22. templateInstance.currentSetting = new ReactiveVar();
  23. templateInstance.isLoading = new ReactiveVar(false);
  24. Meteor.subscribe('setting', {
  25. onReady() {
  26. templateInstance.currentSetting.set(Settings.findOne());
  27. let currSetting = templateInstance.currentSetting.curValue;
  28. let oidcBtnElt = $("#at-oidc");
  29. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  30. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  31. oidcBtnElt.html(htmlvalue);
  32. }
  33. return this.stop();
  34. },
  35. });
  36. Meteor.call('isPasswordLoginDisabled', (_, result) => {
  37. if (result) {
  38. $('.at-pwd-form').hide();
  39. }
  40. });
  41. });
  42. Template.userFormsLayout.onRendered(() => {
  43. AccountsTemplates.state.form.keys = new Proxy(
  44. AccountsTemplates.state.form.keys,
  45. validator,
  46. );
  47. const i18nTag = navigator.language;
  48. if (i18nTag) {
  49. T9n.setLanguage(i18nTagToT9n(i18nTag));
  50. }
  51. EscapeActions.executeAll();
  52. });
  53. Template.userFormsLayout.helpers({
  54. currentSetting() {
  55. return Template.instance().currentSetting.get();
  56. },
  57. isLoading() {
  58. return Template.instance().isLoading.get();
  59. },
  60. afterBodyStart() {
  61. return currentSetting.customHTMLafterBodyStart;
  62. },
  63. beforeBodyEnd() {
  64. return currentSetting.customHTMLbeforeBodyEnd;
  65. },
  66. languages() {
  67. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  68. const tag = code;
  69. let name = lang.name;
  70. if (lang.name === 'br') {
  71. name = 'Brezhoneg';
  72. } else if (lang.name === 'ar-EG') {
  73. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  74. name = 'مَصرى';
  75. } else if (lang.name === 'de-CH') {
  76. name = 'Deutsch (Schweiz)';
  77. } else if (lang.name === 'fa-IR') {
  78. // fa-IR = Persian (Iran)
  79. name = 'فارسی/پارسی (ایران‎)';
  80. } else if (lang.name === 'fr-BE') {
  81. name = 'Français (Belgique)';
  82. } else if (lang.name === 'fr-CA') {
  83. name = 'Français (Canada)';
  84. } else if (lang.name === 'fr-CH') {
  85. name = 'Français (Schweiz)';
  86. } else if (lang.name === 'ig') {
  87. name = 'Igbo';
  88. } else if (lang.name === 'lv') {
  89. name = 'Latviešu';
  90. } else if (lang.name === 'latviešu valoda') {
  91. name = 'Latviešu';
  92. } else if (lang.name === 'en-IT') {
  93. name = 'English (Italy)';
  94. } else if (lang.name === 'Español') {
  95. name = 'español';
  96. } else if (lang.name === 'es_419') {
  97. name = 'español de América Latina';
  98. } else if (lang.name === 'es-419') {
  99. name = 'español de América Latina';
  100. } else if (lang.name === 'Español de América Latina') {
  101. name = 'español de América Latina';
  102. } else if (lang.name === 'es-LA') {
  103. name = 'español de América Latina';
  104. } else if (lang.name === 'Español de Argentina') {
  105. name = 'español de Argentina';
  106. } else if (lang.name === 'Español de Chile') {
  107. name = 'español de Chile';
  108. } else if (lang.name === 'Español de Colombia') {
  109. name = 'español de Colombia';
  110. } else if (lang.name === 'Español de México') {
  111. name = 'español de México';
  112. } else if (lang.name === 'es-PY') {
  113. name = 'español de Paraguayo';
  114. } else if (lang.name === 'Español de Paraguayo') {
  115. name = 'español de Paraguayo';
  116. } else if (lang.name === 'Español de Perú') {
  117. name = 'español de Perú';
  118. } else if (lang.name === 'Español de Puerto Rico') {
  119. name = 'español de Puerto Rico';
  120. } else if (lang.name === 'oc') {
  121. name = 'Occitan';
  122. } else if (lang.name === 'st') {
  123. name = 'Sãotomense';
  124. } else if (lang.name === '繁体中文(台湾)') {
  125. name = '繁體中文(台灣)';
  126. }
  127. return { tag, name };
  128. }).sort(function(a, b) {
  129. if (a.name === b.name) {
  130. return 0;
  131. } else {
  132. return a.name > b.name ? 1 : -1;
  133. }
  134. });
  135. },
  136. isCurrentLanguage() {
  137. const t9nTag = i18nTagToT9n(this.tag);
  138. const curLang = T9n.getLanguage() || 'en';
  139. return t9nTag === curLang;
  140. },
  141. });
  142. Template.userFormsLayout.events({
  143. 'change .js-userform-set-language'(event) {
  144. const i18nTag = $(event.currentTarget).val();
  145. T9n.setLanguage(i18nTagToT9n(i18nTag));
  146. event.preventDefault();
  147. },
  148. 'click #at-btn'(event, templateInstance) {
  149. if (FlowRouter.getRouteName() === 'atSignIn') {
  150. templateInstance.isLoading.set(true);
  151. authentication(event, templateInstance).then(() => {
  152. templateInstance.isLoading.set(false);
  153. });
  154. }
  155. },
  156. });
  157. Template.defaultLayout.events({
  158. 'click .js-close-modal': () => {
  159. Modal.close();
  160. },
  161. });
  162. async function authentication(event, templateInstance) {
  163. const match = $('#at-field-username_and_email').val();
  164. const password = $('#at-field-password').val();
  165. if (!match || !password) return undefined;
  166. const result = await getAuthenticationMethod(
  167. templateInstance.currentSetting.get(),
  168. match,
  169. );
  170. if (result === 'password') return undefined;
  171. // Stop submit #at-pwd-form
  172. event.preventDefault();
  173. event.stopImmediatePropagation();
  174. switch (result) {
  175. case 'ldap':
  176. return new Promise(resolve => {
  177. Meteor.loginWithLDAP(match, password, function() {
  178. resolve(FlowRouter.go('/'));
  179. });
  180. });
  181. case 'saml':
  182. return new Promise(resolve => {
  183. const provider = Meteor.settings.public.SAML_PROVIDER;
  184. Meteor.loginWithSaml(
  185. {
  186. provider,
  187. },
  188. function() {
  189. resolve(FlowRouter.go('/'));
  190. },
  191. );
  192. });
  193. case 'cas':
  194. return new Promise(resolve => {
  195. Meteor.loginWithCas(match, password, function() {
  196. resolve(FlowRouter.go('/'));
  197. });
  198. });
  199. default:
  200. return undefined;
  201. }
  202. }
  203. function getAuthenticationMethod(
  204. { displayAuthenticationMethod, defaultAuthenticationMethod },
  205. match,
  206. ) {
  207. if (displayAuthenticationMethod) {
  208. return $('.select-authentication').val();
  209. }
  210. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  211. }
  212. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  213. return new Promise(resolve => {
  214. try {
  215. Meteor.subscribe('user-authenticationMethod', match, {
  216. onReady() {
  217. const user = Users.findOne();
  218. const authenticationMethod = user
  219. ? user.authenticationMethod
  220. : defaultAuthenticationMethod;
  221. resolve(authenticationMethod);
  222. },
  223. });
  224. } catch (error) {
  225. resolve(defaultAuthenticationMethod);
  226. }
  227. });
  228. }