Permissions
SwiftlyS2's permission service handles access checks, grant/revoke, and permission hierarchies.
Accessing the Permission Service
Available through Core.Permission (APIISwiftlyCore.Permission, type APIIPermissionManager). All checks and mutations key off SteamID64 (ulong) - use player.SteamID.
services.AddSwiftly(Core) registers Core.Permission as a singleton, so
APIIPermissionManager can be constructor-injected - see
Dependency Injection.
Checking Permissions
APIIPermissionManager.PlayerHasPermission takes (ulong steamId, string permission) and checks one key;
APIIPermissionManager.PlayerHasPermissions takes (ulong steamId, IEnumerable<string> permissions) and requires every key in the list.
Both return bool and support xxx.* wildcard keys.
if (Core.Permission.PlayerHasPermission(player.SteamID, "myplugin.admin"))
player.SendChat("You have admin access.");
bool canUseRestrictedAction = Core.Permission.PlayerHasPermissions(
player.SteamID,
["myplugin.admin.kick", "myplugin.admin.ban"]
);
bool isAdmin = Core.Permission.PlayerHasPermission(player.SteamID, "myplugin.admin.*"); // wildcard keyGranting and Revoking Permissions
APIIPermissionManager.AddPermission and
APIIPermissionManager.RemovePermission each take a
(ulong steamId, string permission) pair;
APIIPermissionManager.ClearPermissions takes only the
SteamID and clears all of the player's permissions, both base and temporary:
Core.Permission.AddPermission(player.SteamID, "myplugin.vip");
Core.Permission.RemovePermission(player.SteamID, "myplugin.vip");
Core.Permission.ClearPermissions(player.SteamID); // clears both base and temporary permissionsClearPermission (singular) is obsolete, forwarding to ClearPermissions -
use the plural in new code.
Listing a Player's Permissions
APIIPermissionManager.GetPlayerPermissions takes the
ulong steamId and returns an IEnumerable<string> of everything the player
effectively has, including permissions inherited from sub-permissions.
IEnumerable<string> permissions = Core.Permission.GetPlayerPermissions(player.SteamID);Sub-Permissions (Hierarchy)
One permission key can imply another: APIIPermissionManager.AddSubPermission takes (string permission, string subPermission)
and records that permission also grants subPermission. Both hierarchy
members take only the two keys, because the hierarchy is defined for the
permission keys themselves rather than per player.
Core.Permission.AddSubPermission("myplugin.admin", "myplugin.moderator");
Core.Permission.AddPermission(player.SteamID, "myplugin.admin");
bool isModerator = Core.Permission.PlayerHasPermission(player.SteamID, "myplugin.moderator"); // true, inherited from myplugin.adminRemove an edge with APIIPermissionManager.RemoveSubPermission, which takes the same pair:
Core.Permission.RemoveSubPermission("myplugin.admin", "myplugin.moderator");Permission Naming Convention
A consistent plugin.scope.action scheme keeps wildcards and hierarchies predictable, e.g. myplugin.admin.kick, myplugin.admin.ban, myplugin.vip.access, myplugin.commands.reload.
Using Permissions with Commands
APICommand's permission parameter checks this same store, so you rarely need
APIIPermissionManager.PlayerHasPermission directly
inside a handler:
[Command("ban", permission: "myplugin.admin.ban")]
public void OnBanCommand(ICommandContext context)
{
// Only reached if context.Sender has "myplugin.admin.ban"
}See the Commands page for the full attribute reference.
Reference
| Member | Signature | Returns |
|---|---|---|
| APIIPermissionManager.PlayerHasPermission | bool PlayerHasPermission(ulong steamId, string permission) | true if the player has permission; supports xxx.* wildcards |
| APIIPermissionManager.PlayerHasPermissions | bool PlayerHasPermissions(ulong steamId, IEnumerable<string> permissions) | true if the player has every key in permissions |
| APIIPermissionManager.AddPermission | void AddPermission(ulong steamId, string permission) | - |
| APIIPermissionManager.RemovePermission | void RemovePermission(ulong steamId, string permission) | - |
| APIIPermissionManager.AddSubPermission | void AddSubPermission(string permission, string subPermission) | - |
| APIIPermissionManager.RemoveSubPermission | void RemoveSubPermission(string permission, string subPermission) | - |
| APIIPermissionManager.ClearPermissions | void ClearPermissions(ulong steamId) | - |
| APIIPermissionManager.GetPlayerPermissions | IEnumerable<string> GetPlayerPermissions(ulong steamId) | every permission the player has, including inherited ones |