-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
43e97b7
commit 89555a8
Showing
8 changed files
with
83 additions
and
124 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
apps/server/src/modules/board/controller/dto/submission-item/submission-item.response.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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { AnyBoardDo, EntityId, SubmissionItem, UserRoleEnum } from '@shared/domain'; | ||
import { ForbiddenException, forwardRef, Inject } from '@nestjs/common'; | ||
import { Action, AuthorizationService } from '../../authorization'; | ||
import { BoardDoAuthorizableService } from '../service'; | ||
|
||
export abstract class BaseUc { | ||
constructor( | ||
@Inject(forwardRef(() => AuthorizationService)) | ||
protected readonly authorizationService: AuthorizationService, | ||
protected readonly boardDoAuthorizableService: BoardDoAuthorizableService | ||
) {} | ||
|
||
protected async checkPermission( | ||
userId: EntityId, | ||
boardDo: AnyBoardDo, | ||
action: Action, | ||
requiredUserRole?: UserRoleEnum | ||
): Promise<void> { | ||
const user = await this.authorizationService.getUserWithPermissions(userId); | ||
const boardDoAuthorizable = await this.boardDoAuthorizableService.getBoardAuthorizable(boardDo); | ||
if (requiredUserRole) { | ||
boardDoAuthorizable.requiredUserRole = requiredUserRole; | ||
} | ||
const context = { action, requiredPermissions: [] }; | ||
|
||
return this.authorizationService.checkPermission(user, boardDoAuthorizable, context); | ||
} | ||
|
||
protected async isAuthorizedStudent(userId: EntityId, boardDo: AnyBoardDo): Promise<boolean> { | ||
const boardDoAuthorizable = await this.boardDoAuthorizableService.getBoardAuthorizable(boardDo); | ||
const userRoleEnum = boardDoAuthorizable.users.find((u) => u.userId === userId)?.userRoleEnum; | ||
|
||
if (!userRoleEnum) { | ||
throw new ForbiddenException('User not part of this board'); | ||
} | ||
|
||
// TODO do this with permission instead of role and using authorizable rules | ||
if (userRoleEnum === UserRoleEnum.STUDENT) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
protected async checkSubmissionItemEditPermission(userId: EntityId, submissionItem: SubmissionItem) { | ||
if (submissionItem.userId !== userId) { | ||
throw new ForbiddenException(); | ||
} | ||
await this.checkPermission(userId, submissionItem, Action.read, UserRoleEnum.STUDENT); | ||
} | ||
} |
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
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