layouts.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 instance = this;
  22. instance.currentSetting = new ReactiveVar();
  23. Meteor.subscribe('setting', {
  24. onReady() {
  25. instance.currentSetting.set(Settings.findOne());
  26. return this.stop();
  27. },
  28. });
  29. });
  30. Template.userFormsLayout.onRendered(() => {
  31. AccountsTemplates.state.form.keys = new Proxy(AccountsTemplates.state.form.keys, validator);
  32. const i18nTag = navigator.language;
  33. if (i18nTag) {
  34. T9n.setLanguage(i18nTagToT9n(i18nTag));
  35. }
  36. EscapeActions.executeAll();
  37. });
  38. Template.userFormsLayout.helpers({
  39. currentSetting() {
  40. return Template.instance().currentSetting.get();
  41. },
  42. afterBodyStart() {
  43. return currentSetting.customHTMLafterBodyStart;
  44. },
  45. beforeBodyEnd() {
  46. return currentSetting.customHTMLbeforeBodyEnd;
  47. },
  48. languages() {
  49. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  50. const tag = code;
  51. let name = lang.name;
  52. if (lang.name === 'br') {
  53. name = 'Brezhoneg';
  54. } else if (lang.name === 'ig') {
  55. name = 'Igbo';
  56. }
  57. return { tag, name };
  58. }).sort(function(a, b) {
  59. if (a.name === b.name) {
  60. return 0;
  61. } else {
  62. return a.name > b.name ? 1 : -1;
  63. }
  64. });
  65. },
  66. isCurrentLanguage() {
  67. const t9nTag = i18nTagToT9n(this.tag);
  68. const curLang = T9n.getLanguage() || 'en';
  69. return t9nTag === curLang;
  70. },
  71. });
  72. Template.userFormsLayout.events({
  73. 'change .js-userform-set-language'(evt) {
  74. const i18nTag = $(evt.currentTarget).val();
  75. T9n.setLanguage(i18nTagToT9n(i18nTag));
  76. evt.preventDefault();
  77. },
  78. 'click #at-btn'(event, instance) {
  79. if (FlowRouter.getRouteName() === 'atSignIn') {
  80. authentication(event, instance);
  81. }
  82. },
  83. });
  84. Template.defaultLayout.events({
  85. 'click .js-close-modal': () => {
  86. Modal.close();
  87. },
  88. });
  89. async function authentication(event, instance) {
  90. // If header login id is set, use it for login.
  91. // Header username = Email address
  92. // Header password = Login ID
  93. // Not user currently: req.headers[process.env.HEADER_LOGIN_FIRSTNAME]
  94. // and req.headers[process.env.HEADER_LOGIN_LASTNAME]
  95. const match = req.headers[process.env.HEADER_LOGIN_EMAIL] || $('#at-field-username_and_email').val();
  96. const password = req.headers[process.env.HEADER_LOGIN_ID] || $('#at-field-password').val();
  97. if (!match || !password) return;
  98. const result = await getAuthenticationMethod(instance.currentSetting.get(), match);
  99. if (result === 'password') return;
  100. // If header login id is not set, don't try to login automatically.
  101. if (!process.env.HEADER_LOGIN_ID) {
  102. // Stop submit #at-pwd-form
  103. event.preventDefault();
  104. event.stopImmediatePropagation();
  105. }
  106. switch (result) {
  107. case 'ldap':
  108. Meteor.loginWithLDAP(match, password, function() {
  109. FlowRouter.go('/');
  110. });
  111. break;
  112. case 'cas':
  113. Meteor.loginWithCas(function() {
  114. FlowRouter.go('/');
  115. });
  116. break;
  117. default:
  118. break;
  119. }
  120. }
  121. function getAuthenticationMethod({displayAuthenticationMethod, defaultAuthenticationMethod}, match) {
  122. if (displayAuthenticationMethod) {
  123. return $('.select-authentication').val();
  124. }
  125. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  126. }
  127. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  128. return new Promise((resolve) => {
  129. try {
  130. Meteor.subscribe('user-authenticationMethod', match, {
  131. onReady() {
  132. const user = Users.findOne();
  133. const authenticationMethod = user
  134. ? user.authenticationMethod
  135. : defaultAuthenticationMethod;
  136. resolve(authenticationMethod);
  137. },
  138. });
  139. } catch(error) {
  140. resolve(defaultAuthenticationMethod);
  141. }
  142. });
  143. }