layouts.js 8.5 KB

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