From 6a5f2694868551411a113421c5a21fdc578dc7f5 Mon Sep 17 00:00:00 2001 From: Tom Date: Thu, 15 Jun 2023 20:48:33 +0200 Subject: [PATCH] Adds Hook support for multi permission grants/revokes --- src/Libraries/Permission.cs | 279 +++++++++++++++++++----------------- 1 file changed, 151 insertions(+), 128 deletions(-) diff --git a/src/Libraries/Permission.cs b/src/Libraries/Permission.cs index d9601607..fce43fe1 100644 --- a/src/Libraries/Permission.cs +++ b/src/Libraries/Permission.cs @@ -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 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); + } } /// @@ -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); } /// @@ -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)); } /// @@ -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(); } + /// + /// Returns usersIDs in that group + /// + /// + /// + [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(); + } + /// /// Returns the title of the specified group /// @@ -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 permissions; - - if (owner == null) - { - permissions = new HashSet(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 permissions; + if (owner == null) + { + permissions = new HashSet(registeredPermissions.Values.SelectMany(v => v)); + } + else if (!registeredPermissions.TryGetValue(owner, out permissions)) { return; } - // Call hook for plugins - Interface.Call("OnUserPermissionGranted", playerId, permission); + IEnumerable 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); + } } /// @@ -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 @@ -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 permissions; - - if (owner == null) - { - permissions = new HashSet(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 permissions; + if (owner == null) + { + permissions = new HashSet(registeredPermissions.Values.SelectMany(v => v)); + } + else if (!registeredPermissions.TryGetValue(owner, out permissions)) { return; } - // Call hook for plugins - Interface.Call("OnGroupPermissionGranted", groupName, permission); + IEnumerable 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); + } } /// @@ -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