layouts.js 9.2 KB

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