init
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
import { SetMetadata } from '@nestjs/common';
|
||||
import { UserRole } from '../enums';
|
||||
|
||||
export const ROLES_KEY = 'roles';
|
||||
export const Roles = (...roles: UserRole[]) => SetMetadata(ROLES_KEY, roles);
|
||||
@@ -0,0 +1,41 @@
|
||||
// Shared enums used across the platform
|
||||
|
||||
export enum UserRole {
|
||||
USER = 'user',
|
||||
ADMIN = 'admin',
|
||||
}
|
||||
|
||||
export enum AppRuntime {
|
||||
NODEJS = 'nodejs',
|
||||
LARAVEL = 'laravel',
|
||||
}
|
||||
|
||||
export enum DatabaseType {
|
||||
MYSQL = 'mysql',
|
||||
POSTGRESQL = 'postgresql',
|
||||
NONE = 'none',
|
||||
}
|
||||
|
||||
export enum DeploymentStatus {
|
||||
PENDING = 'pending',
|
||||
BUILDING = 'building',
|
||||
BUILD_FAILED = 'build_failed',
|
||||
DEPLOYING = 'deploying',
|
||||
RUNNING = 'running',
|
||||
FAILED = 'failed',
|
||||
STOPPED = 'stopped',
|
||||
DELETING = 'deleting',
|
||||
}
|
||||
|
||||
export enum ClusterStatus {
|
||||
ACTIVE = 'active',
|
||||
INACTIVE = 'inactive',
|
||||
MAINTENANCE = 'maintenance',
|
||||
}
|
||||
|
||||
export enum BuildStatus {
|
||||
QUEUED = 'queued',
|
||||
IN_PROGRESS = 'in_progress',
|
||||
SUCCESS = 'success',
|
||||
FAILED = 'failed',
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { UserRole } from '../enums';
|
||||
import { ROLES_KEY } from '../decorators/roles.decorator';
|
||||
|
||||
@Injectable()
|
||||
export class RolesGuard implements CanActivate {
|
||||
constructor(private reflector: Reflector) {}
|
||||
|
||||
canActivate(context: ExecutionContext): boolean {
|
||||
const requiredRoles = this.reflector.getAllAndOverride<UserRole[]>(ROLES_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
|
||||
if (!requiredRoles) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const { user } = context.switchToHttp().getRequest();
|
||||
return requiredRoles.some((role) => user.role === role);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user