-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 유저 본인이 아니면 유저 정보를 가져올 수 없도록 권한 부여 #158
- Loading branch information
Showing
4 changed files
with
36 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 @@ | ||
import { BaseException } from '@src/common/exception/BaseException'; | ||
|
||
export class UserPermissionException extends BaseException { | ||
constructor(id: number) { | ||
super({ | ||
code: 2001, | ||
message: `id:${id} 유저에 대한 권한이 없습니다.`, | ||
status: 403, | ||
}); | ||
} | ||
} |
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,20 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'; | ||
import { UserService } from '@src/user/user.service'; | ||
import { UserPermissionException } from '@src/user/exception/UserPermissionException'; | ||
|
||
@Injectable() | ||
export class UserPermissionGuard implements CanActivate { | ||
constructor(private readonly userService: UserService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest(); | ||
const userId = Number(request.params.id); | ||
const requesterId = Number(request.user.userId); | ||
|
||
const user = await this.userService.getUserInfo(userId); | ||
if (user.id !== requesterId) { | ||
throw new UserPermissionException(userId); | ||
} | ||
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