This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user