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