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