-
Notifications
You must be signed in to change notification settings - Fork 5
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
978f5db
commit d7a55f9
Showing
10 changed files
with
176 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export enum Action { | ||
Manage = 'manage', | ||
Create = 'create', | ||
Read = 'read', | ||
Update = 'update', | ||
Delete = 'delete', | ||
} |
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,30 @@ | ||
import { | ||
AbilityBuilder, | ||
ExtractSubjectType, | ||
InferSubjects, | ||
PureAbility, | ||
AbilityClass, | ||
} from '@casl/ability'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { Action } from './action.enum'; | ||
import { User } from '@app/modules/user/user.entity'; | ||
|
||
type Subjects = InferSubjects<typeof User | User> | 'all'; | ||
|
||
export type AppAbility = PureAbility<[Action, Subjects]>; | ||
|
||
@Injectable() | ||
export class CaslAbilityFactory { | ||
createForUser(user: User) { | ||
const { can, cannot, build } = new AbilityBuilder<AppAbility>( | ||
PureAbility as AbilityClass<AppAbility>, | ||
); | ||
|
||
// TODO: Build abilities | ||
|
||
return build({ | ||
detectSubjectType: (subject) => | ||
subject.constructor as ExtractSubjectType<Subjects>, | ||
}); | ||
} | ||
} |
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,9 @@ | ||
import { Global, Module } from '@nestjs/common'; | ||
import { CaslAbilityFactory } from './casl-ability.factory'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [CaslAbilityFactory], | ||
exports: [CaslAbilityFactory], | ||
}) | ||
export class CaslModule {} |
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 { SetMetadata } from '@nestjs/common'; | ||
import { PolicyHandler } from './policies.guard'; | ||
|
||
export const CHECK_POLICIES_KEY = 'check_policy'; | ||
export const CheckPolicies = (...handlers: PolicyHandler[]) => | ||
SetMetadata(CHECK_POLICIES_KEY, handlers); |
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,42 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { AppAbility, CaslAbilityFactory } from './casl-ability.factory'; | ||
import { CHECK_POLICIES_KEY } from './check-policies.decorator'; | ||
|
||
interface IPolicyHandler { | ||
handle(ability: AppAbility): boolean; | ||
} | ||
|
||
type PolicyHandlerCallback = (ability: AppAbility) => boolean; | ||
|
||
export type PolicyHandler = IPolicyHandler | PolicyHandlerCallback; | ||
|
||
@Injectable() | ||
export class PoliciesGuard implements CanActivate { | ||
constructor( | ||
private reflector: Reflector, | ||
private caslAbilityFactory: CaslAbilityFactory, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const policyHandlers = | ||
this.reflector.get<PolicyHandler[]>( | ||
CHECK_POLICIES_KEY, | ||
context.getHandler(), | ||
) || []; | ||
|
||
const { user } = context.switchToHttp().getRequest(); | ||
const ability = this.caslAbilityFactory.createForUser(user); | ||
|
||
return policyHandlers.every((handler) => | ||
this.execPolicyHandler(handler, ability), | ||
); | ||
} | ||
|
||
private execPolicyHandler(handler: PolicyHandler, ability: AppAbility) { | ||
if (typeof handler === 'function') { | ||
return handler(ability); | ||
} | ||
return handler.handle(ability); | ||
} | ||
} |
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,18 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; | ||
|
||
@Entity({ | ||
name: 'roles', | ||
}) | ||
export class Role { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column({ length: 50 }) | ||
name: string; | ||
} | ||
|
||
// We treat roles as schema | ||
export enum RoleEnum { | ||
Admin = 'admin', | ||
User = 'user', | ||
} |
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