use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.
the class AlertAccessConfigurationModificationInterceptor method getPermissionTypePrefix.
private String getPermissionTypePrefix(List<AccessConfiguration> accessConfigurations, boolean capitalize) {
boolean allOfSameType = true;
PermissionTarget.TargetType type = null;
for (AccessConfiguration deletedAccessConfiguration : accessConfigurations) {
PermissionTarget.TargetType targetType = deletedAccessConfiguration.getPermissionType().asEnum();
if (type != null && targetType != type) {
allOfSameType = false;
break;
} else if (type == null) {
type = targetType;
}
}
String prefix;
if (allOfSameType) {
// type is not null since deletedAccessConfigurations is not empty
// noinspection ConstantConditions
String typeName = type.getName();
if (capitalize) {
if (typeName.length() > 1) {
prefix = typeName.substring(0, 1).toUpperCase() + typeName.substring(1);
} else {
prefix = typeName.toUpperCase();
}
} else {
prefix = typeName;
}
} else if (capitalize) {
prefix = "Permission";
} else {
prefix = "permission";
}
return prefix;
}
use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.
the class DeleteGrantedRolesForDeletedRolesTask method run.
@Override
protected void run(JobExecutionContext jobExecutionContext) {
Aiode aiode = Aiode.get();
ShardManager shardManager = aiode.getShardManager();
QueryBuilderFactory queryBuilderFactory = aiode.getQueryBuilderFactory();
StaticSessionProvider.consumeSession(session -> {
List<GuildSpecification> guildSpecifications = queryBuilderFactory.find(GuildSpecification.class).build(session).getResultList();
int deletionCounter = 0;
for (GuildSpecification guildSpecification : guildSpecifications) {
Guild guild = guildSpecification.getGuild(shardManager);
if (guild == null) {
continue;
}
for (AccessConfiguration accessConfiguration : guildSpecification.getAccessConfigurations()) {
Set<GrantedRole> grantedRoles = accessConfiguration.getRoles();
for (GrantedRole grantedRole : grantedRoles) {
Role guildRole = grantedRole.getRole(guild);
if (guildRole == null) {
// role has been deleted
accessConfiguration.removeRole(grantedRole);
session.delete(grantedRole);
++deletionCounter;
}
}
}
}
if (deletionCounter > 0) {
logger.info("Deleted " + deletionCounter + " GrantedRole entities for roles that no longer exist");
}
});
}
use of net.robinfriedli.aiode.entities.AccessConfiguration in project aiode by robinfriedli.
the class GuildManager method initializeGuild.
private GuildContext initializeGuild(Guild guild) {
GuildContext createdContext = hibernateComponent.invokeWithSession(session -> {
AudioPlayer player = audioManager.getPlayerManager().createPlayer();
Optional<Long> existingSpecification = queryBuilderFactory.select(GuildSpecification.class, "pk", Long.class).where((cb, root) -> cb.equal(root.get("guildId"), guild.getId())).build(session).uniqueResultOptional();
if (existingSpecification.isPresent()) {
return new GuildContext(guild, new AudioPlayback(player, guild), existingSpecification.get());
} else {
GuildSpecification newSpecification = new GuildSpecification(guild);
session.persist(newSpecification);
commandManager.getCommandContributionContext().query(attribute("restrictedAccess").is(true), CommandContribution.class).getResultStream().forEach(restrictedCommand -> {
AccessConfiguration permissionConfiguration = new AccessConfiguration(restrictedCommand, session);
session.persist(permissionConfiguration);
newSpecification.addAccessConfiguration(permissionConfiguration);
});
session.flush();
GuildContext guildContext = new GuildContext(guild, new AudioPlayback(player, guild), newSpecification.getPk());
handleNewGuild(guild, guildContext);
return guildContext;
}
});
guildContexts.put(guild, createdContext);
return createdContext;
}
use of net.robinfriedli.aiode.entities.AccessConfiguration 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.entities.AccessConfiguration in project aiode by robinfriedli.
the class HelpCommand method showCommandHelp.
private void showCommandHelp() {
getManager().getCommand(getContext(), getCommandInput()).ifPresentOrElse(command -> {
String prefix;
GuildSpecification specification = getContext().getGuildContext().getSpecification();
String setPrefix = specification.getPrefix();
String botName = specification.getBotName();
if (!Strings.isNullOrEmpty(setPrefix)) {
prefix = setPrefix;
} else if (!Strings.isNullOrEmpty(botName)) {
prefix = botName + " ";
} else {
prefix = PrefixProperty.DEFAULT_FALLBACK + " ";
}
char argumentPrefix = ArgumentPrefixProperty.getForCurrentContext().getArgumentPrefix();
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Command " + command.getIdentifier() + ":");
String descriptionFormat = command.getDescription();
String descriptionText = String.format(descriptionFormat, prefix, argumentPrefix);
embedBuilder.setDescription(descriptionText);
Guild guild = getContext().getGuild();
Optional<AccessConfiguration> accessConfiguration = Aiode.get().getSecurityManager().getAccessConfiguration(command.getPermissionTarget(), guild);
if (accessConfiguration.isPresent()) {
String title = "Available to roles: ";
String text;
List<Role> roles = accessConfiguration.get().getRoles(guild);
if (!roles.isEmpty()) {
text = StringList.create(roles, Role::getName).toSeparatedString(", ");
} else {
text = "Guild owner and administrator roles only";
}
embedBuilder.addField(title, text, false);
}
ArgumentController argumentController = command.getArgumentController();
if (argumentController.hasArguments()) {
embedBuilder.addField("__Arguments__", "Keywords that alter the command behavior or define a search scope.", false);
argumentController.getArguments().values().stream().sorted(Comparator.comparing(CommandArgument::getIdentifier)).forEach(argument -> embedBuilder.addField(argumentPrefix + argument.getIdentifier(), String.format(argument.getDescription(), prefix, argumentPrefix), false));
}
List<XmlElement> examples = command.getCommandContribution().query(tagName("example")).collect();
if (!examples.isEmpty()) {
embedBuilder.addField("__Examples__", "Practical usage examples for this command.", false);
for (XmlElement example : examples) {
String exampleText = String.format(example.getTextContent(), prefix, argumentPrefix);
String titleText = String.format(example.getAttribute("title").getValue(), prefix, argumentPrefix);
embedBuilder.addField(titleText, exampleText, false);
}
}
sendMessage(embedBuilder);
}, () -> {
throw new InvalidCommandException(String.format("No command found for '%s'", getCommandInput()));
});
}
Aggregations