oak-general-business/es/components/user/password/update/index.js

154 lines
4.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { encryptPasswordSha1 } from '../../../../utils/password';
export default OakComponent({
entity: 'user',
projection: {
id: 1,
hasPassword: 1,
verifyPasswordAt: 1,
},
isList: false,
properties: {
once: false,
onSuccess: () => undefined,
},
formData({ features, data }) {
const isRoot = features.token.isRoot();
if (!isRoot && features.token.getUserId() === data.id) {
// 绝大部分情况应该都是自己更新自己的密码除了root
const needVerifyPassword = features.token.needVerifyPassword();
return { needVerifyPassword };
}
return {};
},
data: {
pwd: undefined,
again: undefined,
error: '',
successful: false,
mode: 'all',
pwdMin: 8,
pwdMax: 24,
needVerify: false,
regexs: [],
tip: '',
},
lifetimes: {
async ready() {
const system = this.features.application.getApplication().system;
const passwordConfig = system?.config.Password;
const mode = passwordConfig?.mode ?? 'all';
const pwdMin = passwordConfig?.min ?? 8;
const pwdMax = passwordConfig?.max ?? 24;
const needVerify = !!passwordConfig?.verify;
const regexs = (passwordConfig?.regexs && passwordConfig?.regexs.length > 0) ? passwordConfig?.regexs : [];
const tip = passwordConfig?.tip ?? '';
this.setState({
mode,
pwdMin,
pwdMax,
needVerify,
regexs,
tip,
});
this.checkValid();
}
},
methods: {
checkValid() {
const { once } = this.props;
const { pwd, again, pwdMin, pwdMax, needVerify, regexs, } = this.state;
if (!pwd) {
this.setState({
error: this.t('rule.notNull'),
});
}
else if (pwd.length < pwdMin) {
this.setState({
error: this.t('rule.lengthMin', { min: pwdMin }),
});
}
else if (pwd.length > pwdMax) {
this.setState({
error: this.t('rule.lengthMax', { max: pwdMax }),
});
}
else if (!!needVerify && regexs && regexs.length > 0) {
for (const regex of regexs) {
const pattern = new RegExp(regex);
if (!pattern.test(pwd)) {
this.setState({
error: this.t('rule.regex'),
});
return;
}
}
if (!once && pwd !== again) {
this.setState({
error: this.t('rule.unequal'),
});
}
else {
this.setState({
error: '',
});
}
}
else if (!once && pwd !== again) {
this.setState({
error: this.t('rule.unequal'),
});
}
else {
this.setState({
error: '',
});
}
},
setPwd(pwd) {
this.setState({ pwd }, () => this.checkValid());
},
setPwdMp(evt) {
const { value } = evt.detail;
this.setPwd(value);
},
clearPwdMp() {
this.setPwd('');
},
setAgain(again) {
this.setState({ again }, () => this.checkValid());
},
setAgainMp(evt) {
const { value } = evt.detail;
this.setAgain(value);
},
clearAgainMp() {
this.setAgain('');
},
async commit() {
const { pwd, mode } = this.state;
if (mode === 'all') {
this.update({ password: pwd, passwordSha1: encryptPasswordSha1(pwd) });
}
else if (mode === 'plain') {
this.update({ password: pwd });
}
else if (mode === 'sha1') {
this.update({ passwordSha1: encryptPasswordSha1(pwd) });
}
await this.execute();
this.setState({
successful: true,
});
},
onConfirm() {
const { onSuccess } = this.props;
if (onSuccess) {
onSuccess();
}
else {
this.navigateBack();
}
}
}
});