Search in sources :

Example 1 with GrantedRole

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

the class DeleteGrantedRolesForDeletedRolesTask method run.

@Override
protected void run(JobExecutionContext jobExecutionContext) {
    Aiode aiode = Aiode.get();
    ShardManager shardManager = aiode.getShardManager();
    QueryBuilderFactory queryBuilderFactory = aiode.getQueryBuilderFactory();
    StaticSessionProvider.consumeSession(session -> {
        List<GuildSpecification> guildSpecifications = queryBuilderFactory.find(GuildSpecification.class).build(session).getResultList();
        int deletionCounter = 0;
        for (GuildSpecification guildSpecification : guildSpecifications) {
            Guild guild = guildSpecification.getGuild(shardManager);
            if (guild == null) {
                continue;
            }
            for (AccessConfiguration accessConfiguration : guildSpecification.getAccessConfigurations()) {
                Set<GrantedRole> grantedRoles = accessConfiguration.getRoles();
                for (GrantedRole grantedRole : grantedRoles) {
                    Role guildRole = grantedRole.getRole(guild);
                    if (guildRole == null) {
                        // role has been deleted
                        accessConfiguration.removeRole(grantedRole);
                        session.delete(grantedRole);
                        ++deletionCounter;
                    }
                }
            }
        }
        if (deletionCounter > 0) {
            logger.info("Deleted " + deletionCounter + " GrantedRole entities for roles that no longer exist");
        }
    });
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) QueryBuilderFactory(net.robinfriedli.aiode.persist.qb.QueryBuilderFactory) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) GuildSpecification(net.robinfriedli.aiode.entities.GuildSpecification) ShardManager(net.dv8tion.jda.api.sharding.ShardManager) Guild(net.dv8tion.jda.api.entities.Guild) Aiode(net.robinfriedli.aiode.Aiode) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 2 with GrantedRole

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

the class GuildManagementListener method onRoleDelete.

@Override
public void onRoleDelete(@Nonnull RoleDeleteEvent event) {
    EventHandlerPool.execute(() -> {
        Role role = event.getRole();
        String roleId = role.getId();
        hibernateComponent.consumeSession(session -> {
            CriteriaBuilder cb = session.getCriteriaBuilder();
            CriteriaDelete<GrantedRole> deleteQuery = cb.createCriteriaDelete(GrantedRole.class);
            Root<GrantedRole> queryRoot = deleteQuery.from(GrantedRole.class);
            deleteQuery.where(cb.equal(queryRoot.get("id"), roleId));
            int rowCount = session.createQuery(deleteQuery).executeUpdate();
            if (rowCount > 0) {
                Guild guild = event.getGuild();
                GuildContext guildContext = guildManager.getContextForGuild(guild);
                logger.info(String.format("Deleted %s GrantedRole entities upon deletion of role %s on guild %s", rowCount, roleId, guild));
                EmbedBuilder embedBuilder = new EmbedBuilder();
                embedBuilder.setTitle("Deletion of role referenced by your permission configuration");
                embedBuilder.setDescription(String.format("The deleted role '%s' was referenced by the permission configuration. Check the current permissions by using the permission command.", role.getName()));
                embedBuilder.setColor(ColorSchemeProperty.getColor(guildContext.getSpecification(session)));
                TextChannel textChannel = guildManager.getDefaultTextChannelForGuild(guild);
                if (textChannel != null) {
                    messageService.sendTemporary(embedBuilder.build(), textChannel);
                }
            }
        });
    });
}
Also used : Role(net.dv8tion.jda.api.entities.Role) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) GuildContext(net.robinfriedli.aiode.discord.GuildContext) EmbedBuilder(net.dv8tion.jda.api.EmbedBuilder) TextChannel(net.dv8tion.jda.api.entities.TextChannel) GrantedRole(net.robinfriedli.aiode.entities.GrantedRole) Guild(net.dv8tion.jda.api.entities.Guild)

Example 3 with GrantedRole

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

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

the class MigrateGuildSpecificationsTask method migrateSpecification.

private void migrateSpecification(JDA shard, XmlElement specification, Session session) {
    String guildId = specification.getAttribute("guildId").getValue();
    Guild guild = shard.getGuildById(guildId);
    if (guild != null) {
        Optional<GuildSpecification> existingGuildSpecification = session.createQuery("from " + GuildSpecification.class.getName() + " where guildId = '" + guild.getId() + "'", GuildSpecification.class).uniqueResultOptional();
        if (existingGuildSpecification.isPresent()) {
            return;
        }
        String botifyName = specification.getAttribute("botifyName").getValue();
        String guildName = specification.getAttribute("guildName").getValue();
        String prefix;
        if (specification.hasAttribute("prefix")) {
            prefix = specification.getAttribute("prefix").getValue();
        } else {
            prefix = null;
        }
        GuildSpecification guildSpecification = new GuildSpecification();
        guildSpecification.setGuildName(guildName);
        guildSpecification.setGuildId(guildId);
        guildSpecification.setBotName(botifyName);
        guildSpecification.setPrefix(prefix);
        List<XmlElement> accessConfigurations = specification.query(tagName("accessConfiguration")).collect();
        for (XmlElement accessConfiguration : accessConfigurations) {
            String commandIdentifier = accessConfiguration.getAttribute("commandIdentifier").getValue();
            AccessConfiguration ac = new AccessConfiguration(commandIdentifier, session);
            String roleIdString = accessConfiguration.getAttribute("roleIds").getValue();
            Iterable<String> roleIds = splitter.split(roleIdString);
            for (String roleId : roleIds) {
                Role role = guild.getRoleById(roleId);
                if (role != null) {
                    GrantedRole grantedRole = new GrantedRole(role);
                    session.persist(grantedRole);
                    ac.addRole(grantedRole);
                }
            }
            guildSpecification.addAccessConfiguration(ac);
            session.persist(ac);
        }
        session.persist(guildSpecification);
    }
}
Also used : 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) XmlElement(net.robinfriedli.jxp.api.XmlElement) Guild(net.dv8tion.jda.api.entities.Guild) AccessConfiguration(net.robinfriedli.aiode.entities.AccessConfiguration)

Example 5 with GrantedRole

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

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