wekan/client/components/main/layouts.js

333 lines
9 KiB
JavaScript
Raw Normal View History

2022-12-13 23:20:04 +01:00
import { ReactiveCache } from '/imports/reactiveCache';
import { TAPi18n } from '/imports/i18n';
2026-01-14 00:13:21 +02:00
import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
let alreadyCheck = 1;
let isCheckDone = false;
let counter = 0;
const validator = {
set(obj, prop, value) {
if (prop === 'state' && value !== 'signIn') {
$('.at-form-authentication').hide();
} else if (prop === 'state' && value === 'signIn') {
$('.at-form-authentication').show();
}
// The default behavior to store the value
obj[prop] = value;
// Indicate success
return true;
},
};
Template.userFormsLayout.onCreated(function () {
2019-06-28 12:52:09 -05:00
const templateInstance = this;
templateInstance.currentSetting = new ReactiveVar();
templateInstance.isLoading = new ReactiveVar(false);
2019-02-01 19:00:44 +01:00
if (!ReactiveCache.getCurrentUser()?.profile) {
Meteor.call('isOidcRedirectionEnabled', (_, result) => {
if (result) {
AccountsTemplates.options.socialLoginStyle = 'redirect';
options = {
loginStyle: AccountsTemplates.options.socialLoginStyle,
};
Meteor.loginWithOidc(options);
}
});
Meteor.subscribe('setting', {
onReady() {
templateInstance.currentSetting.set(ReactiveCache.getCurrentSetting());
return this.stop();
},
});
}
});
Template.userFormsLayout.onRendered(() => {
Meteor.call('getAuthenticationsEnabled', (_, result) => {
2026-01-14 00:13:21 +02:00
let enabledAuthenticationMethods = ['password']; // we show/hide this based on isPasswordLoginEnabled
if (result) {
Object.keys(result).forEach((m) => {
if (result[m]) enabledAuthenticationMethods.push(m);
});
}
Meteor.call('isPasswordLoginEnabled', (_, result) => {
if (result) {
$('.at-pwd-form').show();
}
});
Meteor.call('isDisableRegistration', (_, result) => {
if (result) {
$('.at-signup-link').hide();
}
});
Meteor.call('isDisableForgotPassword', (_, result) => {
if (result) {
$('.at-pwd-link').hide();
}
});
if (enabledAuthenticationMethods.indexOf('oauth2') !== -1) {
// TODO find better way to run this code once the oauth2 UI is injected in the DOM
(function waitForElementAndShow() {
if (!$('.at-oauth')[0]) return setTimeout(waitForElementAndShow, 100);
$('.at-oauth').show();
})();
}
AccountsTemplates.state.form.keys = new Proxy(
AccountsTemplates.state.form.keys,
validator,
);
EscapeActions.executeAll();
// Add autocomplete attribute to login input for WCAG compliance
2026-01-14 00:13:21 +02:00
const loginInput = document.querySelector(
'input[type="text"], input[type="email"]',
);
if (
loginInput &&
loginInput.name &&
(loginInput.name.toLowerCase().includes('user') ||
loginInput.name.toLowerCase().includes('email'))
) {
loginInput.setAttribute('autocomplete', 'username email');
}
// Add autocomplete attributes to password fields for WCAG compliance
const passwordInputs = document.querySelectorAll('input[type="password"]');
2026-01-14 00:13:21 +02:00
passwordInputs.forEach((input) => {
if (input.name && input.name.includes('password')) {
2026-01-14 00:13:21 +02:00
if (
input.name.includes('password_again') ||
input.name.includes('new_password')
) {
input.setAttribute('autocomplete', 'new-password');
} else {
input.setAttribute('autocomplete', 'current-password');
}
}
});
});
});
Template.userFormsLayout.helpers({
isLegalNoticeLinkExist() {
const currSet = Template.instance().currentSetting.get();
if (currSet && currSet !== undefined && currSet != null) {
2026-01-14 00:13:21 +02:00
return (
currSet.legalNotice !== undefined && currSet.legalNotice.trim() != ''
);
} else return false;
},
getLegalNoticeWithWritTraduction() {
2026-01-14 00:13:21 +02:00
let spanLegalNoticeElt = $('#legalNoticeSpan');
if (spanLegalNoticeElt != null && spanLegalNoticeElt != undefined) {
spanLegalNoticeElt.html(TAPi18n.__('acceptance_of_our_legalNotice', {}));
}
2026-01-14 00:13:21 +02:00
let atLinkLegalNoticeElt = $('#legalNoticeAtLink');
if (atLinkLegalNoticeElt != null && atLinkLegalNoticeElt != undefined) {
atLinkLegalNoticeElt.html(TAPi18n.__('legalNotice', {}));
}
return true;
},
2019-04-24 12:28:11 +02:00
isLoading() {
return Template.instance().isLoading.get();
},
afterBodyStart() {
return currentSetting.customHTMLafterBodyStart;
},
beforeBodyEnd() {
return currentSetting.customHTMLbeforeBodyEnd;
},
languages() {
return TAPi18n.getSupportedLanguages()
.map(({ tag, name }) => ({ tag: tag, name }))
.sort((a, b) => {
if (a.name === b.name) {
return 0;
} else {
return a.name > b.name ? 1 : -1;
}
});
},
isCurrentLanguage() {
const curLang = TAPi18n.getLanguage();
return this.tag === curLang;
},
});
Template.userFormsLayout.events({
2019-06-28 12:52:09 -05:00
'change .js-userform-set-language'(event) {
const tag = $(event.currentTarget).val();
TAPi18n.setLanguage(tag);
2019-06-28 12:52:09 -05:00
event.preventDefault();
},
2019-06-28 12:52:09 -05:00
'click #at-btn'(event, templateInstance) {
2019-02-01 19:00:44 +01:00
if (FlowRouter.getRouteName() === 'atSignIn') {
2019-06-28 12:52:09 -05:00
templateInstance.isLoading.set(true);
authentication(event, templateInstance).then(() => {
templateInstance.isLoading.set(false);
});
}
2021-11-11 19:25:19 +02:00
isCheckDone = false;
},
'click #at-signUp'(event, templateInstance) {
2021-11-11 19:25:19 +02:00
isCheckDone = false;
},
'DOMSubtreeModified #at-oidc'(event) {
if (alreadyCheck <= 2) {
let currSetting = ReactiveCache.getCurrentSetting();
2026-01-14 00:13:21 +02:00
let oidcBtnElt = $('#at-oidc');
if (
currSetting &&
currSetting !== undefined &&
currSetting.oidcBtnText !== undefined &&
oidcBtnElt != null &&
oidcBtnElt != undefined
) {
let htmlvalue = "<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
if (alreadyCheck == 1) {
alreadyCheck++;
2026-01-14 00:13:21 +02:00
oidcBtnElt.html('');
} else {
alreadyCheck++;
oidcBtnElt.html(htmlvalue);
}
}
2026-01-14 00:13:21 +02:00
} else {
alreadyCheck = 1;
}
},
'DOMSubtreeModified .at-form'(event) {
if (alreadyCheck <= 2 && !isCheckDone) {
2026-01-14 00:13:21 +02:00
if (document.getElementById('at-oidc') != null) {
let currSetting = ReactiveCache.getCurrentSetting();
2026-01-14 00:13:21 +02:00
let oidcBtnElt = $('#at-oidc');
if (
currSetting &&
currSetting !== undefined &&
currSetting.oidcBtnText !== undefined &&
oidcBtnElt != null &&
oidcBtnElt != undefined
) {
let htmlvalue =
"<i class='fa fa-oidc'></i>" + currSetting.oidcBtnText;
if (alreadyCheck == 1) {
alreadyCheck++;
2026-01-14 00:13:21 +02:00
oidcBtnElt.html('');
} else {
alreadyCheck++;
isCheckDone = true;
oidcBtnElt.html(htmlvalue);
}
}
}
2026-01-14 00:13:21 +02:00
} else {
alreadyCheck = 1;
}
},
});
Template.defaultLayout.events({
'click .js-close-modal': () => {
Modal.close();
},
});
2019-02-01 19:00:44 +01:00
2019-06-28 12:52:09 -05:00
async function authentication(event, templateInstance) {
const match = $('#at-field-username_and_email').val();
const password = $('#at-field-password').val();
2019-02-01 19:00:44 +01:00
2019-04-24 12:35:00 +02:00
if (!match || !password) return undefined;
2019-02-01 19:00:44 +01:00
2019-06-28 12:52:09 -05:00
const result = await getAuthenticationMethod(
templateInstance.currentSetting.get(),
match,
);
2019-02-01 19:00:44 +01:00
2019-04-24 12:35:00 +02:00
if (result === 'password') return undefined;
2019-02-01 19:00:44 +01:00
// Stop submit #at-pwd-form
event.preventDefault();
event.stopImmediatePropagation();
2019-02-01 19:00:44 +01:00
2019-02-07 11:38:04 +01:00
switch (result) {
2019-06-28 12:52:09 -05:00
case 'ldap':
2026-01-14 00:13:21 +02:00
return new Promise((resolve) => {
Meteor.loginWithLDAP(match, password, function () {
2019-06-28 12:52:09 -05:00
resolve(FlowRouter.go('/'));
});
2019-04-24 12:28:11 +02:00
});
2019-02-01 19:00:44 +01:00
case 'saml':
2026-01-14 00:13:21 +02:00
return new Promise((resolve) => {
const provider = Meteor.settings.public.SAML_PROVIDER;
Meteor.loginWithSaml(
{
provider,
},
function () {
resolve(FlowRouter.go('/'));
},
);
});
2019-06-28 12:52:09 -05:00
case 'cas':
2026-01-14 00:13:21 +02:00
return new Promise((resolve) => {
Meteor.loginWithCas(match, password, function () {
2019-06-28 12:52:09 -05:00
resolve(FlowRouter.go('/'));
});
2019-04-24 12:28:11 +02:00
});
2019-02-07 11:38:04 +01:00
2019-06-28 12:52:09 -05:00
default:
return undefined;
2019-02-01 19:00:44 +01:00
}
}
2019-06-28 12:52:09 -05:00
function getAuthenticationMethod(
2026-01-14 00:13:21 +02:00
settings,
2019-06-28 12:52:09 -05:00
match,
) {
2026-01-14 00:13:21 +02:00
if (!settings) {
return getUserAuthenticationMethod(undefined, match);
}
const { displayAuthenticationMethod, defaultAuthenticationMethod } = settings;
2019-02-01 19:00:44 +01:00
if (displayAuthenticationMethod) {
return $('.select-authentication').val();
}
return getUserAuthenticationMethod(defaultAuthenticationMethod, match);
}
function getUserAuthenticationMethod(defaultAuthenticationMethod, match) {
2026-01-14 00:13:21 +02:00
return new Promise((resolve) => {
2019-02-01 19:00:44 +01:00
try {
Meteor.subscribe('user-authenticationMethod', match, {
onReady() {
const user = Users.findOne();
const authenticationMethod = user
? user.authenticationMethod
: defaultAuthenticationMethod;
2019-02-01 21:26:04 +02:00
2019-02-01 19:00:44 +01:00
resolve(authenticationMethod);
},
});
2019-06-28 12:52:09 -05:00
} catch (error) {
2019-02-01 19:00:44 +01:00
resolve(defaultAuthenticationMethod);
}
2019-02-01 21:26:04 +02:00
});
2019-02-01 19:00:44 +01:00
}