-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/Test: Add ContributerProblem Guard and remove unused test codes
- Loading branch information
Showing
6 changed files
with
68 additions
and
111 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
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
43 changes: 43 additions & 0 deletions
43
src/judge/contributer/decorator/contributer-problem.guard.ts
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,43 @@ | ||
import { | ||
BadRequestException, | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
Inject, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
import { PrismaService } from 'app/prisma/prisma.service'; | ||
import { Request } from 'express'; | ||
|
||
/** | ||
* Contributer problem Id Checker | ||
* | ||
* Assuem that user id already check from controller level Auth Guard | ||
* | ||
* Return 403 Forbidden Error if problem with contributer ID not found | ||
*/ | ||
|
||
@Injectable() | ||
export class ContributerProblemGuard implements CanActivate { | ||
constructor(@Inject(PrismaService) private prisma: PrismaService) {} | ||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const request = context.switchToHttp().getRequest<Request>(); | ||
|
||
// Get User info | ||
const userId = request.user['id']; | ||
// Get Problem Id | ||
const problemId = request.params['pid']; | ||
|
||
try { | ||
await this.prisma.problem.findUniqueOrThrow({ | ||
where: { | ||
id: parseInt(problemId), | ||
contributerId: userId, | ||
}, | ||
}); | ||
return true; | ||
} catch (err) { | ||
throw new ForbiddenException('FORBIDDEN_REQUEST'); | ||
} | ||
} | ||
} |
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