Skip to content

Open-source eCommerce platform for C# and ASP.NET Core 10 with MongoDB. Build multi-store shops and multi-vendor marketplaces with B2B/B2C, REST API, plugins, Vue 3 and Docker.

Refactor, add allowApi perrmission

dkucia485500a15d20
diff --git a/src/API/Grand.Api/Grand.Api.csproj b/src/API/Grand.Api/Grand.Api.csproj
index 9684f93e2..e154ea6b6 100644
--- a/src/API/Grand.Api/Grand.Api.csproj
+++ b/src/API/Grand.Api/Grand.Api.csproj
@@ -18,7 +18,6 @@
<ProjectReference Include="..\..\Business\Grand.Business.System\Grand.Business.System.csproj" />
<ProjectReference Include="..\..\Core\Grand.Infrastructure\Grand.Infrastructure.csproj" />
<ProjectReference Include="..\..\Core\Grand.SharedKernel\Grand.SharedKernel.csproj" />
- <ProjectReference Include="..\..\Web\Grand.Web.Common\Grand.Web.Common.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OData" Version="7.5.8" />
diff --git a/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs b/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs
index c89d59c13..e5416e8f7 100644
--- a/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs
+++ b/src/API/Grand.Api/Infrastructure/ApiAuthenticationRegistrar.cs
@@ -102,11 +102,9 @@ namespace Grand.Api.Infrastructure
if (config.Enabled)
{
var jwtAuthentication = context.HttpContext.RequestServices.GetRequiredService<IJwtBearerCustomerAuthenticationService>();
- var (isValid, customer) = await jwtAuthentication.Valid(context);
+ var isValid = await jwtAuthentication.Valid(context);
if (!isValid)
throw new Exception(await jwtAuthentication.ErrorMessage());
- context.Success();
- context.Result.Properties.Parameters.Add("Customer", customer);
}
else
throw new Exception("API is disable");
diff --git a/src/API/Grand.Api/Web/TokenController.cs b/src/API/Grand.Api/Web/TokenController.cs
index 2f76f4a45..d951b2a6d 100644
--- a/src/API/Grand.Api/Web/TokenController.cs
+++ b/src/API/Grand.Api/Web/TokenController.cs
@@ -2,7 +2,6 @@
using Grand.Business.Customers.Interfaces;
using Grand.Domain.Customers;
using Grand.Infrastructure;
-using Grand.Web.Common.Controllers;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Components;
@@ -15,20 +14,19 @@ using System.Threading.Tasks;
namespace Grand.Api.Web
{
-
- public class TokenController : BaseController
+ public class TokenController : Controller
{
private readonly ICustomerService _customerService;
- private readonly IWorkContext _workContext;
private readonly ICustomerManagerService _customerManagerService;
private readonly IMediator _mediator;
+ private readonly IStoreHelper _storeHelper;
- public TokenController(ICustomerService customerService,IWorkContext workContext,ICustomerManagerService customerManagerService,IMediator mediator)
+ public TokenController(ICustomerService customerService,ICustomerManagerService customerManagerService,IMediator mediator,IStoreHelper storeHelper)
{
_customerService = customerService;
- _workContext = workContext;
_customerManagerService = customerManagerService;
_mediator = mediator;
+ _storeHelper = storeHelper;
}
[AllowAnonymous]
@@ -36,9 +34,9 @@ namespace Grand.Api.Web
[HttpPost]
public async Task<IActionResult> Guest()
{
- var customer=await _customerService.InsertGuestCustomer(_workContext.CurrentStore);
+ var customer=await _customerService.InsertGuestCustomer(_storeHelper.HostStore);
var claims = new Dictionary<string, string> {
- { "Id", customer.Id}
+ { "Guid", customer.CustomerGuid.ToString()}
};
var token = await _mediator.Send(new GenerateGrandWebTokenCommand() { Claims = claims });
diff --git a/src/Business/Grand.Business.Authentication/Interfaces/IJwtBearerCustomerAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Interfaces/IJwtBearerCustomerAuthenticationService.cs
index 9ac701059..7dc8fc152 100644
--- a/src/Business/Grand.Business.Authentication/Interfaces/IJwtBearerCustomerAuthenticationService.cs
+++ b/src/Business/Grand.Business.Authentication/Interfaces/IJwtBearerCustomerAuthenticationService.cs
@@ -10,7 +10,7 @@ namespace Grand.Business.Authentication.Interfaces
{
public interface IJwtBearerCustomerAuthenticationService
{
- Task<(bool, Customer)> Valid(TokenValidatedContext context);
+ Task<bool> Valid(TokenValidatedContext context);
Task<string> ErrorMessage();
}
}
diff --git a/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs
index 8b708e414..39ed14b87 100644
--- a/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs
+++ b/src/Business/Grand.Business.Authentication/Services/ApiAuthenticationService.cs
@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Http;
using Microsoft.Net.Http.Headers;
+using System;
using System.Linq;
using System.Threading.Tasks;
@@ -85,9 +86,16 @@ namespace Grand.Business.Authentication.Services
if (!authResult.Succeeded)
return null;
- if(authResult.Properties.Parameters.TryGetValue("Customer", out var c))
+ var email = authResult.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;
+ if (email is null)
{
- customer = c as Customer;
+ //guest
+ var id = authResult.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Guid")?.Value;
+ customer = await _customerService.GetCustomerByGuid(Guid.Parse(id));
+ }
+ else
+ {
+ customer = await _customerService.GetCustomerByEmail(email);
}
return customer;
diff --git a/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs b/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs
index 9aeeafb71..d301d5e49 100644
--- a/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs
+++ b/src/Business/Grand.Business.Authentication/Services/JwtBearerCustomerAuthenticationService.cs
@@ -1,4 +1,6 @@
using Grand.Business.Authentication.Interfaces;
+using Grand.Business.Common.Interfaces.Security;
+using Grand.Business.Common.Services.Security;
using Grand.Business.Customers.Interfaces;
using Grand.Domain.Customers;
using Microsoft.AspNetCore.Authentication.JwtBearer;
@@ -13,21 +15,24 @@ namespace Grand.Business.Authentication.Services
public class JwtBearerCustomerAuthenticationService : IJwtBearerCustomerAuthenticationService
{
private readonly ICustomerService _customerService;
+ private readonly IPermissionService _permissionService;
private string _errorMessage;
- public JwtBearerCustomerAuthenticationService(ICustomerService customerService)
+
+ public JwtBearerCustomerAuthenticationService(ICustomerService customerService,IPermissionService permissionService)
{
_customerService = customerService;
+ _permissionService = permissionService;
}
- public async Task<(bool, Customer)> Valid(TokenValidatedContext context)
+ public async Task<bool> Valid(TokenValidatedContext context)
{
var email = context.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Email")?.Value;
Customer customer = null;
if(email is null)
{
//guest
- var id= context.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Id")?.Value;
- customer = await _customerService.GetCustomerById(id);
+ var id= context.Principal.Claims.ToList().FirstOrDefault(x => x.Type == "Guid")?.Value;
+ customer = await _customerService.GetCustomerByGuid(Guid.Parse(id));
}
else
{
@@ -36,16 +41,21 @@ namespace Grand.Business.Authentication.Services
if (customer is null)
{
_errorMessage = "Not found customer";
- return (false, null);
+ return false;
}
- if (customer == null || !customer.Active || customer.Deleted)
+ if (customer == null || !customer.Active || customer.Deleted )
{
_errorMessage = "Customer not exists/or not active in the customer table";
- return (false, null);
+ return false;
+ }
+ if(!(await _permissionService.Authorize(StandardPermission.AllowUseApi,customer)))
+ {
+ _errorMessage = "Customer not has perrmission";
+ return false;
}
- return (true, customer);
+ return true;
}
diff --git a/src/Business/Grand.Business.Common/Services/Security/PermissionProvider.cs b/src/Business/Grand.Business.Common/Services/Security/PermissionProvider.cs
index 67653a362..77e2f9bbd 100644
--- a/src/Business/Grand.Business.Common/Services/Security/PermissionProvider.cs
+++ b/src/Business/Grand.Business.Common/Services/Security/PermissionProvider.cs
@@ -86,6 +86,7 @@ namespace Grand.Business.Common.Services.Security
StandardPermission.AccessClosedStore,
StandardPermission.ManageOrderTags,
StandardPermission.ManageOrderStatus,
+ StandardPermission.AllowUseApi
};
}
@@ -172,7 +173,8 @@ namespace Grand.Business.Common.Services.Security
StandardPermission.ManageBanners,
StandardPermission.ManageInteractiveForm,
StandardPermission.ManageActions,
- StandardPermission.ManageReminders
+ StandardPermission.ManageReminders,
+ StandardPermission.AllowUseApi
}
},
new DefaultPermission
@@ -194,7 +196,8 @@ namespace Grand.Business.Common.Services.Security
StandardPermission.DisplayPrices,
StandardPermission.EnableShoppingCart,
StandardPermission.EnableWishlist,
- StandardPermission.PublicStoreAllowNavigation
+ StandardPermission.PublicStoreAllowNavigation,
+ StandardPermission.AllowUseApi
}
},
new DefaultPermission
diff --git a/src/Business/Grand.Business.Common/Services/Security/PermissionService.cs b/src/Business/Grand.Business.Common/Services/Security/PermissionService.cs
index e0773bf38..1eb2cb217 100644
--- a/src/Business/Grand.Business.Common/Services/Security/PermissionService.cs
+++ b/src/Business/Grand.Business.Common/Services/Security/PermissionService.cs
@@ -211,7 +211,7 @@ namespace Grand.Business.Common.Services.Security
if (String.IsNullOrEmpty(permissionSystemName))
return false;
- var customerGroups = await _groupService.GetAllByIds(_workContext.CurrentCustomer.Groups.ToArray());
+ var customerGroups = await _groupService.GetAllByIds(customer.Groups.ToArray());
foreach (var group in customerGroups)
if (await Authorize(permissionSystemName, group))
//yes, we have such permission
diff --git a/src/Business/Grand.Business.Common/Services/Security/PermissionSystemName.cs b/src/Business/Grand.Business.Common/Services/Security/PermissionSystemName.cs
index 28a15f9d1..fcdbf96c2 100644
--- a/src/Business/Grand.Business.Common/Services/Security/PermissionSystemName.cs
+++ b/src/Business/Grand.Business.Common/Services/Security/PermissionSystemName.cs
@@ -72,7 +72,7 @@
public const string UserFields = "UserFields";
public const string HtmlEditor = "HtmlEditor.ManagePictures";
public const string ScheduleTasks = "ManageScheduleTasks";
-
+ public const string AllowUseApi = "AllowUseApi";
//public store permissions
public const string DisplayPrices = "DisplayPrices";
public const string EnableShoppingCart = "EnableShoppingCart";
diff --git a/src/Business/Grand.Business.Common/Services/Security/StandardPermissionSystem.cs b/src/Business/Grand.Business.Common/Services/Security/StandardPermissionSystem.cs
index 735918c4a..ab8281271 100644
--- a/src/Business/Grand.Business.Common/Services/Security/StandardPermissionSystem.cs
+++ b/src/Business/Grand.Business.Common/Services/Security/StandardPermissionSystem.cs
@@ -73,5 +73,11 @@ namespace Grand.Business.Common.Services.Security
Category = CategorySystem,
Actions = new List<string> { PermissionActionName.List, PermissionActionName.Edit, PermissionActionName.Preview }
};
+ public static readonly Permission AllowUseApi = new Permission {
+ Name = "Allow to use api",
+ SystemName = PermissionSystemName.AllowUseApi,
+ Area = "Public store",
+ Category = CategorySystem
+ };
}
}