Skip to content

Commit

Permalink
Adds Hook support for multi permission grants/revokes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZockiRR committed Jun 15, 2023
1 parent f5fad96 commit f3c4a56
Showing 1 changed file with 151 additions and 128 deletions.
279 changes: 151 additions & 128 deletions src/Libraries/Permission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -689,13 +689,29 @@ public void AddUserGroup(string playerId, string groupName)

UserData userData = GetUserData(playerId);

if (!userData.Groups.Add(groupName))
if (!groupName.EndsWith("*"))
{
// Add the permission
if (userData.Groups.Add(groupName))
{
// Call hook for plugins
Interface.Call("OnUserGroupAdded", playerId, groupName);
Interface.Call("OnUserGroupsAdded", playerId, new string[] { groupName });
}
return;
}

// Call hook for plugins
Interface.Call("OnUserGroupAdded", playerId, groupName);
IEnumerable<string> scopedGroups = groupsData.Keys;
if (groupName != "*")
{
string groupPrefix = groupName.TrimEnd('*');
scopedGroups = scopedGroups.Where(g => g.StartsWith(groupPrefix, StringComparison.OrdinalIgnoreCase));
}
string[] addedGroups = scopedGroups.Where(g => userData.Groups.Add(g)).ToArray();
if (addedGroups.Length > 0)
{
Interface.Call("OnUserGroupsAdded", playerId, addedGroups);
}
}

/// <summary>
Expand All @@ -713,25 +729,38 @@ public void RemoveUserGroup(string playerId, string groupName)
}

UserData userData = GetUserData(playerId);
if (userData.Groups.Count <= 0)
{
return;
}

if (groupName.Equals("*"))
if (!groupName.EndsWith("*"))
{
if (userData.Groups.Count <= 0)
if (userData.Groups.Remove(groupName))
{
return;
// Call hook for plugins
Interface.Call("OnUserGroupRemoved", playerId, groupName);
Interface.Call("OnUserGroupsRemoved", playerId, new string[] { groupName });
}

userData.Groups.Clear();
return;
}

if (!userData.Groups.Remove(groupName))
if (groupName == "*")
{
return;
string[] removedGroups = userData.Groups.ToArray();
userData.Groups.Clear();
Interface.Call("OnUserGroupsRemoved", playerId, removedGroups);
}
else
{
string groupPrefix = groupName.TrimEnd('*');
string[] removedGroups = userData.Groups.Where(g => g.StartsWith(groupPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
foreach (string g in removedGroups)
{
userData.Perms.Remove(g);
}
Interface.Call("OnUserGroupsRemoved", playerId, removedGroups);
}

// Call hook for plugins
Interface.Call("OnUserGroupRemoved", playerId, groupName);
}

/// <summary>
Expand Down Expand Up @@ -759,7 +788,7 @@ public bool UserHasGroup(string playerId, string groupName)
[LibraryFunction("GroupExists")]
public bool GroupExists(string groupName)
{
return !string.IsNullOrEmpty(groupName) && (groupName.Equals("*") || groupsData.ContainsKey(groupName));
return !string.IsNullOrEmpty(groupName) && (groupName.EndsWith("*") || groupsData.ContainsKey(groupName));
}

/// <summary>
Expand Down Expand Up @@ -801,6 +830,22 @@ public string[] GetUsersInGroup(string groupName)
return usersData.Where(u => u.Value.Groups.Contains(groupName, StringComparer.OrdinalIgnoreCase)).Select(u => $"{u.Key} ({u.Value.LastSeenNickname})").ToArray();
}

/// <summary>
/// Returns usersIDs in that group
/// </summary>
/// <param name="groupName"></param>
/// <returns></returns>
[LibraryFunction("GetUserIDsInGroup")]
public string[] GetUserIDsInGroup(string groupName)
{
if (string.IsNullOrEmpty(groupName) || !groupsData.ContainsKey(groupName))
{
return new string[0];
}

return usersData.Where(u => u.Value.Groups.Contains(groupName, StringComparer.OrdinalIgnoreCase)).Select(u => u.Key).ToArray();
}

/// <summary>
/// Returns the title of the specified group
/// </summary>
Expand Down Expand Up @@ -868,44 +913,38 @@ public void GrantUserPermission(string playerId, string permission, Plugin owner
// Get the player data
UserData userData = GetUserData(playerId);

if (permission.EndsWith("*"))
if (!permission.EndsWith("*"))
{
HashSet<string> permissions;

if (owner == null)
{
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
}
else if (!registeredPermissions.TryGetValue(owner, out permissions))
// Add the permission
if (userData.Perms.Add(permission))
{
return;
}

if (permission.Equals("*"))
{
if (!permissions.Aggregate(false, (c, s) => c | userData.Perms.Add(s)))
{
return;
}
}
else
{
if (!permissions.Where(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)).Aggregate(false, (c, s) => c | userData.Perms.Add(s)))
{
return;
}
// Call hook for plugins
Interface.Call("OnUserPermissionGranted", playerId, permission);
Interface.Call("OnUserPermissionsGranted", playerId, new string[] { permission });
}
return;
}

// Add the permission
if (!userData.Perms.Add(permission))
HashSet<string> permissions;
if (owner == null)
{
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
}
else if (!registeredPermissions.TryGetValue(owner, out permissions))
{
return;
}

// Call hook for plugins
Interface.Call("OnUserPermissionGranted", playerId, permission);
IEnumerable<string> scopedPermissions = permissions;
if (permission != "*")
{
string permissionPrefix = permission.TrimEnd('*');
scopedPermissions = scopedPermissions.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase));
}
string[] grantedPermissions = scopedPermissions.Where(p => userData.Perms.Add(p)).ToArray();
if (grantedPermissions.Length > 0)
{
Interface.Call("OnUserPermissionsGranted", playerId, grantedPermissions);
}
}

/// <summary>
Expand All @@ -923,36 +962,39 @@ public void RevokeUserPermission(string playerId, string permission)

// Get the player data
UserData userData = GetUserData(playerId);

if (permission.EndsWith("*"))
if (userData.Perms.Count <= 0)
{
if (permission.Equals("*"))
{
if (userData.Perms.Count <= 0)
{
return;
}
return;
}

userData.Perms.Clear();
}
else
if (!permission.EndsWith("*"))
{
// Remove the permission
if (userData.Perms.Remove(permission))
{
if (userData.Perms.RemoveWhere(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)) <= 0)
{
return;
}
// Call hook for plugins
Interface.Call("OnUserPermissionRevoked", playerId, permission);
Interface.Call("OnUserPermissionsRevoked", playerId, new string[] { permission });
}
return;
}

// Remove the permission
if (!userData.Perms.Remove(permission))
if (permission == "*")
{
return;
string[] revokedPermissions = userData.Perms.ToArray();
userData.Perms.Clear();
Interface.Call("OnUserPermissionsRevoked", playerId, revokedPermissions);
}
else
{
string permissionPrefix = permission.TrimEnd('*');
string[] revokedPermissions = userData.Perms.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
foreach (string p in revokedPermissions)
{
userData.Perms.Remove(p);
}
Interface.Call("OnUserPermissionsRevoked", playerId, revokedPermissions);
}

// Call hook for plugins
Interface.Call("OnUserPermissionRevoked", playerId, permission);
}

#endregion User Permission
Expand All @@ -969,56 +1011,44 @@ public void RevokeUserPermission(string playerId, string permission)
public void GrantGroupPermission(string groupName, string permission, Plugin owner)
{
// Check it is even a permission
if (!PermissionExists(permission, owner) || !GroupExists(groupName))
if (!PermissionExists(permission, owner) || string.IsNullOrEmpty(groupName) || !groupsData.TryGetValue(groupName, out GroupData groupData))
{
return;
}

// Get the group data
if (!groupsData.TryGetValue(groupName, out GroupData groupData))
if (!permission.EndsWith("*"))
{
return;
}

if (permission.EndsWith("*"))
{
HashSet<string> permissions;

if (owner == null)
{
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
}
else if (!registeredPermissions.TryGetValue(owner, out permissions))
{
return;
}

if (permission.Equals("*"))
// Add the permission
if (groupData.Perms.Add(permission))
{
if (!permissions.Aggregate(false, (c, s) => c | groupData.Perms.Add(s)))
{
return;
}
}
else
{
if (!permissions.Where(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)).Aggregate(false, (c, s) => c | groupData.Perms.Add(s)))
{
return;
}
// Call hook for plugins
Interface.Call("OnGroupPermissionGranted", groupName, permission);
Interface.Call("OnGroupPermissionsGranted", groupName, new string[] { permission });
}

return;
}

// Add the permission
if (!groupData.Perms.Add(permission))
HashSet<string> permissions;
if (owner == null)
{
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
}
else if (!registeredPermissions.TryGetValue(owner, out permissions))
{
return;
}

// Call hook for plugins
Interface.Call("OnGroupPermissionGranted", groupName, permission);
IEnumerable<string> scopedPermissions = permissions;
if (permission != "*")
{
string permissionPrefix = permission.TrimEnd('*');
scopedPermissions = scopedPermissions.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase));
}
string[] grantedPermissions = scopedPermissions.Where(p => groupData.Perms.Add(p)).ToArray();
if (grantedPermissions.Length > 0)
{
Interface.Call("OnGroupPermissionsGranted", groupName, grantedPermissions);
}
}

/// <summary>
Expand All @@ -1029,46 +1059,39 @@ public void GrantGroupPermission(string groupName, string permission, Plugin own
[LibraryFunction("RevokeGroupPermission")]
public void RevokeGroupPermission(string groupName, string permission)
{
if (!GroupExists(groupName) || string.IsNullOrEmpty(permission))
{
return;
}

// Get the group data
if (!groupsData.TryGetValue(groupName, out GroupData groupData))
if (string.IsNullOrEmpty(permission) || string.IsNullOrEmpty(groupName) || !groupsData.TryGetValue(groupName, out GroupData groupData) || groupData.Perms.Count <= 0)
{
return;
}

if (permission.EndsWith("*"))
if (!permission.EndsWith("*"))
{
if (permission.Equals("*"))
// Remove the permission
if (groupData.Perms.Remove(permission))
{
if (groupData.Perms.Count <= 0)
{
return;
}

groupData.Perms.Clear();
}
else
{
if (groupData.Perms.RemoveWhere(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)) <= 0)
{
return;
}
// Call hook for plugins
Interface.Call("OnGroupPermissionRevoked", groupName, permission);
Interface.Call("OnGroupPermissionsRevoked", groupName, new string[] { permission });
}
return;
}

// Remove the permission
if (!groupData.Perms.Remove(permission))
if (permission == "*")
{
return;
string[] revokedPermissions = groupData.Perms.ToArray();
groupData.Perms.Clear();
Interface.Call("OnGroupPermissionsRevoked", groupName, revokedPermissions);
}
else
{
string permissionPrefix = permission.TrimEnd('*');
string[] revokedPermissions = groupData.Perms.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
foreach (string p in revokedPermissions)
{
groupData.Perms.Remove(p);
}
Interface.Call("OnGroupPermissionsRevoked", groupName, revokedPermissions);
}

// Call hook for plugins
Interface.Call("OnGroupPermissionRevoked", groupName, permission);
}

#endregion Group Permission
Expand Down

0 comments on commit f3c4a56

Please sign in to comment.