LuckPerms

LuckPerms

41.4k Downloads

Unable to replace group subject permissions in bulk

bloodmc opened this issue ยท 3 comments

commented

I'm working on a migrator to convert all legacy GP permission data to the new 5.0.0 system. It does not work properly when attempting to migrate all loaded group subjects.

The code is as follows

migrateSubjectPermissions(GLOBAL_SUBJECT);
        for (Subject group : permissionService.getGroupSubjects().getLoadedSubjects()) {
            GriefPreventionPlugin.instance.getLogger().info("Scanning group '" + group.getIdentifier() + "' for legacy permissions...");
            migrateSubjectPermissions(group);
            GriefPreventionPlugin.instance.getLogger().info("Finished scan on group '" + group.getIdentifier() + "'.");
        }

private static void migrateSubjectPermissions(Subject subject) {
        for (Map.Entry<Set<Context>, Map<String, Boolean>> mapEntry : subject.getSubjectData().getAllPermissions().entrySet()) {
            final Set<Context> contextSet = mapEntry.getKey();
            boolean hasSource = false;
            for (String key : mapEntry.getValue().keySet()) {
                if (key.contains(".source.")) {
                    final String[] parts = key.split("\\.source\\.");
                    if (parts.length > 0) {
                        hasSource = true;
                        break;
                    }
                }
            }
            if (hasSource) {
                Iterator<Map.Entry<String, Boolean>> iterator = mapEntry.getValue().entrySet().iterator();
                while (iterator.hasNext()) {
                    final Map.Entry<String, Boolean> entry = iterator.next();
                    String currentPermission = entry.getKey();
                    final String[] parts = currentPermission.split("\\.source\\.");
                    if (parts.length > 1) {
                        final String newPermission = parts[0];
                        GriefPreventionPlugin.instance.getLogger().info("Detected legacy permission '" + currentPermission + "'. Migrating...");
                        final String sourceId = parts[1].replace(".", ":");
                        Set<Context> newContextSet = new HashSet<>(contextSet);
                        if (newPermission.contains("interact") && ITEM_IDS.contains(sourceId)) {
                            newContextSet.add(new Context("used_item", sourceId));
                            GriefPreventionPlugin.instance.getLogger().info("Created new used_item context for '" + sourceId + "'.");
                        } else {
                            newContextSet.add(new Context("source", sourceId));
                            GriefPreventionPlugin.instance.getLogger().info("Created new source context for '" + sourceId + "'.");
                        }
                        // remove old perm
                        subject.getSubjectData().setPermission(contextSet, currentPermission, Tristate.UNDEFINED);
                        GriefPreventionPlugin.instance.getLogger().info("Removed legacy permission '" + currentPermission + "'.");
                        // add migrated perm with new source context
                        subject.getSubjectData().setPermission(newContextSet, newPermission, Tristate.fromBoolean(entry.getValue()));
                        GriefPreventionPlugin.instance.getLogger().info("Set new permission '" + newPermission + "' with contexts " + newContextSet);
                        GriefPreventionPlugin.instance.getLogger().info("Successfully migrated permission " + currentPermission + " to " + newPermission + " with context " + sourceId + "\n");
                    }
                }
            }
        }
    }

The migration fails to work properly whenever group subjects are used. If I only run the migrator on GP's default user it works fine.

commented

Does it print Scanning group for each?

commented

Yes, but the odd issue is GLOBAL_SUBJECT doesn't migrate properly if I attempt to do groups directly after. If I ONLY migrate GLOBAL_SUBJECT, it works fine. I messaged you on discord with more information including test files.

commented