-
Notifications
You must be signed in to change notification settings - Fork 1
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 #182 from codex-team/feat/note-team-role-access-po…
…licy feat: note team role access policy
- Loading branch information
Showing
19 changed files
with
231 additions
and
68 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
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
53 changes: 53 additions & 0 deletions
53
src/presentation/http/middlewares/noteSettings/useMemberRoleResolver.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,53 @@ | ||
import type { preHandlerHookHandler } from 'fastify'; | ||
import { getLogger } from '@infrastructure/logging/index.js'; | ||
import type NoteSettingsService from '@domain/service/noteSettings.js'; | ||
import type { MemberRole } from '@domain/entities/team'; | ||
import { isEmpty } from '@infrastructure/utils/empty.js'; | ||
|
||
/** | ||
* Add middleware to resolve Member's role in a team by user id and note id and add it to request | ||
* | ||
* @param noteSettingsService - note settings domain service | ||
*/ | ||
export default function useMemberRoleResolver(noteSettingsService: NoteSettingsService): { | ||
/** | ||
* Resolve Member's role in a team by user id and note id and add it to request | ||
* | ||
* Use this middleware as "preHandler" hook with a particular route | ||
*/ | ||
memberRoleResolver: preHandlerHookHandler; | ||
} { | ||
/** | ||
* Get logger instance | ||
*/ | ||
const logger = getLogger('appServer'); | ||
|
||
return { | ||
memberRoleResolver: async function memberRoleResolver(request, reply) { | ||
/** If MemberRole equals null, it means that user is not in the team or is not authenticated */ | ||
let memberRole: MemberRole | undefined; | ||
|
||
try { | ||
if (isEmpty(request.note)) { | ||
throw new Error('Note was not resolved'); | ||
} | ||
|
||
/** If user is not authenticated, we can't resolve his role */ | ||
if (isEmpty(request.userId)) { | ||
memberRole = undefined; | ||
} else { | ||
memberRole = await noteSettingsService.getUserRoleByUserIdAndNoteId(request.userId, request.note.id); | ||
} | ||
|
||
if (memberRole !== undefined) { | ||
request.memberRole = memberRole; | ||
} | ||
} catch (error) { | ||
logger.error('Can not resolve Member role by note [id = ${request.note.id}] and user [id = ${request.userId}]'); | ||
logger.error(error); | ||
|
||
await reply.notAcceptable('Team member not found'); | ||
} | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import authRequired from './authRequired.js'; | ||
import notePublicOrUserInTeam from './notePublicOrUserInTeam.js'; | ||
import userInTeam from './userInTeam.js'; | ||
import userIsCreator from './userIsCreator.js'; | ||
import userCanEdit from './userCanEdit.js'; | ||
|
||
export default { | ||
authRequired, | ||
notePublicOrUserInTeam, | ||
userInTeam, | ||
userIsCreator, | ||
userCanEdit, | ||
}; |
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,40 @@ | ||
import { isEmpty } from '@infrastructure/utils/empty.js'; | ||
import { MemberRole } from '@domain/entities/team'; | ||
import type { PolicyContext } from '@presentation/http/types/PolicyContext.js'; | ||
|
||
/** | ||
* Policy to check whether a user has permission to edit the note | ||
* | ||
* @param context - Context object, containing Fatify request, Fastify reply and domain services | ||
*/ | ||
export default async function userCanEdit(context: PolicyContext): Promise<void> { | ||
const { request, reply, domainServices } = context; | ||
|
||
const { userId } = request; | ||
|
||
/** | ||
* If user is not authorized, we can't check his permissions | ||
*/ | ||
if (isEmpty(userId)) { | ||
return await reply.unauthorized(); | ||
}; | ||
|
||
/** | ||
* If note is not resolved, we can't check permissions | ||
*/ | ||
if (isEmpty(request.note)) { | ||
return await reply.notAcceptable('Note not found'); | ||
}; | ||
|
||
const { creatorId } = request.note; | ||
const memberRole = await domainServices.noteSettingsService.getUserRoleByUserIdAndNoteId(request.userId!, request.note.id); | ||
|
||
/** | ||
* If user is not a creator of the note and | ||
* user has a Read Role or is not in team at all, | ||
* he doesn't have permission to edit the note | ||
*/ | ||
if (creatorId !== userId && memberRole !== MemberRole.Write) { | ||
return await reply.forbidden(); | ||
} | ||
} |
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
Oops, something went wrong.