Added permission checks to all controllers. Added role permission loading. Added frontend permission checks. Implemented user logout in admin panel.
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,11 +1,14 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.Api.Mappers;
|
||||
using Moonlight.Api.Services;
|
||||
using Moonlight.Shared;
|
||||
using Moonlight.Shared.Http.Responses.Admin;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
|
||||
[ApiController]
|
||||
[Authorize(Policy = Permissions.System.Diagnose)]
|
||||
[Route("api/admin/system/diagnose")]
|
||||
public class DiagnoseController : Controller
|
||||
{
|
||||
|
||||
@@ -1,14 +1,17 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Mappers;
|
||||
using Moonlight.Shared;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
using Moonlight.Shared.Http.Responses.Users;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
|
||||
[ApiController]
|
||||
[Authorize(Policy = Permissions.Roles.Members)]
|
||||
[Route("api/admin/roles/{roleId:int}/members")]
|
||||
public class RoleMembersController : Controller
|
||||
{
|
||||
@@ -59,6 +62,7 @@ public class RoleMembersController : Controller
|
||||
|
||||
// Pagination
|
||||
var items = query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(startIndex)
|
||||
.Take(length)
|
||||
.ProjectToDto()
|
||||
@@ -100,6 +104,7 @@ public class RoleMembersController : Controller
|
||||
|
||||
// Pagination
|
||||
var items = query
|
||||
.OrderBy(x => x.Id)
|
||||
.Skip(startIndex)
|
||||
.Take(length)
|
||||
.ProjectToDto()
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Mappers;
|
||||
using Moonlight.Shared;
|
||||
using Moonlight.Shared.Http.Requests;
|
||||
using Moonlight.Shared.Http.Requests.Roles;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
@@ -22,6 +24,7 @@ public class RolesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize(Policy = Permissions.Roles.View)]
|
||||
public async Task<ActionResult<PagedData<RoleDto>>> GetAsync(
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int length,
|
||||
@@ -57,6 +60,7 @@ public class RolesController : Controller
|
||||
|
||||
// Pagination
|
||||
var data = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.ProjectToDto()
|
||||
.Skip(startIndex)
|
||||
.Take(length)
|
||||
@@ -68,6 +72,7 @@ public class RolesController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Roles.View)]
|
||||
public async Task<ActionResult<RoleDto>> GetAsync([FromRoute] int id)
|
||||
{
|
||||
var role = await RoleRepository
|
||||
@@ -81,6 +86,7 @@ public class RolesController : Controller
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = Permissions.Roles.Create)]
|
||||
public async Task<ActionResult<RoleDto>> CreateAsync([FromBody] CreateRoleDto request)
|
||||
{
|
||||
var role = RoleMapper.ToEntity(request);
|
||||
@@ -91,6 +97,7 @@ public class RolesController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Roles.Edit)]
|
||||
public async Task<ActionResult<RoleDto>> UpdateAsync([FromRoute] int id, [FromBody] UpdateRoleDto request)
|
||||
{
|
||||
var role = await RoleRepository
|
||||
@@ -108,6 +115,7 @@ public class RolesController : Controller
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Roles.Delete)]
|
||||
public async Task<ActionResult> DeleteAsync([FromRoute] int id)
|
||||
{
|
||||
var role = await RoleRepository
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Moonlight.Api.Services;
|
||||
using Moonlight.Shared;
|
||||
using Moonlight.Shared.Http.Responses.Admin;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
@@ -16,6 +18,7 @@ public class SystemController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("info")]
|
||||
[Authorize(Policy = Permissions.System.Info)]
|
||||
public async Task<ActionResult<SystemInfoDto>> GetInfoAsync()
|
||||
{
|
||||
var cpuUsage = await ApplicationService.GetCpuUsageAsync();
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Services;
|
||||
using Moonlight.Shared;
|
||||
|
||||
namespace Moonlight.Api.Http.Controllers.Admin;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/admin/users/{id:int}")]
|
||||
public class UserActionsController : Controller
|
||||
{
|
||||
// Consider building a service for deletion and logout or actions in general
|
||||
|
||||
private readonly DatabaseRepository<User> UsersRepository;
|
||||
private readonly IMemoryCache Cache;
|
||||
|
||||
public UserActionsController(DatabaseRepository<User> usersRepository, IMemoryCache cache)
|
||||
{
|
||||
UsersRepository = usersRepository;
|
||||
Cache = cache;
|
||||
}
|
||||
|
||||
[HttpPost("logout")]
|
||||
[Authorize(Policy = Permissions.Users.Logout)]
|
||||
public async Task<ActionResult> LogoutAsync([FromRoute] int id)
|
||||
{
|
||||
var user = await UsersRepository
|
||||
.Query()
|
||||
.FirstOrDefaultAsync(u => u.Id == id);
|
||||
|
||||
if(user == null)
|
||||
return Problem("User not found", statusCode: 404);
|
||||
|
||||
user.InvalidateTimestamp = DateTimeOffset.UtcNow;
|
||||
await UsersRepository.UpdateAsync(user);
|
||||
|
||||
Cache.Remove(string.Format(UserAuthService.ValidationCacheKeyPattern, id));
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Api.Mappers;
|
||||
using Moonlight.Shared;
|
||||
using Moonlight.Shared.Http.Requests;
|
||||
using Moonlight.Shared.Http.Requests.Users;
|
||||
using Moonlight.Shared.Http.Responses;
|
||||
@@ -24,6 +25,7 @@ public class UsersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Authorize(Policy = Permissions.Users.View)]
|
||||
public async Task<ActionResult<PagedData<UserDto>>> GetAsync(
|
||||
[FromQuery] int startIndex,
|
||||
[FromQuery] int length,
|
||||
@@ -62,6 +64,7 @@ public class UsersController : Controller
|
||||
|
||||
// Pagination
|
||||
var data = await query
|
||||
.OrderBy(x => x.Id)
|
||||
.ProjectToDto()
|
||||
.Skip(startIndex)
|
||||
.Take(length)
|
||||
@@ -73,6 +76,7 @@ public class UsersController : Controller
|
||||
}
|
||||
|
||||
[HttpGet("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Users.View)]
|
||||
public async Task<ActionResult<UserDto>> GetAsync([FromRoute] int id)
|
||||
{
|
||||
var user = await UserRepository
|
||||
@@ -86,6 +90,7 @@ public class UsersController : Controller
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[Authorize(Policy = Permissions.Users.Create)]
|
||||
public async Task<ActionResult<UserDto>> CreateAsync([FromBody] CreateUserDto request)
|
||||
{
|
||||
var user = UserMapper.ToEntity(request);
|
||||
@@ -97,6 +102,7 @@ public class UsersController : Controller
|
||||
}
|
||||
|
||||
[HttpPatch("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Users.Edit)]
|
||||
public async Task<ActionResult<UserDto>> UpdateAsync([FromRoute] int id, [FromBody] UpdateUserDto request)
|
||||
{
|
||||
var user = await UserRepository
|
||||
@@ -113,6 +119,7 @@ public class UsersController : Controller
|
||||
}
|
||||
|
||||
[HttpDelete("{id:int}")]
|
||||
[Authorize(Policy = Permissions.Users.Delete)]
|
||||
public async Task<ActionResult> DeleteAsync([FromRoute] int id)
|
||||
{
|
||||
var user = await UserRepository
|
||||
|
||||
@@ -1,11 +1,29 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Moonlight.Shared;
|
||||
|
||||
namespace Moonlight.Api.Implementations;
|
||||
|
||||
public class PermissionAuthorizationHandler : AuthorizationHandler<PermissionRequirement>
|
||||
{
|
||||
protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionRequirement requirement)
|
||||
protected override Task HandleRequirementAsync(
|
||||
AuthorizationHandlerContext context,
|
||||
PermissionRequirement requirement)
|
||||
{
|
||||
|
||||
var permissionClaim = context.User.FindFirst(x =>
|
||||
x.Type.Equals(Permissions.ClaimType, StringComparison.OrdinalIgnoreCase) &&
|
||||
x.Value.Equals(requirement.Identifier, StringComparison.OrdinalIgnoreCase)
|
||||
);
|
||||
|
||||
if (permissionClaim == null)
|
||||
{
|
||||
context.Fail(new AuthorizationFailureReason(
|
||||
this,
|
||||
$"User does not have the requested permission '{requirement.Identifier}'"
|
||||
));
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
context.Succeed(requirement);
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
@@ -15,13 +15,11 @@ public class PermissionPolicyProvider : IAuthorizationPolicyProvider
|
||||
|
||||
public async Task<AuthorizationPolicy?> GetPolicyAsync(string policyName)
|
||||
{
|
||||
if (!policyName.StartsWith("Permission:", StringComparison.OrdinalIgnoreCase))
|
||||
if (!policyName.StartsWith(Permissions.Prefix, StringComparison.OrdinalIgnoreCase))
|
||||
return await FallbackProvider.GetPolicyAsync(policyName);
|
||||
|
||||
var identifier = policyName.Substring(Permissions.Prefix.Length);
|
||||
|
||||
var policy = new AuthorizationPolicyBuilder();
|
||||
policy.AddRequirements(new PermissionRequirement(identifier));
|
||||
policy.AddRequirements(new PermissionRequirement(policyName));
|
||||
|
||||
return policy.Build();
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
|
||||
using Moonlight.Api.Configuration;
|
||||
using Moonlight.Api.Database;
|
||||
using Moonlight.Api.Database.Entities;
|
||||
using Moonlight.Shared;
|
||||
|
||||
namespace Moonlight.Api.Services;
|
||||
|
||||
@@ -18,6 +19,8 @@ public class UserAuthService
|
||||
|
||||
private const string UserIdClaim = "UserId";
|
||||
private const string IssuedAtClaim = "IssuedAt";
|
||||
|
||||
public const string ValidationCacheKeyPattern = $"Moonlight.{nameof(UserAuthService)}.{nameof(ValidateAsync)}-{{0}}";
|
||||
|
||||
public UserAuthService(
|
||||
DatabaseRepository<User> userRepository,
|
||||
@@ -93,7 +96,10 @@ public class UserAuthService
|
||||
.Query()
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Id == userId)
|
||||
.Select(u => new UserSession(u.InvalidateTimestamp))
|
||||
.Select(u => new UserSession(
|
||||
u.InvalidateTimestamp,
|
||||
u.RoleMemberships.SelectMany(x => x.Role.Permissions).ToArray())
|
||||
)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (user == null)
|
||||
@@ -122,10 +128,17 @@ public class UserAuthService
|
||||
// everything is fine. If not, it means that the token should be invalidated
|
||||
// as it is too old
|
||||
|
||||
return issuedAt > user.InvalidateTimestamp;
|
||||
if (issuedAt < user.InvalidateTimestamp)
|
||||
return false;
|
||||
|
||||
principal.Identities.First().AddClaims(
|
||||
user.Permissions.Select(x => new Claim(Permissions.ClaimType, x))
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// A small model which contains data queried per session validation after the defined cache time.
|
||||
// Used for projection
|
||||
private record UserSession(DateTimeOffset InvalidateTimestamp);
|
||||
private record UserSession(DateTimeOffset InvalidateTimestamp, string[] Permissions);
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
using System.Security.Claims;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moonlight.Api.Configuration;
|
||||
using Moonlight.Api.Implementations;
|
||||
using Moonlight.Api.Services;
|
||||
|
||||
namespace Moonlight.Api.Startup;
|
||||
@@ -80,7 +82,8 @@ public partial class Startup
|
||||
options.GetClaimsFromUserInfoEndpoint = true;
|
||||
});
|
||||
|
||||
builder.Services.AddAuthorization();
|
||||
builder.Services.AddSingleton<IAuthorizationHandler, PermissionAuthorizationHandler>();
|
||||
builder.Services.AddSingleton<IAuthorizationPolicyProvider, PermissionPolicyProvider>();
|
||||
}
|
||||
|
||||
private static void UseAuth(WebApplication application)
|
||||
|
||||
Reference in New Issue
Block a user