gg
This commit is contained in:
@@ -13,11 +13,13 @@ import {
|
||||
ROLES_KEY,
|
||||
PERMISSIONS_KEY,
|
||||
} from "../../../common/decorators";
|
||||
import { normalizeRole } from "../../../common/constants/roles";
|
||||
|
||||
interface AuthenticatedUser {
|
||||
id: string;
|
||||
email: string;
|
||||
roles: string[];
|
||||
role?: string;
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
@@ -88,11 +90,28 @@ export class RolesGuard implements CanActivate {
|
||||
|
||||
const user = req.user as AuthenticatedUser | undefined;
|
||||
|
||||
if (!user || !user.roles) {
|
||||
if (!user) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasRole = requiredRoles.some((role) => user.roles.includes(role));
|
||||
const normalizedUserRoles = (user.roles?.length
|
||||
? user.roles
|
||||
: user.role
|
||||
? [user.role]
|
||||
: []
|
||||
).map((role) => normalizeRole(role));
|
||||
|
||||
const normalizedRequiredRoles = requiredRoles.map((role) =>
|
||||
normalizeRole(role),
|
||||
);
|
||||
|
||||
if (normalizedUserRoles.length === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasRole = normalizedRequiredRoles.some((role) =>
|
||||
normalizedUserRoles.includes(role),
|
||||
);
|
||||
if (!hasRole) {
|
||||
throw new ForbiddenException("PERMISSION_DENIED");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user