Change permission checking in preparation for permission rework

This commit is contained in:
Magnus Ulf 2018-12-20 18:12:16 +01:00
parent f347c1058a
commit 0efccf335c
4 changed files with 69 additions and 45 deletions

View File

@ -170,8 +170,7 @@ public class EngineChunkChange extends Engine
MPerm claimnear = MPerm.getPermClaimnear();
for (Faction nearbyFaction : nearbyFactions)
{
if (claimnear.has(newFaction, nearbyFaction)) continue;
mplayer.message(claimnear.createDeniedMessage(mplayer, nearbyFaction));
if (claimnear.has(mplayer, nearbyFaction, true)) continue;
event.setCancelled(true);
return;
}

View File

@ -39,7 +39,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
public class Faction extends Entity<Faction> implements FactionsParticipator
public class Faction extends Entity<Faction> implements FactionsParticipator, MPerm.MPermable
{
// -------------------------------------------- //
// CONSTANTS
@ -761,13 +761,53 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
// IS PERMITTED
public boolean isPermitted(MPerm.MPermable mpermable, String permId)
public boolean isPlayerPermitted(MPlayer mplayer, String permId)
{
if (mpermable == null) throw new NullPointerException("mpermable");
if (isPermitted(mplayer.getId(), permId)) return true;
if (isPermitted(mplayer.getFaction().getId(), permId)) return true;
if (isPermitted(mplayer.getRank().getId(), permId)) return true;
if (isPermitted(RelationUtil.getRelationOfThatToMe(mplayer, this).toString(), permId)) return true;
return false;
}
public boolean isPlayerPermitted(MPlayer mplayer, MPerm mperm)
{
return isPlayerPermitted(mplayer, mperm.getId());
}
public boolean isFactionPermitted(Faction faction, String permId)
{
if (isPermitted(faction.getId(), permId)) return true;
if (isPermitted(RelationUtil.getRelationOfThatToMe(faction, this).toString(), permId)) return true;
return false;
}
public boolean isFactionPermitted(Faction faction, MPerm mperm)
{
return isFactionPermitted(faction, mperm.getId());
}
@Deprecated
public boolean isPermablePermitted(MPerm.MPermable permable, String permId)
{
return isPermitted(permable.getId(), permId);
}
@Deprecated
public boolean isPermablePermitted(MPerm.MPermable permable, MPerm mperm)
{
return isPermablePermitted(permable, mperm.getId());
}
private boolean isPermitted(String permableId, String permId)
{
if (permableId == null) throw new NullPointerException("permableId");
if (permId == null) throw new NullPointerException("permId");
var perms = this.perms.get(permId);
if (perms == null)
var permables = this.perms.get(permId);
if (permables == null)
{
// No perms was found, but likely this is just a new MPerm.
// So if this does not exist in the database, throw an error.
@ -777,14 +817,7 @@ public class Faction extends Entity<Faction> implements FactionsParticipator
return false;
}
return perms.contains(mpermable.getId());
}
public boolean isPermitted(MPerm.MPermable mpermable, MPerm mperm)
{
if (mpermable == null) throw new NullPointerException("mpermable");
if (mperm == null) throw new NullPointerException("mperm");
return isPermitted(mpermable, mperm.getId());
return permables.contains(permableId);
}
// SET PERMITTED

View File

@ -351,8 +351,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
if (faction == null) throw new NullPointerException("faction");
if (hostFaction == null) throw new NullPointerException("hostFaction");
Rel rel = faction.getRelationTo(hostFaction);
return hostFaction.isPermitted(rel, this);
return hostFaction.isFactionPermitted(faction, this);
}
public boolean has(MPlayer mplayer, Faction hostFaction, boolean verboose)
@ -365,7 +364,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
Rel rel = mplayer.getRelationTo(hostFaction);
MPermable permable = rel == Rel.FACTION ? mplayer.getRank() : rel;
if (hostFaction.isPermitted(permable, this)) return true;
if (hostFaction.isPlayerPermitted(mplayer, this)) return true;
if (verboose) mplayer.message(this.createDeniedMessage(mplayer, hostFaction));
@ -441,7 +440,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
for (MPermable permable : getPermables(faction))
{
if (faction.isPermitted(permable, this))
if (faction.isPermablePermitted(permable, this))
{
ret += "<g>YES";
}
@ -504,6 +503,7 @@ public class MPerm extends Entity<MPerm> implements Prioritized, Registerable, N
throw new RuntimeException();
}
}
}
}

View File

@ -33,8 +33,7 @@ import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipator
{
public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipator, MPerm.MPermable {
// -------------------------------------------- //
// META
// -------------------------------------------- //
@ -45,8 +44,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
// META
// -------------------------------------------- //
public static MPlayer get(Object oid)
{
public static MPlayer get(Object oid) {
return MPlayerColl.get().get(oid);
}
@ -61,8 +59,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
// -------------------------------------------- //
@Override
public MPlayer load(MPlayer that)
{
public MPlayer load(MPlayer that) {
this.setLastActivityMillis(that.lastActivityMillis);
this.setFactionId(that.factionId);
this.rankId = that.rankId;
@ -81,8 +78,7 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
// -------------------------------------------- //
@Override
public boolean isDefault()
{
public boolean isDefault() {
// Last activity millis is data we use for clearing out cleanable players. So it does not in itself make the player data worth keeping.
if (this.hasFaction()) return false;
// Role means nothing without a faction.
@ -101,22 +97,18 @@ public class MPlayer extends SenderEntity<MPlayer> implements FactionsParticipat
// -------------------------------------------- //
@Override
public void postAttach(String id)
{
public void postAttach(String id) {
FactionsIndex.get().update(this);
}
@Override
public void preDetach(String id)
{
public void preDetach(String id) {
FactionsIndex.get().update(this);
}
@Override
public void preClean()
{
if (this.getRank().isLeader())
{
public void preClean() {
if (this.getRank().isLeader()) {
this.getFaction().promoteNewLeader();
}