forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRestrictAttribute.cs
More file actions
271 lines (235 loc) · 9.92 KB
/
RestrictAttribute.cs
File metadata and controls
271 lines (235 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
//Copyright (c) ServiceStack, Inc. All Rights Reserved.
//License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
using System;
using System.Collections.Generic;
using System.Linq;
namespace ServiceStack
{
/// <summary>
/// Decorate on Request DTO's to alter the accessibility of a service and its visibility on /metadata pages
/// </summary>
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class RestrictAttribute : AttributeBase
{
/// <summary>
/// Allow access but hide from metadata to requests from Localhost only
/// </summary>
public bool VisibleInternalOnly
{
get { return CanShowTo(RequestAttributes.InternalNetworkAccess); }
set
{
if (value == false)
throw new Exception("Only true allowed");
VisibilityTo = RequestAttributes.InternalNetworkAccess.ToAllowedFlagsSet();
}
}
/// <summary>
/// Allow access but hide from metadata to requests from Localhost and Local Intranet only
/// </summary>
public bool VisibleLocalhostOnly
{
get { return CanShowTo(RequestAttributes.Localhost); }
set
{
if (value == false)
throw new Exception("Only true allowed");
VisibilityTo = RequestAttributes.Localhost.ToAllowedFlagsSet();
}
}
/// <summary>
/// Restrict access and hide from metadata to requests from Localhost only
/// </summary>
public bool LocalhostOnly
{
get { return HasAccessTo(RequestAttributes.Localhost) && CanShowTo(RequestAttributes.Localhost); }
set
{
if (value == false)
throw new Exception("Only true allowed");
AccessTo = RequestAttributes.Localhost.ToAllowedFlagsSet();
VisibilityTo = RequestAttributes.Localhost.ToAllowedFlagsSet();
}
}
/// <summary>
/// Restrict access and hide from metadata to requests from Localhost and Local Intranet only
/// </summary>
public bool InternalOnly
{
get { return HasAccessTo(RequestAttributes.InternalNetworkAccess) && CanShowTo(RequestAttributes.InternalNetworkAccess); }
set
{
if (value == false)
throw new Exception("Only true allowed");
AccessTo = RequestAttributes.InternalNetworkAccess.ToAllowedFlagsSet();
VisibilityTo = RequestAttributes.InternalNetworkAccess.ToAllowedFlagsSet();
}
}
/// <summary>
/// Restrict access and hide from metadata to requests from External only
/// </summary>
public bool ExternalOnly
{
get { return HasAccessTo(RequestAttributes.External) && CanShowTo(RequestAttributes.External); }
set
{
if (value == false)
throw new Exception("Only true allowed");
AccessTo = RequestAttributes.External.ToAllowedFlagsSet();
VisibilityTo = RequestAttributes.External.ToAllowedFlagsSet();
}
}
/// <summary>
/// Sets a single access restriction
/// </summary>
/// <value>Restrict Access to.</value>
public RequestAttributes AccessTo
{
get
{
return this.AccessibleToAny.Length == 0
? RequestAttributes.Any
: this.AccessibleToAny[0];
}
set
{
this.AccessibleToAny = new[] { value };
}
}
/// <summary>
/// Restrict access to any of the specified access scenarios
/// </summary>
/// <value>Access restrictions</value>
public RequestAttributes[] AccessibleToAny { get; private set; }
/// <summary>
/// Sets a single metadata Visibility restriction
/// </summary>
/// <value>Restrict metadata Visibility to.</value>
public RequestAttributes VisibilityTo
{
get
{
return this.VisibleToAny.Length == 0
? RequestAttributes.Any
: this.VisibleToAny[0];
}
set
{
this.VisibleToAny = new[] { value };
}
}
/// <summary>
/// Restrict metadata visibility to any of the specified access scenarios
/// </summary>
/// <value>Visibility restrictions</value>
public RequestAttributes[] VisibleToAny { get; private set; }
public RestrictAttribute()
{
this.AccessTo = RequestAttributes.Any;
this.VisibilityTo = RequestAttributes.Any;
}
/// <summary>
/// Restrict access and metadata visibility to any of the specified access scenarios
/// </summary>
/// <value>The restrict access to scenarios.</value>
public RestrictAttribute(params RequestAttributes[] restrictAccessAndVisibilityToScenarios)
{
this.AccessibleToAny = ToAllowedFlagsSet(restrictAccessAndVisibilityToScenarios);
this.VisibleToAny = ToAllowedFlagsSet(restrictAccessAndVisibilityToScenarios);
}
/// <summary>
/// Restrict access and metadata visibility to any of the specified access scenarios
/// </summary>
/// <value>The restrict access to scenarios.</value>
public RestrictAttribute(RequestAttributes[] allowedAccessScenarios, RequestAttributes[] visibleToScenarios)
: this()
{
this.AccessibleToAny = ToAllowedFlagsSet(allowedAccessScenarios);
this.VisibleToAny = ToAllowedFlagsSet(visibleToScenarios);
}
/// <summary>
/// Returns the allowed set of scenarios based on the user-specified restrictions
/// </summary>
/// <param name="restrictToAny"></param>
/// <returns></returns>
private static RequestAttributes[] ToAllowedFlagsSet(RequestAttributes[] restrictToAny)
{
if (restrictToAny.Length == 0)
return new[] { RequestAttributes.Any };
var scenarios = new List<RequestAttributes>();
foreach (var restrictToScenario in restrictToAny)
{
var restrictTo = restrictToScenario.ToAllowedFlagsSet();
scenarios.Add(restrictTo);
}
return scenarios.ToArray();
}
public bool CanShowTo(RequestAttributes restrictions)
{
return this.VisibleToAny.Any(scenario => (restrictions & scenario) == restrictions);
}
public bool HasAccessTo(RequestAttributes restrictions)
{
return this.AccessibleToAny.Any(scenario => (restrictions & scenario) == restrictions);
}
public bool HasNoAccessRestrictions => this.AccessTo == RequestAttributes.Any;
public bool HasNoVisibilityRestrictions => this.VisibilityTo == RequestAttributes.Any;
}
public static class RestrictExtensions
{
/// <summary>
/// Converts from a User intended restriction to a flag with all the allowed attribute flags set, e.g:
///
/// If No Network restrictions were specified all Network access types are allowed, e.g:
/// restrict EndpointAttributes.None => ... 111
///
/// If a Network restriction was specified, only it will be allowed, e.g:
/// restrict EndpointAttributes.LocalSubnet => ... 010
///
/// The returned Enum will have a flag with all the allowed attributes set
/// </summary>
/// <param name="restrictTo"></param>
/// <returns></returns>
public static RequestAttributes ToAllowedFlagsSet(this RequestAttributes restrictTo)
{
if (restrictTo == RequestAttributes.Any)
return RequestAttributes.Any;
var allowedAttrs = RequestAttributes.None;
//Network access
if (!HasAnyRestrictionsOf(restrictTo, RequestAttributes.AnyNetworkAccessType))
allowedAttrs |= RequestAttributes.AnyNetworkAccessType;
else
allowedAttrs |= (restrictTo & RequestAttributes.AnyNetworkAccessType);
//Security
if (!HasAnyRestrictionsOf(restrictTo, RequestAttributes.AnySecurityMode))
allowedAttrs |= RequestAttributes.AnySecurityMode;
else
allowedAttrs |= (restrictTo & RequestAttributes.AnySecurityMode);
//Http Method
if (!HasAnyRestrictionsOf(restrictTo, RequestAttributes.AnyHttpMethod))
allowedAttrs |= RequestAttributes.AnyHttpMethod;
else
allowedAttrs |= (restrictTo & RequestAttributes.AnyHttpMethod);
//Call Style
if (!HasAnyRestrictionsOf(restrictTo, RequestAttributes.AnyCallStyle))
allowedAttrs |= RequestAttributes.AnyCallStyle;
else
allowedAttrs |= (restrictTo & RequestAttributes.AnyCallStyle);
//Format
if (!HasAnyRestrictionsOf(restrictTo, RequestAttributes.AnyFormat))
allowedAttrs |= RequestAttributes.AnyFormat;
else
allowedAttrs |= (restrictTo & RequestAttributes.AnyFormat);
//Endpoint
if (!HasAnyRestrictionsOf(restrictTo, RequestAttributes.AnyEndpoint))
allowedAttrs |= RequestAttributes.AnyEndpoint;
else
allowedAttrs |= (restrictTo & RequestAttributes.AnyEndpoint);
return allowedAttrs;
}
public static bool HasAnyRestrictionsOf(RequestAttributes allRestrictions, RequestAttributes restrictions)
{
return (allRestrictions & restrictions) != 0;
}
}
}