-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
9 changed files
with
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export enum ROLES { | ||
ADMIN = 'admin', | ||
PARTNER = 'partner', | ||
GENERAL_USER = 'general_user', | ||
} | ||
|
||
export const ROLES_HIERARCHY = { | ||
[ROLES.ADMIN]: [ROLES.PARTNER, ROLES.GENERAL_USER], | ||
[ROLES.PARTNER]: [ROLES.GENERAL_USER], | ||
[ROLES.GENERAL_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { ROLES } from '@api/modules/auth/authorisation/roles.enum'; | ||
|
||
export const ROLES_KEY = 'roles'; | ||
export const RequiredRoles = (...roles: ROLES[]) => | ||
SetMetadata(ROLES_KEY, roles); |
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,33 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { | ||
ROLES, | ||
ROLES_HIERARCHY, | ||
} from '@api/modules/auth/authorisation/roles.enum'; | ||
import { ROLES_KEY } from '@api/modules/auth/decorators/roles.decorator'; | ||
|
||
@Injectable() | ||
export class RolesGuard implements CanActivate { | ||
constructor(private reflector: Reflector) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const requiredRoles: ROLES[] = this.reflector.getAllAndOverride<ROLES[]>( | ||
ROLES_KEY, | ||
[context.getHandler(), context.getClass()], | ||
); | ||
if (!requiredRoles) { | ||
return true; | ||
} | ||
const { user } = context.switchToHttp().getRequest(); | ||
|
||
return this.hasRequiredRole(user.role, requiredRoles); | ||
} | ||
|
||
private hasRequiredRole(userRole: ROLES, requiredRoles: ROLES[]): boolean { | ||
return requiredRoles.some( | ||
(requiredRole) => | ||
userRole === requiredRole || | ||
ROLES_HIERARCHY[userRole]?.includes(requiredRole), | ||
); | ||
} | ||
} |
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,26 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { RequiredRoles } from '@api/modules/auth/decorators/roles.decorator'; | ||
import { ROLES } from '@api/modules/auth/authorisation/roles.enum'; | ||
|
||
@Controller('users') | ||
export class UsersController { | ||
// TODO: All of these endpoints are fake, only to test the role guard | ||
|
||
@RequiredRoles(ROLES.ADMIN) | ||
@Get('admin') | ||
async createUserAsAdmin() { | ||
return [ROLES.ADMIN]; | ||
} | ||
|
||
@RequiredRoles(ROLES.PARTNER) | ||
@Get('partner') | ||
async createUserAsPartner() { | ||
return [ROLES.PARTNER, ROLES.ADMIN]; | ||
} | ||
|
||
@RequiredRoles(ROLES.GENERAL_USER) | ||
@Get('user') | ||
async createUserAsUser() { | ||
return [ROLES.GENERAL_USER, ROLES.PARTNER, ROLES.ADMIN]; | ||
} | ||
} |
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,42 @@ | ||
import { TestManager } from '../utils/test-manager'; | ||
|
||
import { User } from '@shared/entities/users/user.entity'; | ||
import { ROLES } from '@api/modules/auth/authorisation/roles.enum'; | ||
|
||
describe('Authorization', () => { | ||
let testManager: TestManager; | ||
|
||
beforeAll(async () => { | ||
testManager = await TestManager.createTestManager(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await testManager.clearDatabase(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await testManager.close(); | ||
}); | ||
test('a user should have a default general user role when signing up', async () => { | ||
await testManager | ||
.request() | ||
.post('/authentication/signup') | ||
.send({ email: '[email protected]', password: '123456' }); | ||
|
||
const user = await testManager | ||
.getDataSource() | ||
.getRepository(User) | ||
.findOne({ where: { email: '[email protected]' } }); | ||
|
||
expect(user.role).toEqual(ROLES.GENERAL_USER); | ||
|
||
describe('ROLE TEST ENDPOINTS, REMOVE!', () => { | ||
test('when role required is general user, the general user and above roles should have access', async () => { | ||
const user = await testManager | ||
.mocks() | ||
.createUser({ role: ROLES.GENERAL_USER }); | ||
const { jwtToken } = await testManager.logUserIn(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