Search in sources :

Example 1 with PermissionTarget

use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.

the class PermissionListPaginationWidget method getColumns.

@SuppressWarnings("unchecked")
@Override
protected AbstractPaginationWidget.Column<PermissionTarget>[] getColumns() {
    return new AbstractPaginationWidget.Column[] { new Column<PermissionTarget>("Permission", permissionTarget -> {
        int childTargetLevel = 0;
        PermissionTarget currentParent = permissionTarget.getParentTarget();
        while (currentParent != null) {
            childTargetLevel++;
            currentParent = currentParent.getParentTarget();
        }
        if (childTargetLevel == 0) {
            return permissionTarget.getPermissionTargetIdentifier();
        } else {
            return "-".repeat(childTargetLevel) + " *" + permissionTarget.getPermissionTargetIdentifier() + "*";
        }
    }, permissionTarget -> {
        PermissionTarget rootTarget = permissionTarget;
        PermissionTarget parent;
        while ((parent = rootTarget.getParentTarget()) != null) {
            rootTarget = parent;
        }
        PermissionTarget.PermissionTypeCategory permissionTypeCategory = rootTarget.getPermissionTypeCategory();
        int categorySorting = permissionTypeCategory.getSorting();
        return EmbedTable.Group.namedGroupOrdered("***" + permissionTypeCategory.getCategoryName() + "***", permissionTypeCategory, categorySorting);
    }), new Column<PermissionTarget>("Available to", permissionTarget -> {
        Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(permissionTarget, getGuild().getIdString());
        if (accessConfiguration.isPresent()) {
            List<Role> roles = accessConfiguration.get().getRoles(getGuild().get());
            if (roles.isEmpty()) {
                return "Guild owner and administrator roles only";
            } else {
                return StringList.create(roles, Role::getName).toSeparatedString(", ");
            }
        } else {
            return "Available to everyone";
        }
    }, permissionTarget -> {
        PermissionTarget rootTarget = permissionTarget;
        PermissionTarget parent;
        while ((parent = rootTarget.getParentTarget()) != null) {
            rootTarget = parent;
        }
        PermissionTarget.PermissionTypeCategory permissionTypeCategory = rootTarget.getPermissionTypeCategory();
        int categorySorting = permissionTypeCategory.getSorting();
        return EmbedTable.Group.silentGroupOrdered("***" + permissionTypeCategory.getCategoryName() + "***", permissionTypeCategory, categorySorting);
    }) };
}
Also used : Role(net.dv8tion.jda.api.entities.Role) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget)

Example 2 with PermissionTarget

use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.

the class PermissionCommand method getSelectedTargets.

private Set<? extends PermissionTarget> getSelectedTargets() {
    Set<? extends PermissionTarget> selectedRootTargets = getSelectedRootTargets();
    if (!argumentSet("argument")) {
        return selectedRootTargets;
    }
    String argumentValue;
    if (argumentSet("all")) {
        argumentValue = getArgumentValueOrElse("argument", getCommandInput());
    } else {
        argumentValue = getArgumentValueWithTypeOrElse("argument", String.class, null);
    }
    if (argumentValue == null) {
        return getAllArguments(selectedRootTargets);
    }
    List<String> argumentIdentifiers = COMMA_SPLITTER.splitToList(argumentValue);
    if (argumentIdentifiers.isEmpty()) {
        return getAllArguments(selectedRootTargets);
    }
    Set<CommandArgument> selectedArguments = Sets.newHashSet();
    for (String argumentIdentifier : argumentIdentifiers) {
        for (PermissionTarget selectedRootTarget : selectedRootTargets) {
            if (selectedRootTarget instanceof CommandContribution) {
                CommandContribution commandContribution = (CommandContribution) selectedRootTarget;
                CommandArgument argument = commandContribution.getArgument(argumentIdentifier);
                if (argument != null) {
                    selectedArguments.add(argument);
                } else {
                    throw new InvalidCommandException(String.format("No such argument '%s' on command '%s'.", argumentIdentifier, commandContribution.getIdentifier()));
                }
            } else {
                throw new InvalidCommandException(String.format("Cannot find argument '%s' on permission target '%s' as it is not a command.", argumentIdentifier, selectedRootTarget.getFullPermissionTargetIdentifier()));
            }
        }
    }
    return selectedArguments;
}
Also used : InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) CommandArgument(net.robinfriedli.aiode.command.argument.CommandArgument) CommandContribution(net.robinfriedli.aiode.entities.xml.CommandContribution) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Example 3 with PermissionTarget

use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.

the class PermissionCommand method listPermissions.

private void listPermissions() {
    CommandContext context = getContext();
    Guild guild = context.getGuild();
    GuildContext guildContext = context.getGuildContext();
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    List<PermissionTarget> targets = Lists.newArrayList();
    for (PermissionTarget.TargetType targetType : PermissionTarget.TargetType.values()) {
        if (!targetType.isChildTargetOnly()) {
            Set<? extends PermissionTarget> permissionTargetsInCategory = targetType.getAllPermissionTargetsInCategory();
            permissionTargetsInCategory.stream().sorted(Comparator.comparingInt((ToIntFunction<PermissionTarget>) permissionTarget -> permissionTarget.getPermissionTypeCategory().getSorting()).thenComparing(PermissionTarget::getPermissionTargetIdentifier)).forEach(permissionTarget -> {
                List<PermissionTarget> flattenedPermissionHierarchy = Lists.newArrayList();
                LinkedList<List<PermissionTarget>> stack = new LinkedList<>();
                stack.push(flattenedPermissionHierarchy);
                flattenPermissionTargetHierarchy(permissionTarget, stack, securityManager, guild, true);
                targets.addAll(flattenedPermissionHierarchy);
            });
        }
    }
    PermissionListPaginationWidget permissionListPaginationWidget = new PermissionListPaginationWidget(guildContext.getWidgetRegistry(), guild, context.getChannel(), targets, securityManager);
    permissionListPaginationWidget.initialise();
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) Guild(net.dv8tion.jda.api.entities.Guild) LinkedList(java.util.LinkedList) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) GuildContext(net.robinfriedli.aiode.discord.GuildContext) PermissionListPaginationWidget(net.robinfriedli.aiode.command.widget.widgets.PermissionListPaginationWidget) ToIntFunction(java.util.function.ToIntFunction) LinkedList(java.util.LinkedList) List(java.util.List)

Example 4 with PermissionTarget

use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.

the class PermissionCommand method deleteCustomPermissionTarget.

private void deleteCustomPermissionTarget() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    String identifier = getCommandInput();
    CommandContext context = getContext();
    Session session = context.getSession();
    Optional<? extends PermissionTarget> existingPermissionTarget = securityManager.getPermissionTarget(identifier);
    if (existingPermissionTarget.isEmpty()) {
        throw new InvalidCommandException(String.format("No such permission target '%s'.", identifier));
    }
    PermissionTarget permissionTarget = existingPermissionTarget.get();
    if (!(permissionTarget instanceof CustomPermissionTarget)) {
        throw new InvalidCommandException(String.format("Permission target '%s' cannot be deleted as it is not a custom target.", identifier));
    }
    CustomPermissionTarget customPermissionTarget = (CustomPermissionTarget) permissionTarget;
    invoke(() -> {
        Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(customPermissionTarget, context.getGuild());
        if (accessConfiguration.isPresent()) {
            for (GrantedRole role : accessConfiguration.get().getRoles()) {
                session.delete(role);
            }
            session.delete(accessConfiguration);
        }
        session.delete(customPermissionTarget);
    });
}
Also used : CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) Session(org.hibernate.Session) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Example 5 with PermissionTarget

use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.

the class PermissionCommand method clearCommands.

private void clearCommands() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    GuildSpecification specification = getContext().getGuildContext().getSpecification();
    Set<? extends PermissionTarget> selectedCommands = getSelectedTargets();
    Session session = getContext().getSession();
    boolean removedAnything = invoke(() -> {
        boolean takenAction = false;
        for (PermissionTarget permissionTarget : selectedCommands) {
            Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(permissionTarget, getContext().getGuild());
            if (accessConfiguration.isPresent()) {
                AccessConfiguration ac = accessConfiguration.get();
                takenAction = specification.removeAccessConfiguration(ac);
                ac.getRoles().forEach(session::delete);
                session.delete(ac);
            }
        }
        return takenAction;
    });
    if (!removedAnything) {
        sendError("None of the selected permissions had an access configuration set up to begin with. No action required.");
    }
}
Also used : SecurityManager(net.robinfriedli.aiode.command.SecurityManager) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) Session(org.hibernate.Session) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Aggregations

PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)9 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)7 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)6 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)6 Session (org.hibernate.Session)6 CommandContext (net.robinfriedli.aiode.command.CommandContext)5 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)4 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)4 Role (net.dv8tion.jda.api.entities.Role)3 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)3 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ToIntFunction (java.util.function.ToIntFunction)1 Guild (net.dv8tion.jda.api.entities.Guild)1 Member (net.dv8tion.jda.api.entities.Member)1 AbstractCommand (net.robinfriedli.aiode.command.AbstractCommand)1 ArgumentController (net.robinfriedli.aiode.command.argument.ArgumentController)1 CommandArgument (net.robinfriedli.aiode.command.argument.CommandArgument)1 PermissionListPaginationWidget (net.robinfriedli.aiode.command.widget.widgets.PermissionListPaginationWidget)1 GuildContext (net.robinfriedli.aiode.discord.GuildContext)1