core&ui: allow SU edit user profile & security (#561) (#562)

This commit is contained in:
panda authored and GitHub committed 2023-04-30 00:42:50 +08:00
1 parent 82460cf8e9
commit 4cb5444f3b
5 files changed
+22 -9

No files matched your search

+12 -3
View File
@@ -166,6 +166,7 @@ class HomeSecurityHandler extends Handler {
}
this.response.template = 'home_security.html';
this.response.body = {
sudoUid: this.session.sudoUid || null,
sessions,
authenticators: this.user._authenticators.map((c) => pick(c, [
'credentialID', 'name', 'credentialType', 'credentialDeviceType',
@@ -180,9 +181,13 @@ class HomeSecurityHandler extends Handler {
@param('current', Types.String)
@param('password', Types.Password)
@param('verifyPassword', Types.Password)
async postChangePassword(_: string, current: string, password: string, verify: string) {
async postChangePassword(domainId: string, current: string, password: string, verify: string) {
if (password !== verify) throw new VerifyPasswordError();
this.user.checkPassword(current);
if (this.session.sudoUid) {
const udoc = await user.getById(domainId, this.session.sudoUid);
if (!udoc) throw new UserNotFoundError(this.session.sudoUid);
udoc.checkPassword(current);
} else this.user.checkPassword(current);
await user.setPassword(this.user._id, password);
await token.delByUid(this.user._id);
this.response.redirect = this.url('user_login');
@@ -194,7 +199,11 @@ class HomeSecurityHandler extends Handler {
async postChangeMail(domainId: string, current: string, email: string) {
const mailDomain = email.split('@')[1];
if (await BlackListModel.get(`mail::${mailDomain}`)) throw new BlacklistedError(mailDomain);
this.user.checkPassword(current);
if (this.session.sudoUid) {
const udoc = await user.getById(domainId, this.session.sudoUid);
if (!udoc) throw new UserNotFoundError(this.session.sudoUid);
udoc.checkPassword(current);
} else this.user.checkPassword(current);
const udoc = await user.getByEmail(domainId, email);
if (udoc) throw new UserAlreadyExistError(email);
await this.limitRate('send_mail', 3600, 30);
+4 -2
View File
@@ -12,7 +12,7 @@ import storage from '../model/storage';
import * as system from '../model/system';
import user from '../model/user';
import {
Handler, param, post, Types,
Handler, param, post, requireSudo, Types,
} from '../service/server';
import { encodeRFC5987ValueChars } from '../service/storage';
import { builtinConfig } from '../settings';
@@ -122,8 +122,10 @@ export class StorageHandler extends Handler {
}
export class SwitchAccountHandler extends Handler {
@requireSudo
@param('uid', Types.Int)
async get(domainId: string, uid: number) {
this.session.sudoUid = this.user._id;
this.session.uid = uid;
this.back();
}
@@ -134,5 +136,5 @@ export async function apply(ctx) {
ctx.Route('home_files', '/file', FilesHandler);
ctx.Route('fs_download', '/file/:uid/:filename', FSDownloadHandler);
ctx.Route('storage', '/storage', StorageHandler);
ctx.Route('switch_account', '/account', SwitchAccountHandler, PRIV.PRIV_EDIT_SYSTEM);
ctx.Route('switch_account', '/account/:uid', SwitchAccountHandler, PRIV.PRIV_EDIT_SYSTEM);
}