This commit is contained in:
2026-06-07 14:21:02 +03:30
parent db32d35fe7
commit 7d8aeea90f
13 changed files with 486 additions and 13 deletions

View File

@@ -0,0 +1,46 @@
import Permission from "../../models/Permission";
import RolePermission from "../../models/RolePermission";
import UserPermission from "../../models/UserPermission";
export function checkPermission(permissionName: string) {
return async (req: any, res: any, next: any) => {
const user = req.user;
const permission = await Permission.findOne({
where: { name: permissionName },
});
if (!permission) {
return res.status(403).json({ message: "Permission not found" });
}
// ✅ 1- چک سطح کاربر
const userPermission = await UserPermission.findOne({
where: {
userId: user.id,
permissionId: permission.id,
},
});
if (userPermission) {
return next();
}
// ✅ 2- چک سطح رول
const rolePermission = await RolePermission.findOne({
where: {
roleId: user.roleId,
permissionId: permission.id,
},
});
if (rolePermission) {
return next();
}
return res.status(403).json({
message: "Access Denied",
});
};
}