Skip to content

Commit

Permalink
fix: revert move checkPermission due to circular dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
MaddyUnderStars committed Dec 25, 2024
1 parent 26a619d commit 22f2880
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 51 deletions.
5 changes: 3 additions & 2 deletions src/entity/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
OneToMany,
} from "typeorm";
import { z } from "zod";
import { HttpError, type PERMISSION } from "../util";
import { checkPermission } from "../util/permission";
import { checkPermission } from "../util/checkPermission";
import { HttpError } from "../util/httperror";
import type { PERMISSION } from "../util/permission";
import { Actor } from "./actor";
import { PublicRole, type Role } from "./role";
import { type GuildTextChannel, PublicGuildTextChannel } from "./textChannel";
Expand Down
2 changes: 1 addition & 1 deletion src/entity/textChannel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChildEntity, Column, Index, ManyToOne } from "typeorm";
import { z } from "zod";
import { checkPermission } from "../util/checkPermission";
import type { PERMISSION } from "../util/permission";
import { checkPermission } from "../util/permission";
import { Channel } from "./channel";
import type { Guild } from "./guild";
import type { User } from "./user";
Expand Down
50 changes: 50 additions & 0 deletions src/util/checkPermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type { Guild, User } from "../entity";
import { Member } from "../entity/member";
import { Role } from "../entity/role";
import { getDatabase } from "./database";
import { PERMISSION } from "./permission";

export const checkPermission = async (
user: User,
guild: Guild,
permission: PERMISSION | PERMISSION[],
) => {
permission = Array.isArray(permission) ? permission : [permission];

if (guild.owner.id === user.id) return true; // we're the owner, all perms
if (permission.includes(PERMISSION.OWNER)) return false; // we're not owner, and requesting owner perms

const roles = (
await getDatabase()
.getRepository(Role)
.createQueryBuilder("roles")
.leftJoin("roles.members", "members")
.where("roles.guildId = :guild_id", { guild_id: guild.id })
.andWhere((qb) => {
const sub = qb
.subQuery()
.select("id")
.from(Member, "members")
.where("members.userId = :user_id", { user_id: user.id })
.getQuery();

qb.where(`roles_members.guildMembersId in ${sub}`);
})
.getMany()
).sort((a, b) => a.position - b.position);

let allowed = false;
// for every role in order
for (const role of roles) {
// this role has admin, allow it
if (role.allow.includes(PERMISSION.ADMIN)) return true;

// if every requested permission is allowed in this role, we're good
if (permission.every((x) => role.allow.includes(x))) allowed = true;
// if one of them is denied, we're not good
if (permission.find((x) => role.deny.includes(x))) allowed = false;
// if it's neutral, we just use the last set value
}

return allowed;
};
48 changes: 0 additions & 48 deletions src/util/permission.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
// Permissions regarding actions within a channel.

import { type Guild, Member, Role, type User } from "../entity";
import { getDatabase } from "./database";

// Stored within the role or channel overwrites
export enum PERMISSION {
/** no permissions */
Expand Down Expand Up @@ -47,48 +44,3 @@ export const DefaultPermissions: PERMISSION[] = [
PERMISSION.CALL_CHANNEL,
PERMISSION.UPLOAD,
];

export const checkPermission = async (
user: User,
guild: Guild,
permission: PERMISSION | PERMISSION[],
) => {
permission = Array.isArray(permission) ? permission : [permission];

if (guild.owner.id === user.id) return true; // we're the owner, all perms
if (permission.includes(PERMISSION.OWNER)) return false; // we're not owner, and requesting owner perms

const roles = (
await getDatabase()
.getRepository(Role)
.createQueryBuilder("roles")
.leftJoin("roles.members", "members")
.where("roles.guildId = :guild_id", { guild_id: guild.id })
.andWhere((qb) => {
const sub = qb
.subQuery()
.select("id")
.from(Member, "members")
.where("members.userId = :user_id", { user_id: user.id })
.getQuery();

qb.where(`roles_members.guildMembersId in ${sub}`);
})
.getMany()
).sort((a, b) => a.position - b.position);

let allowed = false;
// for every role in order
for (const role of roles) {
// this role has admin, allow it
if (role.allow.includes(PERMISSION.ADMIN)) return true;

// if every requested permission is allowed in this role, we're good
if (permission.every((x) => role.allow.includes(x))) allowed = true;
// if one of them is denied, we're not good
if (permission.find((x) => role.deny.includes(x))) allowed = false;
// if it's neutral, we just use the last set value
}

return allowed;
};

0 comments on commit 22f2880

Please sign in to comment.