Search in sources :

Example 1 with SecurityManager

use of net.robinfriedli.aiode.command.SecurityManager 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 2 with SecurityManager

use of net.robinfriedli.aiode.command.SecurityManager 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 3 with SecurityManager

use of net.robinfriedli.aiode.command.SecurityManager 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)

Example 4 with SecurityManager

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

the class PermissionCommand method createCustomPermissionTarget.

private void createCustomPermissionTarget() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    String identifier = getCommandInput();
    CommandContext context = getContext();
    Session session = context.getSession();
    Optional<? extends PermissionTarget> existingPermissionTarget = securityManager.getPermissionTarget(identifier);
    if (existingPermissionTarget.isPresent()) {
        throw new InvalidCommandException(String.format("Permission target '%s' already exists.", identifier));
    }
    Guild guild = context.getGuild();
    User user = context.getUser();
    CustomPermissionTarget customPermissionTarget = new CustomPermissionTarget(identifier, guild, user);
    invoke(() -> session.persist(customPermissionTarget));
}
Also used : CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) User(net.dv8tion.jda.api.entities.User) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) CommandContext(net.robinfriedli.aiode.command.CommandContext) InvalidCommandException(net.robinfriedli.aiode.exceptions.InvalidCommandException) Guild(net.dv8tion.jda.api.entities.Guild) Session(org.hibernate.Session)

Example 5 with SecurityManager

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

the class PermissionCommand method lockCommands.

private void lockCommands() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    GuildSpecification specification = getContext().getGuildContext().getSpecification();
    Set<? extends PermissionTarget> selectedCommands = getSelectedTargets();
    Session session = getContext().getSession();
    boolean lockedAnything = invoke(() -> {
        boolean takenAction = false;
        for (PermissionTarget permissionTarget : selectedCommands) {
            Optional<AccessConfiguration> existingAccessConfiguration = securityManager.getAccessConfiguration(permissionTarget, getContext().getGuild());
            if (existingAccessConfiguration.isPresent()) {
                AccessConfiguration accessConfiguration = existingAccessConfiguration.get();
                Set<GrantedRole> setRoles = accessConfiguration.getRoles();
                for (GrantedRole setRole : setRoles) {
                    accessConfiguration.removeRole(setRole);
                    session.delete(setRole);
                    takenAction = true;
                }
            } else {
                AccessConfiguration accessConfiguration = new AccessConfiguration(permissionTarget, session);
                session.persist(accessConfiguration);
                specification.addAccessConfiguration(accessConfiguration);
                takenAction = true;
            }
        }
        return takenAction;
    });
    if (!lockedAnything) {
        sendError("All selected permissions are already only available to the guild owner and administrator roles.");
    }
}
Also used : SecurityManager(net.robinfriedli.aiode.command.SecurityManager) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) 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

SecurityManager (net.robinfriedli.aiode.command.SecurityManager)8 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)7 CommandContext (net.robinfriedli.aiode.command.CommandContext)6 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)6 Session (org.hibernate.Session)6 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)5 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)4 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)3 Guild (net.dv8tion.jda.api.entities.Guild)2 Role (net.dv8tion.jda.api.entities.Role)2 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ToIntFunction (java.util.function.ToIntFunction)1 User (net.dv8tion.jda.api.entities.User)1 Aiode (net.robinfriedli.aiode.Aiode)1 GroovySandboxComponent (net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)1 PermissionListPaginationWidget (net.robinfriedli.aiode.command.widget.widgets.PermissionListPaginationWidget)1 GuildContext (net.robinfriedli.aiode.discord.GuildContext)1 GroovyVariableManager (net.robinfriedli.aiode.scripting.GroovyVariableManager)1