fix(users): preserve superadmin roles and enforce actor privilege boundary

This commit is contained in:
devops authored and devops committed 2026-10-24 05:14:22 +00:00
1 parent 3321bac22d
commit ca3d4f6a16
7 files changed
+183 -29

No files matched your search

+15 -20
View File
@@ -61,15 +61,8 @@ export class CouponInput {
targets?: CouponTargetInput[];
}
const ALLOWED_USER_ROLES = [
"User_PetOwner",
"User_Vet",
"User_Clinic",
"User_PetShop",
"Customer",
"Admin",
"SUPER_ADMIN",
];
import { ALLOWED_USER_ROLES, assertUserMutationAllowed } from "./user-role-policy";
import type { UserAdminActor } from "./user-role-policy";
@Injectable()
export class AdminService {
@@ -242,7 +235,8 @@ export class AdminService {
return user;
}
async createUser(dto: CreateUserDto) {
async createUser(dto: CreateUserDto, actor?: UserAdminActor) {
assertUserMutationAllowed(actor, undefined, dto.role);
const mobile = normalizeMobile(dto.mobile);
const existingMobile = await this.prisma.user.findUnique({
where: { mobile },
@@ -289,7 +283,7 @@ export class AdminService {
});
}
async updateUser(id: string, dto: UpdateUserDto) {
async updateUser(id: string, dto: UpdateUserDto, actor?: UserAdminActor) {
if (id === "12345678-1234-1234-1234-123456789012") {
throw new BadRequestException(
"امکان ویرایش حساب ادمین پیش‌فرض وجود ندارد",
@@ -301,6 +295,8 @@ export class AdminService {
throw new NotFoundException(`کاربری با شناسه ${id} یافت نشد`);
}
assertUserMutationAllowed(actor, user.role, dto.role);
const normalizedMobile = dto.mobile
? normalizeMobile(dto.mobile)
: undefined;
@@ -355,17 +351,11 @@ export class AdminService {
});
}
async updateUserRole(id: string, role: string) {
if (!ALLOWED_USER_ROLES.includes(role)) {
throw new BadRequestException("نقش کاربری مشخص شده نامعتبر است");
}
return this.prisma.user.update({
where: { id },
data: { role },
});
async updateUserRole(id: string, role: string, actor?: UserAdminActor) {
return this.updateUser(id, { role }, actor);
}
async deleteUser(id: string) {
async deleteUser(id: string, actor?: UserAdminActor) {
if (id === "12345678-1234-1234-1234-123456789012") {
throw new BadRequestException("امکان حذف حساب ادمین پیش‌فرض وجود ندارد");
}
@@ -375,6 +365,8 @@ export class AdminService {
throw new NotFoundException(`کاربری با شناسه ${id} یافت نشد`);
}
assertUserMutationAllowed(actor, user.role);
return this.prisma.$transaction(async (tx) => {
await tx.userAddress.deleteMany({ where: { userId: id } });
await tx.walletTransaction.deleteMany({ where: { userId: id } });
@@ -1412,12 +1404,15 @@ export class AdminService {
amount: number,
type: "deposit" | "withdrawal" | "refund",
description?: string,
actor?: UserAdminActor,
) {
const user = await this.prisma.user.findUnique({ where: { id: userId } });
if (!user) {
throw new NotFoundException(`کاربری با شناسه ${userId} یافت نشد.`);
}
assertUserMutationAllowed(actor, user.role);
const transaction = await this.prisma.walletTransaction.create({
data: {
userId,