2
0

layouts.js 6.9 KB

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