public void Initialize()
        {
            //Seup permissions
            _permissions = new Permission[] { new SavePermission(), new PublishPermission(), new HostnamesPermission(),
                                              new CopyPermission(), new MovePermission(), new BackOfficeAccessPermission() }
            .Select(x => new Lazy <Permission, PermissionMetadata>(() => x, new PermissionMetadata(new Dictionary <string, object>
            {
                { "Id", x.Id },
                { "Name", x.Name },
                { "Type", x.Type }
            })));

            //Setup user groups
            _userGroups = new List <UserGroup>
            {
                new UserGroup
                {
                    Id   = new HiveId("9DB63B97-4C8F-489C-98C7-017DC6AD869A"),
                    Name = "Administrator"
                },
                new UserGroup
                {
                    Id   = new HiveId("F61E4B62-513B-4858-91BF-D873401AD3CA"),
                    Name = "Editor"
                },
                new UserGroup
                {
                    Id   = new HiveId("8B65EF91-D49B-43A3-AFD9-6A47D5341B7D"),
                    Name = "Writter"
                }
            };

            // Setup user id
            _userId = new HiveId("857BD0F6-49DC-4378-84C1-6CD8AE14301D");

            //Setup content nodes
            _systemRootNode = new TypedEntity {
                Id = FixedHiveIds.SystemRoot
            };
            _childContentNode = new TypedEntity {
                Id = new HiveId("00E55027-402F-41CF-9052-B8D8F3DBCD76")
            };

            //Setup relations
            var systemRootPermissionRelations = new List <RelationById>
            {
                new RelationById(
                    _userGroups.First().Id,
                    _systemRootNode.Id,
                    FixedRelationTypes.PermissionRelationType,
                    0,
                    new RelationMetaDatum(FixedPermissionIds.BackOfficeAccess, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Copy, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Save, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Publish, PermissionStatus.Deny.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Hostnames, PermissionStatus.Inherit.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Move, PermissionStatus.Inherit.ToString())
                    ),
                new RelationById(
                    _userGroups.Last().Id,
                    _systemRootNode.Id,
                    FixedRelationTypes.PermissionRelationType,
                    0,
                    new RelationMetaDatum(FixedPermissionIds.Copy, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Save, PermissionStatus.Deny.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Publish, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Hostnames, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Move, PermissionStatus.Inherit.ToString())
                    )
            };

            var childContentNodePermissionRelations = new List <RelationById>
            {
                new RelationById(
                    _userGroups.Skip(1).First().Id,
                    _childContentNode.Id,
                    FixedRelationTypes.PermissionRelationType,
                    0,
                    new RelationMetaDatum(FixedPermissionIds.BackOfficeAccess, PermissionStatus.Deny.ToString()),     // Back office access is not an entity action so this should get ignored
                    new RelationMetaDatum(FixedPermissionIds.Copy, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Save, PermissionStatus.Deny.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Publish, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Hostnames, PermissionStatus.Allow.ToString()),
                    new RelationMetaDatum(FixedPermissionIds.Move, PermissionStatus.Inherit.ToString())
                    )
            };

            var childContentNodeAncestorRelations = new List <RelationById>
            {
                new RelationById(
                    _systemRootNode.Id,
                    _childContentNode.Id,
                    FixedRelationTypes.DefaultRelationType,
                    0)
            };

            var userGroupRelations = new List <RelationById>
            {
                new RelationById(
                    _userGroups.First().Id,
                    _userId,
                    FixedRelationTypes.UserGroupRelationType,
                    0),
                new RelationById(
                    _userGroups.Skip(1).First().Id,
                    _userId,
                    FixedRelationTypes.UserGroupRelationType,
                    0),
                new RelationById(
                    _userGroups.Last().Id,
                    _userId,
                    FixedRelationTypes.UserGroupRelationType,
                    0)
            };

            _membershipService = Substitute.For <IMembershipService <User> >();
            _membershipService.GetById(_userId, true).Returns(new User
            {
                Id       = _userId,
                Username = "******",
                Groups   = _userGroups.Select(x => x.Id).ToArray()
            });


            //Setup hive
            _hive = MockHiveManager.GetManager()
                    .MockContentStore(out _readonlyContentStoreSession, out _readonlyContentStoreSchemaSession, out _contentStoreRepository, out _contentStoreSchemaRepository)
                    .MockSecurityStore(out _readonlySecurityStoreSession, out _readonlySecurityStoreSchemaSession, out _securityStoreRepository, out _securityStoreSchemaRepository);

            //Setup content store
            _readonlyContentStoreSession.Exists <TypedEntity>(HiveId.Empty).ReturnsForAnyArgs(true);

            _readonlySecurityStoreSession
            .Get <UserGroup>(true, Arg.Any <HiveId[]>())
            .Returns(MockHiveManager.MockReturnForGet <UserGroup>());

            _readonlyContentStoreSession
            .Get <UserGroup>(true, Arg.Any <HiveId[]>())
            .Returns(MockHiveManager.MockReturnForGet <UserGroup>());

            _securityStoreRepository
            .Get <UserGroup>(true, Arg.Any <HiveId[]>())
            .Returns(MockHiveManager.MockReturnForGet <UserGroup>());

            _contentStoreRepository
            .Get <UserGroup>(true, Arg.Any <HiveId[]>())
            .Returns(MockHiveManager.MockReturnForGet <UserGroup>());

            //Setup security store
            _readonlySecurityStoreSession.GetParentRelations(_systemRootNode.Id, FixedRelationTypes.PermissionRelationType).Returns(systemRootPermissionRelations);
            _readonlySecurityStoreSession.GetParentRelations(_childContentNode.Id, FixedRelationTypes.PermissionRelationType).Returns(childContentNodePermissionRelations);
            _readonlySecurityStoreSession.GetAncestorRelations(_childContentNode.Id, FixedRelationTypes.DefaultRelationType).Returns(childContentNodeAncestorRelations);
            _readonlySecurityStoreSession.GetParentRelations(_userId, FixedRelationTypes.UserGroupRelationType).Returns(userGroupRelations);

            _readonlySecurityStoreSession.Query <UserGroup>().Returns(x => Enumerable.Repeat(new UserGroup()
            {
                Name = "Administrator"
            }, 1).AsQueryable());
        }
Example #2
0
        public static T GetEntityByPath <T>(this IReadonlyEntityRepositoryGroup <IProviderTypeFilter> repositoryGroup, HiveId sourceId, string path, RevisionStatusType revisionStatusType, bool acceptNearestMatch = false)
            where T : TypedEntity
        {
            // Don't mandate for revisionStatusType to be non-null since that's fine, it means "just get latest"
            Mandate.ParameterNotNull(repositoryGroup, "repositoryGroup");
            Mandate.ParameterNotEmpty(sourceId, "sourceId");
            Mandate.ParameterNotNullOrEmpty(path, "path");

            T found = null;

            var parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            const string nodeName = NodeNameAttributeDefinition.AliasValue;

            // For each part in the path, get nodes that match by name
            // Then for the children of those matches, get nodes that match by name

            if (parts.Length == 0)
            {
                return(null);
            }

            var revisionTypeQuery = (revisionStatusType != null)
                                        ? repositoryGroup.Query <T>().OfRevisionType(revisionStatusType)
                                        : repositoryGroup.Query <T>();

            // Get the first part
            var firstPart = parts[0];

            var withParentQuery = revisionTypeQuery.WithParentIds(sourceId);

            // The original query was "UrlName matches, or if it doesn't, try to match the Name provided UrlName is null."
            // This is a pretty complex query burden for an edge-case. To avoid this, we'll just find by the UrlName first.
            // If a match isn't found, then we'll do another query, splitting the workload in half
            var firstMatch = withParentQuery.FirstOrDefault(x => x.InnerAttribute <string>(nodeName, "UrlName") == firstPart)
                             ??
                             withParentQuery.FirstOrDefault(x => (x.InnerAttribute <string>(nodeName, "UrlName") == null || x.InnerAttribute <string>(nodeName, "UrlName") == string.Empty) &&
                                                            x.InnerAttribute <string>(nodeName, "Name") == firstPart);

            if (firstMatch == null)
            {
                return(null);
            }

            // There was only one path item, so return that
            if (parts.Length == 1)
            {
                return(firstMatch);
            }

            // Get the children of the first match
            var lastMatch = firstMatch;

            foreach (var part in parts.Skip(1))
            {
                withParentQuery = revisionTypeQuery.WithParentIds(lastMatch.Id);

                var nextMatch = withParentQuery.FirstOrDefault(x => x.InnerAttribute <string>(nodeName, "UrlName") == part)
                                ??
                                withParentQuery.FirstOrDefault(x => (x.InnerAttribute <string>(nodeName, "UrlName") == null || x.InnerAttribute <string>(nodeName, "UrlName") == string.Empty) &&
                                                               x.InnerAttribute <string>(nodeName, "Name") == part);

                if (nextMatch == null)
                {
                    return(null);
                }

                // We found a match
                lastMatch = nextMatch;
            }

            found = lastMatch;

            if (found != null)
            {
                var foundUrlName = found.InnerAttribute <string>(nodeName, "UrlName");
                if (foundUrlName.InvariantEquals(path.Split('/').Last()) || acceptNearestMatch)
                {
                    return(found);
                }
            }

            return(null);
        }