layouts.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 === 'ar-EG') {
  67. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  68. name = 'مَصرى';
  69. } else if (lang.name === 'fa-IR') {
  70. // fa-IR = Persian (Iran)
  71. name = 'فارسی/پارسی (ایران‎)';
  72. } else if (lang.name === 'fr-BE') {
  73. name = 'Français (Belgique)';
  74. } else if (lang.name === 'fr-CA') {
  75. name = 'Français (Canada)';
  76. } else if (lang.name === 'ig') {
  77. name = 'Igbo';
  78. } else if (lang.name === 'lv') {
  79. name = 'Latviešu';
  80. } else if (lang.name === 'latviešu valoda') {
  81. name = 'Latviešu';
  82. } else if (lang.name === 'Español') {
  83. name = 'español';
  84. } else if (lang.name === 'es_419') {
  85. name = 'español de América Latina';
  86. } else if (lang.name === 'es-419') {
  87. name = 'español de América Latina';
  88. } else if (lang.name === 'Español de América Latina') {
  89. name = 'español de América Latina';
  90. } else if (lang.name === 'es-LA') {
  91. name = 'español de América Latina';
  92. } else if (lang.name === 'Español de Argentina') {
  93. name = 'español de Argentina';
  94. } else if (lang.name === 'Español de Chile') {
  95. name = 'español de Chile';
  96. } else if (lang.name === 'Español de Colombia') {
  97. name = 'español de Colombia';
  98. } else if (lang.name === 'Español de México') {
  99. name = 'español de México';
  100. } else if (lang.name === 'es-PY') {
  101. name = 'español de Paraguayo';
  102. } else if (lang.name === 'Español de Paraguayo') {
  103. name = 'español de Paraguayo';
  104. } else if (lang.name === 'Español de Perú') {
  105. name = 'español de Perú';
  106. } else if (lang.name === 'Español de Puerto Rico') {
  107. name = 'español de Puerto Rico';
  108. } else if (lang.name === 'oc') {
  109. name = 'Occitan';
  110. } else if (lang.name === 'st') {
  111. name = 'Sãotomense';
  112. } else if (lang.name === '繁体中文(台湾)') {
  113. name = '繁體中文(台灣)';
  114. }
  115. return { tag, name };
  116. }).sort(function(a, b) {
  117. if (a.name === b.name) {
  118. return 0;
  119. } else {
  120. return a.name > b.name ? 1 : -1;
  121. }
  122. });
  123. },
  124. isCurrentLanguage() {
  125. const t9nTag = i18nTagToT9n(this.tag);
  126. const curLang = T9n.getLanguage() || 'en';
  127. return t9nTag === curLang;
  128. },
  129. });
  130. Template.userFormsLayout.events({
  131. 'change .js-userform-set-language'(event) {
  132. const i18nTag = $(event.currentTarget).val();
  133. T9n.setLanguage(i18nTagToT9n(i18nTag));
  134. event.preventDefault();
  135. },
  136. 'click #at-btn'(event, templateInstance) {
  137. if (FlowRouter.getRouteName() === 'atSignIn') {
  138. templateInstance.isLoading.set(true);
  139. authentication(event, templateInstance).then(() => {
  140. templateInstance.isLoading.set(false);
  141. });
  142. }
  143. },
  144. });
  145. Template.defaultLayout.events({
  146. 'click .js-close-modal': () => {
  147. Modal.close();
  148. },
  149. });
  150. async function authentication(event, templateInstance) {
  151. const match = $('#at-field-username_and_email').val();
  152. const password = $('#at-field-password').val();
  153. if (!match || !password) return undefined;
  154. const result = await getAuthenticationMethod(
  155. templateInstance.currentSetting.get(),
  156. match,
  157. );
  158. if (result === 'password') return undefined;
  159. // Stop submit #at-pwd-form
  160. event.preventDefault();
  161. event.stopImmediatePropagation();
  162. switch (result) {
  163. case 'ldap':
  164. return new Promise(resolve => {
  165. Meteor.loginWithLDAP(match, password, function() {
  166. resolve(FlowRouter.go('/'));
  167. });
  168. });
  169. case 'saml':
  170. return new Promise(resolve => {
  171. const provider = Meteor.settings.public.SAML_PROVIDER;
  172. Meteor.loginWithSaml(
  173. {
  174. provider,
  175. },
  176. function() {
  177. resolve(FlowRouter.go('/'));
  178. },
  179. );
  180. });
  181. case 'cas':
  182. return new Promise(resolve => {
  183. Meteor.loginWithCas(match, password, function() {
  184. resolve(FlowRouter.go('/'));
  185. });
  186. });
  187. default:
  188. return undefined;
  189. }
  190. }
  191. function getAuthenticationMethod(
  192. { displayAuthenticationMethod, defaultAuthenticationMethod },
  193. match,
  194. ) {
  195. if (displayAuthenticationMethod) {
  196. return $('.select-authentication').val();
  197. }
  198. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  199. }
  200. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  201. return new Promise(resolve => {
  202. try {
  203. Meteor.subscribe('user-authenticationMethod', match, {
  204. onReady() {
  205. const user = Users.findOne();
  206. const authenticationMethod = user
  207. ? user.authenticationMethod
  208. : defaultAuthenticationMethod;
  209. resolve(authenticationMethod);
  210. },
  211. });
  212. } catch (error) {
  213. resolve(defaultAuthenticationMethod);
  214. }
  215. });
  216. }