layouts.js 7.5 KB

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