-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a93626c
commit c64efc5
Showing
12 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './get-user-id.decorator'; | ||
export * from './get-user.decorator'; | ||
export * from './roles.decorators'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { ROLE_KEY, Role } from 'src/application'; | ||
|
||
export const Roles = (...role: Role[]) => SetMetadata(ROLE_KEY, role); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { CanActivate, ExecutionContext, Inject, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Observable } from 'rxjs'; | ||
import { ROLE_KEY, Role, TYPES } from 'src/application'; | ||
import { IAccessControlService } from 'src/shared/interfaces/access_control_service.interface'; | ||
import { Context, IContextService } from '../context'; | ||
|
||
@Injectable() | ||
export class RoleGuard implements CanActivate { | ||
private context: Context; | ||
constructor( | ||
private readonly reflector: Reflector, | ||
@Inject(TYPES.IAccessControlService) private readonly accessControlService: IAccessControlService, | ||
@Inject(TYPES.IContextService) | ||
private readonly contextService: IContextService, | ||
) { | ||
this.context = this.contextService.getContext(); | ||
} | ||
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> { | ||
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLE_KEY, [ | ||
context.getHandler(), | ||
context.getClass(), | ||
]); | ||
for (const role of requiredRoles) { | ||
const isAuthorized = this.accessControlService.isAuthorized({ | ||
currentRole: this.context.role as Role, | ||
requiredRole: role, | ||
}); | ||
if (isAuthorized) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
backend/src/shared/interfaces/access_control_service.interface.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { IIsAuthorizedProps } from './shared.interface'; | ||
|
||
export interface IAccessControlService { | ||
isAuthorized({ currentRole, requiredRole }: IIsAuthorizedProps): boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Role } from '../../application'; | ||
|
||
export interface IRoleService { | ||
sortRoles(roles: Role[]): Role[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Role } from 'src/application'; | ||
|
||
export interface IIsAuthorizedProps { | ||
currentRole: Role; | ||
requiredRole: Role; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Inject, Injectable } from '@nestjs/common'; | ||
import { Role, TYPES } from 'src/application'; | ||
import { IAccessControlService } from '../interfaces/access_control_service.interface'; | ||
import { IRoleService } from '../interfaces/role_service.interface'; | ||
import { IIsAuthorizedProps } from '../interfaces/shared.interface'; | ||
|
||
@Injectable() | ||
export class AccessControlService implements IAccessControlService { | ||
private hierarchies: Map<string, number>[] = []; | ||
private priority = 1; | ||
|
||
constructor(@Inject(TYPES.IRoleService) private readonly roleService: IRoleService) { | ||
this.mapRoleToPriority(); | ||
} | ||
|
||
private mapRoleToPriority(): void { | ||
const sortedRoles = this.roleService.sortRoles(Object.values(Role)); | ||
if (sortedRoles?.length) { | ||
sortedRoles.reduce((map, role) => { | ||
map.set(role, this.priority); | ||
this.priority++; | ||
this.hierarchies.push(map); | ||
return map; | ||
}, new Map<string, number>()); | ||
} | ||
} | ||
|
||
public isAuthorized({ currentRole, requiredRole }: IIsAuthorizedProps): boolean { | ||
let authorized = false; | ||
for (const hierarchy of this.hierarchies) { | ||
const priority = hierarchy.get(currentRole); | ||
const requirePriority = hierarchy.get(requiredRole); | ||
if (priority && requirePriority && priority >= requirePriority) { | ||
authorized = true; | ||
} | ||
} | ||
return authorized; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Role, RoleOrder } from '../../application'; | ||
import { IRoleService } from '../interfaces/role_service.interface'; | ||
|
||
@Injectable() | ||
export class RoleService implements IRoleService { | ||
sortRoles(roles: Role[]): Role[] { | ||
const validRoles = roles.filter((role) => role in RoleOrder); | ||
if (validRoles?.length) { | ||
validRoles.sort((a, b) => RoleOrder[a] - RoleOrder[b]); | ||
} | ||
return validRoles; | ||
} | ||
} |