-
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.
Merge pull request #49 from SAIG-KMITL/feat/chapter-module-service
Implement comprehensive features for course, exam, chat, and user management
- Loading branch information
Showing
2 changed files
with
85 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,76 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Injectable, | ||
UnauthorizedException, | ||
NotFoundException, | ||
ForbiddenException, | ||
} from '@nestjs/common'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
import { JwtPayloadDto } from 'src/auth/dtos/jwt-payload.dto'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { GLOBAL_CONFIG } from 'src/shared/constants/global-config.constant'; | ||
import { AuthenticatedRequest } from 'src/auth/interfaces/authenticated-request.interface'; | ||
import { EnrollmentService } from 'src/enrollment/enrollment.service'; | ||
import { Role } from 'src/shared/enums'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { Chapter } from '../chapter.entity'; | ||
import { Repository } from 'typeorm'; | ||
|
||
@Injectable() | ||
export class VideoGuard implements CanActivate { | ||
constructor( | ||
@InjectRepository(Chapter) | ||
private readonly chapterRepository: Repository<Chapter>, | ||
private readonly jwtService: JwtService, | ||
private readonly configService: ConfigService, | ||
private readonly enrollmentService: EnrollmentService, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request: AuthenticatedRequest = context.switchToHttp().getRequest(); | ||
const chapterId = request.params.id; | ||
const chapter = await this.chapterRepository.findOne({ | ||
where: { id: chapterId }, | ||
relations: { | ||
module: { | ||
course: { | ||
teacher: true, | ||
}, | ||
}, | ||
} | ||
}); | ||
if (!chapter) throw new NotFoundException('Chapter not found'); | ||
if (chapter.isPreview) return true; | ||
const token = request.query.token as string; | ||
if (!token) throw new UnauthorizedException('Unauthorized access'); | ||
try { | ||
request.user = await this.jwtService.verifyAsync<JwtPayloadDto>(token, { | ||
secret: this.configService.getOrThrow<string>( | ||
GLOBAL_CONFIG.JWT_ACCESS_SECRET, | ||
), | ||
}); | ||
} catch (error) { | ||
throw new UnauthorizedException('Unauthorized access'); | ||
} | ||
const userId = request.user.id; | ||
const courseId = chapter.module.course.id; | ||
switch (request.user.role) { | ||
case Role.TEACHER: | ||
if (chapter.module.course.teacher.id !== userId) | ||
throw new ForbiddenException('Insufficient permissions'); | ||
break; | ||
case Role.STUDENT: | ||
const enrollment = await this.enrollmentService.findOne({ | ||
course: { id: courseId }, | ||
user: { id: userId }, | ||
}); | ||
if (!enrollment) | ||
throw new ForbiddenException('Insufficient permissions'); | ||
break; | ||
default: | ||
throw new ForbiddenException('Insufficient permissions'); | ||
} | ||
return true; | ||
} | ||
} |