Skip to content
This repository has been archived by the owner on Jul 7, 2023. It is now read-only.

Commit

Permalink
WIP: Access method for commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Svenja Reissaus committed Aug 21, 2018
1 parent 94c4ee8 commit 9c6cc18
Show file tree
Hide file tree
Showing 21 changed files with 111 additions and 66 deletions.
Binary file added classes/artifacts/Factions_jar/Factions.jar
Binary file not shown.
12 changes: 4 additions & 8 deletions src/main/java/com/massivecraft/factions/cmd/CmdBan.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,12 @@ public CmdBan() {
@Override
public void perform() {

// Adds bypass to admins and clean permission check
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.BAN);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "ban");
return;
}
// Simplified for clarity
if (!this.hasAccess(PermissableAction.BAN)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage bans");
return;
}


// Good on permission checks. Now lets just ban the player.
FPlayer target = argAsFPlayer(0);
if (target == null) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/massivecraft/factions/cmd/CmdBanlist.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.massivecraft.factions.Factions;
import com.massivecraft.factions.struct.BanInfo;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;

import java.util.ArrayList;
Expand All @@ -32,6 +33,10 @@ public CmdBanlist() {

@Override
public void perform() {
if (!this.hasAccess(PermissableAction.BAN)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage bans");
return;
}
Faction target = myFaction;
if (!args.isEmpty()) {
target = argAsFaction(0);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@ public void perform() {
return;
}
// This permission check is way too explicit but it's clean
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.CHEST);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "access chest");
return;
}
if (!this.hasAccess(PermissableAction.CHEST)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "access chest");
return;
}

me.openInventory(fme.getFaction().getChest());
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdClaim.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ public void perform() {
int radius = this.argAsInt(0, 1); // Default to 1
final Faction forFaction = this.argAsFaction(1, myFaction); // Default to own

if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.TERRITORY);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "change faction territory");
return;
}
if (!this.hasAccess(PermissableAction.TERRITORY)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "change faction territory");
return;
}


Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdDeinvite.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ public CmdDeinvite() {
@Override
public void perform() {
FPlayer you = this.argAsBestFPlayerMatch(0);
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.INVITE);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage invites");
return;
}
if (!this.hasAccess(PermissableAction.INVITE)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage invites");
return;
}
if (you == null) {
FancyMessage msg = new FancyMessage(TL.COMMAND_DEINVITE_CANDEINVITE.toString()).color(ChatColor.GOLD);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/massivecraft/factions/cmd/CmdDelFWarp.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;

public class CmdDelFWarp extends FCommand {
Expand All @@ -22,6 +23,10 @@ public CmdDelFWarp() {
@Override
public void perform() {
String warp = argAsString(0);
if (!this.hasAccess(PermissableAction.WARP)){
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage warps");
return;
}
if (myFaction.isWarp(warp)) {
if (!transact(fme)) {
return;
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/com/massivecraft/factions/cmd/CmdDisband.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,6 @@ public void perform() {

boolean isMyFaction = fme != null && faction == myFaction;

if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.DISBAND);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "disband faction");
return;
}
}
if (!faction.isNormal()) {
msg(TL.COMMAND_DISBAND_IMMUTABLE.toString());
return;
Expand All @@ -60,6 +53,10 @@ public void perform() {
msg(TL.COMMAND_DISBAND_MARKEDPERMANENT.toString());
return;
}
if (!this.hasAccess(PermissableAction.DISBAND)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "disband faction");
return;
}


// check for tnt before disbanding.
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdFWarp.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ public CmdFWarp() {
@Override
public void perform() {
//TODO: check if in combat.
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.WARP);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "use warps");
return;
}
if (!this.hasAccess(PermissableAction.WARP)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "use warps");
return;
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/massivecraft/factions/cmd/CmdFly.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public CmdFly() {
this.senderMustBeMember = true;
this.senderMustBeModerator = false;
}
/// I'll optimize this later today or tomorrow

public static void startParticles() {
id = Bukkit.getScheduler().scheduleSyncRepeatingTask(P.p, new Runnable() {
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/com/massivecraft/factions/cmd/CmdGetVault.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.massivecraft.factions.P;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.Location;
import org.bukkit.Material;
Expand Down Expand Up @@ -33,7 +34,10 @@ public void perform() {
Location vaultLocation = fme.getFaction().getVault();
ItemStack vault = P.p.createItem(Material.CHEST, 1, (short) 0, P.p.color(P.p.getConfig().getString("fvault.Item.Name")), P.p.colorList(P.p.getConfig().getStringList("fvault.Item.Lore")));


if (!this.hasAccess(PermissableAction.VAULT)) {
fme.msg(TL.GENERIC_NOPERMISSION, "use vault");
return;
}
//check if vault is set
if (vaultLocation != null) {
fme.msg(TL.COMMAND_GETVAULT_ALREADYSET);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdHome.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,9 @@ public void perform() {
fme.msg(TL.COMMAND_HOME_TELEPORTDISABLED);
return;
}
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.HOME);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "teleport home");
return;
}
if (!this.hasAccess(PermissableAction.HOME)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "teleport home");
return;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public CmdInspect() {

@Override
public void perform() {
// Who can inspect?
if (fme.isInspectMode()) {
fme.setInspectMode(false);
msg(TL.COMMAND_INSPECT_DISABLED_MSG);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdInvite.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,9 @@ public void perform() {
return;
}

if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.INVITE);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage invites");
return;
}
if (!this.hasAccess(PermissableAction.INVITE)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "manage invites");
return;
}

if (myFaction.isInvited(target)) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/massivecraft/factions/cmd/CmdKick.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public void perform() {
// - Make sure the player is in the faction.
// - Make sure the kicked player has lower rank than the kicker.
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.KICK);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
if (!this.hasAccess(PermissableAction.KICK, false)) {
fme.msg(TL.GENERIC_NOPERMISSION, "kick");
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/massivecraft/factions/cmd/CmdLogins.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.massivecraft.factions.cmd;

import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.zcore.util.TL;

public class CmdLogins extends FCommand {
Expand All @@ -18,6 +19,11 @@ public CmdLogins() {

@Override
public void perform() {
/// Perhaps add a PermissableAction later?
if (!fme.getRole().isAtLeast(Role.MODERATOR)) {
fme.msg(TL.GENERIC_NOPERMISSION, "monitor joins");
return;
}
boolean monitor = fme.isMonitoringJoins();
fme.msg(TL.COMMAND_LOGINS_TOGGLE, String.valueOf(!monitor));
fme.setMonitorJoins(!monitor);
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdSetFWarp.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,9 @@ public void perform() {

// This statement allows us to check if they've specifically denied it, or default to
// the old setting of allowing moderators to set warps.
if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.SETWARP);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "set warps");
return;
}
if (!this.hasAccess(PermissableAction.SETWARP)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "set warps");
return;
}


Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/massivecraft/factions/cmd/CmdSethome.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,9 @@ public void perform() {
return;
}

if (!fme.isAdminBypassing()) {
Access access = myFaction.getAccess(fme, PermissableAction.SETHOME);
if (access != Access.ALLOW && fme.getRole() != Role.ADMIN && !Permission.SETHOME_ANY.has(sender, true)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "set home");
return;
}
if (!this.hasAccess(PermissableAction.SETHOME) && !Permission.SETHOME_ANY.has(sender, true)) {
fme.msg(TL.GENERIC_FPERM_NOPERMISSION, "set home");
return;
}

// Can the player set the faction home HERE?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.common.collect.ListMultimap;
import com.massivecraft.factions.FLocation;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;

public class CmdShowClaims extends FCommand {
Expand All @@ -25,6 +26,11 @@ public CmdShowClaims() {

@Override
public void perform() {
// #suggestion
if (!this.hasAccess(PermissableAction.TERRITORY)) {
fme.msg(TL.GENERIC_NOPERMISSION, "manage territory");
return;
}
sendMessage(TL.COMMAND_SHOWCLAIMS_HEADER.toString().replace("{faction}", fme.getFaction().describeTo(fme)));
ListMultimap<String, String> chunkMap = ArrayListMultimap.create();
String format = TL.COMMAND_SHOWCLAIMS_CHUNKSFORMAT.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.massivecraft.factions.FPlayer;
import com.massivecraft.factions.FPlayers;
import com.massivecraft.factions.struct.Permission;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;
import mkremins.fanciful.FancyMessage;
import org.bukkit.ChatColor;
Expand All @@ -21,6 +22,10 @@ public CmdShowInvites() {

@Override
public void perform() {
if (!this.hasAccess(PermissableAction.INVITE)) {
fme.msg(TL.GENERIC_NOPERMISSION, "manage invites");
return;
}
FancyMessage msg = new FancyMessage(TL.COMMAND_SHOWINVITES_PENDING.toString()).color(ChatColor.GOLD);
for (String id : myFaction.getInvites()) {
FPlayer fp = FPlayers.getInstance().getById(id);
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/com/massivecraft/factions/cmd/FCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.massivecraft.factions.struct.Role;
import com.massivecraft.factions.util.WarmUpUtil;
import com.massivecraft.factions.zcore.MCommand;
import com.massivecraft.factions.zcore.fperms.Access;
import com.massivecraft.factions.zcore.fperms.PermissableAction;
import com.massivecraft.factions.zcore.util.TL;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
Expand All @@ -25,6 +27,7 @@ public abstract class FCommand extends MCommand<P> {
public boolean senderMustBeModerator;
public boolean senderMustBeAdmin;
public boolean senderMustBeColeader;
protected PermissableAction actionPermission;

public boolean isMoneyCommand;

Expand All @@ -43,6 +46,47 @@ public FCommand() {
senderMustBeAdmin = false;
}

public boolean hasAccess() {
if (this.permission == null || this.fme == null) return false;
if (!this.fme.isAdminBypassing()) {
Access access = myFaction.getAccess(this.fme, permission);
if (access != Access.ALLOW && this.fme.getRole() != Role.ADMIN) {
return false;
}
}
return true;
}
public boolean hasAccess(boolean checkifAdmin) {
if (this.permission == null || this.fme == null) return false;
if (!this.fme.isAdminBypassing() && checkifAdmin) {
Access access = myFaction.getAccess(this.fme, permission);
if (access != Access.ALLOW && this.fme.getRole() != Role.ADMIN) {
return false;
}
}
return true;
}
public boolean hasAccess(PermissableAction perm) {
if (this.permission == null || this.fme == null) return false;
if (!this.fme.isAdminBypassing()) {
Access access = myFaction.getAccess(this.fme, perm);
if (access != Access.ALLOW && this.fme.getRole() != Role.ADMIN) {
return false;
}
}
return true;
}
public boolean hasAccess(PermissableAction perm, boolean checkifAdmin) {
if (this.permission == null || this.fme == null) return false;
if (!this.fme.isAdminBypassing() && checkifAdmin) {
Access access = myFaction.getAccess(this.fme, perm);
if (access != Access.ALLOW && this.fme.getRole() != Role.ADMIN) {
return false;
}
}
return true;
}

@Override
public void execute(CommandSender sender, List<String> args, List<MCommand<?>> commandChain) {
if (sender instanceof Player) {
Expand Down

0 comments on commit 9c6cc18

Please sign in to comment.