use of net.robinfriedli.aiode.command.SecurityManager 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.command.SecurityManager 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.command.SecurityManager in project aiode by robinfriedli.
the class EvalCommand method doRun.
@Override
public void doRun() {
CommandContext context = getContext();
Aiode aiode = Aiode.get();
SecurityManager securityManager = aiode.getSecurityManager();
GroovySandboxComponent groovySandboxComponent = aiode.getGroovySandboxComponent();
GroovyVariableManager groovyVariableManager = aiode.getGroovyVariableManager();
SafeGroovyScriptRunner groovyScriptRunner = new SafeGroovyScriptRunner(context, groovySandboxComponent, groovyVariableManager, securityManager, argumentSet("privileged"));
groovyScriptRunner.runAndSendResult(getCommandInput(), 10, TimeUnit.SECONDS);
}
Aggregations