snappymail/dev/Settings/Admin/Security.js

177 lines
4.2 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';
2021-03-10 18:44:48 +08:00
import { SettingsGet } from 'Common/Globals';
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
import { decorateKoCommands } from 'Knoin/Knoin';
2016-09-10 06:38:16 +08:00
2021-01-22 23:32:08 +08:00
export class SecurityAdminSettings {
2016-07-16 05:29:42 +08:00
constructor() {
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
2020-10-27 18:09:24 +08:00
ko.addObservablesTo(this, {
2021-03-10 18:44:48 +08:00
useLocalProxyForExternalImages: !!SettingsGet('UseLocalProxyForExternalImages'),
2020-10-27 18:09:24 +08:00
2021-03-10 18:44:48 +08:00
verifySslCertificate: !!SettingsGet('VerifySslCertificate'),
allowSelfSigned: !!SettingsGet('AllowSelfSigned'),
2020-10-27 18:09:24 +08:00
isTwoFactorDropperShown: false,
twoFactorDropperUser: '',
twoFactorDropperUserFocused: false,
2021-03-10 18:44:48 +08:00
adminLogin: SettingsGet('AdminLogin'),
2020-10-27 18:09:24 +08:00
adminLoginError: false,
adminPassword: '',
adminPasswordNew: '',
adminPasswordNew2: '',
adminPasswordNewError: false,
adminPasswordUpdateError: false,
adminPasswordUpdateSuccess: false
});
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
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-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);
decorateKoCommands(this, {
saveNewAdminPasswordCommand: self => self.adminLogin().trim() && self.adminPassword()
});
2016-09-10 06:38:16 +08:00
}
2016-07-16 05:29:42 +08:00
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(() => {
2020-10-27 18:09:24 +08:00
this.twoFactorDropperUserFocused(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 => {
2016-07-16 05:29:42 +08:00
Remote.saveAdminConfig(null, {
'CapaOpenPGP': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
this.capaTwoFactorAuth.subscribe(value => {
2016-07-16 05:29:42 +08:00
Remote.saveAdminConfig(null, {
'CapaTwoFactorAuth': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
this.capaTwoFactorAuthForce.subscribe(value => {
2016-07-16 05:29:42 +08:00
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
this.useLocalProxyForExternalImages.subscribe(value => {
2016-07-16 05:29:42 +08:00
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
this.verifySslCertificate.subscribe(value => {
2016-07-16 05:29:42 +08:00
Remote.saveAdminConfig(null, {
'VerifySslCertificate': value ? '1' : '0'
2016-07-16 05:29:42 +08:00
});
2016-06-30 08:02:45 +08:00
});
this.allowSelfSigned.subscribe(value => {
2016-07-16 05:29:42 +08:00
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('');
2020-10-27 18:09:24 +08:00
this.twoFactorDropperUserFocused(false);
2016-07-16 05:29:42 +08:00
}
}