Search in sources :

Example 6 with SecurityManager

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

the class PermissionCommand method grantPermissions.

private void grantPermissions() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    CommandContext context = getContext();
    GuildSpecification specification = context.getGuildContext().getSpecification();
    Session session = context.getSession();
    Set<? extends PermissionTarget> selectedTargets = getSelectedTargets();
    Set<Role> selectedRoles = getSelectedRoles("to");
    boolean addedAnything = invoke(() -> {
        boolean takenAction = false;
        for (PermissionTarget permissionTarget : selectedTargets) {
            Optional<AccessConfiguration> existingConfiguration = securityManager.getAccessConfiguration(permissionTarget, context.getGuild());
            if (existingConfiguration.isPresent()) {
                AccessConfiguration accessConfiguration = existingConfiguration.get();
                Set<String> roleIds = accessConfiguration.getRoleIds();
                for (Role selectedRole : selectedRoles) {
                    if (roleIds.contains(selectedRole.getId())) {
                        continue;
                    }
                    GrantedRole role = new GrantedRole(selectedRole);
                    session.persist(role);
                    accessConfiguration.addRole(role);
                    takenAction = true;
                }
            } else {
                AccessConfiguration accessConfiguration = new AccessConfiguration(permissionTarget, session);
                for (Role selectedRole : selectedRoles) {
                    GrantedRole role = new GrantedRole(selectedRole);
                    session.persist(role);
                    accessConfiguration.addRole(role);
                }
                session.persist(accessConfiguration);
                specification.addAccessConfiguration(accessConfiguration);
                takenAction = true;
            }
        }
        return takenAction;
    });
    if (!addedAnything) {
        sendError("All selected roles have already been granted each selected permission. No changes needed.");
    }
}
Also used : SecurityManager(net.robinfriedli.aiode.command.SecurityManager) CommandContext(net.robinfriedli.aiode.command.CommandContext) PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) Session(org.hibernate.Session)

Example 7 with SecurityManager

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

the class PermissionCommand method denyPermissions.

private void denyPermissions() {
    SecurityManager securityManager = Aiode.get().getSecurityManager();
    CommandContext context = getContext();
    Set<? extends PermissionTarget> selectedCommands = getSelectedTargets();
    Set<Role> selectedRoles = getSelectedRoles("for");
    Session session = context.getSession();
    boolean removedAnything = invoke(() -> {
        boolean takenAction = false;
        for (PermissionTarget permissionTarget : selectedCommands) {
            Optional<AccessConfiguration> existingAccessConfiguration = securityManager.getAccessConfiguration(permissionTarget, context.getGuild());
            if (existingAccessConfiguration.isPresent()) {
                AccessConfiguration accessConfiguration = existingAccessConfiguration.get();
                for (Role selectedRole : selectedRoles) {
                    Optional<GrantedRole> setRole = accessConfiguration.getRole(selectedRole.getId());
                    if (setRole.isPresent()) {
                        GrantedRole grantedRole = setRole.get();
                        accessConfiguration.removeRole(grantedRole);
                        session.delete(grantedRole);
                        takenAction = true;
                    }
                }
            }
        }
        return takenAction;
    });
    if (!removedAnything) {
        sendError("None of the selected roles were granted any of the selected permissions. The deny argument is used " + "to remove granted roles from permissions. If there currently aren't any restrictions for the selected permissions " + "use the grant argument to limit the permission to certain roles.");
    }
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) CommandContext(net.robinfriedli.aiode.command.CommandContext) 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 8 with SecurityManager

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

the class EvalCommand method doRun.

@Override
public void doRun() {
    CommandContext context = getContext();
    Aiode aiode = Aiode.get();
    SecurityManager securityManager = aiode.getSecurityManager();
    GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
    GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
    SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, securityManager, argumentSet("privileged"));
    groovyScriptRunner.runAndSendResult(getCommandInput(), 10, TimeUnit.SECONDS);
}
Also used : CommandContext(net.robinfriedli.aiode.command.CommandContext) SecurityManager(net.robinfriedli.aiode.command.SecurityManager) SafeGroovyScriptRunner(net.robinfriedli.aiode.scripting.SafeGroovyScriptRunner) Aiode(net.robinfriedli.aiode.Aiode) GroovyVariableManager(net.robinfriedli.aiode.scripting.GroovyVariableManager) GroovySandboxComponent(net.robinfriedli.aiode.boot.configurations.GroovySandboxComponent)

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