layouts.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. isLegalNoticeLinkExist(){
  69. const currSet = Template.instance().currentSetting.get();
  70. if(currSet && currSet !== undefined && currSet != null){
  71. return currSet.legalNotice !== undefined && currSet.legalNotice.trim() != "";
  72. }
  73. else
  74. return false;
  75. },
  76. isLoading() {
  77. return Template.instance().isLoading.get();
  78. },
  79. afterBodyStart() {
  80. return currentSetting.customHTMLafterBodyStart;
  81. },
  82. beforeBodyEnd() {
  83. return currentSetting.customHTMLbeforeBodyEnd;
  84. },
  85. languages() {
  86. return _.map(TAPi18n.getLanguages(), (lang, code) => {
  87. const tag = code;
  88. let name = lang.name;
  89. if (lang.name === 'br') {
  90. name = 'Brezhoneg';
  91. } else if (lang.name === 'ar-EG') {
  92. // ar-EG = Arabic (Egypt), simply Masri (مَصرى, [ˈmɑsˤɾi], Egyptian, Masr refers to Cairo)
  93. name = 'مَصرى';
  94. } else if (lang.name === 'de-CH') {
  95. name = 'Deutsch (Schweiz)';
  96. } else if (lang.name === 'de-AT') {
  97. name = 'Deutsch (Österreich)';
  98. } else if (lang.name === 'en-DE') {
  99. name = 'English (Germany)';
  100. } else if (lang.name === 'fa-IR') {
  101. // fa-IR = Persian (Iran)
  102. name = 'فارسی/پارسی (ایران‎)';
  103. } else if (lang.name === 'fr-BE') {
  104. name = 'Français (Belgique)';
  105. } else if (lang.name === 'fr-CA') {
  106. name = 'Français (Canada)';
  107. } else if (lang.name === 'fr-CH') {
  108. name = 'Français (Schweiz)';
  109. } else if (lang.name === 'gu-IN') {
  110. // gu-IN = Gurajati (India)
  111. name = 'ગુજરાતી';
  112. } else if (lang.name === 'hi-IN') {
  113. // hi-IN = Hindi (India)
  114. name = 'हिंदी (भारत)';
  115. } else if (lang.name === 'ig') {
  116. name = 'Igbo';
  117. } else if (lang.name === 'lv') {
  118. name = 'Latviešu';
  119. } else if (lang.name === 'latviešu valoda') {
  120. name = 'Latviešu';
  121. } else if (lang.name === 'ms-MY') {
  122. // ms-MY = Malay (Malaysia)
  123. name = 'بهاس ملايو';
  124. } else if (lang.name === 'en-IT') {
  125. name = 'English (Italy)';
  126. } else if (lang.name === 'el-GR') {
  127. // el-GR = Greek (Greece)
  128. name = 'Ελληνικά (Ελλάδα)';
  129. } else if (lang.name === 'Español') {
  130. name = 'español';
  131. } else if (lang.name === 'es_419') {
  132. name = 'español de América Latina';
  133. } else if (lang.name === 'es-419') {
  134. name = 'español de América Latina';
  135. } else if (lang.name === 'Español de América Latina') {
  136. name = 'español de América Latina';
  137. } else if (lang.name === 'es-LA') {
  138. name = 'español de América Latina';
  139. } else if (lang.name === 'Español de Argentina') {
  140. name = 'español de Argentina';
  141. } else if (lang.name === 'Español de Chile') {
  142. name = 'español de Chile';
  143. } else if (lang.name === 'Español de Colombia') {
  144. name = 'español de Colombia';
  145. } else if (lang.name === 'Español de México') {
  146. name = 'español de México';
  147. } else if (lang.name === 'es-PY') {
  148. name = 'español de Paraguayo';
  149. } else if (lang.name === 'Español de Paraguayo') {
  150. name = 'español de Paraguayo';
  151. } else if (lang.name === 'Español de Perú') {
  152. name = 'español de Perú';
  153. } else if (lang.name === 'Español de Puerto Rico') {
  154. name = 'español de Puerto Rico';
  155. } else if (lang.name === 'oc') {
  156. name = 'Occitan';
  157. } else if (lang.name === 'st') {
  158. name = 'Sãotomense';
  159. } else if (lang.name === '繁体中文(台湾)') {
  160. // Traditional Chinese (Taiwan)
  161. name = '繁體中文(台灣)';
  162. }
  163. return { tag, name };
  164. }).sort(function(a, b) {
  165. if (a.name === b.name) {
  166. return 0;
  167. } else {
  168. return a.name > b.name ? 1 : -1;
  169. }
  170. });
  171. },
  172. isCurrentLanguage() {
  173. const t9nTag = i18nTagToT9n(this.tag);
  174. const curLang = T9n.getLanguage() || 'en';
  175. return t9nTag === curLang;
  176. },
  177. });
  178. Template.userFormsLayout.events({
  179. 'change .js-userform-set-language'(event) {
  180. const i18nTag = $(event.currentTarget).val();
  181. T9n.setLanguage(i18nTagToT9n(i18nTag));
  182. event.preventDefault();
  183. },
  184. 'click #at-btn'(event, templateInstance) {
  185. if (FlowRouter.getRouteName() === 'atSignIn') {
  186. templateInstance.isLoading.set(true);
  187. authentication(event, templateInstance).then(() => {
  188. templateInstance.isLoading.set(false);
  189. });
  190. }
  191. isCheckDone = false;
  192. },
  193. 'click #at-signUp'(event, templateInstance){
  194. isCheckDone = false;
  195. },
  196. 'DOMSubtreeModified #at-oidc'(event){
  197. if(alreadyCheck <= 2){
  198. let currSetting = Settings.findOne();
  199. let oidcBtnElt = $("#at-oidc");
  200. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  201. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  202. if(alreadyCheck == 1){
  203. alreadyCheck++;
  204. oidcBtnElt.html("");
  205. }
  206. else{
  207. alreadyCheck++;
  208. oidcBtnElt.html(htmlvalue);
  209. }
  210. }
  211. }
  212. else{
  213. alreadyCheck = 1;
  214. }
  215. },
  216. 'DOMSubtreeModified .at-form'(event){
  217. if(alreadyCheck <= 2 && !isCheckDone){
  218. if(document.getElementById("at-oidc") != null){
  219. let currSetting = Settings.findOne();
  220. let oidcBtnElt = $("#at-oidc");
  221. if(currSetting && currSetting !== undefined && currSetting.oidcBtnText !== undefined && oidcBtnElt != null && oidcBtnElt != undefined){
  222. let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
  223. if(alreadyCheck == 1){
  224. alreadyCheck++;
  225. oidcBtnElt.html("");
  226. }
  227. else{
  228. alreadyCheck++;
  229. isCheckDone = true;
  230. oidcBtnElt.html(htmlvalue);
  231. }
  232. }
  233. }
  234. }
  235. else{
  236. alreadyCheck = 1;
  237. }
  238. },
  239. });
  240. Template.defaultLayout.events({
  241. 'click .js-close-modal': () => {
  242. Modal.close();
  243. },
  244. });
  245. async function authentication(event, templateInstance) {
  246. const match = $('#at-field-username_and_email').val();
  247. const password = $('#at-field-password').val();
  248. if (!match || !password) return undefined;
  249. const result = await getAuthenticationMethod(
  250. templateInstance.currentSetting.get(),
  251. match,
  252. );
  253. if (result === 'password') return undefined;
  254. // Stop submit #at-pwd-form
  255. event.preventDefault();
  256. event.stopImmediatePropagation();
  257. switch (result) {
  258. case 'ldap':
  259. return new Promise(resolve => {
  260. Meteor.loginWithLDAP(match, password, function() {
  261. resolve(FlowRouter.go('/'));
  262. });
  263. });
  264. case 'saml':
  265. return new Promise(resolve => {
  266. const provider = Meteor.settings.public.SAML_PROVIDER;
  267. Meteor.loginWithSaml(
  268. {
  269. provider,
  270. },
  271. function() {
  272. resolve(FlowRouter.go('/'));
  273. },
  274. );
  275. });
  276. case 'cas':
  277. return new Promise(resolve => {
  278. Meteor.loginWithCas(match, password, function() {
  279. resolve(FlowRouter.go('/'));
  280. });
  281. });
  282. default:
  283. return undefined;
  284. }
  285. }
  286. function getAuthenticationMethod(
  287. { displayAuthenticationMethod, defaultAuthenticationMethod },
  288. match,
  289. ) {
  290. if (displayAuthenticationMethod) {
  291. return $('.select-authentication').val();
  292. }
  293. return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
  294. }
  295. function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
  296. return new Promise(resolve => {
  297. try {
  298. Meteor.subscribe('user-authenticationMethod', match, {
  299. onReady() {
  300. const user = Users.findOne();
  301. const authenticationMethod = user
  302. ? user.authenticationMethod
  303. : defaultAuthenticationMethod;
  304. resolve(authenticationMethod);
  305. },
  306. });
  307. } catch (error) {
  308. resolve(defaultAuthenticationMethod);
  309. }
  310. });
  311. }