feat(Permissions): add checkAdmin to permission overwrite checks (#6847)

This commit is contained in:
ckohen
2021-10-16 15:27:52 -07:00
committed by GitHub
parent e8b69974dc
commit 7513b4528c
4 changed files with 34 additions and 19 deletions

View File

@@ -153,13 +153,14 @@ class GuildChannel extends Channel {
/** /**
* Gets the overall set of permissions for a member or role in this channel, taking into account channel overwrites. * Gets the overall set of permissions for a member or role in this channel, taking into account channel overwrites.
* @param {GuildMemberResolvable|RoleResolvable} memberOrRole The member or role to obtain the overall permissions for * @param {GuildMemberResolvable|RoleResolvable} memberOrRole The member or role to obtain the overall permissions for
* @param {boolean} [checkAdmin=true] Whether having `ADMINISTRATOR` will return all permissions
* @returns {?Readonly<Permissions>} * @returns {?Readonly<Permissions>}
*/ */
permissionsFor(memberOrRole) { permissionsFor(memberOrRole, checkAdmin = true) {
const member = this.guild.members.resolve(memberOrRole); const member = this.guild.members.resolve(memberOrRole);
if (member) return this.memberPermissions(member); if (member) return this.memberPermissions(member, checkAdmin);
const role = this.guild.roles.resolve(memberOrRole); const role = this.guild.roles.resolve(memberOrRole);
return role && this.rolePermissions(role); return role && this.rolePermissions(role, checkAdmin);
} }
overwritesFor(member, verified = false, roles = null) { overwritesFor(member, verified = false, roles = null) {
@@ -191,16 +192,19 @@ class GuildChannel extends Channel {
/** /**
* Gets the overall set of permissions for a member in this channel, taking into account channel overwrites. * Gets the overall set of permissions for a member in this channel, taking into account channel overwrites.
* @param {GuildMember} member The member to obtain the overall permissions for * @param {GuildMember} member The member to obtain the overall permissions for
* @param {boolean} checkAdmin=true Whether having `ADMINISTRATOR` will return all permissions
* @returns {Readonly<Permissions>} * @returns {Readonly<Permissions>}
* @private * @private
*/ */
memberPermissions(member) { memberPermissions(member, checkAdmin) {
if (member.id === this.guild.ownerId) return new Permissions(Permissions.ALL).freeze(); if (checkAdmin && member.id === this.guild.ownerId) return new Permissions(Permissions.ALL).freeze();
const roles = member.roles.cache; const roles = member.roles.cache;
const permissions = new Permissions(roles.map(role => role.permissions)); const permissions = new Permissions(roles.map(role => role.permissions));
if (permissions.has(Permissions.FLAGS.ADMINISTRATOR)) return new Permissions(Permissions.ALL).freeze(); if (checkAdmin && permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
return new Permissions(Permissions.ALL).freeze();
}
const overwrites = this.overwritesFor(member, true, roles); const overwrites = this.overwritesFor(member, true, roles);
@@ -217,11 +221,14 @@ class GuildChannel extends Channel {
/** /**
* Gets the overall set of permissions for a role in this channel, taking into account channel overwrites. * Gets the overall set of permissions for a role in this channel, taking into account channel overwrites.
* @param {Role} role The role to obtain the overall permissions for * @param {Role} role The role to obtain the overall permissions for
* @param {boolean} checkAdmin Whether having `ADMINISTRATOR` will return all permissions
* @returns {Readonly<Permissions>} * @returns {Readonly<Permissions>}
* @private * @private
*/ */
rolePermissions(role) { rolePermissions(role, checkAdmin) {
if (role.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) return new Permissions(Permissions.ALL).freeze(); if (checkAdmin && role.permissions.has(Permissions.FLAGS.ADMINISTRATOR)) {
return new Permissions(Permissions.ALL).freeze();
}
const everyoneOverwrites = this.permissionOverwrites.cache.get(this.guild.id); const everyoneOverwrites = this.permissionOverwrites.cache.get(this.guild.id);
const roleOverwrites = this.permissionOverwrites.cache.get(role.id); const roleOverwrites = this.permissionOverwrites.cache.get(role.id);

View File

@@ -232,12 +232,13 @@ class Role extends Base {
* Returns `channel.permissionsFor(role)`. Returns permissions for a role in a guild channel, * Returns `channel.permissionsFor(role)`. Returns permissions for a role in a guild channel,
* taking into account permission overwrites. * taking into account permission overwrites.
* @param {GuildChannel|Snowflake} channel The guild channel to use as context * @param {GuildChannel|Snowflake} channel The guild channel to use as context
* @param {boolean} [checkAdmin=true] Whether having `ADMINISTRATOR` will return all permissions
* @returns {Readonly<Permissions>} * @returns {Readonly<Permissions>}
*/ */
permissionsIn(channel) { permissionsIn(channel, checkAdmin = true) {
channel = this.guild.channels.resolve(channel); channel = this.guild.channels.resolve(channel);
if (!channel) throw new Error('GUILD_CHANNEL_RESOLVE'); if (!channel) throw new Error('GUILD_CHANNEL_RESOLVE');
return channel.rolePermissions(this); return channel.rolePermissions(this, checkAdmin);
} }
/** /**

View File

@@ -227,10 +227,11 @@ class ThreadChannel extends Channel {
* Gets the overall set of permissions for a member or role in this thread's parent channel, taking overwrites into * Gets the overall set of permissions for a member or role in this thread's parent channel, taking overwrites into
* account. * account.
* @param {GuildMemberResolvable|RoleResolvable} memberOrRole The member or role to obtain the overall permissions for * @param {GuildMemberResolvable|RoleResolvable} memberOrRole The member or role to obtain the overall permissions for
* @param {boolean} [checkAdmin=true] Whether having `ADMINISTRATOR` will return all permissions
* @returns {?Readonly<Permissions>} * @returns {?Readonly<Permissions>}
*/ */
permissionsFor(memberOrRole) { permissionsFor(memberOrRole, checkAdmin) {
return this.parent?.permissionsFor(memberOrRole) ?? null; return this.parent?.permissionsFor(memberOrRole, checkAdmin) ?? null;
} }
/** /**

20
typings/index.d.ts vendored
View File

@@ -883,8 +883,8 @@ export class GuildBan extends Base {
export abstract class GuildChannel extends Channel { export abstract class GuildChannel extends Channel {
public constructor(guild: Guild, data?: RawGuildChannelData, client?: Client, immediatePatch?: boolean); public constructor(guild: Guild, data?: RawGuildChannelData, client?: Client, immediatePatch?: boolean);
private memberPermissions(member: GuildMember): Readonly<Permissions>; private memberPermissions(member: GuildMember, checkAdmin: boolean): Readonly<Permissions>;
private rolePermissions(role: Role): Readonly<Permissions>; private rolePermissions(role: Role, checkAdmin: boolean): Readonly<Permissions>;
public readonly calculatedPosition: number; public readonly calculatedPosition: number;
public readonly deletable: boolean; public readonly deletable: boolean;
@@ -906,8 +906,11 @@ export abstract class GuildChannel extends Channel {
public edit(data: ChannelData, reason?: string): Promise<this>; public edit(data: ChannelData, reason?: string): Promise<this>;
public equals(channel: GuildChannel): boolean; public equals(channel: GuildChannel): boolean;
public lockPermissions(): Promise<this>; public lockPermissions(): Promise<this>;
public permissionsFor(memberOrRole: GuildMember | Role): Readonly<Permissions>; public permissionsFor(memberOrRole: GuildMember | Role, checkAdmin?: boolean): Readonly<Permissions>;
public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly<Permissions> | null; public permissionsFor(
memberOrRole: GuildMemberResolvable | RoleResolvable,
checkAdmin?: boolean,
): Readonly<Permissions> | null;
public setName(name: string, reason?: string): Promise<this>; public setName(name: string, reason?: string): Promise<this>;
public setParent(channel: CategoryChannelResolvable | null, options?: SetParentOptions): Promise<this>; public setParent(channel: CategoryChannelResolvable | null, options?: SetParentOptions): Promise<this>;
public setPosition(position: number, options?: SetChannelPositionOptions): Promise<this>; public setPosition(position: number, options?: SetChannelPositionOptions): Promise<this>;
@@ -1725,7 +1728,7 @@ export class Role extends Base {
public edit(data: RoleData, reason?: string): Promise<Role>; public edit(data: RoleData, reason?: string): Promise<Role>;
public equals(role: Role): boolean; public equals(role: Role): boolean;
public iconURL(options?: StaticImageURLOptions): string | null; public iconURL(options?: StaticImageURLOptions): string | null;
public permissionsIn(channel: GuildChannel | Snowflake): Readonly<Permissions>; public permissionsIn(channel: GuildChannel | Snowflake, checkAdmin?: boolean): Readonly<Permissions>;
public setColor(color: ColorResolvable, reason?: string): Promise<Role>; public setColor(color: ColorResolvable, reason?: string): Promise<Role>;
public setHoist(hoist?: boolean, reason?: string): Promise<Role>; public setHoist(hoist?: boolean, reason?: string): Promise<Role>;
public setMentionable(mentionable?: boolean, reason?: string): Promise<Role>; public setMentionable(mentionable?: boolean, reason?: string): Promise<Role>;
@@ -2004,8 +2007,11 @@ export class ThreadChannel extends TextBasedChannel(Channel) {
public edit(data: ThreadEditData, reason?: string): Promise<ThreadChannel>; public edit(data: ThreadEditData, reason?: string): Promise<ThreadChannel>;
public join(): Promise<ThreadChannel>; public join(): Promise<ThreadChannel>;
public leave(): Promise<ThreadChannel>; public leave(): Promise<ThreadChannel>;
public permissionsFor(memberOrRole: GuildMember | Role): Readonly<Permissions>; public permissionsFor(memberOrRole: GuildMember | Role, checkAdmin?: boolean): Readonly<Permissions>;
public permissionsFor(memberOrRole: GuildMemberResolvable | RoleResolvable): Readonly<Permissions> | null; public permissionsFor(
memberOrRole: GuildMemberResolvable | RoleResolvable,
checkAdmin?: boolean,
): Readonly<Permissions> | null;
public fetchOwner(options?: BaseFetchOptions): Promise<ThreadMember | null>; public fetchOwner(options?: BaseFetchOptions): Promise<ThreadMember | null>;
public fetchStarterMessage(options?: BaseFetchOptions): Promise<Message>; public fetchStarterMessage(options?: BaseFetchOptions): Promise<Message>;
public setArchived(archived?: boolean, reason?: string): Promise<ThreadChannel>; public setArchived(archived?: boolean, reason?: string): Promise<ThreadChannel>;