layouts.js 8.0 KB

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