From fed06411874f3bf14da6919db2dd5e9c001d5afd Mon Sep 17 00:00:00 2001 From: Justin Aquadro Date: Wed, 10 Feb 2016 02:11:09 -0500 Subject: [PATCH] Add security provider system --- .../api/security/ISecurityProvider.java | 9 +++++++ .../api/storage/attribute/IProtectable.java | 8 ++++++ .../storagedrawers/block/BlockController.java | 6 ++--- .../storagedrawers/block/BlockDrawers.java | 5 ++-- .../block/tile/TileEntityController.java | 27 +++++++++---------- .../block/tile/TileEntityDrawers.java | 6 +++++ .../storagedrawers/integration/Waila.java | 10 +++---- .../storagedrawers/item/ItemTape.java | 5 ++-- .../security/DefaultSecurityProvider.java | 16 +++++++++++ .../security/SecurityManager.java | 21 +++++++++++++++ 10 files changed, 86 insertions(+), 27 deletions(-) create mode 100644 src/com/jaquadro/minecraft/storagedrawers/api/security/ISecurityProvider.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java create mode 100644 src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/security/ISecurityProvider.java b/src/com/jaquadro/minecraft/storagedrawers/api/security/ISecurityProvider.java new file mode 100644 index 0000000..119c8bb --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/api/security/ISecurityProvider.java @@ -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); +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IProtectable.java b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IProtectable.java index 3a38009..845f4d0 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IProtectable.java +++ b/src/com/jaquadro/minecraft/storagedrawers/api/storage/attribute/IProtectable.java @@ -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 (); } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java index 4bc29e5..3dfc0dc 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockController.java @@ -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; } } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java index 09cc1b8..2d02e45 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/BlockDrawers.java @@ -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); diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java index 1d12fe8..99c4c10 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityController.java @@ -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 attributes, LockAttribute key, UUID owner) { + public void toggleLock (EnumSet 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; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java index f650489..9d67794 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java +++ b/src/com/jaquadro/minecraft/storagedrawers/block/tile/TileEntityDrawers.java @@ -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; } diff --git a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java index 42131f1..f1782ba 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java +++ b/src/com/jaquadro/minecraft/storagedrawers/integration/Waila.java @@ -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 getWailaBody (ItemStack itemStack, List 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(); diff --git a/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java b/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java index 46b69f0..1145666 100644 --- a/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java +++ b/src/com/jaquadro/minecraft/storagedrawers/item/ItemTape.java @@ -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) { diff --git a/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java b/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java new file mode 100644 index 0000000..6bea448 --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/security/DefaultSecurityProvider.java @@ -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()); + } +} diff --git a/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java b/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java new file mode 100644 index 0000000..78f29dd --- /dev/null +++ b/src/com/jaquadro/minecraft/storagedrawers/security/SecurityManager.java @@ -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); + } +}