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