Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Permissions to Give access based on the User Subscription #55

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
22 changes: 22 additions & 0 deletions BlazorShop.Application/Commands/ClaimCommand/CreateClaimCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="CreateClaimCommand.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

namespace BlazorShop.Application.Commands.ClaimCommand
{
/// <summary>
/// A model to create a claim.
/// </summary>
public class CreateClaimCommand : IRequest<RequestResponse>
{
/// <summary>
/// Gets or sets the value of the claim.
/// </summary>
public string Value { get; set; }

/// <summary>
/// Gets or sets the type of the claim.
/// </summary>
public string Type { get; set; }
}
}
17 changes: 17 additions & 0 deletions BlazorShop.Application/Commands/ClaimCommand/DeleteClaimCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// <copyright file="DeleteClaimCommand.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

namespace BlazorShop.Application.Commands.ClaimCommand
{
/// <summary>
/// A model to delete a claim.
/// </summary>
public class DeleteClaimCommand : IRequest<RequestResponse>
{
/// <summary>
/// Gets or sets the id of the claim.
/// </summary>
public int Id { get; set; }
}
}
27 changes: 27 additions & 0 deletions BlazorShop.Application/Commands/ClaimCommand/UpdateClaimCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <copyright file="UpdateClaimCommand.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

namespace BlazorShop.Application.Commands.ClaimCommand
{
/// <summary>
/// A model to update a claim.
/// </summary>
public class UpdateClaimCommand : IRequest<RequestResponse>
{
/// <summary>
/// Gets or sets the id of the claim.
/// </summary>
public int Id { get; set; }

/// <summary>
/// Gets or sets the value of the claim.
/// </summary>
public string Value { get; set; }

/// <summary>
/// Gets or sets the type of the claim.
/// </summary>
public string Type { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,5 @@ public class CreateMusicCommand : IRequest<RequestResponse>
/// Gets or sets The image path of the music.
/// </summary>
public string ImagePath { get; set; }

/// <summary>
/// Gets or sets The access level of the music.
/// </summary>
public int AccessLevel { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,5 @@ public class UpdateMusicCommand : IRequest<RequestResponse>
/// Gets or sets The image path of the music.
/// </summary>
public string ImagePath { get; set; }

/// <summary>
/// Gets or sets The access level of the music.
/// </summary>
public int AccessLevel { get; set; }
}
}
45 changes: 25 additions & 20 deletions BlazorShop.Application/Common/Interfaces/IApplicationDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,59 @@ namespace BlazorShop.Application.Common.Interfaces
public interface IApplicationDbContext
{
/// <summary>
/// Gets or sets the <see cref="Cart"/> set.
/// Gets the <see cref="Cart"/> set.
/// </summary>
DbSet<Cart> Carts { get; set; }
DbSet<Cart> Carts { get; }

/// <summary>
/// Gets or sets the <see cref="Clothe"/> set.
/// Gets the <see cref="Clothe"/> set.
/// </summary>
DbSet<Clothe> Clothes { get; set; }
DbSet<Clothe> Clothes { get; }

/// <summary>
/// Gets or sets the <see cref="Invoice"/> set.
/// Gets the <see cref="Invoice"/> set.
/// </summary>
DbSet<Invoice> Invoices { get; set; }
DbSet<Invoice> Invoices { get; }

/// <summary>
/// Gets or sets the <see cref="Music"/> set.
/// Gets the <see cref="Music"/> set.
/// </summary>
DbSet<Music> Musics { get; set; }
DbSet<Music> Musics { get; }

/// <summary>
/// Gets or sets the <see cref="Order"/> set.
/// Gets the <see cref="Order"/> set.
/// </summary>
DbSet<Order> Orders { get; set; }
DbSet<Order> Orders { get; }

/// <summary>
/// Gets or sets the <see cref="Subscriber"/> set.
/// Gets the <see cref="Subscriber"/> set.
/// </summary>
DbSet<Subscriber> Subscribers { get; set; }
DbSet<Subscriber> Subscribers { get; }

/// <summary>
/// Gets or sets the <see cref="Subscription"/> set.
/// Gets the <see cref="Subscription"/> set.
/// </summary>
DbSet<Subscription> Subscriptions { get; set; }
DbSet<Subscription> Subscriptions { get; }

/// <summary>
/// Gets or sets the <see cref="Receipt"/> set.
/// Gets the <see cref="Receipt"/> set.
/// </summary>
DbSet<Receipt> Receipts { get; set; }
DbSet<Receipt> Receipts { get; }

/// <summary>
/// Gets or sets the <see cref="TodoItem"/> set.
/// Gets the <see cref="TodoItem"/> set.
/// </summary>
DbSet<TodoItem> TodoItems { get; set; }
DbSet<TodoItem> TodoItems { get; }

/// <summary>
/// Gets or sets the <see cref="TodoList"/> set.
/// Gets the <see cref="TodoList"/> set.
/// </summary>
DbSet<TodoList> TodoLists { get; set; }
DbSet<TodoList> TodoLists { get; }

/// <summary>
/// Gets the <see cref="CustomClaim"/> set.
/// </summary>
DbSet<CustomClaim> CustomClaims { get; }

/// <summary>
/// Applying the chnages made to the application database context.
Expand Down
74 changes: 74 additions & 0 deletions BlazorShop.Application/Common/Interfaces/IClaimService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// <copyright file="IClaimService.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

namespace BlazorShop.Application.Common.Interfaces
{
/// <summary>
/// A service to provide Claim features.
/// </summary>
public interface IClaimService
{
/// <summary>
/// Check the Claims of an user.
/// </summary>
/// <param name="user">The user data.</param>
/// <returns>A list of user's Claims.</returns>
Task<List<string>> CheckUserClaimsAsync(User user);

/// <summary>
/// Get the user Claim.
/// </summary>
/// <returns>The user Claim.</returns>
ClaimResponse GetUserClaim();

/// <summary>
/// Set the Claim to the user.
/// </summary>
/// <param name="user">The user data.</param>
/// <param name="claimValue">The Claim name.</param>
/// <returns>The request response.</returns>
Task<RequestResponse> SetUserClaimAsync(User user, string claimValue);

/// <summary>
/// Creates a new Claim.
/// </summary>
/// <param name="claim">The name of the Claim.</param>
/// <returns>The created Claim.</returns>
Task<RequestResponse> CreateClaimAsync(CreateClaimCommand claim);

/// <summary>
/// Updates the Claim data.
/// </summary>
/// <param name="claim">The Claim data.</param>
/// <returns>The request response.</returns>
Task<RequestResponse> UpdateClaimAsync(UpdateClaimCommand claim);

/// <summary>
/// Deletes a Claim.
/// </summary>
/// <param name="claimId">The id of the Claim.</param>
/// <returns>The request response.</returns>
Task<RequestResponse> DeleteClaimAsync(int claimId);

/// <summary>
/// Gets all the Claims for a non-admin user.
/// </summary>
/// <returns>The list of Claims.</returns>
List<ClaimResponse> GetClaims();

/// <summary>
/// Find the Claim by id.
/// </summary>
/// <param name="id">The id of the Claim.</param>
/// <returns>The Claim data.</returns>
ClaimResponse GetClaimById(int id);

/// <summary>
/// Gets the Claim by value.
/// </summary>
/// <param name="claimValue">The value of the Claim.</param>
/// <returns>The Claim data.</returns>
ClaimResponse GetClaimByValue(string claimValue);
}
}
18 changes: 18 additions & 0 deletions BlazorShop.Application/Common/Interfaces/ICustomClaimsService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <copyright file="IClaimService.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

namespace BlazorShop.Application.Common.Interfaces
{
/// <summary>
/// A service for the custom user claims.
/// </summary>
public interface ICustomClaimsService
{
Task AddUserClaim();

Task DeleteUserClaim();

Task UpdateUserClaim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <copyright file="CreateClaimCommandHandler.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

namespace BlazorShop.Application.Handlers.Commands.ClaimHandler
{
/// <summary>
/// An implementation of the <see cref="IRequestHandler{CreateClaimCommand, RequestResponse}"/>.
/// </summary>
public class CreateClaimCommandHandler : IRequestHandler<CreateClaimCommand, RequestResponse>
{
/// <summary>
/// Initializes a new instance of the <see cref="CreateClaimCommandHandler"/> class.
/// </summary>
/// <param name="claimService">Gets An instance of <see cref="IClaimService"/>.</param>
/// <param name="logger">Gets An instance of <see cref="ILogger{CreateClaimCommandHandler}"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if there is no logger provided.</exception>
public CreateClaimCommandHandler(IClaimService claimService, ILogger<CreateClaimCommandHandler> logger)
{
this.ClaimService = claimService;
this.Logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

/// <summary>
/// Gets An instance of <see cref="IClaimService"/>.
/// </summary>
private IClaimService ClaimService { get; }

/// <summary>
/// Gets An instance of <see cref="ILogger{CreateClaimCommandHandler}"/>.
/// </summary>
private ILogger<CreateClaimCommandHandler> Logger { get; }

/// <summary>
/// An implementation of the handler for <see cref="CreateClaimCommand"/>.
/// </summary>
/// <param name="request">The request object to handle.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{RequestResponse}"/> representing the result of the asynchronous operation.</returns>
public async Task<RequestResponse> Handle(CreateClaimCommand request, CancellationToken cancellationToken)
{
RequestResponse? response;

try
{
response = await this.ClaimService.CreateClaimAsync(request);
}
catch (Exception ex)
{
this.Logger.LogError(ex, ErrorsManager.CreateClaimCommand);
response = RequestResponse.Failure($"{ErrorsManager.CreateClaimCommand}. {ex.Message}. {ex.InnerException?.Message}");
}

return response;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// <copyright file="DeleteClaimCommandHandler.cs" company="Beniamin Jitca">
// Copyright (c) Beniamin Jitca. All rights reserved.
// </copyright>

using BlazorShop.Application.Common.Interfaces;

namespace BlazorShop.Application.Handlers.Commands.ClaimHandler
{
/// <summary>
/// An implementation of the <see cref="IRequestHandler{DeleteClaimCommand, RequestResponse}"/>.
/// </summary>
public class DeleteClaimCommandHandler : IRequestHandler<DeleteClaimCommand, RequestResponse>
{
/// <summary>
/// Initializes a new instance of the <see cref="DeleteClaimCommandHandler"/> class.
/// </summary>
/// <param name="claimService">Gets An instance of <see cref="IClaimService"/>.</param>
/// <param name="logger">Gets An instance of <see cref="ILogger{DeleteClaimCommandHandler}"/>.</param>
/// <exception cref="ArgumentNullException">Thrown if there is no logger provided.</exception>
public DeleteClaimCommandHandler(IClaimService claimService, ILogger<DeleteClaimCommandHandler> logger)
{
this.ClaimService = claimService;
this.Logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

/// <summary>
/// Gets An instance of <see cref="IClaimService"/>.
/// </summary>
private IClaimService ClaimService { get; }

/// <summary>
/// Gets An instance of <see cref="ILogger{DeleteClaimCommandHandler}"/>.
/// </summary>
private ILogger<DeleteClaimCommandHandler> Logger { get; }

/// <summary>
/// An implementation of the handler for <see cref="DeleteClaimCommand"/>.
/// </summary>
/// <param name="request">The request object to handle.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A <see cref="Task{RequestResponse}"/> representing the result of the asynchronous operation.</returns>
public async Task<RequestResponse> Handle(DeleteClaimCommand request, CancellationToken cancellationToken)
{
RequestResponse? response;

try
{
response = await this.ClaimService.DeleteClaimAsync(request.Id);
}
catch (Exception ex)
{
this.Logger.LogError(ex, ErrorsManager.DeleteClaimCommand);
response = RequestResponse.Failure($"{ErrorsManager.DeleteClaimCommand}. {ex.Message}. {ex.InnerException?.Message}");
}

return response;
}
}
}
Loading