layouts.js 7.9 KB

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