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);
});
}
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));
}
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()));
}
}
}
Aggregations