feat: 增加员工端工作台后端能力
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { unauthorized } from "../../shared/http-error";
|
||||
import { badRequest, forbidden, unauthorized } from "../../shared/http-error";
|
||||
import { authRepository } from "./auth.repository";
|
||||
import type {
|
||||
AuthJwtPayload,
|
||||
@@ -8,7 +8,8 @@ import type {
|
||||
LoginScene,
|
||||
SuperAdmin,
|
||||
} from "./auth.types";
|
||||
import { verifyPassword } from "./password";
|
||||
import { hashPassword, verifyPassword } from "./password";
|
||||
import { credentialService } from "../credentials/credential.service";
|
||||
import { permissionService } from "../permissions/permission.service";
|
||||
|
||||
async function toAuthUser(admin: SuperAdmin): Promise<AuthUser> {
|
||||
@@ -54,6 +55,7 @@ async function toEmployeeAuthUser(
|
||||
roles: employee.roles,
|
||||
permissions,
|
||||
canManage: permissions.some((permission) => permission.endsWith(":manage")),
|
||||
mustChangePassword: employee.mustChangePassword,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -212,4 +214,42 @@ export const authService = {
|
||||
|
||||
throw unauthorized("登录已失效,请重新登录");
|
||||
},
|
||||
|
||||
async getCurrentEmployeeUser(payload: AuthJwtPayload): Promise<AuthUser> {
|
||||
const user = await this.getCurrentUser(payload);
|
||||
|
||||
if (user.accountType !== "EMPLOYEE") {
|
||||
throw forbidden("只有员工账号可以访问员工端接口");
|
||||
}
|
||||
|
||||
return user;
|
||||
},
|
||||
|
||||
async updateOwnPassword(
|
||||
payload: AuthJwtPayload,
|
||||
input: { oldPassword: string; newPassword: string },
|
||||
meta: { ip?: string | null; userAgent?: string | null },
|
||||
): Promise<AuthUser> {
|
||||
if (payload.accountType !== "EMPLOYEE" || !payload.employeeId) {
|
||||
throw forbidden("超级管理员暂不通过员工端接口修改密码");
|
||||
}
|
||||
|
||||
const employee = await authRepository.findActiveEmployeeById(payload.employeeId);
|
||||
|
||||
if (!employee) {
|
||||
throw unauthorized("登录已失效,请重新登录");
|
||||
}
|
||||
|
||||
const passwordMatched = await verifyPassword(input.oldPassword, employee.passwordHash);
|
||||
|
||||
if (!passwordMatched) {
|
||||
throw badRequest("旧密码不正确");
|
||||
}
|
||||
|
||||
const passwordHash = await hashPassword(input.newPassword);
|
||||
await authRepository.updateEmployeePasswordHash(employee.id, passwordHash);
|
||||
const actor = await this.getCurrentEmployeeUser(payload);
|
||||
await credentialService.recordOwnPasswordChanged(employee.id, actor, meta);
|
||||
return this.getCurrentUser(payload);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user