snappymail/dev/Settings/Admin/Security.js

120 lines
2.6 KiB
JavaScript
Raw Normal View History

2022-02-24 19:22:27 +08:00
import { SettingsGet, SettingsCapa } from 'Common/Globals';
2022-02-17 16:36:29 +08:00
import { addObservablesTo, addSubscribablesTo } from 'External/ko';
import Remote from 'Remote/Admin/Fetch';
2016-07-16 05:29:42 +08:00
import { decorateKoCommands } from 'Knoin/Knoin';
2022-03-01 17:18:12 +08:00
import { AbstractViewSettings } from 'Knoin/AbstractViews';
2016-09-10 06:38:16 +08:00
export class AdminSettingsSecurity extends AbstractViewSettings {
2016-07-16 05:29:42 +08:00
constructor() {
2022-03-01 17:18:12 +08:00
super();
2014-08-21 23:08:34 +08:00
this.addSettings(['UseLocalProxyForExternalImages']);
2020-10-27 18:09:24 +08:00
2022-03-01 17:18:12 +08:00
this.weakPassword = rl.app.weakPassword;
2020-10-27 18:09:24 +08:00
2022-03-01 17:18:12 +08:00
addObservablesTo(this, {
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,
2022-08-29 18:17:08 +08:00
adminTOTP: '',
2020-10-27 18:09:24 +08:00
2022-08-29 18:17:08 +08:00
saveError: false,
saveSuccess: false,
viewQRCode: '',
2022-03-06 05:25:32 +08:00
capaOpenPGP: SettingsCapa('OpenPGP')
2016-07-16 05:29:42 +08:00
});
2016-07-01 06:50:11 +08:00
2022-03-01 17:18:12 +08:00
const reset = () => {
2022-08-29 18:17:08 +08:00
this.saveError(false);
this.saveSuccess(false);
2022-03-01 17:18:12 +08:00
this.adminPasswordNewError(false);
};
addSubscribablesTo(this, {
2021-03-16 17:46:22 +08:00
adminPassword: () => {
2022-08-29 18:17:08 +08:00
this.saveError(false);
this.saveSuccess(false);
2021-03-16 17:46:22 +08:00
},
adminLogin: () => this.adminLoginError(false),
2022-08-29 18:17:08 +08:00
adminTOTP: value => {
2022-08-29 21:51:16 +08:00
if (/[A-Z2-7]{16,}/.test(value) && 0 == value.length * 5 % 8) {
2022-08-29 18:17:08 +08:00
Remote.request('AdminQRCode', (iError, data) => {
if (!iError) {
console.dir({data:data});
this.viewQRCode(data.Result);
}
}, {
'username': this.adminLogin(),
'TOTP': this.adminTOTP()
});
} else {
this.viewQRCode('');
}
},
2022-03-01 17:18:12 +08:00
adminPasswordNew: reset,
2021-03-16 17:46:22 +08:00
2022-03-01 17:18:12 +08:00
adminPasswordNew2: reset,
2021-03-16 17:46:22 +08:00
2022-03-01 17:18:12 +08:00
capaOpenPGP: value => Remote.saveSetting('CapaOpenPGP', value)
2016-07-16 05:29:42 +08:00
});
2014-08-21 23:08:34 +08:00
2022-08-29 18:17:08 +08:00
this.adminTOTP(SettingsGet('AdminTOTP'));
decorateKoCommands(this, {
2022-08-29 22:44:06 +08:00
saveAdminUserCommand: self => self.adminLogin().trim() && self.adminPassword()
});
2016-09-10 06:38:16 +08:00
}
2016-07-16 05:29:42 +08:00
2022-08-29 22:44:06 +08:00
saveAdminUserCommand() {
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
2022-08-29 18:17:08 +08:00
this.saveError(false);
this.saveSuccess(false);
2016-07-16 05:29:42 +08:00
Remote.request('AdminPasswordUpdate', (iError, data) => {
2021-04-23 03:56:01 +08:00
if (iError) {
2022-08-29 18:17:08 +08:00
this.saveError(true);
2021-04-23 03:56:01 +08:00
} else {
this.adminPassword('');
this.adminPasswordNew('');
this.adminPasswordNew2('');
2022-08-29 18:17:08 +08:00
this.saveSuccess(true);
2021-04-23 03:56:01 +08:00
this.weakPassword(!!data.Result.Weak);
}
}, {
Login: this.adminLogin(),
Password: this.adminPassword(),
newPassword: this.adminPasswordNew(),
TOTP: this.adminTOTP()
2016-09-10 06:38:16 +08:00
});
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
onHide() {
this.adminPassword('');
this.adminPasswordNew('');
this.adminPasswordNew2('');
}
}