layouts.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. let alreadyCheck = 1;
  8. let isCheckDone = false;
  9. const validator = {
  10. set(obj, prop, value) {
  11. if (prop === 'state' && value !== 'signIn') {
  12. $('.at-form-authentication').hide();
  13. } else if (prop === 'state' && value === 'signIn') {
  14. $('.at-form-authentication').show();
  15. }
  16. // The default behavior to store the value
  17. obj[prop] = value;
  18. // Indicate success
  19. return true;
  20. },
  21. };
  22. Template.userFormsLayout.onCreated(function() {
  23. const templateInstance = this;
  24. templateInstance.currentSetting = new ReactiveVar();
  25. templateInstance.isLoading = new ReactiveVar(false);
  26. Meteor.subscribe('setting', {
  27. onReady() {
  28. templateInstance.currentSetting.set(Settings.findOne());
  29. let currSetting = templateInstance.currentSetting.curValue;
  30. let oidcBtnElt = $("#at-oidc");
  31. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  32. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  33. oidcBtnElt.html(htmlvalue);
  34. }
  35. return this.stop();
  36. },
  37. });
  38. Meteor.call('isPasswordLoginDisabled', (_, result) => {
  39. if (result) {
  40. $('.at-pwd-form').hide();
  41. }
  42. });
  43. });
  44. Template.userFormsLayout.onRendered(() => {
  45. AccountsTemplates.state.form.keys = new Proxy(
  46. AccountsTemplates.state.form.keys,
  47. validator,
  48. );
  49. const i18nTag = navigator.language;
  50. if (i18nTag) {
  51. T9n.setLanguage(i18nTagToT9n(i18nTag));
  52. }
  53. EscapeActions.executeAll();
  54. });
  55. Template.userFormsLayout.helpers({
  56. currentSetting() {
  57. return Template.instance().currentSetting.get();
  58. },
  59. isLoading() {
  60. return Template.instance().isLoading.get();
  61. },
  62. afterBodyStart() {
  63. return currentSetting.customHTMLafterBodyStart;
  64. },
  65. beforeBodyEnd() {
  66. return currentSetting.customHTMLbeforeBodyEnd;
  67. },
  68. languages() {
  69. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  70. const tag = code;
  71. let name = lang.name;
  72. if (lang.name === 'br') {
  73. name = 'Brezhoneg';
  74. } else if (lang.name === 'ar-EG') {
  75. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  76. name = 'مَصرى';
  77. } else if (lang.name === 'de-CH') {
  78. name = 'Deutsch (Schweiz)';
  79. } else if (lang.name === 'fa-IR') {
  80. // fa-IR = Persian (Iran)
  81. name = 'فارسی/پارسی (ایران‎)';
  82. } else if (lang.name === 'fr-BE') {
  83. name = 'Français (Belgique)';
  84. } else if (lang.name === 'fr-CA') {
  85. name = 'Français (Canada)';
  86. } else if (lang.name === 'fr-CH') {
  87. name = 'Français (Schweiz)';
  88. } else if (lang.name === 'ig') {
  89. name = 'Igbo';
  90. } else if (lang.name === 'lv') {
  91. name = 'Latviešu';
  92. } else if (lang.name === 'latviešu valoda') {
  93. name = 'Latviešu';
  94. } else if (lang.name === 'en-IT') {
  95. name = 'English (Italy)';
  96. } else if (lang.name === 'Español') {
  97. name = 'español';
  98. } else if (lang.name === 'es_419') {
  99. name = 'español de América Latina';
  100. } else if (lang.name === 'es-419') {
  101. name = 'español de América Latina';
  102. } else if (lang.name === 'Español de América Latina') {
  103. name = 'español de América Latina';
  104. } else if (lang.name === 'es-LA') {
  105. name = 'español de América Latina';
  106. } else if (lang.name === 'Español de Argentina') {
  107. name = 'español de Argentina';
  108. } else if (lang.name === 'Español de Chile') {
  109. name = 'español de Chile';
  110. } else if (lang.name === 'Español de Colombia') {
  111. name = 'español de Colombia';
  112. } else if (lang.name === 'Español de México') {
  113. name = 'español de México';
  114. } else if (lang.name === 'es-PY') {
  115. name = 'español de Paraguayo';
  116. } else if (lang.name === 'Español de Paraguayo') {
  117. name = 'español de Paraguayo';
  118. } else if (lang.name === 'Español de Perú') {
  119. name = 'español de Perú';
  120. } else if (lang.name === 'Español de Puerto Rico') {
  121. name = 'español de Puerto Rico';
  122. } else if (lang.name === 'oc') {
  123. name = 'Occitan';
  124. } else if (lang.name === 'st') {
  125. name = 'Sãotomense';
  126. } else if (lang.name === '繁体中文(台湾)') {
  127. name = '繁體中文(台灣)';
  128. }
  129. return { tag, name };
  130. }).sort(function(a, b) {
  131. if (a.name === b.name) {
  132. return 0;
  133. } else {
  134. return a.name > b.name ? 1 : -1;
  135. }
  136. });
  137. },
  138. isCurrentLanguage() {
  139. const t9nTag = i18nTagToT9n(this.tag);
  140. const curLang = T9n.getLanguage() || 'en';
  141. return t9nTag === curLang;
  142. },
  143. });
  144. Template.userFormsLayout.events({
  145. 'change .js-userform-set-language'(event) {
  146. const i18nTag = $(event.currentTarget).val();
  147. T9n.setLanguage(i18nTagToT9n(i18nTag));
  148. event.preventDefault();
  149. },
  150. 'click #at-btn'(event, templateInstance) {
  151. if (FlowRouter.getRouteName() === 'atSignIn') {
  152. templateInstance.isLoading.set(true);
  153. authentication(event, templateInstance).then(() => {
  154. templateInstance.isLoading.set(false);
  155. });
  156. }
  157. },
  158. 'DOMSubtreeModified #at-oidc'(event){
  159. if(alreadyCheck <= 2){
  160. let currSetting = Settings.findOne();
  161. let oidcBtnElt = $("#at-oidc");
  162. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  163. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  164. if(alreadyCheck == 1){
  165. alreadyCheck++;
  166. oidcBtnElt.html("");
  167. }
  168. else{
  169. alreadyCheck++;
  170. oidcBtnElt.html(htmlvalue);
  171. }
  172. }
  173. }
  174. else{
  175. alreadyCheck = 1;
  176. }
  177. },
  178. 'DOMSubtreeModified .at-form'(event){
  179. if(alreadyCheck <= 2 && !isCheckDone){
  180. if(document.getElementById("at-oidc") != null){
  181. let currSetting = Settings.findOne();
  182. let oidcBtnElt = $("#at-oidc");
  183. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  184. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  185. if(alreadyCheck == 1){
  186. alreadyCheck++;
  187. oidcBtnElt.html("");
  188. }
  189. else{
  190. alreadyCheck++;
  191. isCheckDone = true;
  192. oidcBtnElt.html(htmlvalue);
  193. }
  194. }
  195. }
  196. }
  197. else{
  198. alreadyCheck = 1;
  199. }
  200. },
  201. });
  202. Template.defaultLayout.events({
  203. 'click .js-close-modal': () => {
  204. Modal.close();
  205. },
  206. });
  207. async function authentication(event, templateInstance) {
  208. const match = $('#at-field-username_and_email').val();
  209. const password = $('#at-field-password').val();
  210. if (!match || !password) return undefined;
  211. const result = await getAuthenticationMethod(
  212. templateInstance.currentSetting.get(),
  213. match,
  214. );
  215. if (result === 'password') return undefined;
  216. // Stop submit #at-pwd-form
  217. event.preventDefault();
  218. event.stopImmediatePropagation();
  219. switch (result) {
  220. case 'ldap':
  221. return new Promise(resolve => {
  222. Meteor.loginWithLDAP(match, password, function() {
  223. resolve(FlowRouter.go('/'));
  224. });
  225. });
  226. case 'saml':
  227. return new Promise(resolve => {
  228. const provider = Meteor.settings.public.SAML_PROVIDER;
  229. Meteor.loginWithSaml(
  230. {
  231. provider,
  232. },
  233. function() {
  234. resolve(FlowRouter.go('/'));
  235. },
  236. );
  237. });
  238. case 'cas':
  239. return new Promise(resolve => {
  240. Meteor.loginWithCas(match, password, function() {
  241. resolve(FlowRouter.go('/'));
  242. });
  243. });
  244. default:
  245. return undefined;
  246. }
  247. }
  248. function getAuthenticationMethod(
  249. { displayAuthenticationMethod, defaultAuthenticationMethod },
  250. match,
  251. ) {
  252. if (displayAuthenticationMethod) {
  253. return $('.select-authentication').val();
  254. }
  255. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  256. }
  257. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  258. return new Promise(resolve => {
  259. try {
  260. Meteor.subscribe('user-authenticationMethod', match, {
  261. onReady() {
  262. const user = Users.findOne();
  263. const authenticationMethod = user
  264. ? user.authenticationMethod
  265. : defaultAuthenticationMethod;
  266. resolve(authenticationMethod);
  267. },
  268. });
  269. } catch (error) {
  270. resolve(defaultAuthenticationMethod);
  271. }
  272. });
  273. }