-
Notifications
You must be signed in to change notification settings - Fork 41
feat(permissions): custom roles with scopes, API guards, and post review workflows #630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mezotv
wants to merge
12
commits into
main
Choose a base branch
from
emdash/ready-chairs-prove-0oicf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ac546ec
feat(permissions): custom roles with scopes, API guards, and post rev…
mezotv 4d966d1
fix(permissions): address react-doctor findings
mezotv d877874
fix(reviews): lock approval requests during review and gate withdrawal
mezotv aa4af34
fix(reviews): let reviewers withdraw posts and sync API status changes
mezotv c2165e3
fix(api): cancel pending approval requests atomically with status change
mezotv 89c4803
fix(roles): prevent privilege escalation via role assignment and hand…
mezotv 1a26e10
fix(content): cancel pending reviews atomically with dashboard status…
mezotv 314324d
fix(roles): block scope escalation through role create and update
mezotv 75df4d7
fix(content): guard status transitions against concurrent changes
mezotv 53e2917
refactor(access-groups): rename custom roles to access groups
mezotv 596bb52
fix(permissions): restrict legacy scope fallback to pre-adoption work…
mezotv e5c8d4f
fix(api): guard REST status transitions against concurrent changes
mezotv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,129 @@ | ||
| import type { createDb } from "@notra/db/drizzle"; | ||
| import { | ||
| accessGroupMembers, | ||
| approvalWorkflows, | ||
| members, | ||
| } from "@notra/db/schema"; | ||
| import { and, asc, eq } from "drizzle-orm"; | ||
| import { Data, Effect } from "effect"; | ||
|
|
||
| type Database = ReturnType<typeof createDb>; | ||
|
|
||
| class PublishRequirementError extends Data.TaggedError( | ||
| "PublishRequirementError" | ||
| )<{ | ||
| readonly message: string; | ||
| }> {} | ||
|
|
||
| class PublishCheckError extends Data.TaggedError("PublishCheckError")<{ | ||
| readonly message: string; | ||
| readonly cause: unknown; | ||
| }> {} | ||
|
|
||
| const tryDb = <T>(run: () => Promise<T>) => | ||
| Effect.tryPromise({ | ||
| try: run, | ||
| catch: (cause) => | ||
| new PublishCheckError({ | ||
| message: "Failed to check publishing requirements", | ||
| cause, | ||
| }), | ||
| }); | ||
|
|
||
| const getAuthorAccessGroupIds = Effect.fn("getAuthorAccessGroupIds")( | ||
| function* ({ | ||
| db, | ||
| organizationId, | ||
| authorUserId, | ||
| }: { | ||
| db: Database; | ||
| organizationId: string; | ||
| authorUserId: string | null; | ||
| }) { | ||
| if (!authorUserId) { | ||
| return [] as string[]; | ||
| } | ||
|
|
||
| const membership = yield* tryDb(() => | ||
| db.query.members.findFirst({ | ||
| where: and( | ||
| eq(members.userId, authorUserId), | ||
| eq(members.organizationId, organizationId) | ||
| ), | ||
| columns: { | ||
| id: true, | ||
| }, | ||
| }) | ||
| ); | ||
|
|
||
| if (!membership) { | ||
| return [] as string[]; | ||
| } | ||
|
|
||
| const memberships = yield* tryDb(() => | ||
| db.query.accessGroupMembers.findMany({ | ||
| where: eq(accessGroupMembers.memberId, membership.id), | ||
| columns: { | ||
| accessGroupId: true, | ||
| }, | ||
| }) | ||
| ); | ||
|
|
||
| return memberships.map((groupMembership) => groupMembership.accessGroupId); | ||
| } | ||
| ); | ||
|
|
||
| export const assertApiPublishAllowed = Effect.fn("assertApiPublishAllowed")( | ||
| function* ({ | ||
| db, | ||
| organizationId, | ||
| post, | ||
| }: { | ||
| db: Database; | ||
| organizationId: string; | ||
| post: { | ||
| status: "draft" | "in_review" | "approved" | "published"; | ||
| createdBy: string | null; | ||
| }; | ||
| }) { | ||
| if (post.status === "approved" || post.status === "published") { | ||
| return; | ||
| } | ||
|
|
||
| const authorAccessGroupIds = yield* getAuthorAccessGroupIds({ | ||
| db, | ||
| organizationId, | ||
| authorUserId: post.createdBy, | ||
| }); | ||
|
|
||
| const workflows = yield* tryDb(() => | ||
| db.query.approvalWorkflows.findMany({ | ||
| where: eq(approvalWorkflows.organizationId, organizationId), | ||
| with: { | ||
| steps: { | ||
| columns: { | ||
| id: true, | ||
| }, | ||
| }, | ||
| }, | ||
| orderBy: [asc(approvalWorkflows.createdAt)], | ||
| }) | ||
| ); | ||
|
|
||
| const withSteps = workflows.filter((workflow) => workflow.steps.length > 0); | ||
| const applicable = | ||
| withSteps.find( | ||
| (workflow) => | ||
| workflow.appliesToAccessGroupId && | ||
| authorAccessGroupIds.includes(workflow.appliesToAccessGroupId) | ||
| ) ?? withSteps.find((workflow) => workflow.isDefault); | ||
|
|
||
| if (applicable) { | ||
| return yield* Effect.fail( | ||
| new PublishRequirementError({ | ||
| message: `This post must be approved through the "${applicable.name}" workflow before it can be published`, | ||
| }) | ||
| ); | ||
| } | ||
|
greptile-apps[bot] marked this conversation as resolved.
|
||
| } | ||
| ); | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.