forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExecuteServiceStackFiltersAttribute.cs
More file actions
85 lines (70 loc) · 2.66 KB
/
ExecuteServiceStackFiltersAttribute.cs
File metadata and controls
85 lines (70 loc) · 2.66 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServiceStack.Auth;
namespace ServiceStack.Mvc
{
public class ExecuteServiceStackFiltersAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var ssController = filterContext.Controller as ServiceStackController;
if (ssController == null) return;
var authAttrs = GetActionAndControllerAttributes<AuthenticateAttribute>(filterContext);
if (authAttrs.Count > 0 && ( ssController.AuthSession==null || !ssController.AuthSession.IsAuthenticated))
{
filterContext.Result = ssController.AuthenticationErrorResult;
return;
}
var roleAttrs = GetActionAndControllerAttributes<RequiredRoleAttribute>(filterContext);
var anyRoleAttrs = GetActionAndControllerAttributes<RequiresAnyRoleAttribute>(filterContext);
var permAttrs = GetActionAndControllerAttributes<RequiredPermissionAttribute>(filterContext);
var anyPermAttrs = GetActionAndControllerAttributes<RequiresAnyPermissionAttribute>(filterContext);
if (roleAttrs.Count + anyRoleAttrs.Count + permAttrs.Count + anyPermAttrs.Count == 0) return;
var httpReq = HttpContext.Current.Request.ToRequest();
var userAuthRepo = httpReq.TryResolve<IAuthRepository>();
var hasRoles = roleAttrs.All(x => x.HasAllRoles(httpReq, ssController.AuthSession, userAuthRepo));
if (!hasRoles)
{
filterContext.Result = ssController.AuthorizationErrorResult;
return;
}
var hasAnyRole = anyRoleAttrs.All(x => x.HasAnyRoles(httpReq, ssController.AuthSession, userAuthRepo));
if (!hasAnyRole)
{
filterContext.Result = ssController.AuthorizationErrorResult;
return;
}
var hasPermssions = permAttrs.All(x => x.HasAllPermissions(httpReq, ssController.AuthSession, userAuthRepo));
if (!hasPermssions)
{
filterContext.Result = ssController.AuthorizationErrorResult;
return;
}
var hasAnyPermission = anyPermAttrs.All(x => x.HasAnyPermissions(httpReq, ssController.AuthSession, userAuthRepo));
if (!hasAnyPermission)
{
filterContext.Result = ssController.AuthorizationErrorResult;
return;
}
}
private static List<T> GetActionAndControllerAttributes<T>(ActionExecutingContext filterContext)
where T : Attribute
{
var attrs = new List<T>();
var attr = filterContext.ActionDescriptor
.GetCustomAttributes(typeof(T), true)
.FirstOrDefault() as T;
if (attr != null)
attrs.Add(attr);
attr = filterContext.Controller.GetType()
.GetCustomAttributes(typeof(T), true)
.FirstOrDefault() as T;
if (attr != null)
attrs.Add(attr);
return attrs;
}
}
}