snappymail/dev/Settings/Admin/Security.js

175 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-07-16 05:29:42 +08:00
import ko from 'ko';
2014-08-21 23:08:34 +08:00
2020-08-14 04:58:41 +08:00
import { StorageResultType } from 'Common/Enums';
2016-07-16 05:29:42 +08:00
import AppAdminStore from 'Stores/Admin/App';
import CapaAdminStore from 'Stores/Admin/Capa';
2014-08-21 23:08:34 +08:00
import Remote from 'Remote/Admin/Fetch';
2016-07-16 05:29:42 +08:00
2019-07-05 03:19:24 +08:00
import { command } from 'Knoin/Knoin';
2016-09-10 06:38:16 +08:00
const settingsGet = rl.settings.get;
2019-07-05 03:19:24 +08:00
class SecurityAdminSettings {
2016-07-16 05:29:42 +08:00
constructor() {
this.useLocalProxyForExternalImages = AppAdminStore.useLocalProxyForExternalImages;
2016-07-16 05:29:42 +08:00
this.weakPassword = AppAdminStore.weakPassword;
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.capaOpenPGP = CapaAdminStore.openPGP;
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuth = CapaAdminStore.twoFactorAuth;
this.capaTwoFactorAuthForce = CapaAdminStore.twoFactorAuthForce;
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuth.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (!value) {
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuthForce(false);
}
});
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.verifySslCertificate = ko.observable(!!settingsGet('VerifySslCertificate'));
this.allowSelfSigned = ko.observable(!!settingsGet('AllowSelfSigned'));
2014-10-18 23:19:37 +08:00
2016-07-16 05:29:42 +08:00
this.verifySslCertificate.subscribe((value) => {
2019-07-05 03:19:24 +08:00
if (!value) {
2016-07-16 05:29:42 +08:00
this.allowSelfSigned(true);
}
});
2014-08-21 23:08:34 +08:00
2016-07-17 23:03:38 +08:00
this.isTwoFactorDropperShown = ko.observable(false);
this.twoFactorDropperUser = ko.observable('');
this.twoFactorDropperUser.focused = ko.observable(false);
2016-07-16 05:29:42 +08:00
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);
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.adminPasswordUpdateError = ko.observable(false);
this.adminPasswordUpdateSuccess = ko.observable(false);
2016-07-16 05:29:42 +08:00
this.adminPassword.subscribe(() => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
2014-08-21 23:08:34 +08:00
});
2016-07-16 05:29:42 +08:00
this.adminLogin.subscribe(() => {
this.adminLoginError(false);
});
2016-07-01 06:50:11 +08:00
2016-07-16 05:29:42 +08:00
this.adminPasswordNew.subscribe(() => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
});
2016-07-16 05:29:42 +08:00
this.adminPasswordNew2.subscribe(() => {
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
this.adminPasswordNewError(false);
});
2014-08-21 23:08:34 +08:00
this.onNewAdminPasswordResponse = this.onNewAdminPasswordResponse.bind(this);
2016-09-10 06:38:16 +08:00
}
2016-07-16 05:29:42 +08:00
@command((self) => self.adminLogin().trim() && self.adminPassword())
2016-09-10 06:38:16 +08:00
saveNewAdminPasswordCommand() {
if (!this.adminLogin().trim()) {
2016-09-10 06:38:16 +08:00
this.adminLoginError(true);
return false;
}
2014-08-21 23:08:34 +08:00
2019-07-05 03:19:24 +08:00
if (this.adminPasswordNew() !== this.adminPasswordNew2()) {
2016-09-10 06:38:16 +08:00
this.adminPasswordNewError(true);
return false;
}
2016-06-30 08:02:45 +08:00
2016-09-10 06:38:16 +08:00
this.adminPasswordUpdateError(false);
this.adminPasswordUpdateSuccess(false);
2016-07-16 05:29:42 +08:00
2016-09-10 06:38:16 +08:00
Remote.saveNewAdminPassword(this.onNewAdminPasswordResponse, {
'Login': this.adminLogin(),
'Password': this.adminPassword(),
'NewPassword': this.adminPasswordNew()
});
2016-07-16 05:29:42 +08:00
2016-09-10 06:38:16 +08:00
return true;
2016-06-30 08:02:45 +08:00
}
2016-07-16 05:29:42 +08:00
2016-07-17 23:03:38 +08:00
showTwoFactorDropper() {
this.twoFactorDropperUser('');
this.isTwoFactorDropperShown(true);
setTimeout(() => {
2016-07-17 23:03:38 +08:00
this.twoFactorDropperUser.focused(true);
2020-08-14 04:58:41 +08:00
}, 50);
2016-07-17 23:03:38 +08:00
}
2016-07-16 05:29:42 +08:00
onNewAdminPasswordResponse(result, data) {
2019-07-05 03:19:24 +08:00
if (StorageResultType.Success === result && data && data.Result) {
2016-07-16 05:29:42 +08:00
this.adminPassword('');
this.adminPasswordNew('');
this.adminPasswordNew2('');
this.adminPasswordUpdateSuccess(true);
this.weakPassword(!!data.Result.Weak);
2019-07-05 03:19:24 +08:00
} else {
2016-07-16 05:29:42 +08:00
this.adminPasswordUpdateError(true);
}
2016-06-30 08:02:45 +08:00
}
2016-07-16 05:29:42 +08:00
onBuild() {
this.capaOpenPGP.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaOpenPGP': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuth.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuth': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuthForce.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuthForce': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-25 15:10:51 +08:00
2016-07-16 05:29:42 +08:00
this.useLocalProxyForExternalImages.subscribe((value) => {
Remote.saveAdminConfig(null, {
'UseLocalProxyForExternalImages': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
2014-08-21 23:08:34 +08:00
2016-07-16 05:29:42 +08:00
this.verifySslCertificate.subscribe((value) => {
Remote.saveAdminConfig(null, {
'VerifySslCertificate': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
this.allowSelfSigned.subscribe((value) => {
Remote.saveAdminConfig(null, {
'AllowSelfSigned': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
}
2016-06-30 08:02:45 +08:00
2016-07-16 05:29:42 +08:00
onHide() {
this.adminPassword('');
this.adminPasswordNew('');
this.adminPasswordNew2('');
2016-07-17 23:03:38 +08:00
this.isTwoFactorDropperShown(false);
this.twoFactorDropperUser('');
this.twoFactorDropperUser.focused(false);
2016-07-16 05:29:42 +08:00
}
}
2016-06-30 08:02:45 +08:00
2019-07-05 03:19:24 +08:00
export { SecurityAdminSettings, SecurityAdminSettings as default };