/// <summary> /// Assigns one permission to an entity for multiple users /// </summary> /// <param name="entity"></param> /// <param name="permission"></param> /// <param name="userIds"></param> public void AssignEntityPermission(TEntity entity, char permission, IEnumerable <int> userIds) { var db = _unitOfWork.Database; using (var trans = db.GetTransaction()) { db.Execute("DELETE FROM umbracoUser2NodePermission WHERE nodeId=@nodeId AND permission=@permission AND userId in (@userIds)", new { nodeId = entity.Id, permission = permission.ToString(CultureInfo.InvariantCulture), userIds = userIds }); var actions = userIds.Select(id => new User2NodePermissionDto { NodeId = entity.Id, Permission = permission.ToString(CultureInfo.InvariantCulture), UserId = id }).ToArray(); _unitOfWork.Database.BulkInsertRecords(actions, trans); trans.Complete(); //Raise the event AssignedPermissions.RaiseEvent( new SaveEventArgs <EntityPermission>(ConvertToPermissionList(actions), false), this); } }
/// <summary> /// Assigns permissions to an entity for multiple users/permission entries /// </summary> /// <param name="permissionSet"> /// </param> /// <remarks> /// This will first clear the permissions for this entity then re-create them /// </remarks> public void ReplaceEntityPermissions(EntityPermissionSet permissionSet) { var db = _unitOfWork.Database; using (var trans = db.GetTransaction()) { db.Execute("DELETE FROM umbracoUser2NodePermission WHERE nodeId=@nodeId", new { nodeId = permissionSet.EntityId }); var actions = permissionSet.UserPermissionsSet.Select(p => new User2NodePermissionDto { NodeId = permissionSet.EntityId, Permission = p.Permission, UserId = p.UserId }).ToArray(); _unitOfWork.Database.BulkInsertRecords(actions, trans); trans.Complete(); //Raise the event AssignedPermissions.RaiseEvent( new SaveEventArgs <EntityPermission>(ConvertToPermissionList(actions), false), this); } }
/// <summary> /// Assigns the same permission set for a single user to any number of entities /// </summary> /// <param name="userId"></param> /// <param name="permissions"></param> /// <param name="entityIds"></param> /// <remarks> /// This will first clear the permissions for this user and entities and recreate them /// </remarks> public void ReplaceUserPermissions(int userId, IEnumerable <char> permissions, params int[] entityIds) { var db = _unitOfWork.Database; using (var trans = db.GetTransaction()) { //we need to batch these in groups of 2000 so we don't exceed the max 2100 limit foreach (var idGroup in entityIds.InGroupsOf(2000)) { db.Execute("DELETE FROM umbracoUser2NodePermission WHERE userId=@userId AND nodeId in (@nodeIds)", new { userId = userId, nodeIds = idGroup }); } var toInsert = new List <User2NodePermissionDto>(); foreach (var p in permissions) { foreach (var e in entityIds) { toInsert.Add(new User2NodePermissionDto { NodeId = e, Permission = p.ToString(CultureInfo.InvariantCulture), UserId = userId }); } } _unitOfWork.Database.BulkInsertRecords(toInsert, trans); trans.Complete(); //Raise the event AssignedPermissions.RaiseEvent( new SaveEventArgs <EntityPermission>(ConvertToPermissionList(toInsert), false), this); } }