Started adding base PermissionsRepository for sub classes to use for assigning permissions, created a BulkImport extension methods for PetaPoco with tests since we'll be needing that for assigning permissions in a nice way. Wrote unit tests for all sql gen for permissions and assigning permissions. This all starts fixing #U4-2161 but there's still a bit more work to do.
This commit is contained in:
@@ -19,7 +19,7 @@ namespace Umbraco.Core.Models.Membership
|
||||
|
||||
bool NoConsole { get; set; }
|
||||
IUserType UserType { get; }
|
||||
string Permissions { get; set; }
|
||||
string DefaultPermissions { get; set; }
|
||||
}
|
||||
|
||||
internal interface IUserProfile : IProfile
|
||||
|
||||
@@ -91,7 +91,7 @@ namespace Umbraco.Core.Models.Membership
|
||||
[DataMember]
|
||||
public string Language { get; set; }
|
||||
[DataMember]
|
||||
public string Permissions { get; set; }
|
||||
public string DefaultPermissions { get; set; }
|
||||
|
||||
[DataMember]
|
||||
public bool DefaultToLiveEditing { get; set; }
|
||||
|
||||
@@ -32,7 +32,7 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
Language = dto.UserLanguage,
|
||||
DefaultToLiveEditing = dto.DefaultToLiveEditing,
|
||||
NoConsole = dto.NoConsole,
|
||||
Permissions = dto.DefaultPermissions
|
||||
DefaultPermissions = dto.DefaultPermissions
|
||||
};
|
||||
|
||||
foreach (var app in dto.User2AppDtos)
|
||||
@@ -62,7 +62,7 @@ namespace Umbraco.Core.Persistence.Factories
|
||||
UserLanguage = entity.Language,
|
||||
UserName = entity.Name,
|
||||
Type = short.Parse(entity.UserType.Id.ToString()),
|
||||
DefaultPermissions = entity.Permissions,
|
||||
DefaultPermissions = entity.DefaultPermissions,
|
||||
User2AppDtos = new List<User2AppDto>()
|
||||
};
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace Umbraco.Core.Persistence.Mappers
|
||||
CacheMap<User, UserDto>(src => src.Username, dto => dto.Login);
|
||||
CacheMap<User, UserDto>(src => src.Password, dto => dto.Password);
|
||||
CacheMap<User, UserDto>(src => src.Name, dto => dto.UserName);
|
||||
CacheMap<User, UserDto>(src => src.Permissions, dto => dto.DefaultPermissions);
|
||||
CacheMap<User, UserDto>(src => src.DefaultPermissions, dto => dto.DefaultPermissions);
|
||||
CacheMap<User, UserDto>(src => src.StartMediaId, dto => dto.MediaStartId);
|
||||
CacheMap<User, UserDto>(src => src.StartContentId, dto => dto.ContentStartId);
|
||||
CacheMap<User, UserDto>(src => src.DefaultToLiveEditing, dto => dto.DefaultToLiveEditing);
|
||||
|
||||
@@ -384,7 +384,7 @@ namespace Umbraco.Core.Persistence
|
||||
}
|
||||
|
||||
// Add a parameter to a DB command
|
||||
void AddParam(IDbCommand cmd, object item, string ParameterPrefix)
|
||||
internal void AddParam(IDbCommand cmd, object item, string ParameterPrefix)
|
||||
{
|
||||
// Convert value to from poco type to db type
|
||||
if (Database.Mapper != null && item!=null)
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
@@ -28,6 +30,76 @@ namespace Umbraco.Core.Persistence
|
||||
CreateTable(db, overwrite, tableType);
|
||||
}
|
||||
|
||||
public static void BulkInsertRecords<T>(this Database db, IEnumerable<T> collection)
|
||||
{
|
||||
using (var tr = db.GetTransaction())
|
||||
{
|
||||
try
|
||||
{
|
||||
if (SqlSyntaxContext.SqlSyntaxProvider is SqlCeSyntaxProvider)
|
||||
{
|
||||
//SqlCe doesn't support bulk insert statements!
|
||||
|
||||
foreach (var poco in collection)
|
||||
{
|
||||
db.Insert(poco);
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
string sql;
|
||||
using (var cmd = db.GenerateBulkInsertCommand(collection, db.Connection, out sql))
|
||||
{
|
||||
cmd.CommandText = sql;
|
||||
cmd.ExecuteNonQuery();
|
||||
}
|
||||
}
|
||||
|
||||
tr.Complete();
|
||||
}
|
||||
catch
|
||||
{
|
||||
tr.Dispose();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal static IDbCommand GenerateBulkInsertCommand<T>(this Database db, IEnumerable<T> collection, IDbConnection connection, out string sql)
|
||||
{
|
||||
var pd = Database.PocoData.ForType(typeof(T));
|
||||
var tableName = db.EscapeTableName(pd.TableInfo.TableName);
|
||||
|
||||
//get all columns but not the primary key if it is auto-incremental
|
||||
var cols = string.Join(", ", (
|
||||
from c in pd.Columns
|
||||
where c.Value.ResultColumn == false
|
||||
select tableName + "." + db.EscapeSqlIdentifier(c.Key))
|
||||
.ToArray());
|
||||
|
||||
var cmd = db.CreateCommand(connection, "");
|
||||
|
||||
var pocoValues = new List<string>();
|
||||
var index = 0;
|
||||
foreach (var poco in collection)
|
||||
{
|
||||
var values = new List<string>();
|
||||
foreach (var i in pd.Columns)
|
||||
{
|
||||
//if (pd.TableInfo.AutoIncrement && i.Key == pd.TableInfo.PrimaryKey)
|
||||
//{
|
||||
// continue;
|
||||
//}
|
||||
values.Add(string.Format("{0}{1}", "@", index++));
|
||||
db.AddParam(cmd, i.Value.GetValue(poco), "@");
|
||||
}
|
||||
pocoValues.Add("(" + string.Join(",", values.ToArray()) + ")");
|
||||
}
|
||||
sql = string.Format("INSERT INTO {0} ({1}) VALUES {2}", tableName, cols, string.Join(", ", pocoValues));
|
||||
return cmd;
|
||||
}
|
||||
|
||||
public static void CreateTable(this Database db, bool overwrite, Type modelType)
|
||||
{
|
||||
var tableDefinition = DefinitionFactory.GetTableDefinition(modelType);
|
||||
@@ -106,7 +178,7 @@ namespace Umbraco.Core.Persistence
|
||||
public static void DropTable<T>(this Database db)
|
||||
where T : new()
|
||||
{
|
||||
Type type = typeof (T);
|
||||
Type type = typeof(T);
|
||||
var tableNameAttribute = type.FirstAttribute<TableNameAttribute>();
|
||||
if (tableNameAttribute == null)
|
||||
throw new Exception(
|
||||
@@ -184,5 +256,5 @@ namespace Umbraco.Core.Persistence
|
||||
}
|
||||
}
|
||||
|
||||
internal class TableCreationEventArgs : System.ComponentModel.CancelEventArgs{}
|
||||
internal class TableCreationEventArgs : System.ComponentModel.CancelEventArgs { }
|
||||
}
|
||||
@@ -217,6 +217,21 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
entity.SortOrder = sortOrder;
|
||||
entity.Level = level;
|
||||
|
||||
|
||||
//Assign the same permissions to it as the parent node
|
||||
// http://issues.umbraco.org/issue/U4-2161
|
||||
var parentPermissions = GetPermissionsForEntity(entity.ParentId).ToArray();
|
||||
//if there are parent permissions then assign them, otherwise leave null and permissions will become the
|
||||
// user's default permissions.
|
||||
if (parentPermissions.Any())
|
||||
{
|
||||
//group by the unique permission and assign then for the users of that permission set.
|
||||
foreach (var assignedPermission in parentPermissions.GroupBy(x => x.Permission))
|
||||
{
|
||||
AssignEntityPermissions(entity, assignedPermission.Key, assignedPermission.Select(x => (object)x.UserId));
|
||||
}
|
||||
}
|
||||
|
||||
//Create the Content specific data - cmsContent
|
||||
var contentDto = dto.ContentVersionDto.ContentDto;
|
||||
contentDto.NodeId = nodeDto.NodeId;
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Dynamic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.EntityBase;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence.Caching;
|
||||
using Umbraco.Core.Persistence.SqlSyntax;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
/// <summary>
|
||||
/// A repository that exposes functionality to modify assigned permissions to a node
|
||||
/// </summary>
|
||||
/// <typeparam name="TId"></typeparam>
|
||||
/// <typeparam name="TEntity"></typeparam>
|
||||
internal abstract class PermissionRepository<TId, TEntity> : PetaPocoRepositoryBase<TId, TEntity>
|
||||
where TEntity : class, IAggregateRoot
|
||||
{
|
||||
protected PermissionRepository(IDatabaseUnitOfWork work)
|
||||
: base(work)
|
||||
{
|
||||
}
|
||||
|
||||
protected PermissionRepository(IDatabaseUnitOfWork work, IRepositoryCacheProvider cache)
|
||||
: base(work, cache)
|
||||
{
|
||||
}
|
||||
|
||||
protected internal IEnumerable<User2NodePermissionDto> GetPermissionsForEntity(int entityId)
|
||||
{
|
||||
var sql = new Sql();
|
||||
sql.Select("*")
|
||||
.From<User2NodePermissionDto>()
|
||||
.Where<User2NodePermissionDto>(dto => dto.NodeId == entityId);
|
||||
return Database.Fetch<User2NodePermissionDto>(sql);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Assigns permissions to an entity for multiple users
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="permissions"></param>
|
||||
/// <param name="userIds"></param>
|
||||
protected internal void AssignEntityPermissions(TEntity entity, string permissions, IEnumerable<object> userIds)
|
||||
{
|
||||
var actions = userIds.Select(id => new User2NodePermissionDto
|
||||
{
|
||||
NodeId = entity.Id,
|
||||
Permission = permissions,
|
||||
UserId = (int)id
|
||||
});
|
||||
|
||||
Database.BulkInsertRecords(actions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Replace permissions for an entity for multiple users
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="permissions"></param>
|
||||
/// <param name="userIds"></param>
|
||||
protected internal void ReplaceEntityPermissions(TEntity entity, string permissions, IEnumerable<object> userIds)
|
||||
{
|
||||
Database.Update<User2NodePermissionDto>(
|
||||
GenerateReplaceEntityPermissionsSql(entity.Id, permissions, userIds.ToArray()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An overload to replace entity permissions and all replace all descendant permissions
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="permissions"></param>
|
||||
/// <param name="getDescendantIds">
|
||||
/// A callback to get the descendant Ids of the current entity
|
||||
/// </param>
|
||||
/// <param name="userIds"></param>
|
||||
protected internal void ReplaceEntityPermissions(TEntity entity, string permissions, Func<IEntity, IEnumerable<int>> getDescendantIds, IEnumerable<object> userIds)
|
||||
{
|
||||
Database.Update<User2NodePermissionDto>(
|
||||
GenerateReplaceEntityPermissionsSql(
|
||||
new[] {entity.Id}.Concat(getDescendantIds(entity)).ToArray(),
|
||||
permissions,
|
||||
userIds.ToArray()));
|
||||
}
|
||||
|
||||
internal static string GenerateReplaceEntityPermissionsSql(int entityId, string permissions, object[] userIds)
|
||||
{
|
||||
return GenerateReplaceEntityPermissionsSql(new[] {entityId}, permissions, userIds);
|
||||
}
|
||||
|
||||
internal static string GenerateReplaceEntityPermissionsSql(int[] entityIds, string permissions, object[] userIds)
|
||||
{
|
||||
//create the "SET" clause of the update statement
|
||||
var sqlSet = string.Format("SET {0}={1}",
|
||||
SqlSyntaxContext.SqlSyntaxProvider.GetQuotedColumnName("permission"),
|
||||
SqlSyntaxContext.SqlSyntaxProvider.GetQuotedValue(permissions));
|
||||
|
||||
//build the nodeIds part of the where clause
|
||||
var sqlNodeWhere = BuildOrClause(entityIds, "nodeId");
|
||||
|
||||
//build up the userIds part of the where clause
|
||||
var userWhereBuilder = BuildOrClause(userIds, "userId");
|
||||
|
||||
var sqlWhere = new Sql();
|
||||
sqlWhere.Where(string.Format("{0} AND {1}", sqlNodeWhere, userWhereBuilder));
|
||||
|
||||
return string.Format("{0} {1}", sqlSet, sqlWhere.SQL);
|
||||
}
|
||||
|
||||
private static string BuildOrClause<T>(IEnumerable<T> ids, string colName)
|
||||
{
|
||||
var asArray = ids.ToArray();
|
||||
var userWhereBuilder = new StringBuilder();
|
||||
userWhereBuilder.Append("(");
|
||||
for (var index = 0; index < asArray.Length; index++)
|
||||
{
|
||||
var userId = asArray[index];
|
||||
userWhereBuilder.Append(SqlSyntaxContext.SqlSyntaxProvider.GetQuotedColumnName(colName));
|
||||
userWhereBuilder.Append("=");
|
||||
userWhereBuilder.Append(userId);
|
||||
if (index < asArray.Length - 1)
|
||||
{
|
||||
userWhereBuilder.Append(" OR ");
|
||||
}
|
||||
}
|
||||
userWhereBuilder.Append(")");
|
||||
return userWhereBuilder.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
/// <summary>
|
||||
/// Represents the UserRepository for doing CRUD operations for <see cref="IUser"/>
|
||||
/// </summary>
|
||||
internal class UserRepository : PetaPocoRepositoryBase<int, IUser>, IUserRepository
|
||||
internal class UserRepository : PermissionRepository<int, IUser>, IUserRepository
|
||||
{
|
||||
private readonly IUserTypeRepository _userTypeRepository;
|
||||
|
||||
@@ -125,6 +125,9 @@ namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
var list = new List<string>
|
||||
{
|
||||
"DELETE FROM umbracoUser2NodePermission WHERE userId = @Id",
|
||||
"DELETE FROM umbracoUser2NodeNotify WHERE userId = @Id",
|
||||
"DELETE FROM umbracoUserLogins WHERE userId = @Id",
|
||||
"DELETE FROM umbracoUser2app WHERE " + SqlSyntaxContext.SqlSyntaxProvider.GetQuotedColumnName("user") + "=@Id",
|
||||
"DELETE FROM umbracoUser WHERE id = @Id"
|
||||
};
|
||||
|
||||
@@ -8,7 +8,7 @@ using Umbraco.Core.Persistence.UnitOfWork;
|
||||
|
||||
namespace Umbraco.Core.Persistence.Repositories
|
||||
{
|
||||
internal abstract class VersionableRepositoryBase<TId, TEntity> : PetaPocoRepositoryBase<TId, TEntity>
|
||||
internal abstract class VersionableRepositoryBase<TId, TEntity> : PermissionRepository<TId, TEntity>
|
||||
where TEntity : class, IAggregateRoot
|
||||
{
|
||||
protected VersionableRepositoryBase(IDatabaseUnitOfWork work) : base(work)
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Linq;
|
||||
using StackExchange.Profiling;
|
||||
using Umbraco.Core.Logging;
|
||||
|
||||
@@ -47,7 +49,6 @@ namespace Umbraco.Core.Persistence
|
||||
return new StackExchange.Profiling.Data.ProfiledDbConnection(connection as DbConnection, MiniProfiler.Current);
|
||||
}
|
||||
|
||||
|
||||
public override void OnException(Exception x)
|
||||
{
|
||||
LogHelper.Info<UmbracoDatabase>(x.StackTrace);
|
||||
|
||||
@@ -164,7 +164,7 @@ namespace Umbraco.Core.Services
|
||||
Language = Umbraco.Core.Configuration.GlobalSettings.DefaultUILanguage,
|
||||
Name = name,
|
||||
Password = password,
|
||||
Permissions = userType.Permissions,
|
||||
DefaultPermissions = userType.Permissions,
|
||||
Username = login,
|
||||
StartContentId = -1,
|
||||
StartMediaId = -1,
|
||||
|
||||
@@ -491,6 +491,7 @@
|
||||
<Compile Include="Persistence\Repositories\LanguageRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\MediaRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\MediaTypeRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\PermissionRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\PetaPocoRepositoryBase.cs" />
|
||||
<Compile Include="Persistence\Repositories\RelationRepository.cs" />
|
||||
<Compile Include="Persistence\Repositories\RelationTypeRepository.cs" />
|
||||
|
||||
85
src/Umbraco.Tests/Persistence/PetaPocoExtensionsTest.cs
Normal file
85
src/Umbraco.Tests/Persistence/PetaPocoExtensionsTest.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
|
||||
namespace Umbraco.Tests.Persistence
|
||||
{
|
||||
[TestFixture]
|
||||
public class PetaPocoExtensionsTest : BaseDatabaseFactoryTest
|
||||
{
|
||||
[SetUp]
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public override void TearDown()
|
||||
{
|
||||
base.TearDown();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Bulk_Insert()
|
||||
{
|
||||
// Arrange
|
||||
var db = DatabaseContext.Database;
|
||||
|
||||
var servers = new List<ServerRegistrationDto>();
|
||||
for (var i = 0; i < 1000; i++)
|
||||
{
|
||||
servers.Add(new ServerRegistrationDto
|
||||
{
|
||||
Address = "address" + i,
|
||||
ComputerName = "computer" + i,
|
||||
DateRegistered = DateTime.Now,
|
||||
IsActive = true,
|
||||
LastNotified = DateTime.Now
|
||||
});
|
||||
}
|
||||
|
||||
// Act
|
||||
using (DisposableTimer.TraceDuration<PetaPocoExtensionsTest>("starting insert", "finished insert"))
|
||||
{
|
||||
db.BulkInsertRecords(servers);
|
||||
}
|
||||
|
||||
// Assert
|
||||
Assert.That(db.ExecuteScalar<int>("SELECT COUNT(*) FROM umbracoServer"), Is.EqualTo(1000));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Generate_Bulk_Import_Sql()
|
||||
{
|
||||
// Arrange
|
||||
var db = DatabaseContext.Database;
|
||||
|
||||
var servers = new List<ServerRegistrationDto>();
|
||||
for (var i = 0; i < 2; i++)
|
||||
{
|
||||
servers.Add(new ServerRegistrationDto
|
||||
{
|
||||
Address = "address" + i,
|
||||
ComputerName = "computer" + i,
|
||||
DateRegistered = DateTime.Now,
|
||||
IsActive = true,
|
||||
LastNotified = DateTime.Now
|
||||
});
|
||||
}
|
||||
db.OpenSharedConnection();
|
||||
|
||||
// Act
|
||||
string sql;
|
||||
db.GenerateBulkInsertCommand(servers, db.Connection, out sql);
|
||||
db.CloseSharedConnection();
|
||||
|
||||
// Assert
|
||||
Assert.That(sql,
|
||||
Is.EqualTo("INSERT INTO [umbracoServer] ([umbracoServer].[address], [umbracoServer].[computerName], [umbracoServer].[registeredDate], [umbracoServer].[lastNotifiedDate], [umbracoServer].[isActive]) VALUES (@0,@1,@2,@3,@4), (@5,@6,@7,@8,@9)"));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.Membership;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Repositories;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
|
||||
namespace Umbraco.Tests.Persistence.Querying
|
||||
@@ -9,6 +12,26 @@ namespace Umbraco.Tests.Persistence.Querying
|
||||
[TestFixture]
|
||||
public class PetaPocoSqlTests : BaseUsingSqlCeSyntax
|
||||
{
|
||||
[Test]
|
||||
public void Generate_Replace_Entity_Permissions_Test()
|
||||
{
|
||||
// Act
|
||||
var sql = PermissionRepository<int, IContent>.GenerateReplaceEntityPermissionsSql(123, "abcd", new object[] {10, 11, 12});
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(@"SET [permission]='abcd' WHERE (([nodeId]=123) AND ([userId]=10 OR [userId]=11 OR [userId]=12))", sql);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Generate_Replace_Entity_Permissions_With_Descendants_Test()
|
||||
{
|
||||
// Act
|
||||
var sql = PermissionRepository<int, IContent>.GenerateReplaceEntityPermissionsSql(new[] {123, 456}, "abcd", new object[] {10, 11, 12});
|
||||
|
||||
// Assert
|
||||
Assert.AreEqual(@"SET [permission]='abcd' WHERE (([nodeId]=123 OR [nodeId]=456) AND ([userId]=10 OR [userId]=11 OR [userId]=12))", sql);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Select_From_With_Type()
|
||||
{
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Models.Rdbms;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Mappers;
|
||||
using Umbraco.Core.Persistence.Querying;
|
||||
@@ -32,6 +34,42 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
base.TearDown();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Ensures_Permissions_Are_Set_If_Parent_Entity_Permissions_Exist()
|
||||
{
|
||||
// Arrange
|
||||
var provider = new PetaPocoUnitOfWorkProvider();
|
||||
var unitOfWork = provider.GetUnitOfWork();
|
||||
var contentTypeRepository = RepositoryResolver.Current.ResolveByType<IContentTypeRepository>(unitOfWork);
|
||||
var repository = (ContentRepository)RepositoryResolver.Current.ResolveByType<IContentRepository>(unitOfWork);
|
||||
|
||||
var contentType = MockedContentTypes.CreateSimpleContentType("umbTextpage", "Textpage");
|
||||
contentType.AllowedContentTypes = new List<ContentTypeSort>
|
||||
{
|
||||
new ContentTypeSort
|
||||
{
|
||||
Alias = contentType.Alias,
|
||||
Id = new Lazy<int>(() => contentType.Id),
|
||||
SortOrder = 0
|
||||
}
|
||||
};
|
||||
var parentPage = MockedContent.CreateSimpleContent(contentType);
|
||||
contentTypeRepository.AddOrUpdate(contentType);
|
||||
repository.AddOrUpdate(parentPage);
|
||||
unitOfWork.Commit();
|
||||
|
||||
// Act
|
||||
repository.AssignEntityPermissions(parentPage, "ABCD", new object[] {0});
|
||||
var childPage = MockedContent.CreateSimpleContent(contentType, "child", parentPage);
|
||||
repository.AddOrUpdate(childPage);
|
||||
unitOfWork.Commit();
|
||||
|
||||
// Assert
|
||||
var permissions = repository.GetPermissionsForEntity(childPage.Id);
|
||||
Assert.AreEqual(1, permissions.Count());
|
||||
Assert.AreEqual("ABCD", permissions.Single().Permission);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Can_Instantiate_Repository()
|
||||
{
|
||||
|
||||
@@ -115,7 +115,7 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
var resolved = repository.Get((int)user.Id);
|
||||
|
||||
resolved.Name = "New Name";
|
||||
resolved.Permissions = "ZYX";
|
||||
resolved.DefaultPermissions = "ZYX";
|
||||
resolved.Language = "fr";
|
||||
resolved.IsApproved = false;
|
||||
resolved.Password = "new";
|
||||
@@ -134,7 +134,7 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
// Assert
|
||||
Assert.That(updatedItem.Id, Is.EqualTo(resolved.Id));
|
||||
Assert.That(updatedItem.Name, Is.EqualTo(resolved.Name));
|
||||
Assert.That(updatedItem.Permissions, Is.EqualTo(resolved.Permissions));
|
||||
Assert.That(updatedItem.DefaultPermissions, Is.EqualTo(resolved.DefaultPermissions));
|
||||
Assert.That(updatedItem.Language, Is.EqualTo(resolved.Language));
|
||||
Assert.That(updatedItem.IsApproved, Is.EqualTo(resolved.IsApproved));
|
||||
Assert.That(updatedItem.Password, Is.EqualTo(resolved.Password));
|
||||
@@ -173,6 +173,32 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
Assert.That(resolved, Is.Null);
|
||||
}
|
||||
|
||||
//[Test]
|
||||
//public void Can_Perform_Delete_On_UserRepository_With_Permissions_Assigned()
|
||||
//{
|
||||
// // Arrange
|
||||
// var provider = new PetaPocoUnitOfWorkProvider();
|
||||
// var unitOfWork = provider.GetUnitOfWork();
|
||||
// var repository = RepositoryResolver.Current.ResolveByType<IUserRepository>(unitOfWork);
|
||||
|
||||
// var user = MockedUser.CreateUser(CreateAndCommitUserType());
|
||||
// //repository.AssignPermissions()
|
||||
|
||||
// // Act
|
||||
// repository.AddOrUpdate(user);
|
||||
// unitOfWork.Commit();
|
||||
// var id = user.Id;
|
||||
|
||||
// var repository2 = RepositoryResolver.Current.ResolveByType<IUserRepository>(unitOfWork);
|
||||
// repository2.Delete(user);
|
||||
// unitOfWork.Commit();
|
||||
|
||||
// var resolved = repository2.Get((int)id);
|
||||
|
||||
// // Assert
|
||||
// Assert.That(resolved, Is.Null);
|
||||
//}
|
||||
|
||||
[Test]
|
||||
public void Can_Perform_Get_On_UserRepository()
|
||||
{
|
||||
@@ -405,7 +431,7 @@ namespace Umbraco.Tests.Persistence.Repositories
|
||||
{
|
||||
Assert.That(updatedItem.Id, Is.EqualTo(originalUser.Id));
|
||||
Assert.That(updatedItem.Name, Is.EqualTo(originalUser.Name));
|
||||
Assert.That(updatedItem.Permissions, Is.EqualTo(originalUser.Permissions));
|
||||
Assert.That(updatedItem.DefaultPermissions, Is.EqualTo(originalUser.DefaultPermissions));
|
||||
Assert.That(updatedItem.Language, Is.EqualTo(originalUser.Language));
|
||||
Assert.That(updatedItem.IsApproved, Is.EqualTo(originalUser.IsApproved));
|
||||
Assert.That(updatedItem.Password, Is.EqualTo(originalUser.Password));
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace Umbraco.Tests.Services
|
||||
Assert.That(membershipUser.HasIdentity, Is.True);
|
||||
IUser user = membershipUser as User;
|
||||
Assert.That(user, Is.Not.Null);
|
||||
Assert.That(user.Permissions, Is.EqualTo(userType.Permissions));
|
||||
Assert.That(user.DefaultPermissions, Is.EqualTo(userType.Permissions));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -63,7 +63,7 @@ namespace Umbraco.Tests.Services
|
||||
Assert.That(membershipUser.Password, Is.EqualTo(encodedPassword));
|
||||
IUser user = membershipUser as User;
|
||||
Assert.That(user, Is.Not.Null);
|
||||
Assert.That(user.Permissions, Is.EqualTo(userType.Permissions));
|
||||
Assert.That(user.DefaultPermissions, Is.EqualTo(userType.Permissions));
|
||||
}
|
||||
|
||||
[Test]
|
||||
@@ -140,7 +140,7 @@ namespace Umbraco.Tests.Services
|
||||
Assert.IsNotNull(updatedItem);
|
||||
Assert.That(updatedItem.Id, Is.EqualTo(originalUser.Id));
|
||||
Assert.That(updatedItem.Name, Is.EqualTo(originalUser.Name));
|
||||
Assert.That(updatedItem.Permissions, Is.EqualTo(originalUser.Permissions));
|
||||
Assert.That(updatedItem.DefaultPermissions, Is.EqualTo(originalUser.DefaultPermissions));
|
||||
Assert.That(updatedItem.Language, Is.EqualTo(originalUser.Language));
|
||||
Assert.That(updatedItem.IsApproved, Is.EqualTo(originalUser.IsApproved));
|
||||
Assert.That(updatedItem.Password, Is.EqualTo(originalUser.Password));
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace Umbraco.Tests.TestHelpers.Entities
|
||||
Name = "TestUser" + suffix,
|
||||
Password = "testing",
|
||||
NoConsole = false,
|
||||
Permissions = "ABC",
|
||||
DefaultPermissions = "ABC",
|
||||
StartContentId = -1,
|
||||
StartMediaId = -1,
|
||||
DefaultToLiveEditing = false,
|
||||
|
||||
@@ -185,6 +185,7 @@
|
||||
<Compile Include="Configurations\FileSystemProviderTests.cs" />
|
||||
<Compile Include="CoreXml\FrameworkXmlTests.cs" />
|
||||
<Compile Include="Integration\CreateContent.cs" />
|
||||
<Compile Include="Persistence\PetaPocoExtensionsTest.cs" />
|
||||
<Compile Include="Persistence\Repositories\UserRepositoryTest.cs" />
|
||||
<Compile Include="Persistence\Repositories\UserTypeRepositoryTest.cs" />
|
||||
<Compile Include="Services\UserServiceTests.cs" />
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Cache;
|
||||
using Umbraco.Core.Logging;
|
||||
using umbraco.DataLayer;
|
||||
using System.Collections.Generic;
|
||||
@@ -667,7 +668,7 @@ namespace umbraco.BusinessLogic
|
||||
{
|
||||
if (!_isInitialized)
|
||||
setupUser(_id);
|
||||
string cruds = UserType.DefaultPermissions;
|
||||
string defaultPermissions = UserType.DefaultPermissions;
|
||||
|
||||
if (!_crudsInitialized)
|
||||
initCruds();
|
||||
@@ -681,11 +682,11 @@ namespace umbraco.BusinessLogic
|
||||
}
|
||||
|
||||
// exception to everything. If default cruds is empty and we're on root node; allow browse of root node
|
||||
if (String.IsNullOrEmpty(cruds) && Path == "-1")
|
||||
cruds = "F";
|
||||
if (String.IsNullOrEmpty(defaultPermissions) && Path == "-1")
|
||||
defaultPermissions = "F";
|
||||
|
||||
// else return default user type cruds
|
||||
return cruds;
|
||||
return defaultPermissions;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -904,7 +905,7 @@ namespace umbraco.BusinessLogic
|
||||
public void FlushFromCache()
|
||||
{
|
||||
OnFlushingFromCache(EventArgs.Empty);
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheItem(string.Format("UmbracoUser{0}", Id.ToString()));
|
||||
ApplicationContext.Current.ApplicationCache.ClearCacheItem(string.Format("{0}{1}", CacheKeys.UserCacheKey, Id.ToString()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -915,7 +916,7 @@ namespace umbraco.BusinessLogic
|
||||
public static User GetUser(int id)
|
||||
{
|
||||
return ApplicationContext.Current.ApplicationCache.GetCacheItem(
|
||||
string.Format("UmbracoUser{0}", id.ToString()), () =>
|
||||
string.Format("{0}{1}", CacheKeys.UserCacheKey, id.ToString()), () =>
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user