Adds authorization permissions administrator dashboard

This commit is contained in:
BinaryAlley committed 2024-12-22 19:17:47 +02:00
1 parent 152cb48050
commit 583b8db865
106 files changed
+5916 -2085

No files matched your search

@@ -10,6 +10,9 @@ public static partial class ApiRoutes
/// </summary>
public static class Authorization
{
public const string UPDATE_USER_ROLE_AND_PERMISSIONS_BY_USER_ID = "/auth/users/{userId}/role-and-permissions";
public const string GET_USER_PERMISSIONS_BY_USER_ID = "/auth/users/{userId}/permissions";
public const string GET_USER_ROLE_BY_USER_ID = "/auth/users/{userId}/role";
public const string GET_AUTHORIZATION = "/auth/get-authorization";
}
}
@@ -36,7 +36,7 @@ public class UpdateRoleEndpointSummary : Summary<UpdateRoleEndpoint, UpdateRoleR
RequestParam(r => r.RoleId, "The Id of the role. Required.");
RequestParam(r => r.RoleName, "The name of the role. Required.");
RequestParam(r => r.Permissions, "The collection or permissions of the role. Required.");
RequestParam(r => r.Permissions, "The collection of Ids of the permissions of the role. Required.");
ResponseParam<RolePermissionsResponse>(r => r.Role, "The authorization role entity.");
ResponseParam<RolePermissionsResponse>(r => r.Permissions, "The list of permissions of the role entity.");
@@ -22,7 +22,7 @@ public class GetAuthorizationEndpointSummary : Summary<GetAuthorizationEndpoint,
public GetAuthorizationEndpointSummary()
{
Summary = "Gets the authorization details of an account.";
Description = "Gets the authorization roles and permissions of an account.";
Description = "Gets the authorization role and permissions of an account.";
ExampleRequest = new GetAuthorizationRequest(
UserId: Guid.NewGuid()
@@ -30,10 +30,10 @@ public class GetAuthorizationEndpointSummary : Summary<GetAuthorizationEndpoint,
RequestParam(r => r.UserId, "The Id of the user for whom to get the authorization. Required.");
Response(200, "The authorization roles and permissions of the account are returned.",
Response(200, "The authorization role and permissions of the account are returned.",
example: new AuthorizationResponse(
UserId: Guid.NewGuid(),
Roles: new HashSet<string>() { "Admin" },
Role: "Admin",
Permissions: new HashSet<AuthorizationPermission>() { AuthorizationPermission.CanRegisterUsers, AuthorizationPermission.CanDeleteUsers }
));
@@ -0,0 +1,56 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using FastEndpoints;
using Lumina.Application.Common.Mapping.Authentication;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Requests.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Mediator;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Authorization.GetUserPermissions;
/// <summary>
/// API endpoint for the <c>/auth/users/{userId}/permissions</c> route.
/// </summary>
public class GetUserPermissionsEndpoint : BaseEndpoint<GetUserPermissionsRequest, IResult>
{
private readonly ISender _sender;
/// <summary>
/// Initializes a new instance of the <see cref="GetUserPermissionsEndpoint"/> class.
/// </summary>
/// <param name="sender">Injected service for mediating commands and queries.</param>
public GetUserPermissionsEndpoint(ISender sender)
{
_sender = sender;
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
Verbs(Http.GET);
Routes(ApiRoutes.Authorization.GET_USER_PERMISSIONS_BY_USER_ID);
Version(1);
DontCatchExceptions();
}
/// <summary>
/// Gets the authorization permissions of a user.
/// </summary>
/// <param name="request">The request containing the user for whom to get the authorization permissions.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(GetUserPermissionsRequest request, CancellationToken cancellationToken)
{
ErrorOr<IEnumerable<PermissionResponse>> result = await _sender.Send(request.ToQuery(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -0,0 +1,115 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Requests.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Authorization.GetUserPermissions;
/// <summary>
/// Class used for providing a textual description for the <see cref="GetUserPermissionsEndpoint"/> API endpoint, for OpenAPI.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetUserPermissionsEndpointSummary : Summary<GetUserPermissionsEndpoint, GetUserPermissionsRequest>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetUserPermissionsEndpointSummary"/> class.
/// </summary>
public GetUserPermissionsEndpointSummary()
{
Summary = "Gets the authorization permissions of a user.";
Description = "Gets the authorization ropermissionsle of a user.";
ExampleRequest = new GetUserPermissionsRequest(
UserId: Guid.NewGuid()
);
RequestParam(r => r.UserId, "The Id of the user for whom to get the authorization permissions. Required.");
Response(200, "The authorization permissions of the user are returned.",
example: new[] {
new PermissionResponse(Id: Guid.NewGuid(), AuthorizationPermission.CanViewUsers),
new PermissionResponse(Id: Guid.NewGuid(), AuthorizationPermission.CanDeleteUsers),
new PermissionResponse(Id: Guid.NewGuid(), AuthorizationPermission.CanRegisterUsers)
});
Response(401, "Authentication required.", "application/problem+json",
example: new[]
{
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "You are not authorized",
instance = "/auth/users/{userId}/permissions"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "Invalid token: The token expired at '01/01/2024 01:00:00'",
instance = "/auth/users/{userId}/permissions"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token is invalid",
instance = "/auth/users/{userId}/permissions"
}
}
);
Response(403, "The request failed because the current user is not an Admin.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
title = "General.Failure",
status = 403,
detail = "NotAuthorized",
instance = "/auth/users/{userId}/permissions",
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
}
);
Response(404, "The request failed because the provided user does not exist.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
title = "General.NotFound",
status = 404,
detail = "UsernameDoesNotExist",
instance = "/auth/users/{userId}/permissions",
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
}
);
Response(422, "The request did not pass validation checks.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
title = "General.Validation",
status = 422,
detail = "OneOrMoreValidationErrorsOccurred",
instance = "/auth/users/{userId}/permissions",
errors = new Dictionary<string, string[]>
{
{
"General.Validation", new[]
{
"UserIdCannotBeEmpty"
}
}
},
traceId = "00-839f81e411d7eb91ed5aa91e56b00bbb-7c8bd5dfabdaf2dc-00"
}
);
}
}
@@ -0,0 +1,56 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using FastEndpoints;
using Lumina.Application.Common.Mapping.Authentication;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Requests.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Mediator;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Authorization.GetUserRole;
/// <summary>
/// API endpoint for the <c>/auth/users/{userId}/role</c> route.
/// </summary>
public class GetUserRoleEndpoint : BaseEndpoint<GetUserRoleRequest, IResult>
{
private readonly ISender _sender;
/// <summary>
/// Initializes a new instance of the <see cref="GetUserRoleEndpoint"/> class.
/// </summary>
/// <param name="sender">Injected service for mediating commands and queries.</param>
public GetUserRoleEndpoint(ISender sender)
{
_sender = sender;
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
Verbs(Http.GET);
Routes(ApiRoutes.Authorization.GET_USER_ROLE_BY_USER_ID);
Version(1);
DontCatchExceptions();
}
/// <summary>
/// Gets the authorization role of a user.
/// </summary>
/// <param name="request">The request containing the user for whom to get the authorization role.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(GetUserRoleRequest request, CancellationToken cancellationToken)
{
ErrorOr<RoleResponse?> result = await _sender.Send(request.ToQuery(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -0,0 +1,114 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.Requests.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Authorization.GetUserRole;
/// <summary>
/// Class used for providing a textual description for the <see cref="GetUserRoleEndpoint"/> API endpoint, for OpenAPI.
/// </summary>
[ExcludeFromCodeCoverage]
public class GetUserRoleEndpointSummary : Summary<GetUserRoleEndpoint, GetUserRoleRequest>
{
/// <summary>
/// Initializes a new instance of the <see cref="GetUserRoleEndpointSummary"/> class.
/// </summary>
public GetUserRoleEndpointSummary()
{
Summary = "Gets the authorization role of a user.";
Description = "Gets the authorization role of a user.";
ExampleRequest = new GetUserRoleRequest(
UserId: Guid.NewGuid()
);
RequestParam(r => r.UserId, "The Id of the user for whom to get the authorization role. Required.");
Response(200, "The authorization role of the user is returned.",
example: new RoleResponse(
Id: Guid.NewGuid(),
RoleName: "Admin"
));
Response(401, "Authentication required.", "application/problem+json",
example: new[]
{
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "You are not authorized",
instance = "/api/v1/auth/users/{userId}/role"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "Invalid token: The token expired at '01/01/2024 01:00:00'",
instance = "/api/v1/auth/users/{userId}/role"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token is invalid",
instance = "/api/v1/auth/users/{userId}/role"
}
}
);
Response(403, "The request failed because the current user is not an Admin.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
title = "General.Failure",
status = 403,
detail = "NotAuthorized",
instance = "/api/v1/auth/users/{userId}/role",
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
}
);
Response(404, "The request failed because the provided user does not exist.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
title = "General.NotFound",
status = 404,
detail = "UsernameDoesNotExist",
instance = "/api/v1/auth/users/{userId}/role",
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
}
);
Response(422, "The request did not pass validation checks.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
title = "General.Validation",
status = 422,
detail = "OneOrMoreValidationErrorsOccurred",
instance = "/api/v1/auth/users/{userId}/role",
errors = new Dictionary<string, string[]>
{
{
"General.Validation", new[]
{
"UserIdCannotBeEmpty"
}
}
},
traceId = "00-839f81e411d7eb91ed5aa91e56b00bbb-7c8bd5dfabdaf2dc-00"
}
);
}
}
@@ -0,0 +1,54 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using FastEndpoints;
using Lumina.Application.Common.Mapping.Authorization;
using Lumina.Contracts.Requests.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Presentation.Api.Common.Routes.UsersManagement;
using Lumina.Presentation.Api.Core.Endpoints.Common;
using Mediator;
using Microsoft.AspNetCore.Http;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Authorization.UpdateUserRoleAndPermissions;
/// <summary>
/// API endpoint for the <c>/auth/users/{userId}/role-and-permissions</c> route.
/// </summary>
public class UpdateUserRoleAndPermissionsEndpoint : BaseEndpoint<UpdateUserRoleAndPermissionsRequest, IResult>
{
private readonly ISender _sender;
/// <summary>
/// Initializes a new instance of the <see cref="UpdateUserRoleAndPermissionsEndpoint"/> class.
/// </summary>
/// <param name="sender">Injected service for mediating commands and queries.</param>
public UpdateUserRoleAndPermissionsEndpoint(ISender sender)
{
_sender = sender;
}
/// <summary>
/// Configures the API endpoint.
/// </summary>
public override void Configure()
{
Verbs(Http.PUT);
Routes(ApiRoutes.Authorization.UPDATE_USER_ROLE_AND_PERMISSIONS_BY_USER_ID);
Version(1);
DontCatchExceptions();
}
/// <summary>
/// Updates an the authorization role and permissions of a user.
/// </summary>
/// <param name="request">The request containing the authorization role and permission of the user to be updated.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
public override async Task<IResult> ExecuteAsync(UpdateUserRoleAndPermissionsRequest request, CancellationToken cancellationToken)
{
ErrorOr<AuthorizationResponse> result = await _sender.Send(request.ToCommand(), cancellationToken).ConfigureAwait(false);
return result.Match(success => TypedResults.Ok(success), Problem);
}
}
@@ -0,0 +1,130 @@
#region ========================================================================= USING =====================================================================================
using FastEndpoints;
using Lumina.Contracts.DTO.Authentication;
using Lumina.Contracts.Requests.Authorization;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Domain.Common.Enums.Authorization;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Presentation.Api.Core.Endpoints.UsersManagement.Authorization.UpdateUserRoleAndPermissions;
/// <summary>
/// Class used for providing a textual description for the <see cref="UpdateUserRoleAndPermissionsEndpoint"/> API endpoint, for OpenAPI.
/// </summary>
[ExcludeFromCodeCoverage]
public class UpdateUserRoleAndPermissionsEndpointSummary : Summary<UpdateUserRoleAndPermissionsEndpoint, UpdateUserRoleAndPermissionsRequest>
{
/// <summary>
/// Initializes a new instance of the <see cref="UpdateUserRoleAndPermissionsEndpointSummary"/> class.
/// </summary>
public UpdateUserRoleAndPermissionsEndpointSummary()
{
Summary = "Updates the authorization role and permissions of a user.";
Description = "Updates the authorization role and permissions of a user, and returns the details.";
ExampleRequest = new UpdateUserRoleAndPermissionsRequest(
UserId: Guid.NewGuid(),
RoleId: Guid.NewGuid(),
Permissions: [
Guid.NewGuid(),
Guid.NewGuid(),
]
);
RequestParam(r => r.UserId, "The Id of the user. Required.");
RequestParam(r => r.RoleId, "The Id of the role. Required.");
RequestParam(r => r.Permissions, "The collection of Ids of the permissions of the role. Required.");
ResponseParam<AuthorizationResponse>(r => r.UserId, "The id of the user whose role and permissions were updated.");
ResponseParam<AuthorizationResponse>(r => r.Role, "The authorization role entity.");
ResponseParam<AuthorizationResponse>(r => r.Permissions, "The list of permissions of the user.");
Response(200, "The authorization role and permissions of the user were successfully updated.", example:
new AuthorizationResponse(
UserId: Guid.NewGuid(),
Role: "Editor",
Permissions: new HashSet<AuthorizationPermission>() { AuthorizationPermission.CanRegisterUsers, AuthorizationPermission.CanDeleteUsers }
));
Response(401, "Authentication required.", "application/problem+json",
example: new[]
{
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "You are not authorized",
instance = "/api/v1/auth/users/{userId}/role-and-permissions"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "Invalid token: The token expired at '01/01/2024 01:00:00'",
instance = "/api/v1/auth/users/{userId}/role-and-permissions"
},
new
{
type = "https://tools.ietf.org/html/rfc7235#section-3.1",
status = 401,
title = "Unauthorized",
detail = "The token is invalid",
instance = "/api/v1/auth/users/{userId}/role-and-permissions"
}
}
);
Response(403, "The request failed because the current user is not an Admin, or changing the user's role would result in no user with an Admin account.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.4",
title = "General.Failure",
status = 403,
detail = "NotAuthorized | CannotRemoveLastAdmin | UserMustHaveRoleOrDirectPermissions",
instance = "/api/v1/auth/users/{userId}/role-and-permissions",
traceId = "00-a712bbf99ca8ab485f86a762ae5ae74d-b3a2eb78813b0a5d-00"
}
);
Response(404, "The request failed because the provided role or user do not exist.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc9110#section-15.5.5",
title = "General.NotFound",
status = 404,
detail = "RoleNotFound | UserDoesNotExist",
instance = "/api/v1/auth/users/{userId}/role-and-permissions",
traceId = "00-57d15dadd702dbd4aeb5dc9b7cee68ee-9330237dbb2ce0e5-00"
}
);
Response(422, "The request did not pass validation checks.", "application/problem+json",
example: new
{
type = "https://tools.ietf.org/html/rfc4918#section-11.2",
title = "General.Validation",
status = 422,
detail = "OneOrMoreValidationErrorsOccurred",
instance = "/api/v1/auth/users/{userId}/role-and-permissions",
errors = new Dictionary<string, string[]>
{
{
"General.Validation", new[]
{
"UserIdCannotBeEmpty",
"RoleIdCannotBeEmpty",
"PermissionIdCannotBeEmpty"
}
}
},
traceId = "00-2470be4248a2a5a0c6f70579975a6954-b9c3ba9544a03500-00"
}
);
}
}