5151 - Added GetProfilesById extension

This commit is contained in:
Bjarke Berg
2020-04-23 08:38:29 +02:00
parent c58831925e
commit 27f7d5efae
3 changed files with 33 additions and 2 deletions

View File

@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;
using Umbraco.Core.Models.Membership;
@@ -116,5 +117,17 @@ namespace Umbraco.Core.Services
var permissionCollection = userService.GetPermissions(user, nodeId);
return permissionCollection.SelectMany(c => c.AssignedPermissions).Distinct().ToArray();
}
internal static IEnumerable<IProfile> GetProfilesById(this IUserService userService, params int[] ids)
{
var fullUsers = userService.GetUsersById(ids);
return fullUsers.Select(user =>
{
var asProfile = user as IProfile;
return asProfile ?? new UserProfile(user.Id, user.Name);
});
}
}
}

View File

@@ -53,9 +53,9 @@ namespace Umbraco.Examine
// processing below instead of one by one.
using (var scope = _scopeProvider.CreateScope())
{
creatorIds = content.Select(x => x.CreatorId).Distinct().Select(x => _userService.GetProfileById(x))
creatorIds = _userService.GetProfilesById(content.Select(x => x.CreatorId).ToArray())
.ToDictionary(x => x.Id, x => x);
writerIds = content.Select(x => x.WriterId).Distinct().Select(x => _userService.GetProfileById(x))
writerIds = _userService.GetProfilesById(content.Select(x => x.WriterId).ToArray())
.ToDictionary(x => x.Id, x => x);
scope.Complete();
}

View File

@@ -924,6 +924,24 @@ namespace Umbraco.Tests.Services
Assert.AreEqual(user.Id, profile.Id);
}
[Test]
public void Get_By_Profile_Id_Must_return_null_if_user_not_exists()
{
var profile = ServiceContext.UserService.GetProfileById(42);
// Assert
Assert.IsNull(profile);
}
[Test]
public void GetProfilesById_Must_empty_if_users_not_exists()
{
var profiles = ServiceContext.UserService.GetProfilesById(42);
// Assert
CollectionAssert.IsEmpty(profiles);
}
[Test]
public void Get_User_By_Username()
{