Permissions Provider implementation.
parent
5a3ac763eb
commit
82e709c2e2
@ -1,24 +1,98 @@
|
||||
package net.pingex.dcf.permissions;
|
||||
|
||||
import net.pingex.dcf.commands.Command;
|
||||
import net.pingex.dcf.core.Configuration;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.mapdb.Serializer;
|
||||
import sx.blah.discord.handle.obj.IGuild;
|
||||
import sx.blah.discord.handle.obj.IRole;
|
||||
import sx.blah.discord.handle.obj.IUser;
|
||||
import java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* The one and only permissions provider for now.
|
||||
*/
|
||||
public class DefaultPermissionsProvider implements ICommandPermissionsProvider
|
||||
{
|
||||
/**
|
||||
* Datastore instance
|
||||
*/
|
||||
private DB datastore;
|
||||
|
||||
/**
|
||||
* User permissions store
|
||||
*/
|
||||
private Map<String, Boolean> userStore;
|
||||
|
||||
/**
|
||||
* Group permissions store
|
||||
*/
|
||||
private Map<String, Boolean> groupStore;
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(DefaultPermissionsProvider.class);
|
||||
|
||||
public DefaultPermissionsProvider()
|
||||
{
|
||||
LOGGER.info("Building datastore.");
|
||||
|
||||
datastore = DBMaker.fileDB(new File(Configuration.DATA_DIR + "/permissions.db"))
|
||||
.closeOnJvmShutdown()
|
||||
.make();
|
||||
|
||||
userStore = datastore.hashMap("user", Serializer.STRING, Serializer.BOOLEAN).createOrOpen();
|
||||
groupStore = datastore.hashMap("group", Serializer.STRING, Serializer.BOOLEAN).createOrOpen();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean validateUser(IGuild guild, IUser user, Command command)
|
||||
{
|
||||
return null;
|
||||
return userStore.get(generateUserKey(guild, user, command));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean validateGroup(IRole role, Command command)
|
||||
{
|
||||
return null;
|
||||
return groupStore.get(generateGroupKey(role, command));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserPermission(IGuild guild, IUser user, Command command, Boolean value)
|
||||
{
|
||||
if(value == null) // Unset
|
||||
{
|
||||
userStore.remove(generateUserKey(guild, user, command));
|
||||
}
|
||||
else
|
||||
{
|
||||
userStore.put(generateUserKey(guild, user, command), value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGroupPermissions(IRole role, Command command, Boolean value)
|
||||
{
|
||||
if(value == null) // Unset
|
||||
{
|
||||
groupStore.remove(generateGroupKey(role, command));
|
||||
}
|
||||
else
|
||||
{
|
||||
groupStore.put(generateGroupKey(role, command), value);
|
||||
}
|
||||
}
|
||||
|
||||
private String generateUserKey(IGuild guild, IUser user, Command command)
|
||||
{
|
||||
return (guild != null ? guild.getID() : "*") + "/" + user.getID() + "/" + command.getName();
|
||||
}
|
||||
|
||||
private String generateGroupKey(IRole role, Command command)
|
||||
{
|
||||
return role.getID() + "/" + command.getName();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue