layouts.js 7.0 KB

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