admin perms decorator
deploy to danak / build_and_deploy (push) Has been cancelled

This commit is contained in:
2026-07-24 17:16:54 +03:30
parent 857d10bf44
commit b793dad234
6 changed files with 83 additions and 79 deletions
+13 -14
View File
@@ -17,6 +17,7 @@ import { AuthService } from '../services/auth.service';
export interface AdminAuthRequest extends Request {
adminId: string;
permissions: string[];
}
@Injectable()
@@ -61,26 +62,24 @@ export class AdminAuthGuard implements CanActivate {
request['adminId'] = payload.adminId;
const adminPermissions = await this.authService.getAdminPermissionsFromCache(payload.adminId);
request['permissions'] = adminPermissions;
// check if the user has the required permissions
const requiredPermissions =
this.reflector.getAllAndOverride<string[]>(PERMISSIONS_KEY, [context.getHandler(), context.getClass()]) ?? [];
if (!requiredPermissions || requiredPermissions.length === 0) {
return true;
}
if (requiredPermissions.length > 0) {
const hasPermission = requiredPermissions.every(p => adminPermissions.includes(p));
const adminPermissions = await this.authService.getAdminPermissionsFromCache(payload.adminId)
const hasPermission = requiredPermissions.every(p => adminPermissions.includes(p));
if (!hasPermission) {
this.logger.warn('Insufficient permissions', {
adminId: payload.adminId,
required: requiredPermissions,
has: hasPermission,
});
throw new ForbiddenException('You are not authorized to access this resource');
if (!hasPermission) {
this.logger.warn('Insufficient permissions', {
adminId: payload.adminId,
required: requiredPermissions,
has: hasPermission,
});
throw new ForbiddenException('You are not authorized to access this resource');
}
}
return true;