mirror of
https://github.com/aNNiMON/ffmpegbot
synced 2024-09-19 22:54:20 +03:00
Better permissions
This commit is contained in:
parent
b58535d81f
commit
8e952672d8
@ -4,9 +4,4 @@ import java.util.Set;
|
||||
|
||||
public record BotConfig(String botToken, String botUsername,
|
||||
Set<Long> superUsers, Set<Long> allowedUsers) {
|
||||
|
||||
boolean isUserAllowed(Long userId) {
|
||||
return superUsers().contains(userId)
|
||||
|| allowedUsers().contains(userId);
|
||||
}
|
||||
}
|
||||
|
@ -19,11 +19,13 @@ import java.util.EnumSet;
|
||||
|
||||
public class MainBotHandler extends BotHandler {
|
||||
private final BotConfig botConfig;
|
||||
private final Permissions permissions;
|
||||
private final CommandRegistry<For> commands;
|
||||
private final MediaProcessingBundle mediaProcessingBundle;
|
||||
|
||||
public MainBotHandler(BotConfig botConfig) {
|
||||
this.botConfig = botConfig;
|
||||
permissions = new Permissions(botConfig.superUsers(), botConfig.allowedUsers());
|
||||
commands = new CommandRegistry<>(this, this::checkAccess);
|
||||
final var sessions = new Sessions();
|
||||
mediaProcessingBundle = new MediaProcessingBundle(sessions);
|
||||
@ -39,7 +41,7 @@ public class MainBotHandler extends BotHandler {
|
||||
if (commands.handleUpdate(update)) {
|
||||
return null;
|
||||
}
|
||||
if (update.hasMessage()) {
|
||||
if (update.hasMessage() && permissions.isUserAllowed(update.getMessage().getFrom().getId())) {
|
||||
mediaProcessingBundle.handleMessage(this, update.getMessage());
|
||||
}
|
||||
return null;
|
||||
@ -57,12 +59,7 @@ public class MainBotHandler extends BotHandler {
|
||||
|
||||
private boolean checkAccess(Update update, @NotNull User user, @NotNull EnumSet<For> roles) {
|
||||
final long userId = user.getId();
|
||||
|
||||
if (roles.contains(For.CREATOR) && botConfig.superUsers().contains(userId))
|
||||
return true;
|
||||
if (roles.contains(For.ADMIN) && botConfig.isUserAllowed(userId))
|
||||
return true;
|
||||
return roles.contains(For.USER);
|
||||
return permissions.hasAccess(userId, roles);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
25
src/main/java/com/annimon/ffmpegbot/Permissions.java
Normal file
25
src/main/java/com/annimon/ffmpegbot/Permissions.java
Normal file
@ -0,0 +1,25 @@
|
||||
package com.annimon.ffmpegbot;
|
||||
|
||||
import com.annimon.tgbotsmodule.commands.authority.For;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.EnumSet;
|
||||
import java.util.Set;
|
||||
|
||||
public record Permissions(Set<Long> superUsers, Set<Long> allowedUsers) {
|
||||
public static final EnumSet<For> SUPERUSERS = EnumSet.of(For.CREATOR);
|
||||
public static final EnumSet<For> ALLOWED_USERS = EnumSet.of(For.CREATOR, For.ADMIN);
|
||||
|
||||
boolean hasAccess(long userId, @NotNull EnumSet<For> roles) {
|
||||
if (roles.contains(For.CREATOR) && superUsers().contains(userId))
|
||||
return true;
|
||||
if (roles.contains(For.ADMIN) && isUserAllowed(userId))
|
||||
return true;
|
||||
return roles.contains(For.USER);
|
||||
}
|
||||
|
||||
boolean isUserAllowed(Long userId) {
|
||||
return superUsers().contains(userId)
|
||||
|| allowedUsers().contains(userId);
|
||||
}
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
package com.annimon.ffmpegbot.commands.admin;
|
||||
|
||||
import com.annimon.ffmpegbot.Permissions;
|
||||
import com.annimon.tgbotsmodule.commands.TextCommand;
|
||||
import com.annimon.tgbotsmodule.commands.authority.For;
|
||||
import com.annimon.tgbotsmodule.commands.context.MessageContext;
|
||||
@ -25,7 +26,7 @@ public class RunCommand implements TextCommand {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public EnumSet<For> authority() {
|
||||
return EnumSet.of(For.CREATOR);
|
||||
return Permissions.SUPERUSERS;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -21,6 +21,7 @@ import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.annimon.ffmpegbot.Permissions.ALLOWED_USERS;
|
||||
import static com.annimon.ffmpegbot.commands.ffmpeg.CallbackQueryCommands.*;
|
||||
import static com.annimon.ffmpegbot.commands.ffmpeg.MediaProcessingKeyboard.createKeyboard;
|
||||
|
||||
@ -34,10 +35,10 @@ public class MediaProcessingBundle implements CommandBundle<For> {
|
||||
|
||||
@Override
|
||||
public void register(@NotNull CommandRegistry commands) {
|
||||
commands.register(new SimpleCallbackQueryCommand(PREV, For.ADMIN, ctx -> toggleParameter(ctx, true)));
|
||||
commands.register(new SimpleCallbackQueryCommand(NEXT, For.ADMIN, ctx -> toggleParameter(ctx, false)));
|
||||
commands.register(new SimpleCallbackQueryCommand(DETAIL, For.ADMIN, sessionCommand(this::details)));
|
||||
commands.register(new SimpleCallbackQueryCommand(PROCESS, For.ADMIN, sessionCommand(this::process)));
|
||||
commands.register(new SimpleCallbackQueryCommand(PREV, ALLOWED_USERS, ctx -> toggleParameter(ctx, true)));
|
||||
commands.register(new SimpleCallbackQueryCommand(NEXT, ALLOWED_USERS, ctx -> toggleParameter(ctx, false)));
|
||||
commands.register(new SimpleCallbackQueryCommand(DETAIL, ALLOWED_USERS, sessionCommand(this::details)));
|
||||
commands.register(new SimpleCallbackQueryCommand(PROCESS, ALLOWED_USERS, sessionCommand(this::process)));
|
||||
}
|
||||
|
||||
public void handleMessage(final @NotNull CommonAbsSender sender, final @NotNull Message message) {
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.annimon.ffmpegbot.commands.ytdlp;
|
||||
|
||||
import com.annimon.ffmpegbot.Permissions;
|
||||
import com.annimon.ffmpegbot.session.*;
|
||||
import com.annimon.tgbotsmodule.commands.CommandBundle;
|
||||
import com.annimon.tgbotsmodule.commands.CommandRegistry;
|
||||
@ -19,7 +20,7 @@ public class YtDlpCommandBundle implements CommandBundle<For> {
|
||||
public void register(@NotNull CommandRegistry commands) {
|
||||
commands.register(new SimpleRegexCommand(
|
||||
Pattern.compile("/dl (https?://[^ ]+) ?(audio|\\d+)?p?"),
|
||||
For.ADMIN,
|
||||
Permissions.ALLOWED_USERS,
|
||||
this::download));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user