layouts.js 7.7 KB

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