layouts.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. return this.stop();
  28. },
  29. });
  30. Meteor.call('isPasswordLoginDisabled', (_, result) => {
  31. if (result) {
  32. $('.at-pwd-form').hide();
  33. }
  34. });
  35. });
  36. Template.userFormsLayout.onRendered(() => {
  37. AccountsTemplates.state.form.keys = new Proxy(
  38. AccountsTemplates.state.form.keys,
  39. validator,
  40. );
  41. const i18nTag = navigator.language;
  42. if (i18nTag) {
  43. T9n.setLanguage(i18nTagToT9n(i18nTag));
  44. }
  45. EscapeActions.executeAll();
  46. });
  47. Template.userFormsLayout.helpers({
  48. currentSetting() {
  49. return Template.instance().currentSetting.get();
  50. },
  51. isLoading() {
  52. return Template.instance().isLoading.get();
  53. },
  54. afterBodyStart() {
  55. return currentSetting.customHTMLafterBodyStart;
  56. },
  57. beforeBodyEnd() {
  58. return currentSetting.customHTMLbeforeBodyEnd;
  59. },
  60. languages() {
  61. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  62. const tag = code;
  63. let name = lang.name;
  64. if (lang.name === 'br') {
  65. name = 'Brezhoneg';
  66. } else if (lang.name === 'Español') {
  67. name = 'español';
  68. } else if (lang.name === 'Español de Argentina') {
  69. name = 'español de Argentina';
  70. } else if (lang.name === 'Español de Chile') {
  71. name = 'español de Chile';
  72. } else if (lang.name === 'Español de Colombia') {
  73. name = 'español de Colombia';
  74. } else if (lang.name === 'Español de México') {
  75. name = 'español de México';
  76. } else if (lang.name === 'Español de Puerto Rico') {
  77. name = 'español de Puerto Rico';
  78. } else if (lang.name === 'fr-BE') {
  79. name = 'Français (Belgique)';
  80. } else if (lang.name === 'fr-CA') {
  81. name = 'Français (Canada)';
  82. } else if (lang.name === 'fr-CA') {
  83. name = 'Français (Canada)';
  84. } else if (lang.name === 'ig') {
  85. name = 'Igbo';
  86. } else if (lang.name === 'lv') {
  87. name = 'Latviešu';
  88. } else if (lang.name === 'latviešu valoda') {
  89. name = 'Latviešu';
  90. } else if (lang.name === 'oc') {
  91. name = 'Occitan';
  92. } else if (lang.name === 'st') {
  93. name = 'Sãotomense';
  94. } else if (lang.name === '繁体中文(台湾)') {
  95. name = '繁體中文(台灣)';
  96. }
  97. return { tag, name };
  98. }).sort(function(a, b) {
  99. if (a.name === b.name) {
  100. return 0;
  101. } else {
  102. return a.name > b.name ? 1 : -1;
  103. }
  104. });
  105. },
  106. isCurrentLanguage() {
  107. const t9nTag = i18nTagToT9n(this.tag);
  108. const curLang = T9n.getLanguage() || 'en';
  109. return t9nTag === curLang;
  110. },
  111. });
  112. Template.userFormsLayout.events({
  113. 'change .js-userform-set-language'(event) {
  114. const i18nTag = $(event.currentTarget).val();
  115. T9n.setLanguage(i18nTagToT9n(i18nTag));
  116. event.preventDefault();
  117. },
  118. 'click #at-btn'(event, templateInstance) {
  119. if (FlowRouter.getRouteName() === 'atSignIn') {
  120. templateInstance.isLoading.set(true);
  121. authentication(event, templateInstance).then(() => {
  122. templateInstance.isLoading.set(false);
  123. });
  124. }
  125. },
  126. });
  127. Template.defaultLayout.events({
  128. 'click .js-close-modal': () => {
  129. Modal.close();
  130. },
  131. });
  132. async function authentication(event, templateInstance) {
  133. const match = $('#at-field-username_and_email').val();
  134. const password = $('#at-field-password').val();
  135. if (!match || !password) return undefined;
  136. const result = await getAuthenticationMethod(
  137. templateInstance.currentSetting.get(),
  138. match,
  139. );
  140. if (result === 'password') return undefined;
  141. // Stop submit #at-pwd-form
  142. event.preventDefault();
  143. event.stopImmediatePropagation();
  144. switch (result) {
  145. case 'ldap':
  146. return new Promise(resolve => {
  147. Meteor.loginWithLDAP(match, password, function() {
  148. resolve(FlowRouter.go('/'));
  149. });
  150. });
  151. case 'saml':
  152. return new Promise(resolve => {
  153. const provider = Meteor.settings.public.SAML_PROVIDER;
  154. Meteor.loginWithSaml(
  155. {
  156. provider,
  157. },
  158. function() {
  159. resolve(FlowRouter.go('/'));
  160. },
  161. );
  162. });
  163. case 'cas':
  164. return new Promise(resolve => {
  165. Meteor.loginWithCas(match, password, function() {
  166. resolve(FlowRouter.go('/'));
  167. });
  168. });
  169. default:
  170. return undefined;
  171. }
  172. }
  173. function getAuthenticationMethod(
  174. { displayAuthenticationMethod, defaultAuthenticationMethod },
  175. match,
  176. ) {
  177. if (displayAuthenticationMethod) {
  178. return $('.select-authentication').val();
  179. }
  180. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  181. }
  182. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  183. return new Promise(resolve => {
  184. try {
  185. Meteor.subscribe('user-authenticationMethod', match, {
  186. onReady() {
  187. const user = Users.findOne();
  188. const authenticationMethod = user
  189. ? user.authenticationMethod
  190. : defaultAuthenticationMethod;
  191. resolve(authenticationMethod);
  192. },
  193. });
  194. } catch (error) {
  195. resolve(defaultAuthenticationMethod);
  196. }
  197. });
  198. }