tgstation-server
The /tg/station 13 server suite
AuthenticationContext.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
5 
7 {
12  {
14  public User User { get; }
15 
17  public InstanceUser InstanceUser { get; }
18 
21 
25  public AuthenticationContext() { }
26 
33  public AuthenticationContext(ISystemIdentity systemIdentity, User user, InstanceUser instanceUser)
34  {
35  User = user ?? throw new ArgumentNullException(nameof(user));
36  if (systemIdentity == null && User.SystemIdentifier != null)
37  throw new ArgumentNullException(nameof(systemIdentity));
38  InstanceUser = instanceUser;
39  SystemIdentity = systemIdentity;
40  }
41 
43  public void Dispose() => SystemIdentity?.Dispose();
44 
46  public ulong GetRight(RightsType rightsType)
47  {
48  var isInstance = RightsHelper.IsInstanceRight(rightsType);
49 
50  if (User == null)
51  throw new InvalidOperationException("Authentication context has no user!");
52 
53  if (isInstance && InstanceUser == null)
54  return 0;
55  var rightsEnum = RightsHelper.RightToType(rightsType);
56 
57  // use the api versions because they're the ones that contain the actual properties
58  var typeToCheck = isInstance ? typeof(InstanceUser) : typeof(User);
59 
60  var nullableType = typeof(Nullable<>);
61  var nullableRightsType = nullableType.MakeGenericType(rightsEnum);
62 
63  var prop = typeToCheck.GetProperties().Where(x => x.PropertyType == nullableRightsType).First();
64 
65  var right = prop.GetMethod.Invoke(isInstance ? (object)InstanceUser : User, Array.Empty<object>());
66 
67  if (right == null)
68  throw new InvalidOperationException("A user right was null!");
69  return (ulong)right;
70  }
71  }
72 }
static bool IsInstanceRight(RightsType rightsType)
Check if a given rightsType is meant for an Models.Instance
AuthenticationContext(ISystemIdentity systemIdentity, User user, InstanceUser instanceUser)
Construct an AuthenticationContext
AuthenticationContext()
Construct an empty AuthenticationContext
RightsType
The type of rights a model uses
Definition: RightsType.cs:6
Represents a user on the current global::System.Runtime.InteropServices.OSPlatform
Represents the currently authenticated Api.Models.User
ulong GetRight(RightsType rightsType)
Get the value of a given rightsType
static Type RightToType(RightsType rightsType)
Map a given rightsType to its respective Enum Type
string SystemIdentifier
The SID/UID of the User on Windows/POSIX respectively
Definition: User.cs:32