diff --git a/.version b/.version index e3d2b00..4c16340 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.20.23.40 \ No newline at end of file +0.21.24.41 \ No newline at end of file diff --git a/NetinaShop.Api/AppSettings/appsettings.Development.json b/NetinaShop.Api/AppSettings/appsettings.Development.json index 339b09e..be1518e 100644 --- a/NetinaShop.Api/AppSettings/appsettings.Development.json +++ b/NetinaShop.Api/AppSettings/appsettings.Development.json @@ -38,8 +38,8 @@ }, "JwtSettings": { "SecretKey": "YAEMAMZAMAN_KHODET_NEGAHDAR_IN_KEY_BASH_nw+8E0EABj0Wg8c4mHg/bDBf5qGMhmBPb6u16DVe9/MzYva1e+/J1zImyIoQX2Lmra2kvzsIjGiwP7r3Znd_YA_JADE_NASABE_v+Ro/CDixScDv6EkpZnkBv9MFdPnSmFXNGMH9gA1BzQUoC1iSX9Aq+pMIw/cMKXI9WA==_YA_HUSEIN_SEYED_SHOHADA_BE_OMID_KHODET", - "Issuer": "Brizco", - "Audience": "Brizco", + "Issuer": "NetinaShop", + "Audience": "NetinaShop", "ExpireAddDay": "15" } }, diff --git a/NetinaShop.Api/NetinaShop.Api.csproj b/NetinaShop.Api/NetinaShop.Api.csproj index 2c8da95..c872259 100644 --- a/NetinaShop.Api/NetinaShop.Api.csproj +++ b/NetinaShop.Api/NetinaShop.Api.csproj @@ -6,8 +6,8 @@ enable true Linux - 0.20.23.40 - 0.20.23.40 + 0.21.24.41 + 0.21.24.41 diff --git a/NetinaShop.Api/Services/CurrentUserService.cs b/NetinaShop.Api/Services/CurrentUserService.cs index d3a212e..197a20a 100644 --- a/NetinaShop.Api/Services/CurrentUserService.cs +++ b/NetinaShop.Api/Services/CurrentUserService.cs @@ -1,5 +1,7 @@ using System.Security.Cryptography; using NetinaShop.Repository.Abstracts; +using Polly; +using static NetinaShop.Infrastructure.Models.Scrapers.Digikala.GetDigikalProductResponseDto; namespace NetinaShop.Api.Services; @@ -17,6 +19,17 @@ public class CurrentUserService : ICurrentUserService public string? UserName => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.Name); public string? DeviceId => GetDeviceId(_httpContextAccessor.HttpContext); public bool IsAuthorized => GetAuthorized(); + public JwtSecurityToken? JwtToken => GetJwtToken(); + private JwtSecurityToken? GetJwtToken() + { + var stream = _httpContextAccessor.HttpContext?.Request.Headers.Authorization.FirstOrDefault(); + if (stream == null) + return null; + var handler = new JwtSecurityTokenHandler(); + var jsonToken = handler.ReadToken(stream.Split(" ").Last()); + return jsonToken as JwtSecurityToken; + } + public List? Permissions => _httpContextAccessor.HttpContext?.User?.FindAll("Permission")?.Select(c => c.Value)?.ToList(); private string? GetDeviceId(HttpContext? context) @@ -47,4 +60,5 @@ public class CurrentUserService : ICurrentUserService } + } \ No newline at end of file diff --git a/NetinaShop.Repository/Abstracts/ICurrentUserService.cs b/NetinaShop.Repository/Abstracts/ICurrentUserService.cs index a07b244..8e90dfd 100644 --- a/NetinaShop.Repository/Abstracts/ICurrentUserService.cs +++ b/NetinaShop.Repository/Abstracts/ICurrentUserService.cs @@ -1,4 +1,6 @@ -namespace NetinaShop.Repository.Abstracts; +using System.IdentityModel.Tokens.Jwt; + +namespace NetinaShop.Repository.Abstracts; public interface ICurrentUserService : IScopedDependency { @@ -7,5 +9,7 @@ public interface ICurrentUserService : IScopedDependency string? UserName { get; } string? DeviceId { get; } bool IsAuthorized { get; } + JwtSecurityToken? JwtToken { get; } + public List? Permissions { get; } } \ No newline at end of file diff --git a/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs b/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs index ccd96c5..00c84e4 100644 --- a/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs +++ b/NetinaShop.Repository/Handlers/Products/GetProductsQueryHandler.cs @@ -1,4 +1,8 @@ using Microsoft.EntityFrameworkCore; +using static Microsoft.AspNetCore.Hosting.Internal.HostingApplication; +using System.IdentityModel.Tokens.Jwt; +using System.Net; +using System.Security.Claims; namespace NetinaShop.Repository.Handlers.Products; @@ -6,16 +10,24 @@ public class GetProductsQueryHandler : IRequestHandler Handle(GetProductsQuery request, CancellationToken cancellationToken) { var response = new GetProductsResponseDto(); var products = _repositoryWrapper.SetRepository().TableNoTracking; + if (_currentUserService.JwtToken == null) + products = products.Where(p => p.BeDisplayed); + var roleClaim = _currentUserService.JwtToken?.Claims.FirstOrDefault(c => c.Type == "role"); + if (roleClaim != null && roleClaim.Value.Contains("Customer")) + products = products.Where(p => p.BeDisplayed); + if (request.IsActive != null) products = products.Where(p => p.IsEnable == request.IsActive); if (request.ProductName != null) @@ -66,7 +78,7 @@ public class GetProductsQueryHandler : IRequestHandler p.Cost, cancellationToken); response.Filters.Price.MinimumValue = await products.MinAsync(p => p.Cost, cancellationToken); @@ -88,4 +100,5 @@ public class GetProductsQueryHandler : IRequestHandler