layouts.js 3.6 KB

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