Search in sources :

Example 1 with CustomPermissionTarget

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

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

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

the class AlertAccessConfigurationModificationInterceptor method alertPermissionTargetModification.

private void alertPermissionTargetModification(String verb, StringBuilder builder, List<CustomPermissionTarget> affectedEntities) {
    if (!affectedEntities.isEmpty()) {
        if (!builder.toString().isEmpty()) {
            builder.append(System.lineSeparator());
        }
        if (affectedEntities.size() == 1) {
            CustomPermissionTarget permissionTarget = affectedEntities.get(0);
            builder.append(String.format("%s permission target '%s'", verb, permissionTarget.getFullPermissionTargetIdentifier()));
        } else {
            builder.append(String.format("%s %d permission targets", verb, affectedEntities.size()));
        }
    }
}
Also used : CustomPermissionTarget(net.robinfriedli.aiode.entities.CustomPermissionTarget)

Aggregations

CustomPermissionTarget (net.robinfriedli.aiode.entities.CustomPermissionTarget)3 CommandContext (net.robinfriedli.aiode.command.CommandContext)2 SecurityManager (net.robinfriedli.aiode.command.SecurityManager)2 InvalidCommandException (net.robinfriedli.aiode.exceptions.InvalidCommandException)2 Session (org.hibernate.Session)2 Guild (net.dv8tion.jda.api.entities.Guild)1 User (net.dv8tion.jda.api.entities.User)1 PermissionTarget (net.robinfriedli.aiode.command.PermissionTarget)1 AccessConfiguration (net.robinfriedli.aiode.entities.AccessConfiguration)1 GrantedRole (net.robinfriedli.aiode.entities.GrantedRole)1