tgstation-server 5.12.7
The /tg/station 13 server suite
Loading...
Searching...
No Matches
AuthenticationContext.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
3
6
8{
11 {
13 public User User { get; }
14
17
20
23
28 {
29 }
30
37 public AuthenticationContext(ISystemIdentity systemIdentity, User user, InstancePermissionSet instanceUser)
38 {
39 User = user ?? throw new ArgumentNullException(nameof(user));
40 if (systemIdentity == null && User.SystemIdentifier != null)
41 throw new ArgumentNullException(nameof(systemIdentity));
42 PermissionSet = user.PermissionSet
43 ?? user.Group.PermissionSet
44 ?? throw new ArgumentException("No PermissionSet provider", nameof(user));
45 InstancePermissionSet = instanceUser;
46 SystemIdentity = systemIdentity;
47 }
48
50 public void Dispose() => SystemIdentity?.Dispose();
51
53 public ulong GetRight(RightsType rightsType)
54 {
55 var isInstance = RightsHelper.IsInstanceRight(rightsType);
56
57 if (User == null)
58 throw new InvalidOperationException("Authentication context has no user!");
59
60 if (isInstance && InstancePermissionSet == null)
61 return 0;
62 var rightsEnum = RightsHelper.RightToType(rightsType);
63
64 // use the api versions because they're the ones that contain the actual properties
65 var typeToCheck = isInstance ? typeof(InstancePermissionSet) : typeof(PermissionSet);
66
67 var nullableType = typeof(Nullable<>);
68 var nullableRightsType = nullableType.MakeGenericType(rightsEnum);
69
70 var prop = typeToCheck.GetProperties().Where(x => x.PropertyType == nullableRightsType).First();
71
72 var right = prop.GetMethod.Invoke(isInstance ? (object)InstancePermissionSet : PermissionSet, Array.Empty<object>());
73
74 if (right == null)
75 throw new InvalidOperationException("A user right was null!");
76 return (ulong)right;
77 }
78 }
79}
string? SystemIdentifier
The SID/UID of the UserModelBase on Windows/POSIX respectively.
static bool IsInstanceRight(RightsType rightsType)
Check if a given rightsType is meant for an Models.Instance.
static Type RightToType(RightsType rightsType)
Map a given rightsType to its respective Enum Type.
ISystemIdentity SystemIdentity
The ISystemIdentity of User if applicable.
AuthenticationContext()
Initializes a new instance of the AuthenticationContext class.
AuthenticationContext(ISystemIdentity systemIdentity, User user, InstancePermissionSet instanceUser)
Initializes a new instance of the AuthenticationContext class.
ulong GetRight(RightsType rightsType)
Get the value of a given rightsType . The value of rightsType . Note that if InstancePermissionSet is...
Represents the currently authenticated Models.User.
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform.
RightsType
The type of rights a model uses.
Definition: RightsType.cs:7