-
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
Ahmet AYDIN
committed
Feb 8, 2020
1 parent
461213e
commit 77af396
Showing
6 changed files
with
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export const Roles = (...roles: string[]) => SetMetadata('roles', 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,62 @@ | ||
import { | ||
Injectable, | ||
CanActivate, | ||
ExecutionContext, | ||
HttpException, | ||
HttpStatus, | ||
Inject, | ||
Module, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { RoleModel } from 'tools/models/role.model'; | ||
import { GroupModel } from 'tools/models/group.model'; | ||
import { GroupService } from 'src/group/group.service'; | ||
|
||
@Injectable() | ||
export class AuthGuard implements CanActivate { | ||
constructor( | ||
private readonly reflector: Reflector, | ||
@Inject('GroupService') private readonly groupService: GroupService, | ||
) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const allowedRoles = this.reflector.get<string[]>( | ||
'roles', | ||
context.getHandler(), | ||
); | ||
if (!allowedRoles) { | ||
return true; | ||
} | ||
|
||
const request = context.switchToHttp().getRequest(); | ||
const user = request.user.user; | ||
const allowed = this.isAllowed(allowedRoles, user.roles, user.groups); | ||
|
||
if (!allowed) { | ||
throw new HttpException('Forbidden Method!', HttpStatus.FORBIDDEN); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
async isAllowed( | ||
allowedRoles, | ||
userRoles: RoleModel[], | ||
userGroups: GroupModel[], | ||
) { | ||
const allUsersRoles = []; | ||
userRoles.map(data => { | ||
allUsersRoles.push(data.name); | ||
}); | ||
await Promise.all( | ||
userGroups.map(async data => { | ||
const groupRoles = await this.groupService.findOne(data._id); | ||
groupRoles[0].roles.map(resp => { | ||
allUsersRoles.push(resp['name']); | ||
}); | ||
}), | ||
); | ||
const hasRole = allUsersRoles.some(role => allowedRoles.includes(role)); | ||
return hasRole; | ||
} | ||
} |
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
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