Skip to content
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

Make channel access checks consistent #2679

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public boolean canTalk()
}

@Override
public void checkCanAccessChannel() {}
public void checkCanAccess() {}

@Override
public void checkCanSendMessage() {
Expand Down Expand Up @@ -158,8 +158,6 @@ public PrivateChannelImpl setLatestMessageIdLong(long latestMessageId)
return this;
}

// -- Object --

@Override
public int hashCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public String getStatus()
public AuditableRestAction<Void> modifyStatus(@Nonnull String status)
{
Checks.notLonger(status, MAX_STATUS_LENGTH, "Voice Status");
checkCanAccessChannel();
checkCanAccess();
if (this.equals(getGuild().getSelfMember().getVoiceState().getChannel()))
checkPermission(Permission.VOICE_SET_STATUS);
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,7 @@ default RestAction<Void> delete()

// ---- State Accessors ----
T setName(String name);

// ---- Hooks ----
void checkCanAccess();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
package net.dv8tion.jda.internal.entities.channel.mixin.middleman;

import gnu.trove.map.TLongObjectMap;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.channel.unions.AudioChannelUnion;
import net.dv8tion.jda.api.exceptions.MissingAccessException;

public interface AudioChannelMixin<T extends AudioChannelMixin<T>>
extends AudioChannelUnion, StandardGuildChannelMixin<T>
Expand All @@ -31,4 +33,14 @@ public interface AudioChannelMixin<T extends AudioChannelMixin<T>>
T setUserLimit(int userlimit);

T setRegion(String region);

// AudioChannels also require connect permission to grant access
@Override
default void checkCanAccess()
{
if (!hasPermission(Permission.VIEW_CHANNEL))
throw new MissingAccessException(this, Permission.VIEW_CHANNEL);
if (!hasPermission(Permission.VOICE_CONNECT))
throw new MissingAccessException(this, Permission.VOICE_CONNECT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.unions.GuildChannelUnion;
import net.dv8tion.jda.api.exceptions.InsufficientPermissionException;
import net.dv8tion.jda.api.exceptions.MissingAccessException;
import net.dv8tion.jda.api.requests.Route;
import net.dv8tion.jda.api.requests.restaction.AuditableRestAction;
import net.dv8tion.jda.internal.entities.channel.mixin.ChannelMixin;
Expand All @@ -40,6 +41,7 @@ public interface GuildChannelMixin<T extends GuildChannelMixin<T>> extends
@CheckReturnValue
default AuditableRestAction<Void> delete()
{
checkCanAccess();
checkCanManage();

Route.CompiledRoute route = Route.Channels.DELETE_CHANNEL.compile(getId());
Expand Down Expand Up @@ -70,4 +72,11 @@ default void checkCanManage()
{
checkPermission(Permission.MANAGE_CHANNEL);
}

// Overridden by AudioChannelMixin
default void checkCanAccess()
{
if (!hasPermission(Permission.VIEW_CHANNEL))
throw new MissingAccessException(this, Permission.VIEW_CHANNEL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public interface GuildMessageChannelMixin<T extends GuildMessageChannelMixin<T>>
@CheckReturnValue
default RestAction<Void> deleteMessagesByIds(@Nonnull Collection<String> messageIds)
{
checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE, "Must have MESSAGE_MANAGE in order to bulk delete messages in this channel regardless of author.");

if (messageIds.size() < 2 || messageIds.size() > 100)
Expand All @@ -66,6 +67,7 @@ default RestAction<Void> removeReactionById(@Nonnull String messageId, @Nonnull
Checks.notNull(emoji, "Emoji");
Checks.notNull(user, "User");

checkCanAccess();
if (!getJDA().getSelfUser().equals(user))
checkPermission(Permission.MESSAGE_MANAGE);

Expand All @@ -85,6 +87,7 @@ default RestAction<Void> clearReactionsById(@Nonnull String messageId)
{
Checks.isSnowflake(messageId, "Message ID");

checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE);

final Route.CompiledRoute route = Route.Messages.REMOVE_ALL_REACTIONS.compile(getId(), messageId);
Expand All @@ -98,6 +101,7 @@ default RestAction<Void> clearReactionsById(@Nonnull String messageId, @Nonnull
Checks.notNull(messageId, "Message ID");
Checks.notNull(emoji, "Emoji");

checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE);

Route.CompiledRoute route = Route.Messages.CLEAR_EMOJI_REACTIONS.compile(getId(), messageId, emoji.getAsReactionCode());
Expand All @@ -108,21 +112,17 @@ default RestAction<Void> clearReactionsById(@Nonnull String messageId, @Nonnull
@Override
default MessageCreateAction sendStickers(@Nonnull Collection<? extends StickerSnowflake> stickers)
{
checkCanAccessChannel();
checkCanSendMessage();
Checks.notEmpty(stickers, "Stickers");
Checks.noneNull(stickers, "Stickers");
return new MessageCreateActionImpl(this).setStickers(stickers);
}

// ---- Default implementation of parent mixins hooks ----
default void checkCanAccessChannel()
{
checkPermission(Permission.VIEW_CHANNEL);
}

default void checkCanSendMessage()
{
checkCanAccess();
if (getType().isThread())
checkPermission(Permission.MESSAGE_SEND_IN_THREADS);
else
Expand All @@ -131,32 +131,38 @@ default void checkCanSendMessage()

default void checkCanSendMessageEmbeds()
{
checkCanAccess();
checkPermission(Permission.MESSAGE_EMBED_LINKS);
}

default void checkCanSendFiles()
{
checkCanAccess();
checkPermission(Permission.MESSAGE_ATTACH_FILES);
}

default void checkCanViewHistory()
{
checkCanAccess();
checkPermission(Permission.MESSAGE_HISTORY);
}

default void checkCanAddReactions()
{
checkCanAccess();
checkPermission(Permission.MESSAGE_ADD_REACTION);
checkPermission(Permission.MESSAGE_HISTORY, "You need MESSAGE_HISTORY to add reactions to a message");
}

default void checkCanRemoveReactions()
{
checkCanAccess();
checkPermission(Permission.MESSAGE_HISTORY, "You need MESSAGE_HISTORY to remove reactions from a message");
}

default void checkCanControlMessagePins()
{
checkCanAccess();
checkPermission(Permission.MESSAGE_MANAGE, "You need MESSAGE_MANAGE to pin or unpin messages.");
}

Expand Down
Loading
Loading