Worked on new access system

This commit is contained in:
DarkGuardsman 2013-10-20 12:38:01 -04:00
parent cbe12e64db
commit 0d87f91c70
7 changed files with 54 additions and 283 deletions

View file

@ -34,9 +34,10 @@ public class AccessUser extends User
return this.group;
}
public void setGroup(AccessGroup group)
public AccessUser setGroup(AccessGroup group)
{
this.group = group;
return this;
}
public boolean hasNode(String node)

View file

@ -2,36 +2,36 @@ package dark.api.access;
import java.util.List;
/** Used by any object that needs to restrict access to it by a set of usernames
/** Used by any object that needs to restrict access to it by a set of users or groups. Make sure to
* always use the default groups(user,admin,owner) so that things work smoothly.
*
* @author DarkGuardsman */
public interface ISpecialAccess
{
/** Gets the player's access level on the machine he is using
*
* @return access level of the player, make sure to return no access if the player doesn't have
* any */
/** Gets the user access instance */
public AccessUser getUserAccess(String username);
/** gets the access list for the machine
*
* @return hasMap of players and there access levels */
/** gets the user access list for the machine */
public List<AccessUser> getUsers();
/** sets the players access level in the access map
*
* @param player
* @return true if the level was set false if something went wrong */
/** sets the players access level in the access map. Make sure to remove the old user first. This
* can also be used to remove users if group is set to null. */
public boolean setUserAccess(String username, AccessGroup group, boolean save);
/** Removes the user from the access list
*
* @param username
* @return */
public boolean removeUserAccess(String username);
/** Sets the players access by using a completed AccessUser instance. Make sure to set its group
* if there is none. As well remove the old user first. */
public boolean setUserAccess(AccessUser user, AccessGroup group);
/** Get a group by name */
public AccessGroup getGroup(String name);
/** Get the master owner group */
public AccessGroup getOwnerGroup();
/** Get all groups linked this */
public List<AccessGroup> getGroups();
/** Add a group to the group list */
public void addGroup(AccessGroup group);
}

View file

@ -1,9 +0,0 @@
package dark.core.prefab.access;
public class GlobalAccess
{
public GlobalAccess(String name)
{
}
}

View file

@ -1,35 +0,0 @@
package dark.core.prefab.access;
import net.minecraftforge.common.MinecraftForge;
import cpw.mods.fml.common.Mod.ServerStarting;
import cpw.mods.fml.common.event.FMLServerStartingEvent;
public class GlobalAccessLoader
{
public static boolean isInitialized = false;
public static GlobalAccessLoader intance = new GlobalAccessLoader();
/** Name of the save file **/
public static final String SAVE_NAME = "Global_Access_List";
public void initiate()
{
if (!isInitialized)
{
MinecraftForge.EVENT_BUS.register(this);
//SaveManager.intance.registerNbtSave(this);
isInitialized = true;
}
}
@ServerStarting
public void serverStarting(FMLServerStartingEvent event)
{
if (!GlobalAccessManager.hasLoaded)
{
GlobalAccessManager.getMasterSaveFile();
}
}
}

View file

@ -1,215 +0,0 @@
package dark.core.prefab.access;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.nbt.NBTTagCompound;
import dark.api.access.AccessUser;
import dark.core.prefab.helpers.NBTFileHelper;
public class GlobalAccessManager
{
/** Hash map of loaded lists **/
private static Map<String, List<AccessUser>> globalUserLists = new HashMap<String, List<AccessUser>>();
/** Master save NBT that gets saved **/
private static NBTTagCompound masterSaveNbt = new NBTTagCompound();
/** Used to check to see if file is in the process of being loaded **/
public static boolean loading = false;
/** Used to check to see if file was loaded at least once **/
public static boolean hasLoaded = false;
/** Used to check to see if file was changed and needs saved **/
public static boolean needsSaving = false;
/** Gets or creates a userAccess list to be used for any reason
*
* @param name - name of the access list being created or loaded
* @param owner - the player's name to be used to create a new list
* @return - UserAccess list */
public static List<UserAccess> getOrCreateList(String name, String owner)
{
if (name.toCharArray().length < 5 || owner.isEmpty() || name.startsWith("Default#"))
{
return null;
}
List<UserAccess> list = getList(name);
if (list == null)
{
list = createList(name, owner);
}
return list;
}
/** gets all the access list by name the user can edit */
public static List<String> getUsersLists(String username)
{
List<String> lists = new ArrayList<String>();
Iterator<Entry<String, List<UserAccess>>> it = GlobalAccessManager.globalUserLists.entrySet().iterator();
while (it.hasNext())
{
Entry<String, List<UserAccess>> entry = it.next();
List<UserAccess> list = entry.getValue();
for (UserAccess access : list)
{
if (access.username.equalsIgnoreCase(username) && access.level.ordinal() >= AccessLevel.ADMIN.ordinal())
{
lists.add(entry.getKey());
break;
}
}
}
return lists;
}
/** creates a new user access list
*
* @param name
* @param owner
* @return */
public static List<UserAccess> createList(String name, String owner)
{
/*** Creates a new List if one doesn't exist ***/
List<UserAccess> list = new ArrayList<UserAccess>();
list.add(new UserAccess(owner, AccessLevel.OWNER, true));
globalUserLists.put(name, list);
saveList(name, list);
needsSaving = true;
return list;
}
/** Loads up a UserAccess List
*
* @param name - name of the list
* @return - the list */
public static List<UserAccess> getList(String name)
{
if (globalUserLists.containsKey(name))
{
/*** Get the list if its already loaded up ***/
return globalUserLists.get(name);
}
else
{
/*** Loads the saved list if it exists ***/
List<UserAccess> list = loadList(name);
if (list != null)
{
globalUserLists.put(name, list);
}
return list;
}
}
/** adds a user to the global list
*
* @param listName - name of the list
* @param user - user being added as a UserAccess instance
* @return true if added */
public boolean addUser(String listName, UserAccess user)
{
if (user == null)
{
return false;
}
List<UserAccess> userList = GlobalAccessManager.getList(listName);
if (userList != null)
{
if (userList.contains(user))
{
userList = UserAccess.removeUserAccess(user.username, userList);
}
if (userList.add(user))
{
globalUserLists.put(listName, userList);
GlobalAccessManager.saveList(listName, userList);
GlobalAccessManager.needsSaving = true;
return true;
}
}
return false;
}
/** Removes a user from the global list
*
* @param listName - name of the list
* @param user - user being removed
* @return true if removed */
public boolean removeUser(String listName, UserAccess user)
{
if (user == null)
{
return false;
}
List<UserAccess> userList = GlobalAccessManager.getList(listName);
if (userList != null)
{
if (userList.contains(user))
{
userList = UserAccess.removeUserAccess(user.username, userList);
globalUserLists.put(listName, userList);
GlobalAccessManager.saveList(listName, userList);
GlobalAccessManager.needsSaving = true;
return true;
}
}
return false;
}
/** Loads a given Global user list from the master save
*
* @param name - name given to the list for reference
* @return - the list of user access levels to be used */
private static List<UserAccess> loadList(String name)
{
NBTTagCompound masterSave = getMasterSaveFile();
if (masterSave != null && masterSave.hasKey(name))
{
NBTTagCompound accessSave = masterSave.getCompoundTag(name);
return UserAccess.readListFromNBT(accessSave, "Users");
}
return null;
}
/** Saves a given Global user list into the master save
*
* @param name - name to save the list as
* @param list - list to be saved */
private static void saveList(String name, List<UserAccess> list)
{
NBTTagCompound masterSave = getMasterSaveFile();
if (masterSave != null)
{
NBTTagCompound accessSave = masterSave.getCompoundTag(name);
UserAccess.writeListToNBT(accessSave, list);
masterSave.setCompoundTag(name, accessSave);
}
}
/** Loads the master save from the world folder */
public static NBTTagCompound getMasterSaveFile()
{
if (masterSaveNbt.hasNoTags())
{
if (!loading)
{
hasLoaded = true;
loading = true;
NBTFileHelper.loadNBTFile(GlobalAccessLoader.SAVE_NAME);
// TODO save the file
loading = false;
}
}
return masterSaveNbt;
}
}

View file

@ -174,7 +174,7 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
public boolean canUserAccess(String username)
{
return this.getUserAccess(username) != null;
return this.getUserAccess(username) != null || this.getOwnerGroup().getMembers().size() <= 0;
}
@Override
@ -191,13 +191,29 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
@Override
public boolean setUserAccess(String player, AccessGroup g, boolean save)
{
this.removeUserAccess(player);
boolean bool = g.addMemeber(new AccessUser(player).setTempary(save));
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
return bool;
return setUserAccess(new AccessUser(player).setTempary(save), g);
}
@Override
public boolean setUserAccess(AccessUser user, AccessGroup group)
{
boolean bool = false;
if (user != null && user.getName() != null)
{
bool = this.removeUserAccess(user.getName()) && group == null;
if (group != null)
{
bool = group.addMemeber(user);
}
if (bool)
{
this.worldObj.markBlockForUpdate(this.xCoord, this.yCoord, this.zCoord);
}
}
return bool;
}
public boolean removeUserAccess(String player)
{
boolean re = false;
@ -238,6 +254,18 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
}
}
@Override
public AccessGroup getOwnerGroup()
{
return this.getGroup("owner");
}
@Override
public List<AccessGroup> getGroups()
{
return this.groups;
}
@Override
public void readFromNBT(NBTTagCompound nbt)
{
@ -296,4 +324,5 @@ public class TileEntityInv extends TileEntityAdvanced implements IExternalInv, I
}
nbt.setTag("groups", usersTag);
}
}

View file

@ -38,7 +38,7 @@ public class CommandUser implements ITerminalCommand
{
if (args[2] != null)
{
if (turret.removeUserAccess(args[2]))
if (turret.setUserAccess(args[2], null, false))
{
terminal.addToConsole("Removed: " + args[2]);
return true;