Download ContactManagementAPI/Security/RequireRightAttribute.cs from abrahamcbe/contact-management-system: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/abrahamcbe/contact-management-system/resolve/main/ContactManagementAPI/Security/RequireRightAttribute.cs
- Command line
-
hf download hf://spaces/abrahamcbe/contact-management-system/ContactManagementAPI/Security/RequireRightAttribute.cs
-
curl -L -o RequireRightAttribute.cs https://huggingface.co/spaces/abrahamcbe/contact-management-system/resolve/main/ContactManagementAPI/Security/RequireRightAttribute.cs
1.71 kB
| using System.Threading.Tasks; | |
| using ContactManagementAPI.Models; | |
| using ContactManagementAPI.Services; | |
| using Microsoft.AspNetCore.Mvc; | |
| using Microsoft.AspNetCore.Mvc.Filters; | |
| namespace ContactManagementAPI.Security | |
| { | |
| public class RequireRightAttribute : TypeFilterAttribute | |
| { | |
| public RequireRightAttribute(string rightKey) : base(typeof(RequireRightFilter)) | |
| { | |
| Arguments = new object[] { rightKey }; | |
| } | |
| } | |
| public class RequireRightFilter : IAsyncActionFilter | |
| { | |
| private readonly string _rightKey; | |
| private readonly UserContextService _userContext; | |
| private readonly AuthorizationService _authorizationService; | |
| public RequireRightFilter(string rightKey, UserContextService userContext, AuthorizationService authorizationService) | |
| { | |
| _rightKey = rightKey; | |
| _userContext = userContext; | |
| _authorizationService = authorizationService; | |
| } | |
| public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | |
| { | |
| if (!_userContext.UserId.HasValue) | |
| { | |
| var returnUrl = context.HttpContext.Request.Path + context.HttpContext.Request.QueryString; | |
| context.Result = new RedirectToActionResult("Login", "Account", new { returnUrl }); | |
| return; | |
| } | |
| var hasRight = _authorizationService.HasRight(_userContext.UserId.Value, _rightKey); | |
| if (!hasRight) | |
| { | |
| context.Result = new RedirectToActionResult("AccessDenied", "Account", null); | |
| return; | |
| } | |
| await next(); | |
| } | |
| } | |
| } | |