dad0136365
Build and Push Multi-Platform Images / build-and-push (push) Successful in 41s
Reformats code style (line breaks, indentation, type annotations) without changing logic. Also includes minor feature additions bundled in the same lint run (stats service, user-settings groups, agrarmonitor polling improvements). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
|
import { Reflector } from '@nestjs/core';
|
|
import { PERMISSIONS_KEY } from './permissions.decorator';
|
|
import { Permission } from './permissions.enum';
|
|
|
|
@Injectable()
|
|
export class PermissionsGuard implements CanActivate {
|
|
constructor(private reflector: Reflector) {}
|
|
|
|
canActivate(context: ExecutionContext): boolean {
|
|
const requiredPermissions = this.reflector.getAllAndOverride<Permission[]>(
|
|
PERMISSIONS_KEY,
|
|
[context.getHandler(), context.getClass()],
|
|
);
|
|
|
|
if (!requiredPermissions) {
|
|
return true;
|
|
}
|
|
|
|
const request = context.switchToHttp().getRequest();
|
|
const { user } = request;
|
|
|
|
if (request.apiKeyMetadata) {
|
|
return true;
|
|
}
|
|
|
|
if (!user || !user.permissions) {
|
|
return false;
|
|
}
|
|
|
|
const userPermissions = user.permissions as Permission[];
|
|
|
|
if (userPermissions.includes(Permission.MANAGE_ALL)) {
|
|
return true;
|
|
}
|
|
|
|
return requiredPermissions.some((permission) =>
|
|
userPermissions.includes(permission),
|
|
);
|
|
}
|
|
}
|