Search in sources :

Example 6 with GrantedRole

use of net.robinfriedli.aiode.entities.GrantedRole 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 GrantedRole

use of net.robinfriedli.aiode.entities.GrantedRole 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 GrantedRole

use of net.robinfriedli.aiode.entities.GrantedRole in project aiode by robinfriedli.

the class AlertAccessConfigurationModificationInterceptor method handleModifiedAccessConfiguration.

private void handleModifiedAccessConfiguration(List<AccessConfiguration> affectedAccessConfigurations, List<GrantedRole> affectedGrantedRoles, boolean added, StringBuilder builder) {
    Multimap<AccessConfiguration, GrantedRole> grantedRoleMap = HashMultimap.create();
    for (GrantedRole grantedRole : affectedGrantedRoles) {
        grantedRoleMap.put(grantedRole.getAccessConfiguration(), grantedRole);
    }
    for (AccessConfiguration accessConfiguration : grantedRoleMap.keySet()) {
        if (!affectedAccessConfigurations.contains(accessConfiguration)) {
            if (!builder.toString().isEmpty()) {
                builder.append(System.lineSeparator());
            }
            Set<String> grantedRoles = grantedRoleMap.get(accessConfiguration).stream().map(role -> role.getRole(context.getGuild()).getName()).collect(Collectors.toSet());
            String roleString = String.join(", ", grantedRoles);
            if (grantedRoles.size() == 1) {
                builder.append("Role '").append(roleString).append("'");
            } else {
                builder.append("Roles [").append(roleString).append("]");
            }
            if (added) {
                builder.append(" now ");
            } else {
                builder.append(" no longer ");
            }
            builder.append(grantedRoles.size() == 1 ? "has " : "have ").append("access to ").append(accessConfiguration.getPermissionType().asEnum().getName()).append(" '").append(accessConfiguration.getPermissionIdentifier()).append("'");
        }
    }
}
Also used : PermissionTarget(net.robinfriedli.aiode.command.PermissionTarget) Logger(org.slf4j.Logger) CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget) MessageService(net.robinfriedli.aiode.discord.MessageService) Set(java.util.Set) Interceptor(org.hibernate.Interceptor) Multimap(com.google.common.collect.Multimap) Collectors(java.util.stream.Collectors) List(java.util.List) HashMultimap(com.google.common.collect.HashMultimap) CommandContext(net.robinfriedli.aiode.command.CommandContext) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Aggregations

GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)8 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)7 Role (net.dv8tion.jda.api.entities.Role)5 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)5 CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)5 CommandContext (net.robinfriedli.aiode.command.CommandContext)4 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)4 GuildSpecification (net.robinfriedli.aiode.entities.GuildSpecification)4 Session (org.hibernate.Session)4 Guild (net.dv8tion.jda.api.entities.Guild)3 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 EmbedBuilder (net.dv8tion.jda.api.EmbedBuilder)1 TextChannel (net.dv8tion.jda.api.entities.TextChannel)1 ShardManager (net.dv8tion.jda.api.sharding.ShardManager)1 Aiode (net.robinfriedli.aiode.Aiode)1