layouts.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { ReactiveCache } from '/imports/reactiveCache';
  2. import { TAPi18n } from '/imports/i18n';
  3. BlazeLayout.setRoot('body');
  4. let alreadyCheck = 1;
  5. let isCheckDone = false;
  6. let counter = 0;
  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. if (!ReactiveCache.getCurrentUser()?.profile) {
  25. Meteor.call('isOidcRedirectionEnabled', (_, result) => {
  26. if (result) {
  27. AccountsTemplates.options.socialLoginStyle = 'redirect';
  28. options = {
  29. loginStyle: AccountsTemplates.options.socialLoginStyle,
  30. };
  31. Meteor.loginWithOidc(options);
  32. }
  33. });
  34. Meteor.subscribe('setting', {
  35. onReady() {
  36. templateInstance.currentSetting.set(ReactiveCache.getCurrentSetting());
  37. return this.stop();
  38. },
  39. });
  40. }
  41. });
  42. Template.userFormsLayout.onRendered(() => {
  43. Meteor.call('getAuthenticationsEnabled', (_, result) => {
  44. let enabledAuthenticationMethods = [ 'password' ]; // we show/hide this based on isPasswordLoginEnabled
  45. if (result) {
  46. Object.keys(result).forEach((m) => {
  47. if (result[m]) enabledAuthenticationMethods.push(m);
  48. });
  49. }
  50. Meteor.call('isPasswordLoginEnabled', (_, result) => {
  51. if (result) {
  52. $('.at-pwd-form').show();
  53. }
  54. });
  55. Meteor.call('isDisableRegistration', (_, result) => {
  56. if (result) {
  57. $('.at-signup-link').hide();
  58. }
  59. });
  60. Meteor.call('isDisableForgotPassword', (_, result) => {
  61. if (result) {
  62. $('.at-pwd-link').hide();
  63. }
  64. });
  65. if (enabledAuthenticationMethods.indexOf('oauth2') !== -1) {
  66. // TODO find better way to run this code once the oauth2 UI is injected in the DOM
  67. (function waitForElementAndShow() {
  68. if (!$('.at-oauth')[0]) return setTimeout(waitForElementAndShow, 100);
  69. $('.at-oauth').show();
  70. })();
  71. }
  72. AccountsTemplates.state.form.keys = new Proxy(
  73. AccountsTemplates.state.form.keys,
  74. validator,
  75. );
  76. EscapeActions.executeAll();
  77. // Add autocomplete attribute to login input for WCAG compliance
  78. const loginInput = document.querySelector('input[type="text"], input[type="email"]');
  79. if (loginInput && loginInput.name && (loginInput.name.toLowerCase().includes('user') || loginInput.name.toLowerCase().includes('email'))) {
  80. loginInput.setAttribute('autocomplete', 'username email');
  81. }
  82. // Add autocomplete attributes to password fields for WCAG compliance
  83. const passwordInputs = document.querySelectorAll('input[type="password"]');
  84. passwordInputs.forEach(input => {
  85. if (input.name && input.name.includes('password')) {
  86. if (input.name.includes('password_again') || input.name.includes('new_password')) {
  87. input.setAttribute('autocomplete', 'new-password');
  88. } else {
  89. input.setAttribute('autocomplete', 'current-password');
  90. }
  91. }
  92. });
  93. });
  94. });
  95. Template.userFormsLayout.helpers({
  96. isLegalNoticeLinkExist() {
  97. const currSet = Template.instance().currentSetting.get();
  98. if (currSet && currSet !== undefined && currSet != null) {
  99. return currSet.legalNotice !== undefined && currSet.legalNotice.trim() != "";
  100. }
  101. else
  102. return false;
  103. },
  104. getLegalNoticeWithWritTraduction() {
  105. let spanLegalNoticeElt = $("#legalNoticeSpan");
  106. if (spanLegalNoticeElt != null && spanLegalNoticeElt != undefined) {
  107. spanLegalNoticeElt.html(TAPi18n.__('acceptance_of_our_legalNotice', {}));
  108. }
  109. let atLinkLegalNoticeElt = $("#legalNoticeAtLink");
  110. if (atLinkLegalNoticeElt != null && atLinkLegalNoticeElt != undefined) {
  111. atLinkLegalNoticeElt.html(TAPi18n.__('legalNotice', {}));
  112. }
  113. return true;
  114. },
  115. isLoading() {
  116. return Template.instance().isLoading.get();
  117. },
  118. afterBodyStart() {
  119. return currentSetting.customHTMLafterBodyStart;
  120. },
  121. beforeBodyEnd() {
  122. return currentSetting.customHTMLbeforeBodyEnd;
  123. },
  124. languages() {
  125. return TAPi18n.getSupportedLanguages()
  126. .map(({ tag, name }) => ({ tag: tag, name }))
  127. .sort((a, b) => {
  128. if (a.name === b.name) {
  129. return 0;
  130. } else {
  131. return a.name > b.name ? 1 : -1;
  132. }
  133. });
  134. },
  135. isCurrentLanguage() {
  136. const curLang = TAPi18n.getLanguage();
  137. return this.tag === curLang;
  138. },
  139. });
  140. Template.userFormsLayout.events({
  141. 'change .js-userform-set-language'(event) {
  142. const tag = $(event.currentTarget).val();
  143. TAPi18n.setLanguage(tag);
  144. event.preventDefault();
  145. },
  146. 'click #at-btn'(event, templateInstance) {
  147. if (FlowRouter.getRouteName() === 'atSignIn') {
  148. templateInstance.isLoading.set(true);
  149. authentication(event, templateInstance).then(() => {
  150. templateInstance.isLoading.set(false);
  151. });
  152. }
  153. isCheckDone = false;
  154. },
  155. 'click #at-signUp'(event, templateInstance) {
  156. isCheckDone = false;
  157. },
  158. 'DOMSubtreeModified #at-oidc'(event) {
  159. if (alreadyCheck <= 2) {
  160. let currSetting = ReactiveCache.getCurrentSetting();
  161. let oidcBtnElt = $("#at-oidc");
  162. if (currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined) {
  163. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  164. if (alreadyCheck == 1) {
  165. alreadyCheck++;
  166. oidcBtnElt.html("");
  167. }
  168. else {
  169. alreadyCheck++;
  170. oidcBtnElt.html(htmlvalue);
  171. }
  172. }
  173. }
  174. else {
  175. alreadyCheck = 1;
  176. }
  177. },
  178. 'DOMSubtreeModified .at-form'(event) {
  179. if (alreadyCheck <= 2 && !isCheckDone) {
  180. if (document.getElementById("at-oidc") != null) {
  181. let currSetting = ReactiveCache.getCurrentSetting();
  182. let oidcBtnElt = $("#at-oidc");
  183. if (currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined) {
  184. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  185. if (alreadyCheck == 1) {
  186. alreadyCheck++;
  187. oidcBtnElt.html("");
  188. }
  189. else {
  190. alreadyCheck++;
  191. isCheckDone = true;
  192. oidcBtnElt.html(htmlvalue);
  193. }
  194. }
  195. }
  196. }
  197. else {
  198. alreadyCheck = 1;
  199. }
  200. },
  201. });
  202. Template.defaultLayout.events({
  203. 'click .js-close-modal': () => {
  204. Modal.close();
  205. },
  206. });
  207. async function authentication(event, templateInstance) {
  208. const match = $('#at-field-username_and_email').val();
  209. const password = $('#at-field-password').val();
  210. if (!match || !password) return undefined;
  211. const result = await getAuthenticationMethod(
  212. templateInstance.currentSetting.get(),
  213. match,
  214. );
  215. if (result === 'password') return undefined;
  216. // Stop submit #at-pwd-form
  217. event.preventDefault();
  218. event.stopImmediatePropagation();
  219. switch (result) {
  220. case 'ldap':
  221. return new Promise(resolve => {
  222. Meteor.loginWithLDAP(match, password, function () {
  223. resolve(FlowRouter.go('/'));
  224. });
  225. });
  226. case 'saml':
  227. return new Promise(resolve => {
  228. const provider = Meteor.settings.public.SAML_PROVIDER;
  229. Meteor.loginWithSaml(
  230. {
  231. provider,
  232. },
  233. function () {
  234. resolve(FlowRouter.go('/'));
  235. },
  236. );
  237. });
  238. case 'cas':
  239. return new Promise(resolve => {
  240. Meteor.loginWithCas(match, password, function () {
  241. resolve(FlowRouter.go('/'));
  242. });
  243. });
  244. default:
  245. return undefined;
  246. }
  247. }
  248. function getAuthenticationMethod(
  249. { displayAuthenticationMethod, defaultAuthenticationMethod },
  250. match,
  251. ) {
  252. if (displayAuthenticationMethod) {
  253. return $('.select-authentication').val();
  254. }
  255. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  256. }
  257. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  258. return new Promise(resolve => {
  259. try {
  260. Meteor.subscribe('user-authenticationMethod', match, {
  261. onReady() {
  262. const user = Users.findOne();
  263. const authenticationMethod = user
  264. ? user.authenticationMethod
  265. : defaultAuthenticationMethod;
  266. resolve(authenticationMethod);
  267. },
  268. });
  269. } catch (error) {
  270. resolve(defaultAuthenticationMethod);
  271. }
  272. });
  273. }