Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions src/Modules/Grand.Module.Api/Filters/AuthorizeApiAdminAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Domain.Permissions;
using Grand.Data;
using Grand.Domain.Security;
Expand Down Expand Up @@ -33,7 +34,7 @@ public AuthorizeApiAdminAttribute(bool ignore = false) : base(typeof(AuthorizeAp
/// Represents a filter that confirms access to the admin panel
/// </summary>
private class AuthorizeApiAdminFilter(bool ignoreFilter, IPermissionService permissionService,
IContextAccessor contextAccessor, SecuritySettings securitySettings, BackendAPIConfig backendApiConfig) : IAsyncAuthorizationFilter
IContextAccessor contextAccessor, IGroupService groupService, SecuritySettings securitySettings, BackendAPIConfig backendApiConfig) : IAsyncAuthorizationFilter
{

#region Ctor
Expand Down Expand Up @@ -74,9 +75,22 @@ public async Task OnAuthorizationAsync(AuthorizationFilterContext filterContext)
if (filterContext.Filters.Any(filter => filter is AuthorizeApiAdminFilter))
{
//authorize permission of access to the admin area
if (!await permissionService.Authorize(StandardPermission.ManageAccessAdminPanel,
contextAccessor.WorkContext?.CurrentCustomer))
var customer = contextAccessor.WorkContext?.CurrentCustomer;
if (!await permissionService.Authorize(StandardPermission.ManageAccessAdminPanel, customer))
{
filterContext.Result = new ForbidResult(JwtBearerDefaults.AuthenticationScheme);
return;
}

//the API works on all data, so the scoped panel roles stay out, as they do in the admin panel;
//a sales manager is turned away too, because the API has no sales employee scope
if (await groupService.IsVendor(customer) || !string.IsNullOrEmpty(customer.VendorId) ||
await groupService.IsStoreManager(customer) || !string.IsNullOrEmpty(customer.StaffStoreId) ||
await groupService.IsSalesManager(customer))
{
filterContext.Result = new ForbidResult(JwtBearerDefaults.AuthenticationScheme);
return;
}

//get allowed IP addresses
var ipAddresses = securitySettings.AdminAreaAllowedIpAddresses;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Security;
using Grand.Data;
using Grand.Domain.Customers;
Expand All @@ -22,6 +23,7 @@ public class AuthorizeApiAdminAttributeTests
{
private readonly Customer _customer = new();
private Mock<IContextAccessor> _contextAccessor;
private Mock<IGroupService> _groupService;
private Mock<IPermissionService> _permissionService;

[TestInitialize]
Expand All @@ -30,6 +32,7 @@ public void Init()
DataSettingsManager.Initialize(Path.Join(Path.GetTempPath(), "grand-api-tests-settings.json"));
DataSettingsManager.Instance.LoadDataSettings(new DataSettings { ConnectionString = "mongodb://localhost" });
_permissionService = new Mock<IPermissionService>();
_groupService = new Mock<IGroupService>();
_contextAccessor = new Mock<IContextAccessor>();
_contextAccessor.Setup(x => x.WorkContext.CurrentCustomer).Returns(_customer);
}
Expand All @@ -42,6 +45,7 @@ private async Task<AuthorizationFilterContext> Run(bool apiEnabled, bool ignore,
new ServiceCollection()
.AddSingleton(_permissionService.Object)
.AddSingleton(_contextAccessor.Object)
.AddSingleton(_groupService.Object)
.AddSingleton(new SecuritySettings())
.AddSingleton(new BackendAPIConfig { Enabled = apiEnabled })
.BuildServiceProvider(),
Expand Down Expand Up @@ -92,4 +96,46 @@ public async Task ApiEnabled_PermissionGranted_Allowed()
var context = await Run(apiEnabled: true, ignore: false, permitted: true);
Assert.IsNull(context.Result);
}

[TestMethod]
public async Task ApiEnabled_SalesManager_Forbid()
{
//the API has no sales employee scope, so a sales manager would reach every customer and order
_customer.SeId = "se1";
_groupService.Setup(x => x.IsSalesManager(_customer)).ReturnsAsync(true);
var context = await Run(apiEnabled: true, ignore: false, permitted: true);
Assert.IsInstanceOfType<ForbidResult>(context.Result);
}

[TestMethod]
public async Task ApiEnabled_StoreManager_Forbid()
{
_groupService.Setup(x => x.IsStoreManager(_customer)).ReturnsAsync(true);
var context = await Run(apiEnabled: true, ignore: false, permitted: true);
Assert.IsInstanceOfType<ForbidResult>(context.Result);
}

[TestMethod]
public async Task ApiEnabled_StaffOfStore_Forbid()
{
_customer.StaffStoreId = "store1";
var context = await Run(apiEnabled: true, ignore: false, permitted: true);
Assert.IsInstanceOfType<ForbidResult>(context.Result);
}

[TestMethod]
public async Task ApiEnabled_Vendor_Forbid()
{
_groupService.Setup(x => x.IsVendor(_customer)).ReturnsAsync(true);
var context = await Run(apiEnabled: true, ignore: false, permitted: true);
Assert.IsInstanceOfType<ForbidResult>(context.Result);
}

[TestMethod]
public async Task ApiEnabled_VendorAccount_Forbid()
{
_customer.VendorId = "vendor1";
var context = await Run(apiEnabled: true, ignore: false, permitted: true);
Assert.IsInstanceOfType<ForbidResult>(context.Result);
}
}
Loading