Search in sources :

Example 1 with CachedGroup

use of com.github.gustav9797.PowerfulPermsAPI.CachedGroup in project LuckPerms by lucko.

the class MigrationPowerfulPerms method applyGroup.

private void applyGroup(PermissionManager pm, PermissionHolder holder, CachedGroup g, String server) {
    Group group = pm.getGroup(g.getGroupId());
    String node = NodeFactory.groupNode(MigrationUtils.standardizeName(group.getName()));
    long expireAt = 0L;
    if (g.willExpire()) {
        expireAt = g.getExpirationDate().getTime() / 1000L;
    }
    Node.Builder nb = NodeFactory.builder(node);
    if (expireAt != 0) {
        nb.setExpiry(expireAt);
    }
    if (server != null) {
        nb.setServer(server);
    }
    holder.setPermission(nb.build());
}
Also used : CachedGroup(com.github.gustav9797.PowerfulPermsAPI.CachedGroup) Group(com.github.gustav9797.PowerfulPermsAPI.Group) Node(me.lucko.luckperms.api.Node)

Example 2 with CachedGroup

use of com.github.gustav9797.PowerfulPermsAPI.CachedGroup in project LuckPerms by lucko.

the class MigrationPowerfulPerms method execute.

@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Object o, List<String> args, String label) {
    ProgressLogger log = new ProgressLogger("PowerfulPerms");
    log.addListener(plugin.getConsoleSender());
    log.addListener(sender);
    log.log("Starting.");
    if (!Bukkit.getPluginManager().isPluginEnabled("PowerfulPerms")) {
        log.logError("Plugin not loaded.");
        return CommandResult.STATE_ERROR;
    }
    String method = plugin.getConfiguration().get(ConfigKeys.STORAGE_METHOD);
    StorageType type = StorageType.parse(method);
    if (type == null || type != StorageType.MYSQL) {
        // We need to load the Hikari/MySQL stuff.
        plugin.getDependencyManager().loadStorageDependencies(ImmutableSet.of(StorageType.MYSQL));
    }
    String address = args.get(0);
    String database = args.get(1);
    String username = args.get(2);
    String password = args.get(3);
    String dbTable = args.get(4);
    // Find a list of UUIDs
    log.log("Getting a list of UUIDs to migrate.");
    Set<UUID> uuids = new HashSet<>();
    try (HikariSupplier hikari = new HikariSupplier(address, database, username, password)) {
        hikari.setup("powerfulperms-migrator-pool");
        try (Connection c = hikari.getConnection()) {
            DatabaseMetaData meta = c.getMetaData();
            try (ResultSet rs = meta.getTables(null, null, dbTable, null)) {
                if (!rs.next()) {
                    log.log("Error - Couldn't find table.");
                    return CommandResult.FAILURE;
                }
            }
        }
        try (Connection c = hikari.getConnection()) {
            try (PreparedStatement ps = c.prepareStatement("SELECT COLUMN_NAME, COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME=?")) {
                ps.setString(1, dbTable);
                try (ResultSet rs = ps.executeQuery()) {
                    log.log("Found table: " + dbTable);
                    while (rs.next()) {
                        log.log("" + rs.getString("COLUMN_NAME") + " - " + rs.getString("COLUMN_TYPE"));
                    }
                }
            }
            try (PreparedStatement ps = c.prepareStatement("SELECT `uuid` FROM " + dbTable)) {
                try (ResultSet rs = ps.executeQuery()) {
                    while (rs.next()) {
                        uuids.add(UUID.fromString(rs.getString("uuid")));
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (uuids.isEmpty()) {
        log.logError("Unable to find any UUIDs to migrate.");
        return CommandResult.FAILURE;
    }
    log.log("Found " + uuids.size() + " uuids. Starting migration.");
    PowerfulPermsPlugin ppPlugin = (PowerfulPermsPlugin) Bukkit.getPluginManager().getPlugin("PowerfulPerms");
    PermissionManager pm = ppPlugin.getPermissionManager();
    Collection<Group> groups = pm.getGroups().values();
    AtomicInteger maxWeight = new AtomicInteger(0);
    // Groups first.
    log.log("Starting group migration.");
    AtomicInteger groupCount = new AtomicInteger(0);
    Iterators.iterate(groups, g -> {
        maxWeight.set(Math.max(maxWeight.get(), g.getRank()));
        String groupName = MigrationUtils.standardizeName(g.getName());
        me.lucko.luckperms.common.model.Group group = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.INTERNAL).join();
        MigrationUtils.setGroupWeight(group, g.getRank());
        for (Permission p : g.getOwnPermissions()) {
            applyPerm(group, p);
        }
        for (Group parent : g.getParents()) {
            group.setPermission(NodeFactory.buildGroupNode(parent.getName().toLowerCase()).build());
        }
        // server --> prefix afaik
        for (Map.Entry<String, String> prefix : g.getPrefixes().entrySet()) {
            if (prefix.getValue().isEmpty())
                continue;
            String server = prefix.getKey().toLowerCase();
            if (prefix.getKey().equals("*") || prefix.getKey().equals("all")) {
                server = null;
            }
            if (server != null) {
                group.setPermission(NodeFactory.buildPrefixNode(g.getRank(), prefix.getValue()).setServer(server).build());
            } else {
                group.setPermission(NodeFactory.buildPrefixNode(g.getRank(), prefix.getValue()).build());
            }
        }
        for (Map.Entry<String, String> suffix : g.getSuffixes().entrySet()) {
            if (suffix.getValue().isEmpty())
                continue;
            String server = suffix.getKey().toLowerCase();
            if (suffix.getKey().equals("*") || suffix.getKey().equals("all")) {
                server = null;
            }
            if (server != null) {
                group.setPermission(NodeFactory.buildSuffixNode(g.getRank(), suffix.getValue()).setServer(server).build());
            } else {
                group.setPermission(NodeFactory.buildSuffixNode(g.getRank(), suffix.getValue()).build());
            }
        }
        plugin.getStorage().saveGroup(group);
        log.logAllProgress("Migrated {} groups so far.", groupCount.incrementAndGet());
    });
    log.log("Migrated " + groupCount.get() + " groups");
    // Migrate all users
    log.log("Starting user migration.");
    AtomicInteger userCount = new AtomicInteger(0);
    // Increment the max weight from the group migrations. All user meta should override.
    maxWeight.addAndGet(5);
    // Migrate all users and their groups
    Iterators.iterate(uuids, uuid -> {
        // Create a LuckPerms user for the UUID
        User user = plugin.getStorage().loadUser(uuid, null).join();
        List<Permission> permissions = joinFuture(pm.getPlayerOwnPermissions(uuid));
        for (Permission p : permissions) {
            applyPerm(user, p);
        }
        // server --> list of groups
        Map<String, List<CachedGroup>> parents = joinFuture(pm.getPlayerOwnGroups(uuid));
        for (Map.Entry<String, List<CachedGroup>> parent : parents.entrySet()) {
            String server = parent.getKey().toLowerCase();
            if (parent.getKey().equals("*") || parent.getKey().equals("all")) {
                server = null;
            }
            for (CachedGroup group : parent.getValue()) {
                applyGroup(pm, user, group, server);
            }
        }
        String prefix = joinFuture(pm.getPlayerOwnPrefix(uuid));
        String suffix = joinFuture(pm.getPlayerOwnSuffix(uuid));
        if (prefix != null && !prefix.isEmpty()) {
            user.setPermission(NodeFactory.buildPrefixNode(maxWeight.get(), prefix).build());
        }
        if (suffix != null && !suffix.isEmpty()) {
            user.setPermission(NodeFactory.buildSuffixNode(maxWeight.get(), suffix).build());
        }
        Group primaryGroup = joinFuture(pm.getPlayerPrimaryGroup(uuid));
        if (primaryGroup != null && primaryGroup.getName() != null) {
            String primary = primaryGroup.getName().toLowerCase();
            if (!primary.equals(NodeFactory.DEFAULT_GROUP_NAME)) {
                user.setPermission(NodeFactory.buildGroupNode(primary).build());
                user.getPrimaryGroup().setStoredValue(primary);
            }
        }
        plugin.getUserManager().cleanup(user);
        plugin.getStorage().saveUser(user);
        log.logProgress("Migrated {} users so far.", userCount.incrementAndGet());
    });
    log.log("Migrated " + userCount.get() + " users.");
    log.log("Success! Migration complete.");
    return CommandResult.SUCCESS;
}
Also used : CachedGroup(com.github.gustav9797.PowerfulPermsAPI.CachedGroup) Group(com.github.gustav9797.PowerfulPermsAPI.Group) User(me.lucko.luckperms.common.model.User) ProgressLogger(me.lucko.luckperms.common.logging.ProgressLogger) ResultSet(java.sql.ResultSet) Permission(com.github.gustav9797.PowerfulPermsAPI.Permission) List(java.util.List) UUID(java.util.UUID) HashSet(java.util.HashSet) StorageType(me.lucko.luckperms.common.storage.StorageType) PermissionManager(com.github.gustav9797.PowerfulPermsAPI.PermissionManager) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CachedGroup(com.github.gustav9797.PowerfulPermsAPI.CachedGroup) DatabaseMetaData(java.sql.DatabaseMetaData) SQLException(java.sql.SQLException) ExecutionException(java.util.concurrent.ExecutionException) PowerfulPermsPlugin(com.github.gustav9797.PowerfulPermsAPI.PowerfulPermsPlugin) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map)

Aggregations

CachedGroup (com.github.gustav9797.PowerfulPermsAPI.CachedGroup)2 Group (com.github.gustav9797.PowerfulPermsAPI.Group)2 Permission (com.github.gustav9797.PowerfulPermsAPI.Permission)1 PermissionManager (com.github.gustav9797.PowerfulPermsAPI.PermissionManager)1 PowerfulPermsPlugin (com.github.gustav9797.PowerfulPermsAPI.PowerfulPermsPlugin)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 UUID (java.util.UUID)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Node (me.lucko.luckperms.api.Node)1 ProgressLogger (me.lucko.luckperms.common.logging.ProgressLogger)1 User (me.lucko.luckperms.common.model.User)1 StorageType (me.lucko.luckperms.common.storage.StorageType)1