UAASSCRUM-1045 Umbraco 7.7: New sites created have an "Administrator” user visible in the back office.

This commit is contained in:
Shannon
2017-08-22 17:55:28 +10:00
parent 98bd044823
commit 3106c22287
5 changed files with 73 additions and 28 deletions

View File

@@ -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<IUser> filter = null);
/// <summary>
/// Get paged users
/// </summary>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <param name="totalRecords"></param>
/// <param name="orderBy"></param>
/// <param name="orderDirection"></param>
/// <param name="userState"></param>
/// <param name="userGroups">
/// A filter to only include user that belong to these user groups
/// </param>
/// <param name="filter"></param>
/// <returns></returns>
IEnumerable<IUser> GetAll(long pageIndex, int pageSize, out long totalRecords,
string orderBy, Direction orderDirection,
UserState[] userState = null,
string[] userGroups = null,
string filter = null);
/// <summary>
/// This is simply a helper method which essentially just wraps the MembershipProvider's ChangePassword method

View File

@@ -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<IUser> GetAll(long pageIndex, int pageSize, out long totalRecords,
string orderBy, Direction orderDirection,
UserState[] userState = null,
string[] userGroups = null,
string filter = null)
{
IQuery<IUser> filterQuery = null;
if (filter.IsNullOrWhiteSpace() == false)
{
filterQuery = Query<IUser>.Builder.Where(x => x.Name.Contains(filter) || x.Username.Contains(filter));
}
return GetAll(pageIndex, pageSize, out totalRecords, orderBy, orderDirection, userState, userGroups, null, filterQuery);
}
/// <summary>
/// Get paged users
/// </summary>
@@ -573,12 +587,12 @@ namespace Umbraco.Core.Services
/// </param>
/// <param name="filter"></param>
/// <returns></returns>
public IEnumerable<IUser> 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<IUser> GetAll(long pageIndex, int pageSize, out long totalRecords,
string orderBy, Direction orderDirection,
UserState[] userState = null,
string[] includeUserGroups = null,
string[] excludeUserGroups = null,
IQuery<IUser> 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<IUser> filterQuery = null;
if (filter.IsNullOrWhiteSpace() == false)
{
filterQuery = Query<IUser>.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));
}
}

View File

@@ -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);

View File

@@ -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<long>(), It.IsAny<int>(), out outVal, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<UserState[]>(), It.IsAny<string[]>(), It.IsAny<string[]>(), It.IsAny<string>()))
userServiceMock.Setup(service => service.GetAll(It.IsAny<long>(), It.IsAny<int>(), out outVal, It.IsAny<string>(), It.IsAny<Direction>(), It.IsAny<UserState[]>(), It.IsAny<string[]>(), It.IsAny<string>()))
.Returns(() => users);
//we need to manually apply automapper mappings with the mocked applicationcontext

View File

@@ -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<IUser>.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)
{