-
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.
Merge pull request #38 from Honeybrain/add-roles
Add roles
- Loading branch information
Showing
18 changed files
with
187 additions
and
68 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,15 @@ | ||
import { applyDecorators, UseGuards } from '@nestjs/common'; | ||
import { RoleEnum } from '../../user/_utils/enums/role.enum'; | ||
import { ApiBearerAuth, ApiForbiddenResponse } from '@nestjs/swagger'; | ||
import { GrpcAuthGuard } from '../../user/_utils/jwt/grpc-auth.guard'; | ||
import { Roles } from './roles.decorator'; | ||
import { RolesGuard } from '../../user/_utils/jwt/roles.guard'; | ||
|
||
export function Protect(...roles: RoleEnum[]) { | ||
return applyDecorators( | ||
ApiBearerAuth(), | ||
Roles(...roles), | ||
UseGuards(GrpcAuthGuard, RolesGuard), | ||
ApiForbiddenResponse({ description: 'Unauthorized' }), | ||
); | ||
} |
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 { SetMetadata } from '@nestjs/common'; | ||
import { RoleEnum } from '../../user/_utils/enums/role.enum'; | ||
|
||
export const ROLES_KEY = 'role'; | ||
export const Roles = (...roles: RoleEnum[]) => 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
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,9 +1,10 @@ | ||
import { IsBoolean, IsString } from 'class-validator'; | ||
import { IsEnum, IsString } from 'class-validator'; | ||
import { RoleEnum } from '../../enums/role.enum'; | ||
|
||
export class ChangeRightsRequestDto { | ||
@IsString() | ||
email: string; | ||
|
||
@IsBoolean() | ||
admin: boolean; | ||
@IsEnum(RoleEnum, { each: true }) | ||
roles: RoleEnum[]; | ||
} |
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,9 +1,10 @@ | ||
import { IsBoolean, IsEmail } from 'class-validator'; | ||
import { IsEmail, IsEnum } from 'class-validator'; | ||
import { RoleEnum } from '../../enums/role.enum'; | ||
|
||
export class InviteUserRequestDto { | ||
@IsEmail() | ||
email: string; | ||
|
||
@IsBoolean() | ||
admin: boolean; | ||
@IsEnum(RoleEnum, { each: true }) | ||
roles: RoleEnum[]; | ||
} |
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 { RoleEnum } from '../../enums/role.enum'; | ||
|
||
export class GetUserDto { | ||
id: string; | ||
email: string; | ||
roles: RoleEnum[]; | ||
activated: boolean; | ||
lan: string; | ||
} |
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,7 +1,5 @@ | ||
import { IsArray, IsString } from 'class-validator'; | ||
import { GetUserDto } from './get-user.dto'; | ||
|
||
export class GetUsersListDto { | ||
@IsArray() | ||
@IsString({ each: true }) | ||
users: string[]; | ||
users: GetUserDto[]; | ||
} |
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,9 +1,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { GetUserDto } from './get-user.dto'; | ||
|
||
export class UserResponseDto { | ||
@ApiProperty() | ||
message: string; | ||
|
||
@ApiProperty() | ||
token: string; | ||
|
||
@ApiProperty() | ||
user: GetUserDto; | ||
} |
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 @@ | ||
export enum RoleEnum { | ||
ADMIN, | ||
CAN_INVITE, | ||
CAN_MANAGE_IP, | ||
CAN_READ_IP, | ||
CAN_READ_LOGS, | ||
CAN_MANAGE_CONFIGURATION, | ||
CAN_READ_SERVICES, | ||
} |
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,24 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { Observable } from 'rxjs'; | ||
import { ROLES_KEY } from '../../../_utils/decorators/roles.decorator'; | ||
import { RoleEnum } from '../enums/role.enum'; | ||
import { User } from '../../user.schema'; | ||
|
||
@Injectable() | ||
export class RolesGuard implements CanActivate { | ||
constructor(private reflector: Reflector) {} | ||
|
||
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> { | ||
const requiredRoles = this.reflector.getAllAndOverride<RoleEnum[]>(ROLES_KEY, [ | ||
context.getHandler(), | ||
context.getClass(), | ||
]); | ||
const { user }: { user: User } = context.switchToRpc().getContext(); | ||
|
||
if (!requiredRoles.length) return true; | ||
if (user.roles.includes(RoleEnum.ADMIN)) return true; | ||
|
||
return requiredRoles.some((role) => user.roles.includes(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
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,17 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { UserDocument } from './user.schema'; | ||
import { GetUsersListDto } from './_utils/dto/response/get-users-list.dto'; | ||
import { GetUserDto } from './_utils/dto/response/get-user.dto'; | ||
|
||
@Injectable() | ||
export class UserMapper { | ||
toGetUser = (user: UserDocument): GetUserDto => ({ | ||
id: user._id.toString(), | ||
email: user.email, | ||
roles: user.roles, | ||
activated: user.activated, | ||
lan: user.lan, | ||
}); | ||
|
||
toGetUsers = (users: UserDocument[]): GetUsersListDto => ({ users: users.map((user) => this.toGetUser(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
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
Oops, something went wrong.