Skip to content

Commit dd80594

Browse files
functional role guard
1 parent 0909adc commit dd80594

File tree

5 files changed

+17
-10
lines changed

5 files changed

+17
-10
lines changed

src/controllers/projects/projects.controller.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@ import { Role } from '../../entities/user.entity'
1717
export class ProjectsController {
1818
constructor(private projectService: ProjectService) { }
1919
@Public()
20-
@Roles(Role.DEFAULT)
2120
@Get(":projectID")
2221
@HttpCode(HttpStatus.FOUND)
2322
async get(@Param("projectID", ParseIntPipe) projectID: number): Promise<object> {
2423
return { data: await this.projectService.getProject(projectID) }//AppService.getProject(projectID);
2524
}
2625
@Public()
27-
@Roles(Role.DEFAULT)
2826
@Get()
2927
@HttpCode(HttpStatus.FOUND)
3028
async list(@Query() params: any): Promise<object> {
@@ -48,6 +46,7 @@ export class ProjectsController {
4846
async update(@Param("projectID", ParseIntPipe) projectID: number, @Body(new JoiValidationPipe(projectSchema)) payload: any): Promise<object> {
4947
return { msg: "updated", data: await this.projectService.updateProject(projectID, payload) }
5048
}
49+
@Roles(Role.DEFAULT)
5150
@Delete(":projectID")
5251
async delete(@Param("projectID", ParseIntPipe) projectID: number): Promise<object> {
5352
let deleted = await this.projectService.deleteProject(projectID)

src/modules/auth/controllers/auth/auth.controller.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export class AuthController {
88
@UseGuards(AuthGuard('local'))
99
@Post('login')
1010
login(@Req() req: Request) {
11-
1211
return this.authService.generateJWT(req.user);
1312
}
1413
}

src/modules/auth/guards/roles.guard.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { CanActivate, ExecutionContext, Injectable, ForbiddenException} from '@n
22
import { Observable } from 'rxjs';
33
import { Reflector } from '@nestjs/core';
44
import { ROLE_KEY } from '../decorators/roles.decorator'
5+
import { IS_PUBLIC_KEY } from '../decorators/public.decorator'
56
import { Role } from '../../../entities/user.entity'
67
import { UsersService } from '../../users/services/users/users.service'
78
@Injectable()
@@ -11,11 +12,18 @@ export class RolesGuard implements CanActivate {
1112

1213
async canActivate(
1314
context: ExecutionContext,
14-
): boolean | Promise<boolean> | Observable<boolean> {
15+
): Promise<boolean> {
1516
const roles = this.reflector.get(ROLE_KEY, context.getHandler())
1617
const request = context.switchToHttp().getRequest();
17-
const userId = request.id
18-
const user = await this.userService.get(userId)
18+
const reqUser = request.user
19+
if (!reqUser){
20+
const isPublic = this.reflector.get(IS_PUBLIC_KEY, context.getHandler())
21+
if (isPublic) return true
22+
else return false
23+
}
24+
if (!roles) return false
25+
26+
const user = await this.userService.get(reqUser.sub)
1927
if(user.role == Role.ADMIN) return true
2028
if(roles.some(item => item === user.role)) return true
2129
throw new ForbiddenException('not enough privilages')

src/modules/auth/services/auth/auth.service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ export class AuthService {
2222
return null
2323
}
2424
generateJWT(user) {
25-
const payload = { id: user.id };
25+
const payload = { sub: user.id };
26+
const {id,password,...resUsser} = user
2627
return {
2728
access_token: this.jwtService.sign(payload),
28-
user,
29+
resUsser,
2930
};
3031
}
3132
}

src/modules/users/services/users/users.service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class UsersService {
1818
let element = this.userRepo.create({ username, email, password: pswHash })
1919
await this.userRepo.save(element)
2020
}
21-
async get(id: number): Promise<object> {
21+
async get(id: number) {
2222
const user = await this.userRepo.findOne(
2323
{
2424
where: { id },
@@ -34,7 +34,7 @@ export class UsersService {
3434
const user = await this.userRepo.findOne(
3535
{
3636
where: { email },
37-
select: ["username", "email", "role", "password", "creationDate"]
37+
select: ["id","username", "email", "role", "password", "creationDate"]
3838
}
3939
);
4040
if (!user) {

0 commit comments

Comments
 (0)