mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11:11:10 +00:00
Adds authorization roles administrator dashboard
This commit is contained in:
1 parent
290af03c83
commit
1df31515fb
191 files changed
+6189
-806
No files matched your search
@@ -0,0 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the request model for adding an authorization role.
|
||||
/// </summary>
|
||||
/// <param name="RoleName">The name of the role.</param>
|
||||
/// <param name="Permissions">The collection or permissions of the role.</param>
|
||||
public record AddRoleRequestModel(
|
||||
string? RoleName,
|
||||
List<Guid>? Permissions
|
||||
);
|
||||
+1
-1
@@ -13,7 +13,7 @@ public class AuthorizationRequirementModel
|
||||
/// <summary>
|
||||
/// The collection of roles required for accessing the resource.
|
||||
/// </summary>
|
||||
public AuthorizationRole[]? Roles { get; set; }
|
||||
public string[]? Roles { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The collection of permissions required for accessing the resource.
|
||||
|
||||
@@ -1,105 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Api;
|
||||
using Lumina.Presentation.Web.Common.Authorization;
|
||||
using Lumina.Presentation.Web.Common.Enums.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Implements authorization logic for API endpoints by validating user roles, permissions, and policies.
|
||||
/// </summary>
|
||||
public class AuthorizationService : IAuthorizationService
|
||||
{
|
||||
private readonly IApiHttpClient _apiHttpClient;
|
||||
private readonly IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AuthorizationService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="apiHttpClient">Injected HTTP typed client for interactions with the API.</param>
|
||||
/// <param name="httpContextAccessor">The <see cref="IHttpContextAccessor"/> used to access the current HTTP context.</param>
|
||||
public AuthorizationService(IApiHttpClient apiHttpClient, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_apiHttpClient = apiHttpClient;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the currently logged in user has a specific permission.
|
||||
/// </summary>
|
||||
/// <param name="permission">The permission to check.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns><see langword="true"/> if the currently logged in user has the specified permission, <see langword="false"/> otherwise.</returns>
|
||||
public async Task<bool> HasPermissionAsync(AuthorizationPermission permission, CancellationToken cancellationToken)
|
||||
{
|
||||
GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(cancellationToken).ConfigureAwait(false);
|
||||
return authorization.Permissions.Contains(permission);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the currently logged in user belongs to a specific role.
|
||||
/// </summary>
|
||||
/// <param name="role">The role to check.</param>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns><see langword="true"/> if the currently logged in user is in the specified role, <see langword="false"/> otherwise.</returns>
|
||||
public async Task<bool> IsInRoleAsync(AuthorizationRole role, CancellationToken cancellationToken)
|
||||
{
|
||||
GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(cancellationToken).ConfigureAwait(false);
|
||||
return authorization.Roles.Contains(role);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization policy.
|
||||
/// </summary>
|
||||
/// <typeparam name="TAuthorizationPolicy">The type of authorization policy to evaluate.</typeparam>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns><see langword="true"/> if the currently logged in user satisfies the policy, <see langword="false"/> otherwise.</returns>
|
||||
public Task<bool> EvaluatePolicyAsync<TAuthorizationPolicy>(CancellationToken cancellationToken)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Evaluates whether the currently logged in user meets the conditions defined in the specified authorization requirement.
|
||||
/// </summary>
|
||||
/// <param name="requirement">The authorization requirements to check.</param>
|
||||
/// <returns><see langword="true"/> if the currently logged in user satisfies the requirement, <see langword="false"/> otherwise.</returns>
|
||||
public async Task<bool> EvaluateAuthorizationAsync(AuthorizationRequirementModel requirement)
|
||||
{
|
||||
GetAuthorizationResponse authorization = await GetUserAuthorizationAsync(CancellationToken.None);
|
||||
|
||||
if (requirement.Roles?.Length > 0 && !requirement.Roles.Any(role => authorization.Roles?.Contains(role) == true))
|
||||
return false;
|
||||
|
||||
if (requirement.Permissions?.Length > 0 && !requirement.Permissions.Any(permission => authorization.Permissions.Contains(permission)))
|
||||
return false;
|
||||
|
||||
//if (_requirement.PolicyType is not null)
|
||||
//{
|
||||
// bool response = await _apiHttpClient.PostAsync<bool, object>($"auth/evaluate-policy/{_requirement.PolicyType.Name}", null!);
|
||||
// if (!response)
|
||||
// return false;
|
||||
//}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves user authorization details from the authorization service.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
|
||||
/// <returns>The user's authorization details.</returns>
|
||||
private async Task<GetAuthorizationResponse> GetUserAuthorizationAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
// get the id of the currently logged in user
|
||||
string userId = _httpContextAccessor.HttpContext!.User.FindFirst(ClaimTypes.NameIdentifier)!.Value;
|
||||
return await _apiHttpClient.GetAsync<GetAuthorizationResponse>($"auth/get-authorization?userId={Uri.EscapeDataString(userId.ToString())}", cancellationToken);
|
||||
}
|
||||
}
|
||||
+1
-2
@@ -1,7 +1,6 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Enums.Authorization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
#endregion
|
||||
|
||||
@@ -16,6 +15,6 @@ namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
[DebuggerDisplay("UserId: {UserId}")]
|
||||
public record GetAuthorizationResponse(
|
||||
Guid UserId,
|
||||
AuthorizationRole[] Roles,
|
||||
string[] Roles,
|
||||
AuthorizationPermission[] Permissions
|
||||
);
|
||||
@@ -0,0 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Enums.Authorization;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a model for an authorization permission.
|
||||
/// </summary>
|
||||
/// <param name="Id">The Id of the authorization permission.</param>
|
||||
/// <param name="PermissionName">The authorization permission.</param>
|
||||
public record PermissionModel(
|
||||
Guid Id,
|
||||
AuthorizationPermission PermissionName
|
||||
);
|
||||
@@ -0,0 +1,15 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a model for an authorization role.
|
||||
/// </summary>
|
||||
/// <param name="Id">The Id of the authorization role.</param>
|
||||
/// <param name="RoleName">The authorization role.</param>
|
||||
public record RoleModel(
|
||||
Guid Id,
|
||||
string RoleName
|
||||
);
|
||||
@@ -0,0 +1,16 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Presentation.Web.Common.Enums.Authorization;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a model for an authorization role.
|
||||
/// </summary>
|
||||
/// <param name="Id">The Id of the authorization role.</param>
|
||||
/// <param name="Role">The authorization role.</param>
|
||||
public record RolePermissionsModel(
|
||||
RoleModel Role,
|
||||
PermissionModel[] Permissions
|
||||
);
|
||||
@@ -0,0 +1,18 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Authorization;
|
||||
|
||||
/// <summary>
|
||||
/// Represents the request model for updating an existing authorization role.
|
||||
/// </summary>
|
||||
/// <param name="RoleId">The Id of the role.</param>
|
||||
/// <param name="RoleName">The name of the role.</param>
|
||||
/// <param name="Permissions">The collection or permissions of the role.</param>
|
||||
public record UpdateRoleRequestModel(
|
||||
Guid RoleId,
|
||||
string? RoleName,
|
||||
List<Guid>? Permissions
|
||||
);
|
||||
@@ -0,0 +1,23 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.Presentation.Web.Common.Models.Common;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a cached HTTP response, including its content and status code.
|
||||
/// </summary>
|
||||
[DebuggerDisplay("StatusCode: {StatusCode}")]
|
||||
public class CachedResponseModel
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the content of the cached HTTP response.
|
||||
/// </summary>
|
||||
public string Content { get; set; } = string.Empty;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the HTTP status code of the cached response.
|
||||
/// </summary>
|
||||
public HttpStatusCode StatusCode { get; set; }
|
||||
}
|
||||
Reference in new issue
Block a user