2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
2015-05-28 20:01:21 +02:00
|
|
|
* Copyright (c) 2013 - 2015, AlgorithmX2, All rights reserved.
|
2014-11-14 12:02:52 +01:00
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
package appeng.me.cache;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-05-28 20:01:21 +02:00
|
|
|
import com.google.common.base.Preconditions;
|
|
|
|
import com.mojang.authlib.GameProfile;
|
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2014-12-29 21:59:05 +01:00
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.api.config.SecurityPermissions;
|
|
|
|
import appeng.api.networking.IGrid;
|
|
|
|
import appeng.api.networking.IGridHost;
|
|
|
|
import appeng.api.networking.IGridNode;
|
|
|
|
import appeng.api.networking.IGridStorage;
|
|
|
|
import appeng.api.networking.events.MENetworkEventSubscribe;
|
|
|
|
import appeng.api.networking.events.MENetworkSecurityChange;
|
|
|
|
import appeng.api.networking.security.ISecurityGrid;
|
2014-05-13 06:15:46 +02:00
|
|
|
import appeng.api.networking.security.ISecurityProvider;
|
2015-05-28 20:01:21 +02:00
|
|
|
import appeng.core.worlddata.WorldData;
|
2014-01-27 05:00:36 +01:00
|
|
|
import appeng.me.GridNode;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-10-04 08:08:28 +02:00
|
|
|
public class SecurityCache implements ISecurityGrid
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private final IGrid myGrid;
|
2015-04-06 00:35:42 +02:00
|
|
|
private final List<ISecurityProvider> securityProvider = new ArrayList<ISecurityProvider>();
|
|
|
|
private final HashMap<Integer, EnumSet<SecurityPermissions>> playerPerms = new HashMap<Integer, EnumSet<SecurityPermissions>>();
|
2015-04-03 08:54:31 +02:00
|
|
|
private long securityKey = -1;
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2015-09-25 23:10:56 +02:00
|
|
|
public SecurityCache( final IGrid g )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.myGrid = g;
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@MENetworkEventSubscribe
|
2015-09-25 23:10:56 +02:00
|
|
|
public void updatePermissions( final MENetworkSecurityChange ev )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.playerPerms.clear();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.securityProvider.isEmpty() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-01-27 05:00:36 +01:00
|
|
|
return;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2014-12-29 15:13:47 +01:00
|
|
|
this.securityProvider.get( 0 ).readPermissions( this.playerPerms );
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public long getSecurityKey()
|
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.securityKey;
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public void onUpdateTick()
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void removeNode( final IGridNode gridNode, final IGridHost machine )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( machine instanceof ISecurityProvider )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.securityProvider.remove( machine );
|
|
|
|
this.updateSecurityKey();
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateSecurityKey()
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final long lastCode = this.securityKey;
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.securityProvider.size() == 1 )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.securityKey = this.securityProvider.get( 0 ).getSecurityKey();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.securityKey = -1;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( lastCode != this.securityKey )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
this.getGrid().postEvent( new MENetworkSecurityChange() );
|
|
|
|
for( final IGridNode n : this.getGrid().getNodes() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
( (GridNode) n ).setLastSecurityKey( this.securityKey );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void addNode( final IGridNode gridNode, final IGridHost machine )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( machine instanceof ISecurityProvider )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
this.securityProvider.add( (ISecurityProvider) machine );
|
2014-12-29 15:13:47 +01:00
|
|
|
this.updateSecurityKey();
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
( (GridNode) gridNode ).setLastSecurityKey( this.securityKey );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onSplit( final IGridStorage destinationStorage )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void onJoin( final IGridStorage sourceStorage )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public void populateGridStorage( final IGridStorage destinationStorage )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
|
|
|
|
2015-08-06 18:49:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public boolean isAvailable()
|
|
|
|
{
|
|
|
|
return this.securityProvider.size() == 1 && this.securityProvider.get( 0 ).isSecurityEnabled();
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean hasPermission( final EntityPlayer player, final SecurityPermissions perm )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-05-28 20:01:21 +02:00
|
|
|
Preconditions.checkNotNull( player );
|
|
|
|
Preconditions.checkNotNull( perm );
|
|
|
|
|
|
|
|
final GameProfile profile = player.getGameProfile();
|
|
|
|
final int playerID = WorldData.instance().playerData().getPlayerID( profile );
|
|
|
|
|
|
|
|
return this.hasPermission( playerID, perm );
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-25 23:10:56 +02:00
|
|
|
public boolean hasPermission( final int playerID, final SecurityPermissions perm )
|
2014-01-27 05:00:36 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.isAvailable() )
|
|
|
|
{
|
2015-09-25 23:10:56 +02:00
|
|
|
final EnumSet<SecurityPermissions> perms = this.playerPerms.get( playerID );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( perms == null )
|
|
|
|
{
|
|
|
|
if( playerID == -1 ) // no default?
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
else
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return this.hasPermission( -1, perm );
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
return perms.contains( perm );
|
|
|
|
}
|
|
|
|
return true;
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getOwner()
|
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.isAvailable() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.securityProvider.get( 0 ).getOwner();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-10-08 15:42:42 +02:00
|
|
|
|
|
|
|
public IGrid getGrid()
|
|
|
|
{
|
|
|
|
return this.myGrid;
|
|
|
|
}
|
2014-01-27 05:00:36 +01:00
|
|
|
}
|