layouts.js 6.9 KB

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