diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs index eb4341880..e7af7e0c6 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/PermissionControllerTests.cs @@ -1,5 +1,6 @@ using Grand.Business.Core.Interfaces.Common.Directory; using Grand.Business.Core.Interfaces.Common.Security; +using Grand.Domain; using Grand.Domain.Customers; using Grand.Domain.Localization; using Grand.Domain.Permissions; @@ -7,7 +8,9 @@ using Grand.Web.Admin.Controllers; using Grand.Web.AdminShared.Models.Permissions; using Grand.Web.Common.Localization; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; @@ -28,6 +31,10 @@ public void Init() { var workContextMock = new Mock(); workContextMock.Setup(w => w.WorkingLanguage).Returns(new Language { Id = "lang-1" }); + //the admin who saves the permissions belongs to the group "admins" + var admin = new Customer(); + admin.Groups.Add("admins"); + workContextMock.Setup(w => w.CurrentCustomer).Returns(admin); var contextAccessorMock = new Mock(); contextAccessorMock.Setup(c => c.WorkContext).Returns(workContextMock.Object); @@ -35,7 +42,20 @@ public void Init() .ReturnsAsync(new List()); _controller = new PermissionController(contextAccessorMock.Object, _permissionServiceMock.Object, - _groupServiceMock.Object, new Mock().Object); + _groupServiceMock.Object, new Mock().Object) { + TempData = new TempDataDictionary(new DefaultHttpContext(), new Mock().Object) + }; + } + + private void PermissionsOfTheAdmins() + { + _groupServiceMock.Setup(g => g.GetAllCustomerGroups("", 0, int.MaxValue, true)) + .ReturnsAsync(new PagedList(new[] { new CustomerGroup { Id = "admins", Active = true } }, 0, 10)); + _permissionServiceMock.Setup(p => p.GetAllPermissions()).ReturnsAsync(new List { + new() { SystemName = PermissionSystemName.AccessAdminPanel, CustomerGroups = { "admins" } }, + new() { SystemName = PermissionSystemName.Acl, CustomerGroups = { "admins" } }, + new() { SystemName = SystemName, CustomerGroups = { "admins" } } + }); } private void GroupExists() => @@ -148,4 +168,38 @@ public async Task PermissionsActionPost_UnknownPermission_IsNotFoundAndWritesNot Assert.IsInstanceOfType(result); VerifyNothingWritten(); } + + [TestMethod] + public async Task PermissionsSave_AdminTakesAwayOwnAdminPanel_IsRefusedAndWritesNothing() + { + PermissionsOfTheAdmins(); + + await _controller.PermissionsSave(new Dictionary { + ["allow_admins"] = [PermissionSystemName.Acl, SystemName] + }); + + _permissionServiceMock.Verify(p => p.UpdatePermission(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task PermissionsSave_EverythingUnchecked_IsRefusedAndWritesNothing() + { + PermissionsOfTheAdmins(); + + await _controller.PermissionsSave(new Dictionary()); + + _permissionServiceMock.Verify(p => p.UpdatePermission(It.IsAny()), Times.Never); + } + + [TestMethod] + public async Task PermissionsSave_AdminKeepsOwnAccess_SavesTheOtherChanges() + { + PermissionsOfTheAdmins(); + + await _controller.PermissionsSave(new Dictionary { + ["allow_admins"] = [PermissionSystemName.AccessAdminPanel, PermissionSystemName.Acl] + }); + + _permissionServiceMock.Verify(p => p.UpdatePermission(It.Is(x => x.SystemName == SystemName)), Times.Once); + } } diff --git a/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs b/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs index 524d667ce..852ae838c 100644 --- a/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/PermissionController.cs @@ -115,6 +115,16 @@ public async Task PermissionsSave(IDictionary m var permissionRecords = await _permissionService.GetAllPermissions(); var customerGroups = await _groupService.GetAllCustomerGroups(showHidden: true); + //whoever saves keeps the admin panel and this page through one of their groups: nobody else may be left to give them back + var ownGroups = customerGroups.Where(g => g.Active && _contextAccessor.WorkContext.CurrentCustomer.Groups.Contains(g.Id)).ToList(); + bool KeepsOwn(string permission) => + ownGroups.Any(g => model.TryGetValue($"allow_{g.Id}", out var allowed) && allowed.Contains(permission)); + if (!KeepsOwn(PermissionSystemName.AccessAdminPanel) || !KeepsOwn(PermissionSystemName.Acl)) + { + Error(_translationService.GetResource("Admin.Configuration.Permissions.CantRemoveOwnAccess")); + return RedirectToAction("Index"); + } + foreach (var cr in customerGroups) { model.TryGetValue($"allow_{cr.Id}", out var permissionIds); diff --git a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs index 009d80ea9..f5ca407e3 100644 --- a/src/Web/Grand.Web.Admin/Controllers/SettingController.cs +++ b/src/Web/Grand.Web.Admin/Controllers/SettingController.cs @@ -714,13 +714,14 @@ public async Task GeneralCommon(GeneralCommonSettingsModel model) //security settings var securitySettings = await settingService.LoadSetting(storeScope); - securitySettings.AdminAreaAllowedIpAddresses ??= []; - securitySettings.AdminAreaAllowedIpAddresses.Clear(); - if (!string.IsNullOrEmpty(model.SecuritySettings.AdminAreaAllowedIpAddresses)) - foreach (var s in model.SecuritySettings.AdminAreaAllowedIpAddresses.Split([','], - StringSplitOptions.RemoveEmptyEntries)) - if (!string.IsNullOrWhiteSpace(s)) - securitySettings.AdminAreaAllowedIpAddresses.Add(s.Trim()); + var allowedIpAddresses = (model.SecuritySettings.AdminAreaAllowedIpAddresses ?? "") + .Split([','], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).ToList(); + //a list without the address the admin saves from would shut them out of the admin panel at once: keep the old one + var currentIp = HttpContext.Connection.RemoteIpAddress?.ToString(); + if (allowedIpAddresses.Count > 0 && !allowedIpAddresses.Contains(currentIp, StringComparer.OrdinalIgnoreCase)) + Error(string.Format(translationService.GetResource("Admin.Settings.GeneralCommon.AdminAreaAllowedIpAddresses.WithoutOwn"), currentIp)); + else + securitySettings.AdminAreaAllowedIpAddresses = allowedIpAddresses; await settingService.SaveSetting(securitySettings); diff --git a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml index 6793f972d..f8d841177 100644 Binary files a/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml and b/src/Web/Grand.Web/App_Data/Resources/DefaultLanguage.xml differ diff --git a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml index c8225fc40..b661b5ca1 100644 --- a/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml +++ b/src/Web/Grand.Web/App_Data/Resources/Upgrade/en_250.xml @@ -2829,4 +2829,10 @@ Sign out on all devices + + You can't take away your own access to the admin panel or to the permissions. Another administrator can change them. + + + The allowed IP addresses were not changed: they don't include your address ({0}), so you would be shut out of the admin panel. +