use of net.robinfriedli.aiode.entities.GrantedRole 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.GrantedRole in project aiode by robinfriedli.
the class GuildManagementListener method onRoleDelete.
@Override
public void onRoleDelete(@Nonnull RoleDeleteEvent event) {
EventHandlerPool.execute(() -> {
Role role = event.getRole();
String roleId = role.getId();
hibernateComponent.consumeSession(session -> {
CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaDelete<GrantedRole> deleteQuery = cb.createCriteriaDelete(GrantedRole.class);
Root<GrantedRole> queryRoot = deleteQuery.from(GrantedRole.class);
deleteQuery.where(cb.equal(queryRoot.get("id"), roleId));
int rowCount = session.createQuery(deleteQuery).executeUpdate();
if (rowCount > 0) {
Guild guild = event.getGuild();
GuildContext guildContext = guildManager.getContextForGuild(guild);
logger.info(String.format("Deleted %s GrantedRole entities upon deletion of role %s on guild %s", rowCount, roleId, guild));
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Deletion of role referenced by your permission configuration");
embedBuilder.setDescription(String.format("The deleted role '%s' was referenced by the permission configuration. Check the current permissions by using the permission command.", role.getName()));
embedBuilder.setColor(ColorSchemeProperty.getColor(guildContext.getSpecification(session)));
TextChannel textChannel = guildManager.getDefaultTextChannelForGuild(guild);
if (textChannel != null) {
messageService.sendTemporary(embedBuilder.build(), textChannel);
}
}
});
});
}
use of net.robinfriedli.aiode.entities.GrantedRole 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.entities.GrantedRole in project aiode by robinfriedli.
the class MigrateGuildSpecificationsTask method migrateSpecification.
private void migrateSpecification(JDA shard, XmlElement specification, Session session) {
String guildId = specification.getAttribute("guildId").getValue();
Guild guild = shard.getGuildById(guildId);
if (guild != null) {
Optional<GuildSpecification> existingGuildSpecification = session.createQuery("from " + GuildSpecification.class.getName() + " where guildId = '" + guild.getId() + "'", GuildSpecification.class).uniqueResultOptional();
if (existingGuildSpecification.isPresent()) {
return;
}
String botifyName = specification.getAttribute("botifyName").getValue();
String guildName = specification.getAttribute("guildName").getValue();
String prefix;
if (specification.hasAttribute("prefix")) {
prefix = specification.getAttribute("prefix").getValue();
} else {
prefix = null;
}
GuildSpecification guildSpecification = new GuildSpecification();
guildSpecification.setGuildName(guildName);
guildSpecification.setGuildId(guildId);
guildSpecification.setBotName(botifyName);
guildSpecification.setPrefix(prefix);
List<XmlElement> accessConfigurations = specification.query(tagName("accessConfiguration")).collect();
for (XmlElement accessConfiguration : accessConfigurations) {
String commandIdentifier = accessConfiguration.getAttribute("commandIdentifier").getValue();
AccessConfiguration ac = new AccessConfiguration(commandIdentifier, session);
String roleIdString = accessConfiguration.getAttribute("roleIds").getValue();
Iterable<String> roleIds = splitter.split(roleIdString);
for (String roleId : roleIds) {
Role role = guild.getRoleById(roleId);
if (role != null) {
GrantedRole grantedRole = new GrantedRole(role);
session.persist(grantedRole);
ac.addRole(grantedRole);
}
}
guildSpecification.addAccessConfiguration(ac);
session.persist(ac);
}
session.persist(guildSpecification);
}
}
use of net.robinfriedli.aiode.entities.GrantedRole 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