layouts.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. isCheckDone = false;
  167. },
  168. 'click #at-signUp'(event, templateInstance){
  169. isCheckDone = false;
  170. },
  171. 'DOMSubtreeModified #at-oidc'(event){
  172. if(alreadyCheck <= 2){
  173. let currSetting = Settings.findOne();
  174. let oidcBtnElt = $("#at-oidc");
  175. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  176. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  177. if(alreadyCheck == 1){
  178. alreadyCheck++;
  179. oidcBtnElt.html("");
  180. }
  181. else{
  182. alreadyCheck++;
  183. oidcBtnElt.html(htmlvalue);
  184. }
  185. }
  186. }
  187. else{
  188. alreadyCheck = 1;
  189. }
  190. },
  191. 'DOMSubtreeModified .at-form'(event){
  192. if(alreadyCheck <= 2 && !isCheckDone){
  193. if(document.getElementById("at-oidc") != null){
  194. let currSetting = Settings.findOne();
  195. let oidcBtnElt = $("#at-oidc");
  196. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  197. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  198. if(alreadyCheck == 1){
  199. alreadyCheck++;
  200. oidcBtnElt.html("");
  201. }
  202. else{
  203. alreadyCheck++;
  204. isCheckDone = true;
  205. oidcBtnElt.html(htmlvalue);
  206. }
  207. }
  208. }
  209. }
  210. else{
  211. alreadyCheck = 1;
  212. }
  213. },
  214. });
  215. Template.defaultLayout.events({
  216. 'click .js-close-modal': () => {
  217. Modal.close();
  218. },
  219. });
  220. async function authentication(event, templateInstance) {
  221. const match = $('#at-field-username_and_email').val();
  222. const password = $('#at-field-password').val();
  223. if (!match || !password) return undefined;
  224. const result = await getAuthenticationMethod(
  225. templateInstance.currentSetting.get(),
  226. match,
  227. );
  228. if (result === 'password') return undefined;
  229. // Stop submit #at-pwd-form
  230. event.preventDefault();
  231. event.stopImmediatePropagation();
  232. switch (result) {
  233. case 'ldap':
  234. return new Promise(resolve => {
  235. Meteor.loginWithLDAP(match, password, function() {
  236. resolve(FlowRouter.go('/'));
  237. });
  238. });
  239. case 'saml':
  240. return new Promise(resolve => {
  241. const provider = Meteor.settings.public.SAML_PROVIDER;
  242. Meteor.loginWithSaml(
  243. {
  244. provider,
  245. },
  246. function() {
  247. resolve(FlowRouter.go('/'));
  248. },
  249. );
  250. });
  251. case 'cas':
  252. return new Promise(resolve => {
  253. Meteor.loginWithCas(match, password, function() {
  254. resolve(FlowRouter.go('/'));
  255. });
  256. });
  257. default:
  258. return undefined;
  259. }
  260. }
  261. function getAuthenticationMethod(
  262. { displayAuthenticationMethod, defaultAuthenticationMethod },
  263. match,
  264. ) {
  265. if (displayAuthenticationMethod) {
  266. return $('.select-authentication').val();
  267. }
  268. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  269. }
  270. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  271. return new Promise(resolve => {
  272. try {
  273. Meteor.subscribe('user-authenticationMethod', match, {
  274. onReady() {
  275. const user = Users.findOne();
  276. const authenticationMethod = user
  277. ? user.authenticationMethod
  278. : defaultAuthenticationMethod;
  279. resolve(authenticationMethod);
  280. },
  281. });
  282. } catch (error) {
  283. resolve(defaultAuthenticationMethod);
  284. }
  285. });
  286. }