layouts.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. BlazeLayout.setRoot('body');
  2. const i18nTagToT9n = (i18nTag) => {
  3. // t9n/i18n tags are same now, see: https://github.com/softwarerero/meteor-accounts-t9n/pull/129
  4. // but we keep this conversion function here, to be aware that that they are different system.
  5. return i18nTag;
  6. };
  7. Template.userFormsLayout.onCreated(function() {
  8. Meteor.call('getDefaultAuthenticationMethod', (error, result) => {
  9. this.data.defaultAuthenticationMethod = new ReactiveVar(error ? undefined : result);
  10. });
  11. });
  12. Template.userFormsLayout.onRendered(() => {
  13. const i18nTag = navigator.language;
  14. if (i18nTag) {
  15. T9n.setLanguage(i18nTagToT9n(i18nTag));
  16. }
  17. EscapeActions.executeAll();
  18. });
  19. Template.userFormsLayout.helpers({
  20. languages() {
  21. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  22. const tag = code;
  23. let name = lang.name;
  24. if (lang.name === 'br') {
  25. name = 'Brezhoneg';
  26. } else if (lang.name === 'ig') {
  27. name = 'Igbo';
  28. }
  29. return { tag, name };
  30. }).sort(function(a, b) {
  31. if (a.name === b.name) {
  32. return 0;
  33. } else {
  34. return a.name > b.name ? 1 : -1;
  35. }
  36. });
  37. },
  38. isCurrentLanguage() {
  39. const t9nTag = i18nTagToT9n(this.tag);
  40. const curLang = T9n.getLanguage() || 'en';
  41. return t9nTag === curLang;
  42. },
  43. /*
  44. isCas() {
  45. return Meteor.settings.public &&
  46. Meteor.settings.public.cas &&
  47. Meteor.settings.public.cas.loginUrl;
  48. },
  49. casSignInLabel() {
  50. return TAPi18n.__('casSignIn', {}, T9n.getLanguage() || 'en');
  51. },
  52. */
  53. });
  54. Template.userFormsLayout.events({
  55. 'change .js-userform-set-language'(evt) {
  56. const i18nTag = $(evt.currentTarget).val();
  57. T9n.setLanguage(i18nTagToT9n(i18nTag));
  58. evt.preventDefault();
  59. },
  60. 'click button#cas'() {
  61. Meteor.loginWithCas(function() {
  62. if (FlowRouter.getRouteName() === 'atSignIn') {
  63. FlowRouter.go('/');
  64. }
  65. });
  66. },
  67. 'click #at-btn'(event, instance) {
  68. /* All authentication method can be managed/called here.
  69. !! DON'T FORGET to correctly fill the fields of the user during its creation if necessary authenticationMethod : String !!
  70. */
  71. const email = $('#at-field-username_and_email').val();
  72. const password = $('#at-field-password').val();
  73. if (FlowRouter.getRouteName() !== 'atSignIn' || password === '') {
  74. return;
  75. }
  76. // Stop submit #at-pwd-form
  77. event.preventDefault();
  78. event.stopImmediatePropagation();
  79. Meteor.subscribe('user-authenticationMethod', email, {
  80. onReady() {
  81. return authentication.call(this, instance, email, password);
  82. },
  83. });
  84. },
  85. });
  86. Template.defaultLayout.events({
  87. 'click .js-close-modal': () => {
  88. Modal.close();
  89. },
  90. });
  91. function authentication(instance, email, password) {
  92. let user = Users.findOne();
  93. // Authentication with password
  94. if (user && user.authenticationMethod === 'password') {
  95. $('#at-pwd-form').submit();
  96. // Meteor.call('logoutWithTimer', user._id, () => {});
  97. return this.stop();
  98. }
  99. // If user doesn't exist, uses the default authentication method if it defined
  100. if (user === undefined) {
  101. user = {
  102. "authenticationMethod": instance.data.defaultAuthenticationMethod.get()
  103. };
  104. }
  105. // Authentication with LDAP
  106. if (user.authenticationMethod === 'ldap') {
  107. // Use the ldap connection package
  108. Meteor.loginWithLDAP(email, password, function(error) {
  109. if (!error) {
  110. // Meteor.call('logoutWithTimer', Users.findOne()._id, () => {});
  111. return FlowRouter.go('/');
  112. }
  113. return error;
  114. });
  115. }
  116. return this.stop();
  117. }