layouts.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. });
  83. });
  84. Template.userFormsLayout.helpers({
  85. isLegalNoticeLinkExist() {
  86. const currSet = Template.instance().currentSetting.get();
  87. if (currSet && currSet !== undefined && currSet != null) {
  88. return currSet.legalNotice !== undefined && currSet.legalNotice.trim() != "";
  89. }
  90. else
  91. return false;
  92. },
  93. getLegalNoticeWithWritTraduction() {
  94. let spanLegalNoticeElt = $("#legalNoticeSpan");
  95. if (spanLegalNoticeElt != null && spanLegalNoticeElt != undefined) {
  96. spanLegalNoticeElt.html(TAPi18n.__('acceptance_of_our_legalNotice', {}));
  97. }
  98. let atLinkLegalNoticeElt = $("#legalNoticeAtLink");
  99. if (atLinkLegalNoticeElt != null && atLinkLegalNoticeElt != undefined) {
  100. atLinkLegalNoticeElt.html(TAPi18n.__('legalNotice', {}));
  101. }
  102. return true;
  103. },
  104. isLoading() {
  105. return Template.instance().isLoading.get();
  106. },
  107. afterBodyStart() {
  108. return currentSetting.customHTMLafterBodyStart;
  109. },
  110. beforeBodyEnd() {
  111. return currentSetting.customHTMLbeforeBodyEnd;
  112. },
  113. languages() {
  114. return TAPi18n.getSupportedLanguages()
  115. .map(({ tag, name }) => ({ tag: tag, name }))
  116. .sort((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 curLang = TAPi18n.getLanguage();
  126. return this.tag === curLang;
  127. },
  128. });
  129. Template.userFormsLayout.events({
  130. 'change .js-userform-set-language'(event) {
  131. const tag = $(event.currentTarget).val();
  132. TAPi18n.setLanguage(tag);
  133. event.preventDefault();
  134. },
  135. 'click #at-btn'(event, templateInstance) {
  136. if (FlowRouter.getRouteName() === 'atSignIn') {
  137. templateInstance.isLoading.set(true);
  138. authentication(event, templateInstance).then(() => {
  139. templateInstance.isLoading.set(false);
  140. });
  141. }
  142. isCheckDone = false;
  143. },
  144. 'click #at-signUp'(event, templateInstance) {
  145. isCheckDone = false;
  146. },
  147. 'DOMSubtreeModified #at-oidc'(event) {
  148. if (alreadyCheck <= 2) {
  149. let currSetting = ReactiveCache.getCurrentSetting();
  150. let oidcBtnElt = $("#at-oidc");
  151. if (currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined) {
  152. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  153. if (alreadyCheck == 1) {
  154. alreadyCheck++;
  155. oidcBtnElt.html("");
  156. }
  157. else {
  158. alreadyCheck++;
  159. oidcBtnElt.html(htmlvalue);
  160. }
  161. }
  162. }
  163. else {
  164. alreadyCheck = 1;
  165. }
  166. },
  167. 'DOMSubtreeModified .at-form'(event) {
  168. if (alreadyCheck <= 2 && !isCheckDone) {
  169. if (document.getElementById("at-oidc") != null) {
  170. let currSetting = ReactiveCache.getCurrentSetting();
  171. let oidcBtnElt = $("#at-oidc");
  172. if (currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined) {
  173. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  174. if (alreadyCheck == 1) {
  175. alreadyCheck++;
  176. oidcBtnElt.html("");
  177. }
  178. else {
  179. alreadyCheck++;
  180. isCheckDone = true;
  181. oidcBtnElt.html(htmlvalue);
  182. }
  183. }
  184. }
  185. }
  186. else {
  187. alreadyCheck = 1;
  188. }
  189. },
  190. });
  191. Template.defaultLayout.events({
  192. 'click .js-close-modal': () => {
  193. Modal.close();
  194. },
  195. });
  196. async function authentication(event, templateInstance) {
  197. const match = $('#at-field-username_and_email').val();
  198. const password = $('#at-field-password').val();
  199. if (!match || !password) return undefined;
  200. const result = await getAuthenticationMethod(
  201. templateInstance.currentSetting.get(),
  202. match,
  203. );
  204. if (result === 'password') return undefined;
  205. // Stop submit #at-pwd-form
  206. event.preventDefault();
  207. event.stopImmediatePropagation();
  208. switch (result) {
  209. case 'ldap':
  210. return new Promise(resolve => {
  211. Meteor.loginWithLDAP(match, password, function () {
  212. resolve(FlowRouter.go('/'));
  213. });
  214. });
  215. case 'saml':
  216. return new Promise(resolve => {
  217. const provider = Meteor.settings.public.SAML_PROVIDER;
  218. Meteor.loginWithSaml(
  219. {
  220. provider,
  221. },
  222. function () {
  223. resolve(FlowRouter.go('/'));
  224. },
  225. );
  226. });
  227. case 'cas':
  228. return new Promise(resolve => {
  229. Meteor.loginWithCas(match, password, function () {
  230. resolve(FlowRouter.go('/'));
  231. });
  232. });
  233. default:
  234. return undefined;
  235. }
  236. }
  237. function getAuthenticationMethod(
  238. { displayAuthenticationMethod, defaultAuthenticationMethod },
  239. match,
  240. ) {
  241. if (displayAuthenticationMethod) {
  242. return $('.select-authentication').val();
  243. }
  244. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  245. }
  246. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  247. return new Promise(resolve => {
  248. try {
  249. Meteor.subscribe('user-authenticationMethod', match, {
  250. onReady() {
  251. const user = Users.findOne();
  252. const authenticationMethod = user
  253. ? user.authenticationMethod
  254. : defaultAuthenticationMethod;
  255. resolve(authenticationMethod);
  256. },
  257. });
  258. } catch (error) {
  259. resolve(defaultAuthenticationMethod);
  260. }
  261. });
  262. }