Last active
December 18, 2015 15:59
-
-
Save bpruitt-goddard/5808322 to your computer and use it in GitHub Desktop.
ServiceStack PR 736
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomRequiredPermissionAttribute : RequestFilterAttribute, IApiResponseAttribute | |
{ | |
public List<string> RequiredPermissions { get; set; } | |
private string permissionsDesc | |
{ | |
get | |
{ | |
return "Required Permissions: {0}".FormatUsing(string.Join(",", RequiredPermissions)); | |
} | |
} | |
public CustomRequiredPermissionAttribute(ApplyTo applyTo, params string[] permissions) | |
{ | |
this.RequiredPermissions = permissions.ToList(); | |
this.ApplyTo = applyTo; | |
this.Priority = (int)RequestFilterPriority.RequiredPermission; | |
} | |
public CustomRequiredPermissionAttribute(params string[] permissions) | |
: this(ApplyTo.All, permissions) { } | |
public override void Execute(IHttpRequest req, IHttpResponse res, object requestDto) | |
{ | |
if (HasAllPermissions(req)) return; | |
throw new HttpError((int)HttpStatusCode.Forbidden, "Invalid Permission", permissionsDesc); | |
} | |
public bool HasAllPermissions(IHttpRequest req) | |
{ | |
var authorizer = req.TryResolve<IAuthorizationService>(); | |
return authorizer.CurrentUserHasPermission(RequiredPermissions.ToArray()); | |
} | |
public int StatusCode | |
{ | |
get { return (int) HttpStatusCode.Forbidden; } | |
} | |
public string Description | |
{ | |
get { return permissionsDesc; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment