Add security provider system

This commit is contained in:
Justin Aquadro committed 2016-02-10 02:11:09 -05:00
1 parent 6b34cefaf2
commit fed0641187
10 files changed
+86 -27

No files matched your search

@@ -0,0 +1,9 @@
package com.jaquadro.minecraft.storagedrawers.api.security;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.mojang.authlib.GameProfile;
public interface ISecurityProvider
{
boolean hasAccess (GameProfile profile, IProtectable target);
}
@@ -1,5 +1,7 @@
package com.jaquadro.minecraft.storagedrawers.api.storage.attribute;
import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider;
import java.util.UUID;
public interface IProtectable
@@ -14,4 +16,10 @@ public interface IProtectable
* @return false if the operation is not supported, true otherwise.
*/
boolean setOwner (UUID owner);
/**
* Gets the provider managing security for the target.
* @return null to use the default provider, which enforces strict owner access.
*/
ISecurityProvider getSecurityProvider ();
}
@@ -121,17 +121,17 @@ public class BlockController extends BlockContainer implements INetworked
if (item != null && item.getItem() != null) {
if (item.getItem() == ModItems.shroudKey) {
if (!world.isRemote)
te.toggleShroud(player.getPersistentID());
te.toggleShroud(player.getGameProfile());
return true;
}
else if (item.getItem() == ModItems.upgradeLock) {
if (!world.isRemote)
te.toggleLock(EnumSet.allOf(LockAttribute.class), LockAttribute.LOCK_POPULATED, player.getPersistentID());
te.toggleLock(EnumSet.allOf(LockAttribute.class), LockAttribute.LOCK_POPULATED, player.getGameProfile());
return true;
}
else if (item.getItem() == ModItems.personalKey) {
if (!world.isRemote)
te.toggleProtection(player.getPersistentID());
te.toggleProtection(player.getGameProfile());
return true;
}
}
@@ -16,6 +16,7 @@ import com.jaquadro.minecraft.storagedrawers.core.handlers.GuiHandler;
import com.jaquadro.minecraft.storagedrawers.item.ItemTrim;
import com.jaquadro.minecraft.storagedrawers.network.BlockClickMessage;
import com.jaquadro.minecraft.storagedrawers.security.SecurityManager;
import cpw.mods.fml.common.FMLLog;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@@ -301,7 +302,7 @@ public class BlockDrawers extends BlockContainer implements IExtendedBlockClickH
TileEntityDrawers tileDrawers = getTileEntitySafe(world, x, y, z);
ItemStack item = player.inventory.getCurrentItem();
if (tileDrawers.getOwner() != null && !player.getPersistentID().equals(tileDrawers.getOwner()))
if (!SecurityManager.hasAccess(player.getGameProfile(), tileDrawers))
return false;
if (StorageDrawers.config.cache.debugTrace) {
@@ -449,7 +450,7 @@ public class BlockDrawers extends BlockContainer implements IExtendedBlockClickH
if (tileDrawers.isSealed())
return;
if (tileDrawers.getOwner() != null && !tileDrawers.getOwner().equals(player.getPersistentID()))
if (!SecurityManager.hasAccess(player.getGameProfile(), tileDrawers))
return;
int slot = getDrawerSlot(side, hitX, hitY, hitZ);
@@ -5,9 +5,11 @@ import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory;
import com.jaquadro.minecraft.storagedrawers.api.storage.*;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.*;
import com.jaquadro.minecraft.storagedrawers.block.BlockSlave;
import com.jaquadro.minecraft.storagedrawers.security.SecurityManager;
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaListRegistry;
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaRegistry;
import com.jaquadro.minecraft.storagedrawers.util.UniqueMetaIdentifier;
import com.mojang.authlib.GameProfile;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
@@ -155,7 +157,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
if (!dumpInventory) {
ItemStack currentStack = player.inventory.getCurrentItem();
if (currentStack != null) {
count = insertItems(currentStack, player.getPersistentID());
count = insertItems(currentStack, player.getGameProfile());
if (currentStack.stackSize == 0)
player.inventory.setInventorySlotContents(player.inventory.currentItem, null);
}
@@ -164,7 +166,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
for (int i = 0, n = player.inventory.getSizeInventory(); i < n; i++) {
ItemStack subStack = player.inventory.getStackInSlot(i);
if (subStack != null) {
count += insertItems(subStack, player.getPersistentID());
count += insertItems(subStack, player.getGameProfile());
if (subStack.stackSize == 0)
player.inventory.setInventorySlotContents(i, null);
}
@@ -180,14 +182,13 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
return count;
}
private int insertItems (ItemStack stack, UUID owner) {
private int insertItems (ItemStack stack, GameProfile profile) {
int itemsLeft = stack.stackSize;
for (int slot : enumerateDrawersForInsertion(stack, false)) {
IDrawerGroup group = getGroupForDrawerSlot(slot);
if (group instanceof IProtectable) {
IProtectable protectable = (IProtectable)group;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
if (!SecurityManager.hasAccess(profile, (IProtectable)group))
continue;
}
@@ -231,7 +232,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
return remainder;
}
public void toggleProtection (UUID owner) {
public void toggleProtection (GameProfile profile) {
IProtectable template = null;
UUID state = null;
@@ -241,14 +242,14 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
if (record.storage instanceof IProtectable) {
IProtectable protectable = (IProtectable)record.storage;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
if (!SecurityManager.hasAccess(profile, protectable))
continue;
if (template == null) {
template = protectable;
if (template.getOwner() == null)
state = owner;
state = profile.getId();
else
state = null;
}
@@ -258,7 +259,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
}
}
public void toggleShroud (UUID owner) {
public void toggleShroud (GameProfile profile) {
IShroudable template = null;
boolean state = false;
@@ -267,8 +268,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
continue;
if (record.storage instanceof IProtectable) {
IProtectable protectable = (IProtectable)record.storage;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
if (!SecurityManager.hasAccess(profile, (IProtectable)record.storage))
continue;
}
@@ -291,7 +291,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
}
}
public void toggleLock (EnumSet<LockAttribute> attributes, LockAttribute key, UUID owner) {
public void toggleLock (EnumSet<LockAttribute> attributes, LockAttribute key, GameProfile profile) {
ILockable template = null;
boolean state = false;
@@ -300,8 +300,7 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
continue;
if (record.storage instanceof IProtectable) {
IProtectable protectable = (IProtectable)record.storage;
if (protectable.getOwner() != null && !protectable.getOwner().equals(owner))
if (!SecurityManager.hasAccess(profile, (IProtectable)record.storage))
continue;
}
@@ -2,6 +2,7 @@ package com.jaquadro.minecraft.storagedrawers.block.tile;
import com.jaquadro.minecraft.storagedrawers.StorageDrawers;
import com.jaquadro.minecraft.storagedrawers.api.inventory.IDrawerInventory;
import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroupInteractive;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ILockable;
@@ -268,6 +269,11 @@ public abstract class TileEntityDrawers extends BaseTileEntity implements IDrawe
return true;
}
@Override
public ISecurityProvider getSecurityProvider () {
return null;
}
public boolean shouldHideUpgrades () {
return hideUpgrade;
}
@@ -8,6 +8,7 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.IFractionalDrawer;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
import com.jaquadro.minecraft.storagedrawers.block.BlockDrawers;
import com.jaquadro.minecraft.storagedrawers.block.tile.TileEntityDrawers;
import com.jaquadro.minecraft.storagedrawers.security.SecurityManager;
import cpw.mods.fml.common.event.FMLInterModComms;
import mcp.mobius.waila.api.IWailaConfigHandler;
import mcp.mobius.waila.api.IWailaDataAccessor;
@@ -62,15 +63,14 @@ public class Waila extends IntegrationModule
public List<String> getWailaBody (ItemStack itemStack, List<String> currenttip, IWailaDataAccessor accessor, IWailaConfigHandler config) {
TileEntityDrawers tile = (TileEntityDrawers) accessor.getTileEntity();
if (tile.getOwner() == null || tile.getOwner().equals(Minecraft.getMinecraft().thePlayer.getPersistentID())) {
IDrawerGroup group = tile;
for (int i = 0; i < group.getDrawerCount(); i++) {
if (!group.isDrawerEnabled(i))
if (SecurityManager.hasAccess(Minecraft.getMinecraft().thePlayer.getGameProfile(), tile)) {
for (int i = 0; i < tile.getDrawerCount(); i++) {
if (!tile.isDrawerEnabled(i))
continue;
String name = StatCollector.translateToLocal("storageDrawers.waila.empty");
IDrawer drawer = group.getDrawer(i);
IDrawer drawer = tile.getDrawer(i);
ItemStack stack = drawer.getStoredItemPrototype();
if (stack != null && stack.getItem() != null) {
String stackName = stack.getDisplayName();
@@ -4,6 +4,7 @@ import com.jaquadro.minecraft.storagedrawers.StorageDrawers;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ISealable;
import com.jaquadro.minecraft.storagedrawers.core.ModCreativeTabs;
import com.jaquadro.minecraft.storagedrawers.security.SecurityManager;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.player.EntityPlayer;
@@ -39,10 +40,8 @@ public class ItemTape extends Item
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof IProtectable) {
IProtectable protectable = (IProtectable)tile;
if (protectable.getOwner() != null && !protectable.getOwner().equals(player.getPersistentID()))
if (!SecurityManager.hasAccess(player.getGameProfile(), (IProtectable)tile))
return false;
}
if (tile instanceof ISealable) {
@@ -0,0 +1,16 @@
package com.jaquadro.minecraft.storagedrawers.security;
import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.mojang.authlib.GameProfile;
public class DefaultSecurityProvider implements ISecurityProvider
{
@Override
public boolean hasAccess (GameProfile profile, IProtectable target) {
if (target == null || profile == null)
return false;
return target.getOwner() == null || target.getOwner().equals(profile.getId());
}
}
@@ -0,0 +1,21 @@
package com.jaquadro.minecraft.storagedrawers.security;
import com.jaquadro.minecraft.storagedrawers.api.security.ISecurityProvider;
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IProtectable;
import com.mojang.authlib.GameProfile;
public class SecurityManager
{
private static ISecurityProvider defaultProvider = new DefaultSecurityProvider();
public static boolean hasAccess (GameProfile profile, IProtectable target) {
if (target == null || profile == null)
return false;
ISecurityProvider provider = target.getSecurityProvider();
if (provider == null)
provider = defaultProvider;
return provider.hasAccess(profile, target);
}
}