Potential fix for #390 fixing the return of group.Permissions listing permissions as negated permissions

Added TotalPermissions method to get the combined total permissions as used for HasPermissions
Added totalpermissions to output of /v2/group/read output in RestAPI
This commit is contained in:
stevenh 2012-02-20 10:58:46 +00:00
parent 1bc785f398
commit 4c09e7b66f
2 changed files with 37 additions and 4 deletions

View file

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System; using System;
using System.Linq;
using System.Collections.Generic; using System.Collections.Generic;
namespace TShockAPI namespace TShockAPI
@ -59,7 +60,7 @@ namespace TShockAPI
get get
{ {
List<string> all = new List<string>(permissions); List<string> all = new List<string>(permissions);
permissions.ForEach(p => all.Add("!" + p)); negatedpermissions.ForEach(p => all.Add("!" + p));
return string.Join(",", all); return string.Join(",", all);
} }
set set
@ -71,6 +72,36 @@ namespace TShockAPI
} }
} }
public List<string> TotalPermissions
{
get
{
var cur = this;
var traversed = new List<Group>();
HashSet<string> all = new HashSet<string>();
while (cur != null)
{
foreach (var perm in cur.permissions)
{
all.Add(perm);
}
foreach (var perm in cur.negatedpermissions)
{
all.Remove(perm);
}
if (traversed.Contains(cur))
{
throw new Exception("Infinite group parenting ({0})".SFormat(cur.Name));
}
traversed.Add(cur);
cur = cur.Parent;
}
return all.ToList();
}
}
public byte R = 255; public byte R = 255;
public byte G = 255; public byte G = 255;
public byte B = 255; public byte B = 255;
@ -85,12 +116,13 @@ namespace TShockAPI
public virtual bool HasPermission(string permission) public virtual bool HasPermission(string permission)
{ {
if (string.IsNullOrEmpty(permission))
return true;
var cur = this; var cur = this;
var traversed = new List<Group>(); var traversed = new List<Group>();
while (cur != null) while (cur != null)
{ {
if (string.IsNullOrEmpty(permission))
return true;
if (cur.negatedpermissions.Contains(permission)) if (cur.negatedpermissions.Contains(permission))
return false; return false;
if (cur.permissions.Contains(permission)) if (cur.permissions.Contains(permission))

View file

@ -584,7 +584,8 @@ namespace TShockAPI
{"parent", group.ParentName}, {"parent", group.ParentName},
{"chatcolor", group.ChatColor}, {"chatcolor", group.ChatColor},
{"permissions", group.permissions}, {"permissions", group.permissions},
{"negatedpermissions", group.negatedpermissions} {"negatedpermissions", group.negatedpermissions},
{"totalpermissions", group.TotalPermissions}
}; };
} }