layouts.js 7.5 KB

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