snappymail/dev/Settings/Admin/Security.js

176 lines
4.5 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
2019-07-05 03:19:24 +08:00
import { trim, boolToAjax } from 'Common/Utils';
import { StorageResultType, Magics } from 'Common/Enums';
2019-07-05 03:19:24 +08:00
import { settingsGet } from 'Storage/Settings';
2014-08-25 15:10:51 +08:00
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
2016-07-16 05:29:42 +08:00
import Remote from 'Remote/Admin/Ajax';
2019-07-05 03:19:24 +08:00
import { command } from 'Knoin/Knoin';
2016-09-10 06:38:16 +08:00
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) => trim(self.adminLogin()) && self.adminPassword())
2016-09-10 06:38:16 +08:00
saveNewAdminPasswordCommand() {
if (!trim(this.adminLogin())) {
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);
}, Magics.Time50ms);
}
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': boolToAjax(value)
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuth.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuth': boolToAjax(value)
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
this.capaTwoFactorAuthForce.subscribe((value) => {
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuthForce': boolToAjax(value)
});
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': boolToAjax(value)
});
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': boolToAjax(value)
});
2016-06-30 08:02:45 +08:00
});
2016-07-16 05:29:42 +08:00
this.allowSelfSigned.subscribe((value) => {
Remote.saveAdminConfig(null, {
'AllowSelfSigned': boolToAjax(value)
});
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 };