use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.
the class PermissionListPaginationWidget method getColumns.
@SuppressWarnings("unchecked")
@Override
protected AbstractPaginationWidget.Column<PermissionTarget>[] getColumns() {
return new AbstractPaginationWidget.Column[] { new Column<PermissionTarget>("Permission", permissionTarget -> {
int childTargetLevel = 0;
PermissionTarget currentParent = permissionTarget.getParentTarget();
while (currentParent != null) {
childTargetLevel++;
currentParent = currentParent.getParentTarget();
}
if (childTargetLevel == 0) {
return permissionTarget.getPermissionTargetIdentifier();
} else {
return "-".repeat(childTargetLevel) + " *" + permissionTarget.getPermissionTargetIdentifier() + "*";
}
}, permissionTarget -> {
PermissionTarget rootTarget = permissionTarget;
PermissionTarget parent;
while ((parent = rootTarget.getParentTarget()) != null) {
rootTarget = parent;
}
PermissionTarget.PermissionTypeCategory permissionTypeCategory = rootTarget.getPermissionTypeCategory();
int categorySorting = permissionTypeCategory.getSorting();
return EmbedTable.Group.namedGroupOrdered("***" + permissionTypeCategory.getCategoryName() + "***", permissionTypeCategory, categorySorting);
}), new Column<PermissionTarget>("Available to", permissionTarget -> {
Optional<AccessConfiguration> accessConfiguration = securityManager.getAccessConfiguration(permissionTarget, getGuild().getIdString());
if (accessConfiguration.isPresent()) {
List<Role> roles = accessConfiguration.get().getRoles(getGuild().get());
if (roles.isEmpty()) {
return "Guild owner and administrator roles only";
} else {
return StringList.create(roles, Role::getName).toSeparatedString(", ");
}
} else {
return "Available to everyone";
}
}, permissionTarget -> {
PermissionTarget rootTarget = permissionTarget;
PermissionTarget parent;
while ((parent = rootTarget.getParentTarget()) != null) {
rootTarget = parent;
}
PermissionTarget.PermissionTypeCategory permissionTypeCategory = rootTarget.getPermissionTypeCategory();
int categorySorting = permissionTypeCategory.getSorting();
return EmbedTable.Group.silentGroupOrdered("***" + permissionTypeCategory.getCategoryName() + "***", permissionTypeCategory, categorySorting);
}) };
}
use of net.robinfriedli.aiode.command.PermissionTarget in project aiode by robinfriedli.
the class PermissionCommand method getSelectedTargets.
private Set<? extends PermissionTarget> getSelectedTargets() {
Set<? extends PermissionTarget> selectedRootTargets = getSelectedRootTargets();
if (!argumentSet("argument")) {
return selectedRootTargets;
}
String argumentValue;
if (argumentSet("all")) {
argumentValue = getArgumentValueOrElse("argument", getCommandInput());
} else {
argumentValue = getArgumentValueWithTypeOrElse("argument", String.class, null);
}
if (argumentValue == null) {
return getAllArguments(selectedRootTargets);
}
List<String> argumentIdentifiers = COMMA_SPLITTER.splitToList(argumentValue);
if (argumentIdentifiers.isEmpty()) {
return getAllArguments(selectedRootTargets);
}
Set<CommandArgument> selectedArguments = Sets.newHashSet();
for (String argumentIdentifier : argumentIdentifiers) {
for (PermissionTarget selectedRootTarget : selectedRootTargets) {
if (selectedRootTarget instanceof CommandContribution) {
CommandContribution commandContribution = (CommandContribution) selectedRootTarget;
CommandArgument argument = commandContribution.getArgument(argumentIdentifier);
if (argument != null) {
selectedArguments.add(argument);
} else {
throw new InvalidCommandException(String.format("No such argument '%s' on command '%s'.", argumentIdentifier, commandContribution.getIdentifier()));
}
} else {
throw new InvalidCommandException(String.format("Cannot find argument '%s' on permission target '%s' as it is not a command.", argumentIdentifier, selectedRootTarget.getFullPermissionTargetIdentifier()));
}
}
}
return selectedArguments;
}
use of net.robinfriedli.aiode.command.PermissionTarget 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.PermissionTarget 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.PermissionTarget 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.");
}
}
Aggregations