tgstation-server
6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
src
Tgstation.Server.Host
GraphQL
Metadata
RightsTypeInterceptor.cs
Go to the documentation of this file.
1
using
System;
2
using
System.Collections.Generic
;
3
using
System.Linq
;
4
using
System.Reflection
;
5
6
using
HotChocolate.Configuration
;
7
using
HotChocolate.Types
;
8
using
HotChocolate.Types.Descriptors
;
9
using
HotChocolate.Types.Descriptors.Definitions
;
10
using
Tgstation.Server.Api.Rights
;
11
using
Tgstation.Server.Host.GraphQL.Types
;
12
13
namespace
Tgstation.Server.Host.GraphQL.Metadata
14
{
18
sealed
class
RightsTypeInterceptor
:
TypeInterceptor
19
{
23
const
string
IsPrefix
=
"is"
;
24
28
const
string
NoneFieldName
=
$
"{IsPrefix}None"
;
29
33
private
readonly
HashSet<string>
inputNames
;
34
38
public
RightsTypeInterceptor
()
39
{
40
var
rightTypes
=
Enum
.GetValues<
RightsType
>();
41
inputNames
=
new
HashSet<string>
(
rightTypes
.Length);
42
43
foreach
(
var
rightType
in
rightTypes
)
44
{
45
var
rightName
=
rightType
.ToString();
46
inputNames
.Add(
$
"{rightName}RightsFlagsInput"
);
47
}
48
}
49
54
static
void
FixFields
(
IBindableList<InputFieldDefinition>
fields
)
55
{
56
InputFieldDefinition
?
noneField
=
null
;
57
58
foreach
(
var
field
in
fields
)
59
{
60
var
fieldName
=
field
.Name;
61
if
(
fieldName
==
NoneFieldName
)
62
{
63
noneField
=
field
;
64
continue
;
65
}
66
67
if
(!
fieldName
.StartsWith(
IsPrefix
))
68
throw
new
InvalidOperationException
(
"Expected flags enum type field to start with \"is\"!"
);
69
70
field.Name
=
$
"can{fieldName[IsPrefix.Length..]}"
;
71
field.Type
=
TypeReference
.Parse(
$
"{ScalarNames.Boolean}!"
);
72
}
73
74
if
(
noneField
==
null
)
75
throw
new
InvalidOperationException
(
$
"Expected flags enum type field to contain \"{NoneFieldName}\"!"
);
76
77
fields
.Remove(
noneField
);
78
}
79
84
static
void
FixFormatter
(
IInputValueFormatter
inputValueFormatter
)
85
{
86
// now we're hacking privates, but there's a dictionary with bad keys here that needs adjusting
87
var
dictionary
= (
Dictionary<string, object>
)(
inputValueFormatter
88
.
GetType
()
89
.GetField(
"_flags"
,
BindingFlags
.Instance |
BindingFlags
.NonPublic)
90
?.GetValue(
inputValueFormatter
)
91
??
throw
new
InvalidOperationException
(
"Could not locate private enum mapping dictionary field!"
));
92
93
foreach
(
var
key
in
dictionary
.Keys.ToList())
94
{
95
if
(
key
==
NoneFieldName
)
96
{
97
dictionary
.Remove(
key
);
98
continue
;
99
}
100
101
var
value
=
dictionary
[
key
];
102
var
newKey
=
$
"can{key.Substring(IsPrefix.Length)}"
;
103
dictionary
.Remove(
key
);
104
dictionary
.Add(
newKey
,
value
);
105
}
106
}
107
109
public
override
void
OnAfterRegisterDependencies
(
ITypeDiscoveryContext
discoveryContext
,
DefinitionBase
definition
)
110
{
111
ArgumentNullException
.ThrowIfNull(
definition
);
112
113
if
(
definition
is
InputObjectTypeDefinition
inputTypeDef
)
114
{
115
const
string
PermissionSetInputName
=
$
"{nameof(PermissionSet)}Input"
;
116
const
string
InstancePermissionSetInputName
=
$
"{nameof(InstancePermissionSet)}Input"
;
117
118
var
name
=
inputTypeDef
.Name;
119
if
(
inputNames
.Contains(
name
))
120
FixFields
(
inputTypeDef
.Fields);
121
else
if
(
name
==
PermissionSetInputName
||
name
==
InstancePermissionSetInputName
)
122
foreach
(
var
field
in
inputTypeDef
.Fields)
123
FixFormatter
(
field
.Formatters.Single());
124
}
125
}
126
}
127
}
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor
Fixes the names used for the default flags types in API rights.
Definition
RightsTypeInterceptor.cs:19
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.FixFormatter
static void FixFormatter(IInputValueFormatter inputValueFormatter)
Fix the inputValueFormatter for a tweaked field.
Definition
RightsTypeInterceptor.cs:84
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.NoneFieldName
const string NoneFieldName
Name given to default None fields.
Definition
RightsTypeInterceptor.cs:28
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.RightsTypeInterceptor
RightsTypeInterceptor()
Initializes a new instance of the RightsTypeInterceptor class.
Definition
RightsTypeInterceptor.cs:38
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.IsPrefix
const string IsPrefix
Prefix normally used by hot chocolate for flag enums.
Definition
RightsTypeInterceptor.cs:23
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.inputNames
readonly HashSet< string > inputNames
Names of rights GraphQL input types.
Definition
RightsTypeInterceptor.cs:33
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.FixFields
static void FixFields(IBindableList< InputFieldDefinition > fields)
Fix the "is" prefix on a given set of fields .
Definition
RightsTypeInterceptor.cs:54
Tgstation.Server.Host.GraphQL.Metadata.RightsTypeInterceptor.OnAfterRegisterDependencies
override void OnAfterRegisterDependencies(ITypeDiscoveryContext discoveryContext, DefinitionBase definition)
Definition
RightsTypeInterceptor.cs:109
TypeInterceptor
Tgstation.Server.Api.Rights
Definition
AdministrationRights.cs:4
Tgstation.Server.Api.Rights.ConfigurationRights.List
@ List
User may list files if the Models.Instance allows it.
Tgstation.Server.Api.Rights.RightsType
RightsType
The type of rights a model uses.
Definition
RightsType.cs:7
Tgstation.Server.Host.GraphQL.Metadata
Definition
RightsHolderType{TRight}.cs:8
Tgstation.Server.Host.GraphQL.Types
Definition
ChatBot.cs:9
Generated by
1.9.8