Permission nodes
This commit is contained in:
parent
0b02b90df0
commit
e22f3e03c4
1 changed files with 32 additions and 19 deletions
|
|
@ -174,26 +174,39 @@ namespace TShockAPI
|
||||||
/// <returns>Returns true if the user has that permission.</returns>
|
/// <returns>Returns true if the user has that permission.</returns>
|
||||||
public virtual bool HasPermission(string permission)
|
public virtual bool HasPermission(string permission)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(permission))
|
string[] nodes = permission.Split('.');
|
||||||
return true;
|
for (int i = nodes.Length - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
var cur = this;
|
if (RealHasPermission(String.Join(".", nodes)))
|
||||||
var traversed = new List<Group>();
|
{
|
||||||
while (cur != null)
|
return true;
|
||||||
{
|
}
|
||||||
if (cur.negatedpermissions.Contains(permission))
|
nodes[i] = "*";
|
||||||
return false;
|
}
|
||||||
if (cur.permissions.Contains(permission))
|
return false;
|
||||||
return true;
|
|
||||||
if (traversed.Contains(cur))
|
|
||||||
{
|
|
||||||
throw new Exception("Infinite group parenting ({0})".SFormat(cur.Name));
|
|
||||||
}
|
|
||||||
traversed.Add(cur);
|
|
||||||
cur = cur.Parent;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
private bool RealHasPermission(string permission)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(permission))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
var cur = this;
|
||||||
|
var traversed = new List<Group>();
|
||||||
|
while (cur != null)
|
||||||
|
{
|
||||||
|
if (cur.negatedpermissions.Contains(permission))
|
||||||
|
return false;
|
||||||
|
if (cur.permissions.Contains(permission))
|
||||||
|
return true;
|
||||||
|
if (traversed.Contains(cur))
|
||||||
|
{
|
||||||
|
throw new Exception("Infinite group parenting ({0})".SFormat(cur.Name));
|
||||||
|
}
|
||||||
|
traversed.Add(cur);
|
||||||
|
cur = cur.Parent;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adds a permission to the list of negated permissions.
|
/// Adds a permission to the list of negated permissions.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue