layouts.js 6.8 KB

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