snappymail/dev/Settings/Admin/Security.js
djmaze ea48f5060b isArray to native Array.isArray
isUnd(*) to native undefined === *
isFunc to native typeof * === 'function'
isObject to native typeof * === 'object'
microtime() to native Date().getTime();
noop to native ()=>{}
noopFalse to native ()=>false
noopTrue to native ()=>true
boolToAjax to native *?'1':'0'
Underscore.js to native
2020-07-29 21:49:41 +02:00

175 lines
4.5 KiB
JavaScript

import ko from 'ko';
import { trim } from 'Common/Utils';
import { StorageResultType, Magics } from 'Common/Enums';
import { settingsGet } from 'Storage/Settings';
import AppAdminStore from 'Stores/Admin/App';
import CapaAdminStore from 'Stores/Admin/Capa';
import Remote from 'Remote/Admin/Ajax';
import { command } from 'Knoin/Knoin';
class SecurityAdminSettings {
constructor() {
this.useLocalProxyForExternalImages = AppAdminStore.useLocalProxyForExternalImages;
this.weakPassword = AppAdminStore.weakPassword;
this.capaOpenPGP = CapaAdminStore.openPGP;
this.capaTwoFactorAuth = CapaAdminStore.twoFactorAuth;
this.capaTwoFactorAuthForce = CapaAdminStore.twoFactorAuthForce;
this.capaTwoFactorAuth.subscribe((value) => {
if (!value) {
this.capaTwoFactorAuthForce(false);
}
});
this.verifySslCertificate = ko.observable(!!settingsGet('VerifySslCertificate'));
this.allowSelfSigned = ko.observable(!!settingsGet('AllowSelfSigned'));
this.verifySslCertificate.subscribe((value) => {
if (!value) {
this.allowSelfSigned(true);
}
});
this.isTwoFactorDropperShown = ko.observable(false);
this.twoFactorDropperUser = ko.observable('');
this.twoFactorDropperUser.focused = ko.observable(false);
this.adminLogin = ko.observable(settingsGet('AdminLogin'));
this.adminLoginError = ko.observable(false);
this.adminPassword = ko.observable('');
this.adminPasswordNew = ko.observable('');
this.adminPasswordNew2 = ko.observable('');
this.adminPasswordNewError = ko.observable(false);
this.adminPasswordUpdateError = ko.observable(false);
this.adminPasswordUpdateSuccess = ko.observable(false);
this.adminPassword.subscribe(() => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
});
this.adminLogin.subscribe(() => {
this.adminLoginError(false);
});
this.adminPasswordNew.subscribe(() => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
});
this.adminPasswordNew2.subscribe(() => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
});
this.onNewAdminPasswordResponse = this.onNewAdminPasswordResponse.bind(this);
}
@command((self) => trim(self.adminLogin()) && self.adminPassword())
saveNewAdminPasswordCommand() {
if (!trim(this.adminLogin())) {
this.adminLoginError(true);
return false;
}
if (this.adminPasswordNew() !== this.adminPasswordNew2()) {
this.adminPasswordNewError(true);
return false;
}
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
Remote.saveNewAdminPassword(this.onNewAdminPasswordResponse, {
'Login': this.adminLogin(),
'Password': this.adminPassword(),
'NewPassword': this.adminPasswordNew()
});
return true;
}
showTwoFactorDropper() {
this.twoFactorDropperUser('');
this.isTwoFactorDropperShown(true);
setTimeout(() => {
this.twoFactorDropperUser.focused(true);
}, Magics.Time50ms);
}
onNewAdminPasswordResponse(result, data) {
if (StorageResultType.Success === result && data && data.Result) {
this.adminPassword('');
this.adminPasswordNew('');
this.adminPasswordNew2('');
this.adminPasswordUpdateSuccess(true);
this.weakPassword(!!data.Result.Weak);
} else {
this.adminPasswordUpdateError(true);
}
}
onBuild() {
this.capaOpenPGP.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaOpenPGP': value ? '1' : '0'
});
});
this.capaTwoFactorAuth.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuth': value ? '1' : '0'
});
});
this.capaTwoFactorAuthForce.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuthForce': value ? '1' : '0'
});
});
this.useLocalProxyForExternalImages.subscribe((value) => {
Remote.saveAdminConfig(null, {
'UseLocalProxyForExternalImages': value ? '1' : '0'
});
});
this.verifySslCertificate.subscribe((value) => {
Remote.saveAdminConfig(null, {
'VerifySslCertificate': value ? '1' : '0'
});
});
this.allowSelfSigned.subscribe((value) => {
Remote.saveAdminConfig(null, {
'AllowSelfSigned': value ? '1' : '0'
});
});
}
onHide() {
this.adminPassword('');
this.adminPasswordNew('');
this.adminPasswordNew2('');
this.isTwoFactorDropperShown(false);
this.twoFactorDropperUser('');
this.twoFactorDropperUser.focused(false);
}
}
export { SecurityAdminSettings, SecurityAdminSettings as default };