tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
RightsTypeInterceptor.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
5
11
13{
18 {
22 const string IsPrefix = "is";
23
27 const string NoneFieldName = $"{IsPrefix}None";
28
33
38
43 {
44 var rightTypes = Enum.GetValues<RightsType>();
47
48 foreach (var rightType in rightTypes)
49 {
50 var rightName = rightType.ToString();
51 var flagName = $"{rightName}RightsFlags";
52
54 inputNames.Add($"{flagName}Input");
55 }
56 }
57
65 {
66 TField? noneField = null;
67
68 foreach (var field in fields)
69 {
70 var fieldName = field.Name;
72 {
74 continue;
75 }
76
77 if (!fieldName.StartsWith(IsPrefix))
78 throw new InvalidOperationException("Expected flags enum type field to start with \"is\"!");
79
80 field.Name = $"can{fieldName[IsPrefix.Length..]}";
81 }
82
83 if (noneField == null)
84 throw new InvalidOperationException($"Expected flags enum type field to contain \"{NoneFieldName}\"!");
85
86 fields.Remove(noneField);
87 }
88
94 {
95 // now we're hacking privates, but there's a dictionary with bad keys here that needs adjusting
97 .GetType()
98 .GetField("_flags", BindingFlags.Instance | BindingFlags.NonPublic)
99 ?.GetValue(inputValueFormatter)
100 ?? throw new InvalidOperationException("Could not locate private enum mapping dictionary field!"));
101
102 foreach (var key in dictionary.Keys.ToList())
103 {
104 if (key == NoneFieldName)
105 {
106 dictionary.Remove(key);
107 continue;
108 }
109
111 var newKey = $"can{key.Substring(IsPrefix.Length)}";
112 dictionary.Remove(key);
113 dictionary.Add(newKey, value);
114 }
115 }
116
119 {
121
123 {
124 if (objectNames.Contains(objectTypeDef.Name))
125 FixFields(objectTypeDef.Fields);
126 }
128 {
129 const string PermissionSetInputName = $"{nameof(PermissionSet)}Input";
130 const string InstancePermissionSetInputName = $"{nameof(InstancePermissionSet)}Input";
131
132 var name = inputTypeDef.Name;
133 if (inputNames.Contains(name))
134 FixFields(inputTypeDef.Fields);
136 foreach (var field in inputTypeDef.Fields)
137 FixFormatter(field.Formatters.Single());
138 }
139 }
140 }
141}
Fixes the names used for the default flags types in API rights.
RightsTypeInterceptor()
Initializes a new instance of the RightsTypeInterceptor class.
readonly HashSet< string > inputNames
Names of rights GraphQL input types.
const string IsPrefix
Prefix normally used by hot chocolate for flag enums.
override void OnAfterRegisterDependencies(ITypeDiscoveryContext discoveryContext, DefinitionBase definition)
const string NoneFieldName
Name given to default None fields.
static void FixFields< TField >(IBindableList< TField > fields)
Fix the "is" prefix on a given set of fields .
static void FixFormatter(IInputValueFormatter inputValueFormatter)
Fix the inputValueFormatter for a tweaked field.
readonly HashSet< string > objectNames
Names of rights GraphQL object types.
@ List
User may list files if the Models.Instance allows it.
RightsType
The type of rights a model uses.
Definition RightsType.cs:7