Skip to content

Commit f3c4a56

Browse files
committed
Adds Hook support for multi permission grants/revokes
1 parent f5fad96 commit f3c4a56

File tree

1 file changed

+151
-128
lines changed

1 file changed

+151
-128
lines changed

src/Libraries/Permission.cs

Lines changed: 151 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -689,13 +689,29 @@ public void AddUserGroup(string playerId, string groupName)
689689

690690
UserData userData = GetUserData(playerId);
691691

692-
if (!userData.Groups.Add(groupName))
692+
if (!groupName.EndsWith("*"))
693693
{
694+
// Add the permission
695+
if (userData.Groups.Add(groupName))
696+
{
697+
// Call hook for plugins
698+
Interface.Call("OnUserGroupAdded", playerId, groupName);
699+
Interface.Call("OnUserGroupsAdded", playerId, new string[] { groupName });
700+
}
694701
return;
695702
}
696703

697-
// Call hook for plugins
698-
Interface.Call("OnUserGroupAdded", playerId, groupName);
704+
IEnumerable<string> scopedGroups = groupsData.Keys;
705+
if (groupName != "*")
706+
{
707+
string groupPrefix = groupName.TrimEnd('*');
708+
scopedGroups = scopedGroups.Where(g => g.StartsWith(groupPrefix, StringComparison.OrdinalIgnoreCase));
709+
}
710+
string[] addedGroups = scopedGroups.Where(g => userData.Groups.Add(g)).ToArray();
711+
if (addedGroups.Length > 0)
712+
{
713+
Interface.Call("OnUserGroupsAdded", playerId, addedGroups);
714+
}
699715
}
700716

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

715731
UserData userData = GetUserData(playerId);
732+
if (userData.Groups.Count <= 0)
733+
{
734+
return;
735+
}
716736

717-
if (groupName.Equals("*"))
737+
if (!groupName.EndsWith("*"))
718738
{
719-
if (userData.Groups.Count <= 0)
739+
if (userData.Groups.Remove(groupName))
720740
{
721-
return;
741+
// Call hook for plugins
742+
Interface.Call("OnUserGroupRemoved", playerId, groupName);
743+
Interface.Call("OnUserGroupsRemoved", playerId, new string[] { groupName });
722744
}
723-
724-
userData.Groups.Clear();
725745
return;
726746
}
727747

728-
if (!userData.Groups.Remove(groupName))
748+
if (groupName == "*")
729749
{
730-
return;
750+
string[] removedGroups = userData.Groups.ToArray();
751+
userData.Groups.Clear();
752+
Interface.Call("OnUserGroupsRemoved", playerId, removedGroups);
753+
}
754+
else
755+
{
756+
string groupPrefix = groupName.TrimEnd('*');
757+
string[] removedGroups = userData.Groups.Where(g => g.StartsWith(groupPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
758+
foreach (string g in removedGroups)
759+
{
760+
userData.Perms.Remove(g);
761+
}
762+
Interface.Call("OnUserGroupsRemoved", playerId, removedGroups);
731763
}
732-
733-
// Call hook for plugins
734-
Interface.Call("OnUserGroupRemoved", playerId, groupName);
735764
}
736765

737766
/// <summary>
@@ -759,7 +788,7 @@ public bool UserHasGroup(string playerId, string groupName)
759788
[LibraryFunction("GroupExists")]
760789
public bool GroupExists(string groupName)
761790
{
762-
return !string.IsNullOrEmpty(groupName) && (groupName.Equals("*") || groupsData.ContainsKey(groupName));
791+
return !string.IsNullOrEmpty(groupName) && (groupName.EndsWith("*") || groupsData.ContainsKey(groupName));
763792
}
764793

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

833+
/// <summary>
834+
/// Returns usersIDs in that group
835+
/// </summary>
836+
/// <param name="groupName"></param>
837+
/// <returns></returns>
838+
[LibraryFunction("GetUserIDsInGroup")]
839+
public string[] GetUserIDsInGroup(string groupName)
840+
{
841+
if (string.IsNullOrEmpty(groupName) || !groupsData.ContainsKey(groupName))
842+
{
843+
return new string[0];
844+
}
845+
846+
return usersData.Where(u => u.Value.Groups.Contains(groupName, StringComparer.OrdinalIgnoreCase)).Select(u => u.Key).ToArray();
847+
}
848+
804849
/// <summary>
805850
/// Returns the title of the specified group
806851
/// </summary>
@@ -868,44 +913,38 @@ public void GrantUserPermission(string playerId, string permission, Plugin owner
868913
// Get the player data
869914
UserData userData = GetUserData(playerId);
870915

871-
if (permission.EndsWith("*"))
916+
if (!permission.EndsWith("*"))
872917
{
873-
HashSet<string> permissions;
874-
875-
if (owner == null)
876-
{
877-
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
878-
}
879-
else if (!registeredPermissions.TryGetValue(owner, out permissions))
918+
// Add the permission
919+
if (userData.Perms.Add(permission))
880920
{
881-
return;
882-
}
883-
884-
if (permission.Equals("*"))
885-
{
886-
if (!permissions.Aggregate(false, (c, s) => c | userData.Perms.Add(s)))
887-
{
888-
return;
889-
}
890-
}
891-
else
892-
{
893-
if (!permissions.Where(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)).Aggregate(false, (c, s) => c | userData.Perms.Add(s)))
894-
{
895-
return;
896-
}
921+
// Call hook for plugins
922+
Interface.Call("OnUserPermissionGranted", playerId, permission);
923+
Interface.Call("OnUserPermissionsGranted", playerId, new string[] { permission });
897924
}
898925
return;
899926
}
900-
901-
// Add the permission
902-
if (!userData.Perms.Add(permission))
927+
HashSet<string> permissions;
928+
if (owner == null)
929+
{
930+
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
931+
}
932+
else if (!registeredPermissions.TryGetValue(owner, out permissions))
903933
{
904934
return;
905935
}
906936

907-
// Call hook for plugins
908-
Interface.Call("OnUserPermissionGranted", playerId, permission);
937+
IEnumerable<string> scopedPermissions = permissions;
938+
if (permission != "*")
939+
{
940+
string permissionPrefix = permission.TrimEnd('*');
941+
scopedPermissions = scopedPermissions.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase));
942+
}
943+
string[] grantedPermissions = scopedPermissions.Where(p => userData.Perms.Add(p)).ToArray();
944+
if (grantedPermissions.Length > 0)
945+
{
946+
Interface.Call("OnUserPermissionsGranted", playerId, grantedPermissions);
947+
}
909948
}
910949

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

924963
// Get the player data
925964
UserData userData = GetUserData(playerId);
926-
927-
if (permission.EndsWith("*"))
965+
if (userData.Perms.Count <= 0)
928966
{
929-
if (permission.Equals("*"))
930-
{
931-
if (userData.Perms.Count <= 0)
932-
{
933-
return;
934-
}
967+
return;
968+
}
935969

936-
userData.Perms.Clear();
937-
}
938-
else
970+
if (!permission.EndsWith("*"))
971+
{
972+
// Remove the permission
973+
if (userData.Perms.Remove(permission))
939974
{
940-
if (userData.Perms.RemoveWhere(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)) <= 0)
941-
{
942-
return;
943-
}
975+
// Call hook for plugins
976+
Interface.Call("OnUserPermissionRevoked", playerId, permission);
977+
Interface.Call("OnUserPermissionsRevoked", playerId, new string[] { permission });
944978
}
945979
return;
946980
}
947981

948-
// Remove the permission
949-
if (!userData.Perms.Remove(permission))
982+
if (permission == "*")
950983
{
951-
return;
984+
string[] revokedPermissions = userData.Perms.ToArray();
985+
userData.Perms.Clear();
986+
Interface.Call("OnUserPermissionsRevoked", playerId, revokedPermissions);
987+
}
988+
else
989+
{
990+
string permissionPrefix = permission.TrimEnd('*');
991+
string[] revokedPermissions = userData.Perms.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
992+
foreach (string p in revokedPermissions)
993+
{
994+
userData.Perms.Remove(p);
995+
}
996+
Interface.Call("OnUserPermissionsRevoked", playerId, revokedPermissions);
952997
}
953-
954-
// Call hook for plugins
955-
Interface.Call("OnUserPermissionRevoked", playerId, permission);
956998
}
957999

9581000
#endregion User Permission
@@ -969,56 +1011,44 @@ public void RevokeUserPermission(string playerId, string permission)
9691011
public void GrantGroupPermission(string groupName, string permission, Plugin owner)
9701012
{
9711013
// Check it is even a permission
972-
if (!PermissionExists(permission, owner) || !GroupExists(groupName))
1014+
if (!PermissionExists(permission, owner) || string.IsNullOrEmpty(groupName) || !groupsData.TryGetValue(groupName, out GroupData groupData))
9731015
{
9741016
return;
9751017
}
9761018

977-
// Get the group data
978-
if (!groupsData.TryGetValue(groupName, out GroupData groupData))
1019+
if (!permission.EndsWith("*"))
9791020
{
980-
return;
981-
}
982-
983-
if (permission.EndsWith("*"))
984-
{
985-
HashSet<string> permissions;
986-
987-
if (owner == null)
988-
{
989-
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
990-
}
991-
else if (!registeredPermissions.TryGetValue(owner, out permissions))
992-
{
993-
return;
994-
}
995-
996-
if (permission.Equals("*"))
1021+
// Add the permission
1022+
if (groupData.Perms.Add(permission))
9971023
{
998-
if (!permissions.Aggregate(false, (c, s) => c | groupData.Perms.Add(s)))
999-
{
1000-
return;
1001-
}
1002-
}
1003-
else
1004-
{
1005-
if (!permissions.Where(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)).Aggregate(false, (c, s) => c | groupData.Perms.Add(s)))
1006-
{
1007-
return;
1008-
}
1024+
// Call hook for plugins
1025+
Interface.Call("OnGroupPermissionGranted", groupName, permission);
1026+
Interface.Call("OnGroupPermissionsGranted", groupName, new string[] { permission });
10091027
}
1010-
10111028
return;
10121029
}
10131030

1014-
// Add the permission
1015-
if (!groupData.Perms.Add(permission))
1031+
HashSet<string> permissions;
1032+
if (owner == null)
1033+
{
1034+
permissions = new HashSet<string>(registeredPermissions.Values.SelectMany(v => v));
1035+
}
1036+
else if (!registeredPermissions.TryGetValue(owner, out permissions))
10161037
{
10171038
return;
10181039
}
10191040

1020-
// Call hook for plugins
1021-
Interface.Call("OnGroupPermissionGranted", groupName, permission);
1041+
IEnumerable<string> scopedPermissions = permissions;
1042+
if (permission != "*")
1043+
{
1044+
string permissionPrefix = permission.TrimEnd('*');
1045+
scopedPermissions = scopedPermissions.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase));
1046+
}
1047+
string[] grantedPermissions = scopedPermissions.Where(p => groupData.Perms.Add(p)).ToArray();
1048+
if (grantedPermissions.Length > 0)
1049+
{
1050+
Interface.Call("OnGroupPermissionsGranted", groupName, grantedPermissions);
1051+
}
10221052
}
10231053

10241054
/// <summary>
@@ -1029,46 +1059,39 @@ public void GrantGroupPermission(string groupName, string permission, Plugin own
10291059
[LibraryFunction("RevokeGroupPermission")]
10301060
public void RevokeGroupPermission(string groupName, string permission)
10311061
{
1032-
if (!GroupExists(groupName) || string.IsNullOrEmpty(permission))
1033-
{
1034-
return;
1035-
}
1036-
1037-
// Get the group data
1038-
if (!groupsData.TryGetValue(groupName, out GroupData groupData))
1062+
if (string.IsNullOrEmpty(permission) || string.IsNullOrEmpty(groupName) || !groupsData.TryGetValue(groupName, out GroupData groupData) || groupData.Perms.Count <= 0)
10391063
{
10401064
return;
10411065
}
10421066

1043-
if (permission.EndsWith("*"))
1067+
if (!permission.EndsWith("*"))
10441068
{
1045-
if (permission.Equals("*"))
1069+
// Remove the permission
1070+
if (groupData.Perms.Remove(permission))
10461071
{
1047-
if (groupData.Perms.Count <= 0)
1048-
{
1049-
return;
1050-
}
1051-
1052-
groupData.Perms.Clear();
1053-
}
1054-
else
1055-
{
1056-
if (groupData.Perms.RemoveWhere(p => p.StartsWith(permission.TrimEnd('*'), StringComparison.OrdinalIgnoreCase)) <= 0)
1057-
{
1058-
return;
1059-
}
1072+
// Call hook for plugins
1073+
Interface.Call("OnGroupPermissionRevoked", groupName, permission);
1074+
Interface.Call("OnGroupPermissionsRevoked", groupName, new string[] { permission });
10601075
}
10611076
return;
10621077
}
10631078

1064-
// Remove the permission
1065-
if (!groupData.Perms.Remove(permission))
1079+
if (permission == "*")
10661080
{
1067-
return;
1081+
string[] revokedPermissions = groupData.Perms.ToArray();
1082+
groupData.Perms.Clear();
1083+
Interface.Call("OnGroupPermissionsRevoked", groupName, revokedPermissions);
1084+
}
1085+
else
1086+
{
1087+
string permissionPrefix = permission.TrimEnd('*');
1088+
string[] revokedPermissions = groupData.Perms.Where(p => p.StartsWith(permissionPrefix, StringComparison.OrdinalIgnoreCase)).ToArray();
1089+
foreach (string p in revokedPermissions)
1090+
{
1091+
groupData.Perms.Remove(p);
1092+
}
1093+
Interface.Call("OnGroupPermissionsRevoked", groupName, revokedPermissions);
10681094
}
1069-
1070-
// Call hook for plugins
1071-
Interface.Call("OnGroupPermissionRevoked", groupName, permission);
10721095
}
10731096

10741097
#endregion Group Permission

0 commit comments

Comments
 (0)