Add primary groups

This commit is contained in:
Luck 2016-06-24 18:38:05 +01:00
parent 34ca3b3c25
commit 697b349167
12 changed files with 100 additions and 22 deletions

View File

@ -90,7 +90,10 @@ class VaultHook extends Permission {
@Override
public boolean playerInGroup(String world, String player, String group) {
final User user = plugin.getUserManager().getUser(player);
return user != null && user.getGroupNames().contains(group);
if (user == null) return false;
final Group group1 = plugin.getGroupManager().getGroup(group);
return group1 != null && user.isInGroup(group1);
}
@Override
@ -126,14 +129,13 @@ class VaultHook extends Permission {
@Override
public String[] getPlayerGroups(String world, String player) {
final User user = plugin.getUserManager().getUser(player);
if (user == null) return new String[0];
return user.getGroupNames().toArray(new String[0]);
return (user == null) ? new String[0] : user.getGroupNames().toArray(new String[0]);
}
@Override
public String getPrimaryGroup(String world, String player) {
throw new UnsupportedOperationException();
final User user = plugin.getUserManager().getUser(player);
return (user == null) ? null : user.getPrimaryGroup();
}
@Override

View File

@ -36,6 +36,7 @@ public class CommandManager {
userCommand.registerSubCommand(new UserListNodesCommand());
userCommand.registerSubCommand(new UserRemoveGroupCommand());
userCommand.registerSubCommand(new UserSetPermissionCommand());
userCommand.registerSubCommand(new UserSetPrimaryGroupCommand());
userCommand.registerSubCommand(new UserUnSetPermissionCommand());
GroupMainCommand groupCommand = new GroupMainCommand();

View File

@ -21,6 +21,7 @@ public class UserInfoCommand extends UserSubCommand {
prefix + "&d-> &eUUID: &6" + user.getUuid() + "\n" +
prefix + "&d-> &eStatus: " + plugin.getPlayerStatus(user.getUuid()) + "\n" +
prefix + "&d-> &eGroups: &6" + Util.listToCommaSep(user.getGroupNames()) + "\n" +
prefix + "&d-> &ePrimary Group: &6" + user.getPrimaryGroup() + "\n" +
prefix + "&d-> &ePermissions: &6" + (user.getNodes().keySet().size() - user.getGroupNames().size()) + "\n" +
prefix + "&d-> &bUse &a/perms user " + user.getName() + " listnodes &bto see all permissions.";

View File

@ -33,6 +33,11 @@ public class UserRemoveGroupCommand extends UserSubCommand {
return;
}
if (server.equalsIgnoreCase("global") && user.getPrimaryGroup().equalsIgnoreCase(group1.getName())) {
Util.sendPluginMessage(sender, "You cannot remove a user from their primary group.");
return;
}
try {
user.removeGroup(group1, server);
Util.sendPluginMessage(sender, "&b" + user.getName() + "&a was removed from group &b" + group + "&a on server &b" + server + "&a.");

View File

@ -0,0 +1,46 @@
package me.lucko.luckperms.commands.user.subcommands;
import me.lucko.luckperms.LuckPermsPlugin;
import me.lucko.luckperms.commands.Sender;
import me.lucko.luckperms.commands.Util;
import me.lucko.luckperms.commands.user.UserSubCommand;
import me.lucko.luckperms.groups.Group;
import me.lucko.luckperms.users.User;
import java.util.List;
public class UserSetPrimaryGroupCommand extends UserSubCommand {
public UserSetPrimaryGroupCommand() {
super("setprimarygroup", "Sets a users primary group",
"/perms user <user> setprimarygroup <group>", "luckperms.user.setprimarygroup");
}
@Override
protected void execute(LuckPermsPlugin plugin, Sender sender, User user, List<String> args) {
Group group = plugin.getGroupManager().getGroup(args.get(0).toLowerCase());
if (group == null) {
Util.sendPluginMessage(sender, "That group does not exist!");
return;
}
if (user.getPrimaryGroup().equalsIgnoreCase(group.getName())) {
Util.sendPluginMessage(sender, "The user already has this group set as their primary group.");
return;
}
if (!user.isInGroup(group)) {
Util.sendPluginMessage(sender, "The user must be a member of the group first! Use &4/perms user <user> addgroup <group>");
return;
}
user.setPrimaryGroup(group.getName());
Util.sendPluginMessage(sender, "&b" + user.getName() + "&a's primary group was set to &b" + group.getName() + "&a.");
saveUser(user, sender, plugin);
}
@Override
public boolean isArgLengthInvalid(int argLength) {
return argLength == 0;
}
}

View File

@ -120,9 +120,6 @@ public class FlatfileDatastore extends Datastore {
@Override
public boolean loadOrCreateUser(UUID uuid, String username) {
User user = plugin.getUserManager().makeUser(uuid, username);
try {
user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true);
} catch (ObjectAlreadyHasException ignored) {}
File userFile = new File(usersDir, uuid.toString() + ".json");
if (!userFile.exists()) {
@ -133,10 +130,17 @@ public class FlatfileDatastore extends Datastore {
return false;
}
// Setup the new user with default values
try {
user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true);
} catch (ObjectAlreadyHasException ignored) {}
user.setPrimaryGroup(plugin.getConfiguration().getDefaultGroupName());
boolean success = doWrite(userFile, writer -> {
writer.beginObject();
writer.name("uuid").value(user.getUuid().toString());
writer.name("name").value(user.getName());
writer.name("primaryGroup").value(user.getPrimaryGroup());
writer.name("perms");
writer.beginObject();
for (Map.Entry<String, Boolean> e : user.getNodes().entrySet()) {
@ -156,6 +160,8 @@ public class FlatfileDatastore extends Datastore {
reader.nextString(); // uuid
reader.nextName(); // name record
reader.nextString(); // name
reader.nextName(); // primaryGroup record
reader.nextString(); // primaryGroup
reader.nextName(); //perms
reader.beginObject();
while (reader.hasNext()) {
@ -189,7 +195,9 @@ public class FlatfileDatastore extends Datastore {
reader.nextString(); // uuid
reader.nextName(); // name record
user.setName(reader.nextString()); // name
reader.nextName(); //perms
reader.nextName(); // primaryGroup record
user.setPrimaryGroup(reader.nextString()); // primaryGroup
reader.nextName(); // perms record
reader.beginObject();
while (reader.hasNext()) {
String node = reader.nextName();
@ -223,6 +231,7 @@ public class FlatfileDatastore extends Datastore {
writer.beginObject();
writer.name("uuid").value(user.getUuid().toString());
writer.name("name").value(user.getName());
writer.name("primaryGroup").value(user.getPrimaryGroup());
writer.name("perms");
writer.beginObject();
for (Map.Entry<String, Boolean> e : user.getNodes().entrySet()) {

View File

@ -10,7 +10,7 @@ import java.sql.SQLException;
public class MySQLDatastore extends SQLDatastore {
private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;";
private final MySQLConfiguration configuration;

View File

@ -27,9 +27,9 @@ abstract class SQLDatastore extends Datastore {
private static final Type NM_TYPE = new TypeToken<Map<String, Boolean>>(){}.getType();
private static final String USER_INSERT = "INSERT INTO lp_users VALUES(?, ?, ?)";
private static final String USER_INSERT = "INSERT INTO lp_users VALUES(?, ?, ?, ?)";
private static final String USER_SELECT = "SELECT * FROM lp_users WHERE uuid=?";
private static final String USER_UPDATE = "UPDATE lp_users SET name=?, perms=? WHERE uuid=?";
private static final String USER_UPDATE = "UPDATE lp_users SET name=?, primary_group = ?, perms=? WHERE uuid=?";
private static final String GROUP_INSERT = "INSERT INTO lp_groups VALUES(?, ?)";
private static final String GROUP_SELECT = "SELECT perms FROM lp_groups WHERE name=?";
@ -139,8 +139,9 @@ abstract class SQLDatastore extends Datastore {
@Override
boolean onResult(ResultSet resultSet) throws SQLException {
if (resultSet.next()) {
user.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
user.setName(resultSet.getString("name"));
user.getNodes().putAll(gson.fromJson(resultSet.getString("perms"), NM_TYPE));
user.setPrimaryGroup(resultSet.getString("primary_group"));
return true;
}
return false;
@ -155,10 +156,6 @@ abstract class SQLDatastore extends Datastore {
@Override
public boolean loadOrCreateUser(UUID uuid, String username) {
User user = plugin.getUserManager().makeUser(uuid, username);
try {
user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true);
} catch (ObjectAlreadyHasException ignored) {}
boolean success = runQuery(new QueryRS(USER_SELECT) {
@Override
void onRun(PreparedStatement preparedStatement) throws SQLException {
@ -169,12 +166,20 @@ abstract class SQLDatastore extends Datastore {
boolean onResult(ResultSet resultSet) throws SQLException {
boolean success = true;
if (!resultSet.next()) {
// Setup the new user with default values
try {
user.setPermission(plugin.getConfiguration().getDefaultGroupNode(), true);
} catch (ObjectAlreadyHasException ignored) {}
user.setPrimaryGroup(plugin.getConfiguration().getDefaultGroupName());
success = runQuery(new QueryPS(USER_INSERT) {
@Override
void onRun(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, user.getUuid().toString());
preparedStatement.setString(2, user.getName());
preparedStatement.setString(3, gson.toJson(user.getNodes()));
preparedStatement.setString(3, user.getPrimaryGroup());
preparedStatement.setString(4, gson.toJson(user.getNodes()));
}
});
} else {
@ -196,7 +201,8 @@ abstract class SQLDatastore extends Datastore {
void onRun(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, gson.toJson(user.getNodes()));
preparedStatement.setString(3, user.getUuid().toString());
preparedStatement.setString(3, user.getPrimaryGroup());
preparedStatement.setString(4, user.getUuid().toString());
}
});
return success;

View File

@ -10,7 +10,7 @@ import java.sql.SQLException;
public class SQLiteDatastore extends SQLDatastore {
private static final String CREATETABLE_UUID = "CREATE TABLE IF NOT EXISTS `lp_uuid` (`name` VARCHAR(16) NOT NULL, `uuid` VARCHAR(36) NOT NULL, PRIMARY KEY (`name`));";
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`));";
private static final String CREATETABLE_USERS = "CREATE TABLE IF NOT EXISTS `lp_users` (`uuid` VARCHAR(36) NOT NULL, `name` VARCHAR(16) NOT NULL, `primary_group` VARCHAR(36) NOT NULL, `perms` TEXT NOT NULL, PRIMARY KEY (`uuid`));";
private static final String CREATETABLE_GROUPS = "CREATE TABLE IF NOT EXISTS `lp_groups` (`name` VARCHAR(36) NOT NULL, `perms` TEXT NULL, PRIMARY KEY (`name`));";
private final File file;

View File

@ -42,7 +42,7 @@ public class GroupManager {
* @param group The group to update or set
*/
public void updateOrSetGroup(Group group) {
if (!groups.containsKey(group.getName())) {
if (!isLoaded(group.getName())) {
// The group isn't already loaded
groups.put(group.getName(), group);
} else {

View File

@ -26,6 +26,13 @@ public abstract class User extends PermissionObject {
@Setter
private String name;
/**
* The users primary group
*/
@Getter
@Setter
private String primaryGroup = null;
User(UUID uuid, LuckPermsPlugin plugin) {
super(plugin, uuid.toString());
this.uuid = uuid;

View File

@ -57,7 +57,7 @@ public abstract class UserManager {
* @param user The user to update or set
*/
public void updateOrSetUser(User user) {
if (!users.containsKey(user.getUuid())) {
if (!isLoaded(user.getUuid())) {
// The user isn't already loaded, so we can just add
users.put(user.getUuid(), user);
// They're probably not online, but in case they are...
@ -65,6 +65,7 @@ public abstract class UserManager {
} else {
// Override the user's current loaded nodes, and force a refresh
users.get(user.getUuid()).setNodes(user.getNodes());
users.get(user.getUuid()).setPrimaryGroup(user.getPrimaryGroup());
users.get(user.getUuid()).refreshPermissions();
}
}