-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add role-based permissions system with channel edit/delete #18
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
530e7af
feat: add role-based permissions system with channel edit/delete
BuckyMcYolo 84f86ff
feat:add guild moderation roles, bans, timeouts, rate limits, and member
BuckyMcYolo b3ba192
fix: changed checkPermission that used headers to assertPermission which
BuckyMcYolo 531d145
fix: fixed various permissions issues
BuckyMcYolo 5f62d98
fix: fixed various permission bugs
BuckyMcYolo 67cbdfa
fix: fixed bugs
BuckyMcYolo 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
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,130 @@ | ||
| import { auth } from "@repo/auth" | ||
| import { | ||
| canManageGuildAuthority, | ||
| type GuildAuthority, | ||
| guildAuthorityHasPermissions, | ||
| isGuildRole, | ||
| type PermissionRequest, | ||
| type StatementKey, | ||
| } from "@repo/auth/permissions" | ||
| import { HTTPException } from "hono/http-exception" | ||
| import * as HttpStatusCodes from "@/lib/helpers/http/status-codes" | ||
| import type { Guild, GuildMember } from "@/lib/types/app-types" | ||
|
|
||
| // ── Type-Safe Permission Types ────────────────────────────────────── | ||
|
|
||
| export type { StatementKey } | ||
|
|
||
| export type PermissionForStatement<T extends StatementKey> = NonNullable< | ||
| PermissionRequest[T] | ||
| >[number] | ||
|
|
||
| function toGuildAuthority( | ||
| member: Pick<GuildMember, "role" | "userId">, | ||
| guild: Pick<Guild, "ownerId"> | ||
| ): GuildAuthority { | ||
| if (!isGuildRole(member.role)) { | ||
| throw new HTTPException(HttpStatusCodes.FORBIDDEN, { | ||
| message: `Unknown guild role: ${member.role}`, | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| role: member.role, | ||
| isOwner: guild.ownerId === member.userId, | ||
| } | ||
| } | ||
|
|
||
| // ── Permission Check ────────────────────────────────────── | ||
|
|
||
| /** | ||
| * Checks if the current user has the specified permissions in their active guild. | ||
| * Uses better-auth's hasPermission API and throws HTTPException(403) when the | ||
| * requested permission is missing. | ||
| * | ||
| * @example | ||
| * await checkPermission(c.req.raw.headers, "channel", ["update"]) | ||
| * | ||
| * // If the permission is missing, checkPermission(...) throws | ||
| * // HTTPException(403) from the internal !result.success branch. | ||
| */ | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| export async function checkPermission< | ||
| TResource extends StatementKey, | ||
| TPermissions extends readonly PermissionForStatement<TResource>[], | ||
| >(headers: Headers, resource: TResource, permissions: TPermissions) { | ||
| const result = await auth.api.hasPermission({ | ||
| headers, | ||
| body: { | ||
| permissions: { | ||
| [resource]: [...permissions], | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| if (!result.success) { | ||
| throw new HTTPException(HttpStatusCodes.FORBIDDEN, { | ||
| message: `You do not have permission to ${permissions.join("/")} ${resource}`, | ||
| }) | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| export function assertGuildPermission( | ||
| member: Pick<GuildMember, "role" | "userId">, | ||
| guild: Pick<Guild, "ownerId">, | ||
| requestedPermissions: PermissionRequest | ||
| ) { | ||
| const authority = toGuildAuthority(member, guild) | ||
|
|
||
| if (!guildAuthorityHasPermissions(authority, requestedPermissions)) { | ||
| throw new HTTPException(HttpStatusCodes.FORBIDDEN, { | ||
| message: "You do not have permission to perform this action", | ||
| }) | ||
| } | ||
|
|
||
| return authority | ||
| } | ||
|
|
||
| export function assertCanManageGuildMember( | ||
| actor: Pick<GuildMember, "role" | "userId">, | ||
| target: Pick<GuildMember, "role" | "userId">, | ||
| guild: Pick<Guild, "ownerId"> | ||
| ) { | ||
| if (actor.userId === target.userId) { | ||
| throw new HTTPException(HttpStatusCodes.FORBIDDEN, { | ||
| message: "You cannot moderate yourself", | ||
| }) | ||
| } | ||
|
|
||
| const actorAuthority = toGuildAuthority(actor, guild) | ||
| const targetAuthority = toGuildAuthority(target, guild) | ||
|
|
||
| if (!canManageGuildAuthority(actorAuthority, targetAuthority)) { | ||
| throw new HTTPException(HttpStatusCodes.FORBIDDEN, { | ||
| message: "You cannot moderate this member", | ||
| }) | ||
| } | ||
|
|
||
| return { | ||
| actorAuthority, | ||
| targetAuthority, | ||
| } | ||
| } | ||
|
|
||
| export function isCommunicationDisabled( | ||
| member: Pick<GuildMember, "communicationDisabledUntil"> | ||
| ) { | ||
| if (!member.communicationDisabledUntil) return false | ||
| return member.communicationDisabledUntil.getTime() > Date.now() | ||
| } | ||
|
|
||
| export function assertMemberCanCommunicate( | ||
| member: Pick<GuildMember, "communicationDisabledUntil"> | ||
| ) { | ||
| if (!isCommunicationDisabled(member)) return | ||
|
|
||
| throw new HTTPException(HttpStatusCodes.FORBIDDEN, { | ||
| message: "You are temporarily timed out and cannot send messages", | ||
| }) | ||
| } | ||
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
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.