595614137c
- 校验 Firebase 身份并映射数字人账号 - 关闭数字人密码和游客入口,拒绝旧会话
267 lines
7.1 KiB
TypeScript
267 lines
7.1 KiB
TypeScript
/** 管理员、员工与 C 端身份分别校验,禁止按同名账号跨体系回退。 */
|
|
import { currentDigitalUser } from "../digital/digital.users";
|
|
import { badRequest, forbidden, unauthorized } from "../../shared/http-error";
|
|
import { authRepository } from "./auth.repository";
|
|
import type {
|
|
AuthJwtPayload,
|
|
AuthUser,
|
|
EmployeeLoginAccount,
|
|
LoginInput,
|
|
LoginScene,
|
|
SuperAdmin,
|
|
} from "./auth.types";
|
|
import { hashPassword, verifyPassword } from "./password";
|
|
import { credentialService } from "../credentials/credential.service";
|
|
import { permissionService } from "../permissions/permission.service";
|
|
|
|
async function toAuthUser(admin: SuperAdmin): Promise<AuthUser> {
|
|
const roleCodes = ["super_admin"];
|
|
const permissions = await permissionService.resolvePermissions(
|
|
"SUPER_ADMIN",
|
|
roleCodes,
|
|
);
|
|
|
|
return {
|
|
id: admin.id,
|
|
username: admin.username,
|
|
displayName: admin.displayName,
|
|
accountType: "SUPER_ADMIN",
|
|
roles: [
|
|
{
|
|
id: 0,
|
|
code: "super_admin",
|
|
name: "超级管理员",
|
|
},
|
|
],
|
|
permissions,
|
|
canManage: true,
|
|
};
|
|
}
|
|
|
|
async function toEmployeeAuthUser(
|
|
employee: EmployeeLoginAccount,
|
|
): Promise<AuthUser> {
|
|
const roleCodes = employee.roles.map((role) => role.code);
|
|
const permissions = await permissionService.resolvePermissions(
|
|
"EMPLOYEE",
|
|
roleCodes,
|
|
);
|
|
|
|
return {
|
|
id: employee.id,
|
|
username: employee.username,
|
|
displayName: employee.displayName,
|
|
accountType: "EMPLOYEE",
|
|
storeId: employee.storeId,
|
|
storeName: employee.storeName,
|
|
roles: employee.roles,
|
|
permissions,
|
|
canManage: permissions.some((permission) => permission.endsWith(":manage")),
|
|
mustChangePassword: employee.mustChangePassword,
|
|
};
|
|
}
|
|
|
|
function hasBackendMenu(user: AuthUser): boolean {
|
|
return user.permissions.includes("*") || user.permissions.length > 0;
|
|
}
|
|
|
|
function toJwtPayload(user: AuthUser, scene: LoginScene): AuthJwtPayload {
|
|
const subjectPrefix =
|
|
user.accountType === "SUPER_ADMIN" ? "super_admin" : "employee";
|
|
|
|
return {
|
|
sub: `${subjectPrefix}:${user.id}`,
|
|
accountType: user.accountType,
|
|
scene,
|
|
adminId: user.accountType === "SUPER_ADMIN" ? user.id : undefined,
|
|
employeeId: user.accountType === "EMPLOYEE" ? user.id : undefined,
|
|
username: user.username,
|
|
roleCodes: user.roles.map((role) => role.code),
|
|
permissions: user.permissions,
|
|
canManage: user.canManage,
|
|
};
|
|
}
|
|
|
|
export const authService = {
|
|
async loginManagement(input: LoginInput): Promise<{
|
|
user: AuthUser;
|
|
payload: AuthJwtPayload;
|
|
}> {
|
|
const admin = await authRepository.findByUsername(input.username);
|
|
|
|
if (admin) {
|
|
const passwordMatched = await verifyPassword(
|
|
input.password,
|
|
admin.passwordHash,
|
|
);
|
|
|
|
if (!passwordMatched) {
|
|
throw unauthorized("用户名或密码错误");
|
|
}
|
|
|
|
if (admin.status === "INACTIVE") {
|
|
throw unauthorized("账号已被禁用");
|
|
}
|
|
|
|
await authRepository.updateLastLoginAt(admin.id);
|
|
|
|
const user = await toAuthUser(admin);
|
|
|
|
return {
|
|
user,
|
|
payload: toJwtPayload(user, "MANAGEMENT"),
|
|
};
|
|
}
|
|
|
|
const employee = await authRepository.findEmployeeByPhoneForLogin(
|
|
input.username,
|
|
);
|
|
|
|
if (!employee) {
|
|
throw unauthorized("用户名或密码错误");
|
|
}
|
|
|
|
const passwordMatched = await verifyPassword(
|
|
input.password,
|
|
employee.passwordHash,
|
|
);
|
|
|
|
if (!passwordMatched) {
|
|
throw unauthorized("用户名或密码错误");
|
|
}
|
|
|
|
if (employee.status === "INACTIVE") {
|
|
throw unauthorized("账号已被禁用");
|
|
}
|
|
|
|
if (employee.storeStatus === "INACTIVE") {
|
|
throw unauthorized("所属门店已被禁用");
|
|
}
|
|
|
|
await authRepository.updateEmployeeLastLoginAt(employee.id);
|
|
|
|
const user = await toEmployeeAuthUser(employee);
|
|
|
|
if (!hasBackendMenu(user)) {
|
|
throw unauthorized("当前账号没有后台登录权限");
|
|
}
|
|
|
|
return {
|
|
user,
|
|
payload: toJwtPayload(user, "MANAGEMENT"),
|
|
};
|
|
},
|
|
|
|
async loginEmployee(input: LoginInput): Promise<{
|
|
user: AuthUser;
|
|
payload: AuthJwtPayload;
|
|
}> {
|
|
const employee = await authRepository.findEmployeeByPhoneForLogin(
|
|
input.username,
|
|
);
|
|
|
|
if (!employee) {
|
|
throw unauthorized("用户名或密码错误");
|
|
}
|
|
|
|
const passwordMatched = await verifyPassword(
|
|
input.password,
|
|
employee.passwordHash,
|
|
);
|
|
|
|
if (!passwordMatched) {
|
|
throw unauthorized("用户名或密码错误");
|
|
}
|
|
|
|
if (employee.status === "INACTIVE") {
|
|
throw unauthorized("账号已被禁用");
|
|
}
|
|
|
|
if (employee.storeStatus === "INACTIVE") {
|
|
throw unauthorized("所属门店已被禁用");
|
|
}
|
|
|
|
await authRepository.updateEmployeeLastLoginAt(employee.id);
|
|
|
|
const user = await toEmployeeAuthUser(employee);
|
|
|
|
return {
|
|
user,
|
|
payload: toJwtPayload(user, "EMPLOYEE_APP"),
|
|
};
|
|
},
|
|
|
|
async getCurrentUser(payload: AuthJwtPayload): Promise<AuthUser> {
|
|
if (payload.accountType === "DIGITAL_USER")
|
|
return currentDigitalUser(payload);
|
|
if (payload.accountType === "SUPER_ADMIN" && payload.adminId) {
|
|
const admin = await authRepository.findActiveById(payload.adminId);
|
|
|
|
if (!admin) {
|
|
throw unauthorized("登录已失效,请重新登录");
|
|
}
|
|
|
|
return toAuthUser(admin);
|
|
}
|
|
|
|
if (payload.accountType === "EMPLOYEE" && payload.employeeId) {
|
|
const employee = await authRepository.findActiveEmployeeById(
|
|
payload.employeeId,
|
|
);
|
|
|
|
if (!employee) {
|
|
throw unauthorized("登录已失效,请重新登录");
|
|
}
|
|
|
|
return toEmployeeAuthUser(employee);
|
|
}
|
|
|
|
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 === "DIGITAL_USER")
|
|
throw forbidden("请在 Google 账号中管理密码");
|
|
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);
|
|
},
|
|
};
|