Skip to content

Commit

Permalink
feat: 코스 수정할 때 권한 확인 Guard 구현 #83
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 9, 2024
1 parent 8ccdd6f commit 2171778
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/src/course/guards/CoursePermissionGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { CourseService } from '../course.service';
import { CoursePermissionException } from '../exception/CoursePermissionException';

@Injectable()
export class CoursePermissionGuard implements CanActivate {
constructor(private readonly courseService: CourseService) {}

async canActivate(context: ExecutionContext): Promise<boolean> {
const request = context.switchToHttp().getRequest();
const courseId = Number(request.params.id);
const userId = Number(request.user.userId);

const course = await this.courseService.getCourseById(courseId);
if (course.user.id !== userId) {
throw new CoursePermissionException(courseId);
}
return course.user.id === userId;
}
}

0 comments on commit 2171778

Please sign in to comment.