Gets last unit tests written for returning all permissions for either user or group when not specifying Ids
This commit is contained in:
@@ -14,7 +14,7 @@ namespace Umbraco.Core.Models.Membership
|
||||
{
|
||||
private readonly IContent _content;
|
||||
|
||||
public ContentPermissionSet(IContent content, IEnumerable<EntityPermission> permissionsSet)
|
||||
public ContentPermissionSet(IContent content, EntityPermissionCollection permissionsSet)
|
||||
: base(content.Id, permissionsSet)
|
||||
{
|
||||
_content = content;
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
using System.Linq;
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Core.Models.Membership
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an entity permission (defined on the user group and derived to retrieve permissions for a given user)
|
||||
/// </summary>
|
||||
public class EntityPermission
|
||||
public class EntityPermission : IEquatable<EntityPermission>
|
||||
{
|
||||
public EntityPermission(int groupId, int entityId, string[] assignedPermissions)
|
||||
{
|
||||
@@ -37,25 +37,30 @@ namespace Umbraco.Core.Models.Membership
|
||||
/// <remarks>
|
||||
/// This will be the case when looking up entity permissions and falling back to the default permissions
|
||||
/// </remarks>
|
||||
public bool IsDefaultPermissions { get; private set; }
|
||||
public bool IsDefaultPermissions { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Adds additional permissions to an existing instance of <see cref="EntityPermission"/>
|
||||
/// ensuring that only ones that aren't already assigned are added
|
||||
/// </summary>
|
||||
/// <param name="additionalPermissions"></param>
|
||||
public void AddAdditionalPermissions(string[] additionalPermissions)
|
||||
{
|
||||
//TODO: Fix the performance of this, we need to use things like HashSet and equality checkers, we are iterating too much
|
||||
public bool Equals(EntityPermission other)
|
||||
{
|
||||
if (ReferenceEquals(null, other)) return false;
|
||||
if (ReferenceEquals(this, other)) return true;
|
||||
return EntityId == other.EntityId && UserGroupId == other.UserGroupId;
|
||||
}
|
||||
|
||||
var newPermissions = AssignedPermissions.ToList();
|
||||
newPermissions.AddRange(additionalPermissions);
|
||||
AssignedPermissions = newPermissions
|
||||
.Distinct()
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (ReferenceEquals(null, obj)) return false;
|
||||
if (ReferenceEquals(this, obj)) return true;
|
||||
if (obj.GetType() != this.GetType()) return false;
|
||||
return Equals((EntityPermission) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return (EntityId * 397) ^ UserGroupId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Umbraco.Core.Models.Membership
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="HashSet{T}"/> of <see cref="EntityPermission"/>
|
||||
/// </summary>
|
||||
public class EntityPermissionCollection : HashSet<EntityPermission>
|
||||
{
|
||||
public EntityPermissionCollection()
|
||||
{
|
||||
}
|
||||
|
||||
public EntityPermissionCollection(IEnumerable<EntityPermission> collection) : base(collection)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the aggregate permissions in the permission set
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <remarks>
|
||||
/// This value is only calculated once
|
||||
/// </remarks>
|
||||
public IEnumerable<string> GetAllPermissions()
|
||||
{
|
||||
return _aggregatePermissions ?? (_aggregatePermissions =
|
||||
this.SelectMany(x => x.AssignedPermissions).Distinct().ToArray());
|
||||
}
|
||||
|
||||
private string[] _aggregatePermissions;
|
||||
}
|
||||
}
|
||||
@@ -14,10 +14,10 @@ namespace Umbraco.Core.Models.Membership
|
||||
/// <returns></returns>
|
||||
public static EntityPermissionSet Empty()
|
||||
{
|
||||
return new EntityPermissionSet(-1, new EntityPermission[0]);
|
||||
return new EntityPermissionSet(-1, new EntityPermissionCollection());
|
||||
}
|
||||
|
||||
public EntityPermissionSet(int entityId, IEnumerable<EntityPermission> permissionsSet)
|
||||
public EntityPermissionSet(int entityId, EntityPermissionCollection permissionsSet)
|
||||
{
|
||||
EntityId = entityId;
|
||||
PermissionsSet = permissionsSet;
|
||||
@@ -31,10 +31,11 @@ namespace Umbraco.Core.Models.Membership
|
||||
/// <summary>
|
||||
/// The key/value pairs of user group id & single permission
|
||||
/// </summary>
|
||||
public IEnumerable<EntityPermission> PermissionsSet { get; private set; }
|
||||
public EntityPermissionCollection PermissionsSet { get; private set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns the aggregte permissions in the permission set
|
||||
/// Returns the aggregate permissions in the permission set
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <remarks>
|
||||
@@ -42,15 +43,11 @@ namespace Umbraco.Core.Models.Membership
|
||||
/// </remarks>
|
||||
public IEnumerable<string> GetAllPermissions()
|
||||
{
|
||||
return _calculatedPermissions ?? (_calculatedPermissions =
|
||||
PermissionsSet.SelectMany(x => x.AssignedPermissions).Distinct().ToArray());
|
||||
return PermissionsSet.GetAllPermissions();
|
||||
}
|
||||
|
||||
private string[] _calculatedPermissions;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -847,7 +847,7 @@ order by umbracoNode.{2}, umbracoNode.parentID, umbracoNode.sortOrder",
|
||||
/// </summary>
|
||||
/// <param name="entityId"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<EntityPermission> GetPermissionsForEntity(int entityId)
|
||||
public EntityPermissionCollection GetPermissionsForEntity(int entityId)
|
||||
{
|
||||
return _permissionRepository.GetPermissionsForEntity(entityId);
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// </summary>
|
||||
/// <param name="entityId"></param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<EntityPermission> GetPermissionsForEntity(int entityId);
|
||||
EntityPermissionCollection GetPermissionsForEntity(int entityId);
|
||||
|
||||
///// <summary>
|
||||
///// Gets the implicit/inherited list of permissions for the content item
|
||||
|
||||
@@ -30,7 +30,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// </summary>
|
||||
/// <param name="groupId">Id of group</param>
|
||||
/// <param name="entityIds">Array of entity Ids, if empty will return permissions for the group for all entities</param>
|
||||
IEnumerable<EntityPermission> GetPermissionsForEntities(int groupId, params int[] entityIds);
|
||||
EntityPermissionCollection GetPermissionsForEntities(int groupId, params int[] entityIds);
|
||||
|
||||
/// <summary>
|
||||
/// Gets explicilt and default permissions (if requested) permissions for the group for specified entities
|
||||
@@ -38,7 +38,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <param name="group">The group</param>
|
||||
/// <param name="fallbackToDefaultPermissions">If true will include the group's default permissions if no permissions are explicitly assigned</param>
|
||||
/// <param name="nodeIds">Array of entity Ids, if empty will return permissions for the group for all entities</param>
|
||||
IEnumerable<EntityPermission> GetPermissionsForEntities(IReadOnlyUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds);
|
||||
EntityPermissionCollection GetPermissionsForEntities(IReadOnlyUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds);
|
||||
|
||||
/// <summary>
|
||||
/// Replaces the same permission set for a single group to any number of entities
|
||||
|
||||
@@ -42,62 +42,43 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <param name="groupId"></param>
|
||||
/// <param name="entityIds"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<EntityPermission> GetPermissionsForEntities(int groupId, params int[] entityIds)
|
||||
//var whereCriteria = GetPermissionsForEntitiesCriteria(groupId, entityIds);
|
||||
var result = new List<EntityPermission>();
|
||||
//iterate in groups of 2000 since we don't want to exceed the max SQL param count
|
||||
foreach (var groupOfIds in entityIds.InGroupsOf(2000))
|
||||
{
|
||||
var ids = groupOfIds;
|
||||
public EntityPermissionCollection GetPermissionsForEntities(int groupId, params int[] entityIds)
|
||||
{
|
||||
var result = new EntityPermissionCollection();
|
||||
|
||||
var sql = new Sql();
|
||||
if (entityIds.Length == 0)
|
||||
{
|
||||
var sql = new Sql();
|
||||
sql.Select("*")
|
||||
.From<UserGroup2NodePermissionDto>(SqlSyntax)
|
||||
.Where<UserGroup2NodePermissionDto>(dto => dto.UserGroupId == groupId && ids.Contains(dto.NodeId), SqlSyntax);
|
||||
.Where<UserGroup2NodePermissionDto>(dto => dto.UserGroupId == groupId, SqlSyntax);
|
||||
var permissions = UnitOfWork.Database.Fetch<UserGroup2NodePermissionDto>(sql);
|
||||
result.AddRange(ConvertToPermissionList(permissions));
|
||||
}
|
||||
return result;
|
||||
|
||||
|
||||
//var entityIdKey = GetEntityIdKey(entityIds);
|
||||
|
||||
//return _runtimeCache.GetCacheItem<IEnumerable<EntityPermission>>(
|
||||
// string.Format("{0}{1}{2}",
|
||||
// CacheKeys.UserGroupPermissionsCacheKey,
|
||||
// groupId,
|
||||
// entityIdKey),
|
||||
// () =>
|
||||
// {
|
||||
// var whereCriteria = GetPermissionsForEntitiesCriteria(groupId, entityIds);
|
||||
// var sql = new Sql();
|
||||
// sql.Select("*")
|
||||
// .From<UserGroup2NodePermissionDto>()
|
||||
// .Where(whereCriteria);
|
||||
// var result = UnitOfWork.Database.Fetch<UserGroup2NodePermissionDto>(sql);
|
||||
// return ConvertToPermissionList(result);
|
||||
// },
|
||||
// GetCacheTimeout(),
|
||||
// priority: GetCachePriority());
|
||||
}
|
||||
|
||||
//private static string GetEntityIdKey(int[] entityIds)
|
||||
//{
|
||||
// return string.Join(",", entityIds.Select(x => x.ToString(CultureInfo.InvariantCulture)));
|
||||
//}
|
||||
|
||||
//private static TimeSpan GetCacheTimeout()
|
||||
//{
|
||||
// //Since this cache can be quite large (http://issues.umbraco.org/issue/U4-2161) we will only have this exist in cache for 20 minutes,
|
||||
// // then it will refresh from the database.
|
||||
// return new TimeSpan(0, 20, 0);
|
||||
//}
|
||||
|
||||
//private static CacheItemPriority GetCachePriority()
|
||||
//{
|
||||
// //Since this cache can be quite large (http://issues.umbraco.org/issue/U4-2161) we will make this priority below average
|
||||
// return CacheItemPriority.BelowNormal;
|
||||
//}
|
||||
foreach (var permission in ConvertToPermissionList(permissions))
|
||||
{
|
||||
result.Add(permission);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//iterate in groups of 2000 since we don't want to exceed the max SQL param count
|
||||
foreach (var groupOfIds in entityIds.InGroupsOf(2000))
|
||||
{
|
||||
var ids = groupOfIds;
|
||||
|
||||
var sql = new Sql();
|
||||
sql.Select("*")
|
||||
.From<UserGroup2NodePermissionDto>(SqlSyntax)
|
||||
.Where<UserGroup2NodePermissionDto>(dto => dto.UserGroupId == groupId && ids.Contains(dto.NodeId), SqlSyntax);
|
||||
var permissions = UnitOfWork.Database.Fetch<UserGroup2NodePermissionDto>(sql);
|
||||
foreach (var permission in ConvertToPermissionList(permissions))
|
||||
{
|
||||
result.Add(permission);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns permissions directly assigned to the content items for all user groups
|
||||
@@ -121,7 +102,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// </summary>
|
||||
/// <param name="entityId"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<EntityPermission> GetPermissionsForEntity(int entityId)
|
||||
public EntityPermissionCollection GetPermissionsForEntity(int entityId)
|
||||
{
|
||||
var sql = new Sql();
|
||||
sql.Select("*")
|
||||
@@ -343,16 +324,16 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
ReplaceEntityPermissions(entity);
|
||||
}
|
||||
|
||||
private static IEnumerable<EntityPermission> ConvertToPermissionList(IEnumerable<UserGroup2NodePermissionDto> result)
|
||||
private static EntityPermissionCollection ConvertToPermissionList(IEnumerable<UserGroup2NodePermissionDto> result)
|
||||
{
|
||||
var permissions = new List<EntityPermission>();
|
||||
var permissions = new EntityPermissionCollection();
|
||||
var nodePermissions = result.GroupBy(x => x.NodeId);
|
||||
foreach (var np in nodePermissions)
|
||||
{
|
||||
var userGroupPermissions = np.GroupBy(x => x.UserGroupId);
|
||||
foreach (var permission in userGroupPermissions)
|
||||
{
|
||||
var perms = permission.Select(x => x.Permission).ToArray();
|
||||
var perms = permission.Select(x => x.Permission).Distinct().ToArray();
|
||||
permissions.Add(new EntityPermission(permission.Key, np.Key, perms));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// </summary>
|
||||
/// <param name="groupId">Id of group</param>
|
||||
/// <param name="entityIds">Array of entity Ids, if empty will return permissions for the group for all entities</param>
|
||||
public IEnumerable<EntityPermission> GetPermissionsForEntities(int groupId, params int[] entityIds)
|
||||
public EntityPermissionCollection GetPermissionsForEntities(int groupId, params int[] entityIds)
|
||||
{
|
||||
return _permissionRepository.GetPermissionsForEntities(groupId, entityIds);
|
||||
}
|
||||
@@ -108,12 +108,12 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <param name="group">The group</param>
|
||||
/// <param name="fallbackToDefaultPermissions">If true will include the group's default permissions if no permissions are explicitly assigned</param>
|
||||
/// <param name="nodeIds">Array of entity Ids, if empty will return permissions for the group for all entities</param>
|
||||
public IEnumerable<EntityPermission> GetPermissionsForEntities(IReadOnlyUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds)
|
||||
public EntityPermissionCollection GetPermissionsForEntities(IReadOnlyUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds)
|
||||
{
|
||||
if (group == null) throw new ArgumentNullException("group");
|
||||
|
||||
var explicitPermissions = GetPermissionsForEntities(group.Id, nodeIds);
|
||||
var result = new List<EntityPermission>(explicitPermissions);
|
||||
var result = new EntityPermissionCollection(explicitPermissions);
|
||||
|
||||
// If requested, and no permissions are assigned to a particular node, then we will fill in those permissions with the group's defaults
|
||||
if (fallbackToDefaultPermissions)
|
||||
@@ -121,8 +121,11 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var missingIds = nodeIds.Except(result.Select(x => x.EntityId)).ToArray();
|
||||
if (missingIds.Length > 0)
|
||||
{
|
||||
result.AddRange(missingIds
|
||||
.Select(i => new EntityPermission(group.Id, i, group.Permissions.ToArray(), isDefaultPermissions: true)));
|
||||
foreach (var permission in missingIds
|
||||
.Select(i => new EntityPermission(group.Id, i, group.Permissions.ToArray(), isDefaultPermissions: true)))
|
||||
{
|
||||
result.Add(permission);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
@@ -134,7 +134,7 @@ namespace Umbraco.Core.Services
|
||||
/// </summary>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
public IEnumerable<EntityPermission> GetPermissionsForEntity(IContent content)
|
||||
public EntityPermissionCollection GetPermissionsForEntity(IContent content)
|
||||
{
|
||||
using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
|
||||
{
|
||||
@@ -1673,14 +1673,14 @@ namespace Umbraco.Core.Services
|
||||
copy.WriterId = userId;
|
||||
|
||||
//get the current permissions, if there are any explicit ones they need to be copied
|
||||
var currentPermissions = GetPermissionsForEntity(content)
|
||||
.Where(x => x.IsDefaultPermissions == false).ToArray();
|
||||
var currentPermissions = GetPermissionsForEntity(content);
|
||||
currentPermissions.RemoveWhere(p => p.IsDefaultPermissions);
|
||||
|
||||
repository.AddOrUpdate(copy);
|
||||
repository.AddOrUpdatePreviewXml(copy, c => _entitySerializer.Serialize(this, _dataTypeService, _userService, c));
|
||||
|
||||
//add permissions
|
||||
if (currentPermissions.Length > 0)
|
||||
if (currentPermissions.Count > 0)
|
||||
{
|
||||
var permissionSet = new ContentPermissionSet(copy, currentPermissions);
|
||||
repository.AddOrUpdatePermissions(permissionSet);
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace Umbraco.Core.Services
|
||||
/// <param name="contentId"></param>
|
||||
public static void RemoveContentPermissions(this IContentService contentService, int contentId)
|
||||
{
|
||||
contentService.ReplaceContentPermissions(new EntityPermissionSet(contentId, Enumerable.Empty<EntityPermission>()));
|
||||
contentService.ReplaceContentPermissions(new EntityPermissionSet(contentId, new EntityPermissionCollection()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -155,7 +155,7 @@ namespace Umbraco.Core.Services
|
||||
/// </summary>
|
||||
/// <param name="content"></param>
|
||||
/// <returns></returns>
|
||||
IEnumerable<EntityPermission> GetPermissionsForEntity(IContent content);
|
||||
EntityPermissionCollection GetPermissionsForEntity(IContent content);
|
||||
|
||||
bool SendToPublication(IContent content, int userId = 0);
|
||||
|
||||
|
||||
@@ -83,8 +83,8 @@ namespace Umbraco.Core.Services
|
||||
/// </summary>
|
||||
/// <remarks>This is useful when an entire section is removed from config</remarks>
|
||||
/// <param name="sectionAlias">Alias of the section to remove</param>
|
||||
void DeleteSectionFromAllUserGroups(string sectionAlias);
|
||||
|
||||
void DeleteSectionFromAllUserGroups(string sectionAlias);
|
||||
|
||||
/// <summary>
|
||||
/// Get explicitly assigned permissions for a user and optional node ids
|
||||
/// </summary>
|
||||
@@ -95,8 +95,8 @@ namespace Umbraco.Core.Services
|
||||
/// <remarks>
|
||||
/// This will return the default permissions for the user's groups for nodes that don't have explicitly defined permissions
|
||||
/// </remarks>
|
||||
IEnumerable<EntityPermission> GetPermissions(IUser user, params int[] nodeIds);
|
||||
|
||||
EntityPermissionCollection GetPermissions(IUser user, params int[] nodeIds);
|
||||
|
||||
/// <summary>
|
||||
/// Get explicitly assigned permissions for a group and optional node Ids
|
||||
/// </summary>
|
||||
@@ -106,7 +106,7 @@ namespace Umbraco.Core.Services
|
||||
/// </param>
|
||||
/// <param name="nodeIds">Specifiying nothing will return all permissions for all nodes</param>
|
||||
/// <returns>An enumerable list of <see cref="EntityPermission"/></returns>
|
||||
IEnumerable<EntityPermission> GetPermissions(IUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds);
|
||||
EntityPermissionCollection GetPermissions(IUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the implicit/inherited permissions for the user for the given path
|
||||
|
||||
@@ -6,6 +6,7 @@ using System.Linq.Expressions;
|
||||
using Umbraco.Core.Configuration;
|
||||
using Umbraco.Core.Events;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.DatabaseModelDefinitions;
|
||||
@@ -845,15 +846,17 @@ namespace Umbraco.Core.Services
|
||||
/// <param name="user">User to retrieve permissions for</param>
|
||||
/// <param name="nodeIds">Specifiying nothing will return all permissions for all nodes</param>
|
||||
/// <returns>An enumerable list of <see cref="EntityPermission"/></returns>
|
||||
public IEnumerable<EntityPermission> GetPermissions(IUser user, params int[] nodeIds)
|
||||
{
|
||||
var result = new List<EntityPermission>();
|
||||
public EntityPermissionCollection GetPermissions(IUser user, params int[] nodeIds)
|
||||
{
|
||||
//TODO: we don't need to run this query for each group assigned, we can do this in one query
|
||||
|
||||
var result = new EntityPermissionCollection();
|
||||
|
||||
foreach (var group in user.Groups)
|
||||
{
|
||||
foreach (var permission in GetPermissions(group, true, nodeIds))
|
||||
{
|
||||
AddOrAmendPermissionList(result, permission);
|
||||
result.Add(permission);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -889,7 +892,7 @@ namespace Umbraco.Core.Services
|
||||
/// </param>
|
||||
/// <param name="nodeIds">Specifiying nothing will return all permissions for all nodes</param>
|
||||
/// <returns>An enumerable list of <see cref="EntityPermission"/></returns>
|
||||
public IEnumerable<EntityPermission> GetPermissions(IUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds)
|
||||
public EntityPermissionCollection GetPermissions(IUserGroup group, bool fallbackToDefaultPermissions, params int[] nodeIds)
|
||||
{
|
||||
if (group == null) throw new ArgumentNullException("group");
|
||||
using (var uow = UowProvider.GetUnitOfWork(readOnly: true))
|
||||
@@ -899,28 +902,6 @@ namespace Umbraco.Core.Services
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// For an existing list of <see cref="EntityPermission"/>, takes a new <see cref="EntityPermission"/> and aggregates it.
|
||||
/// If a permission for the entity associated with the new permission already exists, it's updated with those permissions to create a distinct, most permissive set.
|
||||
/// If it doesn't, it's added to the list.
|
||||
/// </summary>
|
||||
/// <param name="permissions">List of already found permissions</param>
|
||||
/// <param name="groupPermission">New permission to aggregate</param>
|
||||
private static void AddOrAmendPermissionList(ICollection<EntityPermission> permissions, EntityPermission groupPermission)
|
||||
{
|
||||
//TODO: Fix the performance of this, we need to use things like HashSet and equality checkers, we are iterating too much
|
||||
|
||||
var existingPermission = permissions.FirstOrDefault(x => x.EntityId == groupPermission.EntityId);
|
||||
if (existingPermission != null)
|
||||
{
|
||||
existingPermission.AddAdditionalPermissions(groupPermission.AssignedPermissions);
|
||||
}
|
||||
else
|
||||
{
|
||||
permissions.Add(groupPermission);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the implicit/inherited permissions for the user for the given path
|
||||
/// </summary>
|
||||
@@ -980,7 +961,7 @@ namespace Umbraco.Core.Services
|
||||
//The actual entity id being looked at (deepest part of the path)
|
||||
var entityId = pathIds[0];
|
||||
|
||||
var resultPermissions = new List<EntityPermission>();
|
||||
var resultPermissions = new EntityPermissionCollection();
|
||||
|
||||
//create a grouped by dictionary of another grouped by dictionary
|
||||
var permissionsByGroup = groupPermissions
|
||||
@@ -1008,9 +989,8 @@ namespace Umbraco.Core.Services
|
||||
{
|
||||
if (entityPermission.IsDefaultPermissions == false)
|
||||
{
|
||||
//explicit permision found so we'll append it and move on, of course if there was two explicit permissions
|
||||
//found for this group the ones after this one wouldn't matter but considering there should only be one per
|
||||
//group anyways, that is fine.
|
||||
//explicit permision found so we'll append it and move on, the collection is a hashset anyways
|
||||
//so only supports adding one element per groupid/contentid
|
||||
resultPermissions.Add(entityPermission);
|
||||
added = true;
|
||||
break;
|
||||
|
||||
@@ -346,6 +346,7 @@
|
||||
<Compile Include="IHttpContextAccessor.cs" />
|
||||
<Compile Include="Models\EntityBase\IDeletableEntity.cs" />
|
||||
<Compile Include="Models\Membership\ContentPermissionSet.cs" />
|
||||
<Compile Include="Models\Membership\EntityPermissionCollection.cs" />
|
||||
<Compile Include="Models\Membership\IReadOnlyUserGroup.cs" />
|
||||
<Compile Include="Models\Membership\ReadOnlyUserGroup.cs" />
|
||||
<Compile Include="Models\Membership\UserGroupExtensions.cs" />
|
||||
|
||||
@@ -80,21 +80,21 @@ namespace Umbraco.Tests.Services
|
||||
MockedContent.CreateSimpleContent(contentType)
|
||||
};
|
||||
ServiceContext.ContentService.Save(content);
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(0), ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(0), ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(0), ActionMove.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(1), ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(1), ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(2), ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionMove.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[2], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
|
||||
// Act
|
||||
var permissions = userService.GetPermissions(user, content.ElementAt(0).Id, content.ElementAt(1).Id, content.ElementAt(2).Id);
|
||||
var permissions = userService.GetPermissions(user, content[0].Id, content[1].Id, content[2].Id).ToArray();
|
||||
|
||||
//assert
|
||||
Assert.AreEqual(3, permissions.Count());
|
||||
Assert.AreEqual(3, permissions.ElementAt(0).AssignedPermissions.Length);
|
||||
Assert.AreEqual(2, permissions.ElementAt(1).AssignedPermissions.Length);
|
||||
Assert.AreEqual(1, permissions.ElementAt(2).AssignedPermissions.Length);
|
||||
Assert.AreEqual(3, permissions.Length);
|
||||
Assert.AreEqual(3, permissions[0].AssignedPermissions.Length);
|
||||
Assert.AreEqual(2, permissions[1].AssignedPermissions.Length);
|
||||
Assert.AreEqual(1, permissions[2].AssignedPermissions.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -121,13 +121,13 @@ namespace Umbraco.Tests.Services
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(2), ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
|
||||
// Act
|
||||
var permissions = userService.GetPermissions(userGroup, false, content.ElementAt(0).Id, content.ElementAt(1).Id, content.ElementAt(2).Id);
|
||||
var permissions = userService.GetPermissions(userGroup, false, content[0].Id, content[1].Id, content[2].Id).ToArray();
|
||||
|
||||
//assert
|
||||
Assert.AreEqual(3, permissions.Count());
|
||||
Assert.AreEqual(3, permissions.ElementAt(0).AssignedPermissions.Length);
|
||||
Assert.AreEqual(2, permissions.ElementAt(1).AssignedPermissions.Length);
|
||||
Assert.AreEqual(1, permissions.ElementAt(2).AssignedPermissions.Length);
|
||||
Assert.AreEqual(3, permissions.Length);
|
||||
Assert.AreEqual(3, permissions[0].AssignedPermissions.Length);
|
||||
Assert.AreEqual(2, permissions[1].AssignedPermissions.Length);
|
||||
Assert.AreEqual(1, permissions[2].AssignedPermissions.Length);
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -146,14 +146,14 @@ namespace Umbraco.Tests.Services
|
||||
MockedContent.CreateSimpleContent(contentType)
|
||||
};
|
||||
ServiceContext.ContentService.Save(content);
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(0), ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(0), ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(0), ActionMove.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(1), ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content.ElementAt(1), ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionMove.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
|
||||
// Act
|
||||
var permissions = userService.GetPermissions(userGroup, true, content.ElementAt(0).Id, content.ElementAt(1).Id, content.ElementAt(2).Id)
|
||||
var permissions = userService.GetPermissions(userGroup, true, content[0].Id, content[1].Id, content[2].Id)
|
||||
.ToArray();
|
||||
|
||||
//assert
|
||||
@@ -164,9 +164,100 @@ namespace Umbraco.Tests.Services
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Get_All_User_Permissions_For_All_Nodes()
|
||||
{
|
||||
// Arrange
|
||||
var userService = ServiceContext.UserService;
|
||||
var userGroup1 = CreateTestUserGroup();
|
||||
var userGroup2 = CreateTestUserGroup("test2", "Test 2");
|
||||
var user = userService.CreateUserWithIdentity("John Doe", "john@umbraco.io");
|
||||
|
||||
user.AddGroup(userGroup1);
|
||||
user.AddGroup(userGroup2);
|
||||
userService.Save(user);
|
||||
|
||||
var contentType = MockedContentTypes.CreateSimpleContentType();
|
||||
ServiceContext.ContentTypeService.Save(contentType);
|
||||
var content = new[]
|
||||
{
|
||||
MockedContent.CreateSimpleContent(contentType),
|
||||
MockedContent.CreateSimpleContent(contentType),
|
||||
MockedContent.CreateSimpleContent(contentType)
|
||||
};
|
||||
ServiceContext.ContentService.Save(content);
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionBrowse.Instance.Letter, new int[] { userGroup1.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionDelete.Instance.Letter, new int[] { userGroup1.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionMove.Instance.Letter, new int[] { userGroup2.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionBrowse.Instance.Letter, new int[] { userGroup1.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionDelete.Instance.Letter, new int[] { userGroup2.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[2], ActionDelete.Instance.Letter, new int[] { userGroup1.Id });
|
||||
|
||||
// Act
|
||||
//we don't pass in any nodes so it will return all of them
|
||||
var result = userService.GetPermissions(user).ToArray();
|
||||
var permissions = result
|
||||
.GroupBy(x => x.EntityId)
|
||||
.ToDictionary(x => x.Key, x => x.GroupBy(a => a.UserGroupId).ToDictionary(a => a.Key, a => a.ToArray()));
|
||||
|
||||
//assert
|
||||
Assert.AreEqual(3, permissions.Count);
|
||||
Assert.IsTrue(permissions.ContainsKey(content[0].Id));
|
||||
Assert.IsTrue(permissions[content[0].Id].ContainsKey(userGroup1.Id));
|
||||
Assert.IsTrue(permissions[content[0].Id].ContainsKey(userGroup2.Id));
|
||||
Assert.AreEqual(2, permissions[content[0].Id][userGroup1.Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
Assert.AreEqual(1, permissions[content[0].Id][userGroup2.Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
Assert.IsTrue(permissions.ContainsKey(content[1].Id));
|
||||
Assert.IsTrue(permissions[content[1].Id].ContainsKey(userGroup1.Id));
|
||||
Assert.IsTrue(permissions[content[1].Id].ContainsKey(userGroup2.Id));
|
||||
Assert.AreEqual(1, permissions[content[1].Id][userGroup1.Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
Assert.AreEqual(1, permissions[content[1].Id][userGroup2.Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
Assert.IsTrue(permissions.ContainsKey(content[2].Id));
|
||||
Assert.IsTrue(permissions[content[2].Id].ContainsKey(userGroup1.Id));
|
||||
Assert.IsFalse(permissions[content[2].Id].ContainsKey(userGroup2.Id));
|
||||
Assert.AreEqual(1, permissions[content[2].Id][userGroup1.Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Calculate_Permissions_For_User_For_Path_1()
|
||||
public void Get_All_User_Group_Permissions_For_All_Nodes()
|
||||
{
|
||||
// Arrange
|
||||
var userService = ServiceContext.UserService;
|
||||
var userGroup = CreateTestUserGroup();
|
||||
|
||||
var contentType = MockedContentTypes.CreateSimpleContentType();
|
||||
ServiceContext.ContentTypeService.Save(contentType);
|
||||
var content = new[]
|
||||
{
|
||||
MockedContent.CreateSimpleContent(contentType),
|
||||
MockedContent.CreateSimpleContent(contentType),
|
||||
MockedContent.CreateSimpleContent(contentType)
|
||||
};
|
||||
ServiceContext.ContentService.Save(content);
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[0], ActionMove.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionBrowse.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[1], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
ServiceContext.ContentService.AssignContentPermission(content[2], ActionDelete.Instance.Letter, new int[] { userGroup.Id });
|
||||
|
||||
// Act
|
||||
//we don't pass in any nodes so it will return all of them
|
||||
var permissions = userService.GetPermissions(userGroup, true)
|
||||
.GroupBy(x => x.EntityId)
|
||||
.ToDictionary(x => x.Key, x => x);
|
||||
|
||||
//assert
|
||||
Assert.AreEqual(3, permissions.Count);
|
||||
Assert.IsTrue(permissions.ContainsKey(content[0].Id));
|
||||
Assert.AreEqual(3, permissions[content[0].Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
Assert.IsTrue(permissions.ContainsKey(content[1].Id));
|
||||
Assert.AreEqual(2, permissions[content[1].Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
Assert.IsTrue(permissions.ContainsKey(content[2].Id));
|
||||
Assert.AreEqual(1, permissions[content[2].Id].SelectMany(x => x.AssignedPermissions).Count());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Calculate_Permissions_For_User_For_Path()
|
||||
{
|
||||
//see: http://issues.umbraco.org/issue/U4-10075#comment=67-40085
|
||||
// for an overview of what this is testing
|
||||
@@ -857,12 +948,12 @@ namespace Umbraco.Tests.Services
|
||||
return user;
|
||||
}
|
||||
|
||||
private UserGroup CreateTestUserGroup()
|
||||
private UserGroup CreateTestUserGroup(string alias = "testGroup", string name = "Test Group")
|
||||
{
|
||||
var userGroup = new UserGroup
|
||||
{
|
||||
Alias = "testGroup",
|
||||
Name = "Test Group",
|
||||
Alias = alias,
|
||||
Name = name,
|
||||
Permissions = "ABCDEFGHIJ1234567".ToCharArray().Select(x => x.ToString())
|
||||
};
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
contentServiceMock.Setup(x => x.GetById(0)).Returns(content);
|
||||
var contentService = contentServiceMock.Object;
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>();
|
||||
var permissions = new EntityPermissionCollection();
|
||||
var permissionSet = new EntityPermissionSet(1234, permissions);
|
||||
userServiceMock.Setup(x => x.GetPermissionsForPath(user, "-1,1234,5678")).Returns(permissionSet);
|
||||
var userService = userServiceMock.Object;
|
||||
@@ -73,7 +73,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
contentServiceMock.Setup(x => x.GetById(1234)).Returns(content);
|
||||
var contentService = contentServiceMock.Object;
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>();
|
||||
var permissions = new EntityPermissionCollection();
|
||||
var permissionSet = new EntityPermissionSet(1234, permissions);
|
||||
userServiceMock.Setup(x => x.GetPermissionsForPath(user, "-1,1234")).Returns(permissionSet);
|
||||
var userService = userServiceMock.Object;
|
||||
@@ -100,7 +100,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
contentServiceMock.Setup(x => x.GetById(1234)).Returns(content);
|
||||
var contentService = contentServiceMock.Object;
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1234, new string[]{ "A", "B", "C" })
|
||||
};
|
||||
@@ -130,7 +130,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
contentServiceMock.Setup(x => x.GetById(1234)).Returns(content);
|
||||
var contentService = contentServiceMock.Object;
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1234, new string[]{ "A", "F", "C" })
|
||||
};
|
||||
@@ -219,7 +219,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
var user = userMock.Object;
|
||||
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1234, new string[]{ "A" })
|
||||
};
|
||||
@@ -244,7 +244,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
var user = userMock.Object;
|
||||
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1234, new string[]{ "A" })
|
||||
};
|
||||
@@ -269,7 +269,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
var user = userMock.Object;
|
||||
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1234, new string[]{ "A" })
|
||||
};
|
||||
@@ -295,7 +295,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
var user = userMock.Object;
|
||||
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1234, new string[]{ "A" })
|
||||
};
|
||||
|
||||
@@ -110,7 +110,7 @@ namespace Umbraco.Tests.Web.Controllers
|
||||
|
||||
var userServiceMock = new Mock<IUserService>();
|
||||
//we're only assigning 3 nodes browse permissions so that is what we expect as a result
|
||||
var permissions = new List<EntityPermission>
|
||||
var permissions = new EntityPermissionCollection
|
||||
{
|
||||
new EntityPermission(9876, 1, new string[]{ "F" }),
|
||||
new EntityPermission(9876, 2, new string[]{ "F" }),
|
||||
|
||||
Reference in New Issue
Block a user