Skip to content

Commit

Permalink
feat: 코스 사용자 확인 권한에서 admin은 모두 통과할 수 있도록 설정 #163
Browse files Browse the repository at this point in the history
  • Loading branch information
koomchang committed Nov 23, 2024
1 parent 8b5fb07 commit 1c04daa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 2 additions & 0 deletions backend/src/admin/admin.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ import { AuthModule } from '@src/auth/auth.module';
},
]),
],
providers: [AdminGuard],
exports: [AdminGuard],
})
export class AdminModule {}
13 changes: 8 additions & 5 deletions backend/src/admin/guard/AdminGuard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ export class AdminGuard extends JwtAuthGuard {
if (!isAuthenticated) {
return false;
}

const request = context.switchToHttp().getRequest();
const user = request.user;

if (!user || user.role !== UserRole.ADMIN) {
if (!this.isAdmin(context)) {
throw new AuthorizationException('관리자 권한이 없습니다.');
}

return true;
}

isAdmin(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
const user = request.user;

return user?.role === UserRole.ADMIN;
}
}
18 changes: 16 additions & 2 deletions backend/src/course/course.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ import { CourseRepository } from './course.repository';
import { PlaceModule } from '../place/place.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CoursePlace } from '@src/course/entity/course-place.entity';
import { AdminGuard } from '@src/admin/guard/AdminGuard';
import { CoursePermissionGuard } from '@src/course/guards/CoursePermissionGuard';
import { AdminModule } from '@src/admin/admin.module';

@Module({
imports: [UserModule, PlaceModule, TypeOrmModule.forFeature([CoursePlace])],
imports: [
UserModule,
PlaceModule,
AdminModule,
TypeOrmModule.forFeature([CoursePlace]),
],
controllers: [CourseController],
providers: [CourseService, CourseRepository],
providers: [
CourseService,
CourseRepository,
CoursePermissionGuard,
AdminGuard,
],
exports: [CoursePermissionGuard],
})
export class CourseModule {}
12 changes: 10 additions & 2 deletions backend/src/course/guards/CoursePermissionGuard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
import { CourseService } from '../course.service';
import { CoursePermissionException } from '../exception/CoursePermissionException';
import { AdminGuard } from '@src/admin/guard/AdminGuard';

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

async canActivate(context: ExecutionContext): Promise<boolean> {
const isAdmin = this.adminGuard.isAdmin(context);
if (isAdmin) {
return true;
}

const request = context.switchToHttp().getRequest();
const courseId = Number(request.params.id);
const userId = Number(request.user.userId);

const courseOwnerId = await this.courseService.getCourseOwnerId(courseId);
if (courseOwnerId !== userId) {
throw new CoursePermissionException(courseId);
Expand Down

0 comments on commit 1c04daa

Please sign in to comment.