replaces the tuple collection for bulk permission assignment to use a new model: EntityPermissionSet
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
namespace Umbraco.Core.Models.Membership
|
||||
using System.Collections;
|
||||
|
||||
namespace Umbraco.Core.Models.Membership
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a user -> entity permission
|
||||
|
||||
59
src/Umbraco.Core/Models/Membership/EntityPermissionSet.cs
Normal file
59
src/Umbraco.Core/Models/Membership/EntityPermissionSet.cs
Normal file
@@ -0,0 +1,59 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Umbraco.Core.Models.Membership
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents an entity -> user & permission key value pair collection
|
||||
/// </summary>
|
||||
public class EntityPermissionSet
|
||||
{
|
||||
/// <summary>
|
||||
/// The entity id with permissions assigned
|
||||
/// </summary>
|
||||
public int EntityId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// The key/value pairs of user id & single permission
|
||||
/// </summary>
|
||||
public IEnumerable<UserPermission> UserPermissionsSet { get; private set; }
|
||||
|
||||
public EntityPermissionSet(int entityId, IEnumerable<UserPermission> userPermissionsSet)
|
||||
{
|
||||
EntityId = entityId;
|
||||
UserPermissionsSet = userPermissionsSet;
|
||||
}
|
||||
|
||||
public class UserPermission
|
||||
{
|
||||
public UserPermission(int userId, string permission)
|
||||
{
|
||||
UserId = userId;
|
||||
Permission = permission;
|
||||
}
|
||||
|
||||
public int UserId { get; private set; }
|
||||
public string Permission { get; private set; }
|
||||
|
||||
protected bool Equals(UserPermission other)
|
||||
{
|
||||
return UserId == other.UserId && string.Equals(Permission, other.Permission);
|
||||
}
|
||||
|
||||
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((UserPermission) obj);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
unchecked
|
||||
{
|
||||
return (UserId*397) ^ Permission.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -296,9 +296,9 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
var userPermissions = (
|
||||
from perm in parentPermissions
|
||||
from p in perm.AssignedPermissions
|
||||
select new Tuple<int, string>(perm.UserId, p)).ToList();
|
||||
|
||||
permissionsRepo.ReplaceEntityPermissions(entity, userPermissions);
|
||||
select new EntityPermissionSet.UserPermission(perm.UserId, p)).ToList();
|
||||
|
||||
permissionsRepo.ReplaceEntityPermissions(new EntityPermissionSet(entity.Id, userPermissions));
|
||||
//flag the entity's permissions changed flag so we can track those changes.
|
||||
//Currently only used for the cache refreshers to detect if we should refresh all user permissions cache.
|
||||
((Content) entity).PermissionsChanged = true;
|
||||
|
||||
@@ -191,25 +191,23 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <summary>
|
||||
/// Assigns permissions to an entity for multiple users/permission entries
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="userPermissions">
|
||||
/// A key/value pair list containing a userId and a permission to assign
|
||||
/// <param name="permissionSet">
|
||||
/// </param>
|
||||
/// <remarks>
|
||||
/// This will first clear the permissions for this entity then re-create them
|
||||
/// </remarks>
|
||||
public void ReplaceEntityPermissions(TEntity entity, IEnumerable<Tuple<int, string>> userPermissions)
|
||||
public void ReplaceEntityPermissions(EntityPermissionSet permissionSet)
|
||||
{
|
||||
var db = _unitOfWork.Database;
|
||||
using (var trans = db.GetTransaction())
|
||||
{
|
||||
db.Execute("DELETE FROM umbracoUser2NodePermission WHERE nodeId=@nodeId", new { nodeId = entity.Id });
|
||||
db.Execute("DELETE FROM umbracoUser2NodePermission WHERE nodeId=@nodeId", new { nodeId = permissionSet.EntityId });
|
||||
|
||||
var actions = userPermissions.Select(p => new User2NodePermissionDto
|
||||
var actions = permissionSet.UserPermissionsSet.Select(p => new User2NodePermissionDto
|
||||
{
|
||||
NodeId = entity.Id,
|
||||
Permission = p.Item2,
|
||||
UserId = p.Item1
|
||||
NodeId = permissionSet.EntityId,
|
||||
Permission = p.Permission,
|
||||
UserId = p.UserId
|
||||
}).ToArray();
|
||||
|
||||
_unitOfWork.Database.BulkInsertRecords(actions, trans);
|
||||
|
||||
@@ -351,6 +351,7 @@
|
||||
<Compile Include="Models\IPublishedProperty.cs" />
|
||||
<Compile Include="Models\IRelation.cs" />
|
||||
<Compile Include="Models\IRelationType.cs" />
|
||||
<Compile Include="Models\Membership\EntityPermissionSet.cs" />
|
||||
<Compile Include="Models\Membership\MembershipScenario.cs" />
|
||||
<Compile Include="Models\MemberGroup.cs" />
|
||||
<Compile Include="Models\MemberTypePropertyProfileAccess.cs" />
|
||||
|
||||
Reference in New Issue
Block a user