From 3106c22287961002aeba08d0185c2c44a9c45dd9 Mon Sep 17 00:00:00 2001 From: Shannon Date: Tue, 22 Aug 2017 17:55:28 +1000 Subject: [PATCH] =?UTF-8?q?UAASSCRUM-1045=20Umbraco=207.7:=20New=20sites?= =?UTF-8?q?=20created=20have=20an=20"Administrator=E2=80=9D=20user=20visib?= =?UTF-8?q?le=20in=20the=20back=20office.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Umbraco.Core/Services/IUserService.cs | 26 ++++++++- src/Umbraco.Core/Services/UserService.cs | 54 +++++++++++-------- .../Services/UserServiceTests.cs | 2 +- .../Web/Controllers/UsersControllerTests.cs | 2 +- src/Umbraco.Web/Editors/UsersController.cs | 17 +++++- 5 files changed, 73 insertions(+), 28 deletions(-) diff --git a/src/Umbraco.Core/Services/IUserService.cs b/src/Umbraco.Core/Services/IUserService.cs index db6afb55ff..adbd2a3c76 100644 --- a/src/Umbraco.Core/Services/IUserService.cs +++ b/src/Umbraco.Core/Services/IUserService.cs @@ -1,7 +1,9 @@ using System.Collections.Generic; using System.Linq; +using Umbraco.Core.Models; using Umbraco.Core.Models.Membership; using Umbraco.Core.Persistence.DatabaseModelDefinitions; +using Umbraco.Core.Persistence.Querying; namespace Umbraco.Core.Services { @@ -36,8 +38,28 @@ namespace Umbraco.Core.Services string orderBy, Direction orderDirection, UserState[] userState = null, string[] includeUserGroups = null, - string[] excludeUserGroups = null, - string filter = ""); + string[] excludeUserGroups = null, + IQuery filter = null); + + /// + /// Get paged users + /// + /// + /// + /// + /// + /// + /// + /// + /// A filter to only include user that belong to these user groups + /// + /// + /// + IEnumerable GetAll(long pageIndex, int pageSize, out long totalRecords, + string orderBy, Direction orderDirection, + UserState[] userState = null, + string[] userGroups = null, + string filter = null); /// /// This is simply a helper method which essentially just wraps the MembershipProvider's ChangePassword method diff --git a/src/Umbraco.Core/Services/UserService.cs b/src/Umbraco.Core/Services/UserService.cs index e6e4cb7cb7..8e34fbe5f1 100644 --- a/src/Umbraco.Core/Services/UserService.cs +++ b/src/Umbraco.Core/Services/UserService.cs @@ -155,7 +155,7 @@ namespace Umbraco.Core.Services using (var uow = UowProvider.GetUnitOfWork(readOnly: true)) { var repository = RepositoryFactory.CreateUserRepository(uow); - var result = repository.Get(id); + var result = repository.Get(id); return result; } } @@ -200,8 +200,8 @@ namespace Umbraco.Core.Services { using (var uow = UowProvider.GetUnitOfWork(readOnly: true)) { - var repository = RepositoryFactory.CreateUserRepository(uow); - + var repository = RepositoryFactory.CreateUserRepository(uow); + try { return repository.GetByUsername(username, includeSecurityData: true); @@ -314,11 +314,11 @@ namespace Umbraco.Core.Services if (string.IsNullOrWhiteSpace(entity.Name)) { throw new ArgumentException("Cannot save user with empty name."); - } - + } + //Now we have to check for backwards compat hacks, we'll need to process any groups - //to save first before we update the user since these groups might be new groups. - var explicitUser = entity as User; + //to save first before we update the user since these groups might be new groups. + var explicitUser = entity as User; if (explicitUser != null && explicitUser.GroupsToSave.Count > 0) { var groupRepository = RepositoryFactory.CreateUserGroupRepository(uow); @@ -556,6 +556,20 @@ namespace Umbraco.Core.Services } } + public IEnumerable GetAll(long pageIndex, int pageSize, out long totalRecords, + string orderBy, Direction orderDirection, + UserState[] userState = null, + string[] userGroups = null, + string filter = null) + { + IQuery filterQuery = null; + if (filter.IsNullOrWhiteSpace() == false) + { + filterQuery = Query.Builder.Where(x => x.Name.Contains(filter) || x.Username.Contains(filter)); + } + return GetAll(pageIndex, pageSize, out totalRecords, orderBy, orderDirection, userState, userGroups, null, filterQuery); + } + /// /// Get paged users /// @@ -573,12 +587,12 @@ namespace Umbraco.Core.Services /// /// /// - public IEnumerable GetAll(long pageIndex, int pageSize, out long totalRecords, - string orderBy, Direction orderDirection, - UserState[] userState = null, - string[] includeUserGroups = null, - string[] excludeUserGroups = null, - string filter = "") + public IEnumerable GetAll(long pageIndex, int pageSize, out long totalRecords, + string orderBy, Direction orderDirection, + UserState[] userState = null, + string[] includeUserGroups = null, + string[] excludeUserGroups = null, + IQuery filter = null) { using (var uow = UowProvider.GetUnitOfWork(readOnly: true)) { @@ -617,16 +631,10 @@ namespace Umbraco.Core.Services break; default: throw new IndexOutOfRangeException("The orderBy parameter " + orderBy + " is not valid"); - } - - IQuery filterQuery = null; - if (filter.IsNullOrWhiteSpace() == false) - { - filterQuery = Query.Builder.Where(x => x.Name.Contains(filter) || x.Username.Contains(filter)); - } - + } + var repository = RepositoryFactory.CreateUserRepository(uow); - return repository.GetPagedResultsByQuery(null, pageIndex, pageSize, out totalRecords, sort, orderDirection, includeUserGroups, excludeUserGroups, userState, filterQuery); + return repository.GetPagedResultsByQuery(null, pageIndex, pageSize, out totalRecords, sort, orderDirection, includeUserGroups, excludeUserGroups, userState, filter); } } @@ -811,7 +819,7 @@ namespace Umbraco.Core.Services x => new EntityPermission( groupId, x, - new[] {permission.ToString(CultureInfo.InvariantCulture)})) + new[] { permission.ToString(CultureInfo.InvariantCulture) })) .ToArray(), false)); } } diff --git a/src/Umbraco.Tests/Services/UserServiceTests.cs b/src/Umbraco.Tests/Services/UserServiceTests.cs index f3d5aa8aaf..946f5b7b8f 100644 --- a/src/Umbraco.Tests/Services/UserServiceTests.cs +++ b/src/Umbraco.Tests/Services/UserServiceTests.cs @@ -644,7 +644,7 @@ namespace Umbraco.Tests.Services ServiceContext.UserService.Save(users); long totalRecs; - var found = ServiceContext.UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, includeUserGroups: new[] { userGroup.Alias }, filter: "blah"); + var found = ServiceContext.UserService.GetAll(0, 2, out totalRecs, "username", Direction.Ascending, userGroups: new[] { userGroup.Alias }, filter: "blah"); Assert.AreEqual(2, found.Count()); Assert.AreEqual(2, totalRecs); diff --git a/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs b/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs index 8cc5e046ca..435cfe9cb1 100644 --- a/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs +++ b/src/Umbraco.Tests/Web/Controllers/UsersControllerTests.cs @@ -104,7 +104,7 @@ namespace Umbraco.Tests.Web.Controllers var userServiceMock = Mock.Get(helper.UmbracoContext.Application.Services.UserService); var users = MockedUser.CreateMulipleUsers(10); long outVal = 10; - userServiceMock.Setup(service => service.GetAll(It.IsAny(), It.IsAny(), out outVal, It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) + userServiceMock.Setup(service => service.GetAll(It.IsAny(), It.IsAny(), out outVal, It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())) .Returns(() => users); //we need to manually apply automapper mappings with the mocked applicationcontext diff --git a/src/Umbraco.Web/Editors/UsersController.cs b/src/Umbraco.Web/Editors/UsersController.cs index e01199936e..65eb0054e6 100644 --- a/src/Umbraco.Web/Editors/UsersController.cs +++ b/src/Umbraco.Web/Editors/UsersController.cs @@ -25,6 +25,7 @@ using Umbraco.Core.Models; using Umbraco.Core.Models.Identity; using Umbraco.Core.Models.Membership; using Umbraco.Core.Persistence.DatabaseModelDefinitions; +using Umbraco.Core.Persistence.Querying; using Umbraco.Core.Security; using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; @@ -224,9 +225,23 @@ namespace Umbraco.Web.Editors excludeUserGroups = new[] {Constants.Security.AdminGroupAlias}; } + var filterQuery = Query.Builder; + + //if the current user is not the administrator, then don't include this in the results. + var isAdminUser = Security.CurrentUser.Id == 0; + if (isAdminUser == false) + { + filterQuery.Where(x => x.Id != 0); + } + + if (filter.IsNullOrWhiteSpace() == false) + { + filterQuery.Where(x => x.Name.Contains(filter) || x.Username.Contains(filter)); + } + long pageIndex = pageNumber - 1; long total; - var result = Services.UserService.GetAll(pageIndex, pageSize, out total, orderBy, orderDirection, userStates, userGroups, excludeUserGroups, filter); + var result = Services.UserService.GetAll(pageIndex, pageSize, out total, orderBy, orderDirection, userStates, userGroups, excludeUserGroups, filterQuery); var paged = new PagedUserResult(total, pageNumber, pageSize) {