Ejemplo n.º 1
0
        public async Task <Unit> Handle(RenameEntityCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                var renameRootNode = await _commandsHelper.VerifyEntity(request.EntityId, edgesCollection, SecurityRight.Rename);

                var renameDescendants = edgesCollection.GetDescendants(renameRootNode)
                                        .ToList();

                var oldRootPath = renameRootNode.Path;
                var newRootPath = _pathBuilder.RenameLeaf(oldRootPath, request.NewName);

                // renameRootNode is set with the instance taken from renameDescendants
                renameRootNode          = renameDescendants.First(x => x.Id == renameRootNode.Id);
                renameRootNode.NodeName = request.NewName;

                renameDescendants
                .ForEach(x => UpdateNodePath(x, oldRootPath, newRootPath));

                await edgesCollection.Update(renameDescendants);

                _transactionService.CommitTransaction();
                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntitiesRenameException("Entity rename failed", e);
            }
        }
        public async Task <Unit> Handle(SetPermissionsCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                await _commandsHelper.VerifyEntity(request.EntityId, edgesCollection, SecurityRight.Administer);

                var securityRulesCollection = _transactionService.GetCollection <EntitySecurityRule, SecurityRulesCollection>();

                if (request.Rules != null)
                {
                    await securityRulesCollection.SetRules(request.EntityId, request.Subject, request.Rules);
                }
                else
                {
                    await securityRulesCollection.ClearRules(request.EntityId, request.Subject);
                }


                _transactionService.CommitTransaction();
                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new SetPermissionsException("Error setting permissions", e);
            }
        }
        public async Task <Unit> Handle(MoveEntityCommand <TContext> request, CancellationToken cancellationToken)
        {
            try
            {
                _transactionService.StartTransaction();

                var edgesCollection = _transactionService.GetCollection <EntityNode, EntityNodesCollection>();

                var moveTargetNode = await _commandsHelper.VerifyEntity(request.NewParentId, edgesCollection, SecurityRight.Create);

                if (moveTargetNode == null)
                {
                    throw new NodeNotFoundException($"Target node {request.NewParentId} not found");
                }

                var moveSourceNode = await _commandsHelper.VerifyEntity(request.EntityId, edgesCollection, SecurityRight.Move);

                if (moveSourceNode == null)
                {
                    throw new NodeNotFoundException($"Source node {request.EntityId} not found");
                }

                var nodesToMove = edgesCollection.GetDescendants(moveSourceNode)
                                  .ToList();

                MoveNodes(nodesToMove, moveSourceNode, moveTargetNode);

                await edgesCollection.Update(nodesToMove);

                _transactionService.CommitTransaction();
                return(Unit.Value);
            }
            catch (Exception e)
            {
                _transactionService.AbortTransaction();
                throw new EntityMoveException("Entity move failed", e);
            }
        }
        private async Task <EntityNode> GetParentNode(EntityNodesCollection edgesCollection, Guid parentId)
        {
            if (parentId == Guid.Empty)
            {
                await _commandsHelper.VerifyRootNodePermission(SecurityRight.Create);

                return(null);
            }

            var parent = await _commandsHelper.VerifyEntity(parentId, edgesCollection, SecurityRight.Create);

            if (parent == null)
            {
                throw new ParentNodeNotFoundException($"Parent node {parentId} not found");
            }

            return(parent);
        }