tgstation-server 6.8.0
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 bool Valid { get; private set; }
14
16 public User User => user ?? throw new InvalidOperationException("AuthenticationContext is invalid!");
17
19 public PermissionSet PermissionSet => permissionSet ?? throw new InvalidOperationException("AuthenticationContext is invalid!");
20
22 public InstancePermissionSet? InstancePermissionSet { get; private set; }
23
25 public ISystemIdentity? SystemIdentity { get; private set; }
26
31
36
41 {
42 }
43
45 public void Dispose() => SystemIdentity?.Dispose();
46
53 public void Initialize(ISystemIdentity? systemIdentity, User user, InstancePermissionSet? instanceUser)
54 {
55 this.user = user ?? throw new ArgumentNullException(nameof(user));
56 if (systemIdentity == null && User.SystemIdentifier != null)
57 throw new ArgumentNullException(nameof(systemIdentity));
58 permissionSet = user.PermissionSet
60 ?? throw new ArgumentException("No PermissionSet provider", nameof(user));
61 InstancePermissionSet = instanceUser;
62 SystemIdentity = systemIdentity;
63
64 Valid = true;
65 }
66
68 public ulong GetRight(RightsType rightsType)
69 {
70 var isInstance = RightsHelper.IsInstanceRight(rightsType);
71
72 if (User == null)
73 throw new InvalidOperationException("Authentication context has no user!");
74
75 if (isInstance && InstancePermissionSet == null)
76 return 0;
77 var rightsEnum = RightsHelper.RightToType(rightsType);
78
79 // use the api versions because they're the ones that contain the actual properties
80 var typeToCheck = isInstance ? typeof(InstancePermissionSet) : typeof(PermissionSet);
81
82 var nullableType = typeof(Nullable<>);
83 var nullableRightsType = nullableType.MakeGenericType(rightsEnum);
84
85 var prop = typeToCheck.GetProperties().Where(x => x.PropertyType == nullableRightsType && x.CanRead).First();
86
87 var right = prop.GetMethod!.Invoke(
88 isInstance
91 Array.Empty<object>())
92 ?? throw new InvalidOperationException("A user right was null!");
93
94 return (ulong)right;
95 }
96 }
97}
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.
PermissionSet? PermissionSet
The Models.PermissionSet the UserGroup has.
Definition: UserGroup.cs:20
UserGroup? Group
The UserGroup the User belongs to, if any.
Definition: User.cs:32
void Initialize(ISystemIdentity? systemIdentity, User user, InstancePermissionSet? instanceUser)
Initializes the AuthenticationContext.
AuthenticationContext()
Initializes a new instance of the AuthenticationContext class.
PermissionSet? permissionSet
Backing field for PermissionSet.
ISystemIdentity? SystemIdentity
The ISystemIdentity of User if applicable.
bool Valid
If the IAuthenticationContext is for a valid login.
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