use of net.robinfriedli.aiode.command.SecurityManager in project aiode by robinfriedli.
the class PermissionCommand method listPermissions.
private void listPermissions() {
CommandContext context = getContext();
Guild guild = context.getGuild();
GuildContext guildContext = context.getGuildContext();
SecurityManager securityManager = Aiode.get().getSecurityManager();
List<PermissionTarget> targets = Lists.newArrayList();
for (PermissionTarget.TargetType targetType : PermissionTarget.TargetType.values()) {
if (!targetType.isChildTargetOnly()) {
Set<? extends PermissionTarget> permissionTargetsInCategory = targetType.getAllPermissionTargetsInCategory();
permissionTargetsInCategory.stream().sorted(Comparator.comparingInt((ToIntFunction<PermissionTarget>) permissionTarget -> permissionTarget.getPermissionTypeCategory().getSorting()).thenComparing(PermissionTarget::getPermissionTargetIdentifier)).forEach(permissionTarget -> {
List<PermissionTarget> flattenedPermissionHierarchy = Lists.newArrayList();
LinkedList<List<PermissionTarget>> stack = new LinkedList<>();
stack.push(flattenedPermissionHierarchy);
flattenPermissionTargetHierarchy(permissionTarget, stack, securityManager, guild, true);
targets.addAll(flattenedPermissionHierarchy);
});
}
}
PermissionListPaginationWidget permissionListPaginationWidget = new PermissionListPaginationWidget(guildContext.getWidgetRegistry(), guild, context.getChannel(), targets, securityManager);
permissionListPaginationWidget.initialise();
}
use of net.robinfriedli.aiode.command.SecurityManager 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.command.SecurityManager in project aiode by robinfriedli.
the class PermissionCommand method clearCommands.
private void clearCommands() {
SecurityManager securityManager = Aiode.get().getSecurityManager();
GuildSpecification specification = getContext().getGuildContext().getSpecification();
Set<? extends PermissionTarget> selectedCommands = getSelectedTargets();
Session session = getContext().getSession();
boolean removedAnything = invoke(() -> {
boolean takenAction = false;
for (PermissionTarget permissionTarget : selectedCommands) {
Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(permissionTarget, getContext().getGuild());
if (accessConfiguration.isPresent()) {
AccessConfiguration ac = accessConfiguration.get();
takenAction = specification.removeAccessConfiguration(ac);
ac.getRoles().forEach(session::delete);
session.delete(ac);
}
}
return takenAction;
});
if (!removedAnything) {
sendError("None of the selected permissions had an access configuration set up to begin with. No action required.");
}
}
use of net.robinfriedli.aiode.command.SecurityManager 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.command.SecurityManager 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.");
}
}
Aggregations