forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRequestFilterAttribute.cs
More file actions
65 lines (57 loc) · 2.06 KB
/
RequestFilterAttribute.cs
File metadata and controls
65 lines (57 loc) · 2.06 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.ServiceHost;
using ServiceStack.Common;
using ServiceStack.WebHost.Endpoints;
namespace ServiceStack.ServiceInterface
{
public enum RequestFilterPriority : int
{
Authenticate = -100,
RequiredRole = -90,
RequiredPermission = -80,
}
/// <summary>
/// Base class to create request filter attributes only for specific HTTP methods (GET, POST...)
/// </summary>
public abstract class RequestFilterAttribute : Attribute, IHasRequestFilter
{
public int Priority { get; set; }
public ApplyTo ApplyTo { get; set; }
public RequestFilterAttribute()
{
ApplyTo = ApplyTo.All;
}
/// <summary>
/// Creates a new <see cref="RequestFilterAttribute"/>
/// </summary>
/// <param name="applyTo">Defines when the filter should be executed</param>
public RequestFilterAttribute(ApplyTo applyTo)
{
ApplyTo = applyTo;
}
public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
{
ApplyTo httpMethod = req.HttpMethodAsApplyTo();
if (ApplyTo.Has(httpMethod))
this.Execute(req, res, requestDto);
}
/// <summary>
/// This method is only executed if the HTTP method matches the <see cref="ApplyTo"/> property.
/// </summary>
/// <param name="req">The http request wrapper</param>
/// <param name="res">The http response wrapper</param>
/// <param name="requestDto">The request DTO</param>
public abstract void Execute(IHttpRequest req, IHttpResponse res, object requestDto);
/// <summary>
/// Create a ShallowCopy of this instance.
/// </summary>
/// <returns></returns>
public virtual IHasRequestFilter Copy()
{
return (IHasRequestFilter)this.MemberwiseClone();
}
}
}