layouts.js 7.3 KB

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