Skip to content

Commit

Permalink
Permission inheritance and target power (Issue #48)
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Walker committed Jan 28, 2014
1 parent ab6706a commit 8caf903
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
6 changes: 6 additions & 0 deletions Commands/BanC.cs
Expand Up @@ -45,6 +45,12 @@ public override bool doProcess(string[] args)
Client target = StarryboundServer.getClient(player);
if (target != null)
{
if (!this.player.group.canTarget(target.playerData.group))
{
this.client.sendCommandMessage("^#f75d5d;You do not have permission to target this user.");
return false;
}

string uuid = target.playerData.uuid;
string name = target.playerData.name;
string ip = target.playerData.ip;
Expand Down
7 changes: 7 additions & 0 deletions Commands/Build.cs
Expand Up @@ -39,6 +39,13 @@ public override bool doProcess(string[] args)
if (player == null || player.Length < 1) { showHelpText(); return false; }

Client target = StarryboundServer.getClient(player);

if (!this.player.group.canTarget(target.playerData.group))
{
this.client.sendCommandMessage("^#f75d5d;You do not have permission to target this user.");
return false;
}

if (target != null)
{
PlayerData pData = target.playerData;
Expand Down
7 changes: 7 additions & 0 deletions Commands/Kick.cs
Expand Up @@ -51,6 +51,13 @@ public override bool doProcess(string[] args)
if (player == null || player.Length < 1) { showHelpText(); return false; }

Client target = StarryboundServer.getClient(player);

if (!this.player.group.canTarget(target.playerData.group))
{
this.client.sendCommandMessage("^#f75d5d;You do not have permission to target this user.");
return false;
}

if (target != null)
{
target.kickClient(reason);
Expand Down
7 changes: 7 additions & 0 deletions Commands/Mute.cs
Expand Up @@ -39,6 +39,13 @@ public override bool doProcess(string[] args)
if (player == null || player.Length < 1) { showHelpText(); return false; }

Client target = StarryboundServer.getClient(player);

if (!this.player.group.canTarget(target.playerData.group))
{
this.client.sendCommandMessage("^#f75d5d;You do not have permission to target this user.");
return false;
}

if (target != null)
{
PlayerData pData = target.playerData;
Expand Down
71 changes: 43 additions & 28 deletions Permissions/Groups.cs
Expand Up @@ -24,18 +24,22 @@ public class Group
public string name;
public string nameColor;
public string prefix;
public int targetPower;
public bool isDefault = false;
public bool isStaff = false;
public string parent = null;
public Dictionary<string, bool> permissions;

public Group(string name, string nameColor, string prefix, Dictionary<string, bool> permissions, bool isDefault = false, bool isStaff = false)
public Group(string name, string nameColor, string prefix, Dictionary<string, bool> permissions, bool isDefault = false, bool isStaff = false, int targetpower = 1, string parent = null)
{
this.name = name;
this.nameColor = nameColor;
this.prefix = prefix;
this.permissions = permissions;
this.isDefault = isDefault;
this.isStaff = isStaff;
this.targetPower = targetpower;
this.parent = parent;
}

public bool hasPermission(string node)
Expand All @@ -57,9 +61,17 @@ public bool hasPermission(string node)
}
}

if (!String.IsNullOrWhiteSpace(this.parent)) return StarryboundServer.groups[this.parent].hasPermission(node);

return false;
}

public bool canTarget(Group group)
{
if (this.targetPower > group.targetPower) return true;
else return false;
}

public bool givePermission(string node)
{
if (!node.Contains(".")) return false;
Expand Down Expand Up @@ -139,24 +151,37 @@ class GroupFile
{
public static void ProcessGroups(List<Group> groupList)
{
string defaultGroup = null;

foreach (Group group in groupList)
try
{
StarryboundServer.groups.Add(group.name, group);
if (group.isDefault) defaultGroup = group.name;
}
string defaultGroup = null;

foreach (Group group in groupList)
{
StarryboundServer.groups.Add(group.name, group);
if (!String.IsNullOrWhiteSpace(group.parent))
{
if (!StarryboundServer.groups.ContainsKey(group.parent)) throw new RankException("Parent (" + group.parent + ") for group " + group.name + " does not exist!");
}
if (group.isDefault) defaultGroup = group.name;
}

if (String.IsNullOrWhiteSpace(defaultGroup))
{
StarryboundServer.logFatal("Default user group flag (isDefault) is not set for any groups - Please set this in the groups.json!");
Thread.Sleep(5000);
Environment.Exit(5);
}

if (String.IsNullOrWhiteSpace(defaultGroup))
StarryboundServer.defaultGroup = defaultGroup;

StarryboundServer.logInfo("Loaded " + StarryboundServer.groups.Count + " group(s). Default group is " + defaultGroup);
}
catch (RankException e)
{
StarryboundServer.logFatal("Default user group flag (isDefault) is not set for any groups - Please set this in the groups.json!");
StarryboundServer.logFatal("A fatal exception occurred while processing the groups (Groups::ProcessGroups): " + e.ToString());
Thread.Sleep(5000);
Environment.Exit(5);
}

StarryboundServer.defaultGroup = defaultGroup;

StarryboundServer.logInfo("Loaded " + StarryboundServer.groups.Count + " group(s). Default group is " + defaultGroup);
}

public static object getGroup(string name)
Expand All @@ -177,22 +202,14 @@ static List<Group> DefaultGroups()

Dictionary<string, bool> saPerms = new Dictionary<string,bool>();
saPerms.Add("*", true);
Group superAdmin = new Group("superadmin", "#9801ba", "[SA]", saPerms, false, true);
Group superAdmin = new Group("superadmin", "#9801ba", "[SA]", saPerms, false, true, 20);
groups.Add(superAdmin);

Dictionary<string, bool> aPerms = new Dictionary<string, bool>();
aPerms.Add("admin.kick", true);
aPerms.Add("admin.mute", true);
aPerms.Add("admin.ban", true);
aPerms.Add("admin.unban", true);
aPerms.Add("admin.build", true);
aPerms.Add("admin.give", true);
aPerms.Add("admin.broadcast", true);
aPerms.Add("admin.chat", true);
aPerms.Add("client.*", true);
aPerms.Add("chat.*", true);
aPerms.Add("world.*", true);
Group admin = new Group("admin", "#ba0123", "[A]", aPerms, false, true);
Group admin = new Group("admin", "#ba0123", "[A]", aPerms, false, true, 10, "moderator");
groups.Add(admin);

Dictionary<string, bool> mPerms = new Dictionary<string, bool>();
Expand All @@ -201,24 +218,22 @@ static List<Group> DefaultGroups()
mPerms.Add("admin.ban", true);
mPerms.Add("admin.build", true);
mPerms.Add("admin.chat", true);
mPerms.Add("client.*", true);
mPerms.Add("chat.*", true);
mPerms.Add("world.*", true);
Group mod = new Group("moderator", "#ea6207", "[M]", mPerms, false, true);
Group mod = new Group("moderator", "#ea6207", "[M]", mPerms, false, true, 5, "player");
groups.Add(mod);

Dictionary<string, bool> pPerms = new Dictionary<string, bool>();
pPerms.Add("client.*", true);
pPerms.Add("chat.*", true);
pPerms.Add("world.build", true);
Group player = new Group("player", null, null, pPerms);
Group player = new Group("player", null, null, pPerms, false, false, 1, "guest");
groups.Add(player);

Dictionary<string, bool> gPerms = new Dictionary<string, bool>();
gPerms.Add("client.*", true);
gPerms.Add("chat.*", true);
gPerms.Add("world.build", true);
Group guest = new Group("guest", null, null, gPerms, true);
Group guest = new Group("guest", null, null, gPerms, true, false);
groups.Add(guest);

return groups;
Expand Down

0 comments on commit 8caf903

Please sign in to comment.