-
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
1 parent
184fdf6
commit dae0c4d
Showing
17 changed files
with
385 additions
and
58 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
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,23 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
|
||
import { Roles } from '~/app/auth/auth.decorator'; | ||
import { Session } from '~/schemas/session.schema'; | ||
|
||
@Injectable() | ||
export class RoleGuard implements CanActivate { | ||
constructor(private readonly reflector: Reflector) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const roles = this.reflector.get(Roles, context.getHandler()); | ||
|
||
if (roles) { | ||
const req = context.switchToHttp().getRequest(); | ||
const session = req.user as Session; | ||
|
||
return roles.includes(session.user.role); | ||
} | ||
|
||
return true; | ||
} | ||
} |
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,96 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
HttpStatus, | ||
Param, | ||
Post, | ||
Put, | ||
Query, | ||
Res, | ||
UseGuards, | ||
} from '@nestjs/common'; | ||
import { ApiResponse, ApiTags } from '@nestjs/swagger'; | ||
import { Response } from 'express'; | ||
|
||
import { | ||
CreateAuthorDto, | ||
SearchAuthorDto, | ||
UpdateAuthorDto, | ||
} from '~/app/author/author.dto'; | ||
import { AuthorService } from '~/app/author/author.service'; | ||
import { JwtAuthGuard } from '~/app/auth/auth.guard'; | ||
import { RoleGuard } from '~/app/auth/role.guard'; | ||
import { Roles } from '~/app/auth/auth.decorator'; | ||
import { Role } from '~/types/role.enum'; | ||
|
||
@ApiTags('Author') | ||
@Controller('author') | ||
export class AuthorController { | ||
constructor(private readonly authorService: AuthorService) {} | ||
|
||
@Post('create') | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Create new author', | ||
}) | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles([Role.ADMIN]) | ||
async create(@Body() body: CreateAuthorDto, @Res() res: Response) { | ||
const data = await this.authorService.create(body); | ||
|
||
return res.status(HttpStatus.CREATED).json(data); | ||
} | ||
|
||
@Get(':id') | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Get a author', | ||
}) | ||
async getById(@Param('id') id: string, @Res() res: Response) { | ||
const data = await this.authorService.getById(id); | ||
|
||
return res.status(HttpStatus.CREATED).json(data); | ||
} | ||
|
||
@Delete('delete/:id') | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Delete a author', | ||
}) | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles([Role.ADMIN]) | ||
async delete(@Param('id') id: string, @Res() res: Response) { | ||
await this.authorService.delete(id); | ||
|
||
return res.status(HttpStatus.OK).send(); | ||
} | ||
|
||
@Put('update/:id') | ||
@ApiResponse({ | ||
status: 201, | ||
description: 'Update a author', | ||
}) | ||
@UseGuards(JwtAuthGuard, RoleGuard) | ||
@Roles([Role.ADMIN]) | ||
async update( | ||
@Body() body: UpdateAuthorDto, | ||
@Param('id') id: string, | ||
@Res() res: Response, | ||
) { | ||
const data = await this.authorService.update(id, body); | ||
|
||
return res.status(HttpStatus.OK).json(data); | ||
} | ||
|
||
@Get('search') | ||
@ApiResponse({ | ||
status: 200, | ||
description: 'Author search', | ||
}) | ||
async search(@Query() query: SearchAuthorDto, @Res() res: Response) { | ||
const data = await this.authorService.search(query); | ||
return res.status(HttpStatus.OK).json(data); | ||
} | ||
} |
Oops, something went wrong.