public UmbracoLatchResponse Edit([FromUri] int operationId, LatchOperationRequestModel operation)
        {
            if (!latchOperationSvc.OperationExists(operationId))
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var response = latchOperationSvc.EditOperation(operationId, operation);

            return(response);
        }
Example #2
0
        public void CreateDefaultOperation()
        {
            var operations = GetAllOperations();

            if (!operations.Any())
            {
                var defaultOperationName = textService.Localize("latch_operation/defaultName");
                var operationRequest     = new LatchOperationRequestModel
                {
                    Name            = defaultOperationName,
                    Type            = LatchConstants.OperationTypes.Login,
                    ApplyToAllUsers = true
                };
                CreateOperation(operationRequest);
            }
        }
Example #3
0
        public UmbracoLatchResponse CreateOperation(LatchOperationRequestModel operation)
        {
            if (!AccountIsPaired)
            {
                var errorMessage = GetResponseMessage("accountUnpaired");
                return(new UmbracoLatchResponse(false, errorMessage));
            }

            var application = GetApplication();
            var latch       = new Latch(application.ApplicationId, application.Secret);
            var response    = latch.CreateOperation(application.ApplicationId, operation.Name);

            if (response.Error != null)
            {
                var errorMessage = GetResponseMessage("error" + response.Error.Code);
                return(new UmbracoLatchResponse(false, errorMessage));
            }

            if (response.Data == null || !response.Data.ContainsKey("operationId"))
            {
                var errorMessage = GetResponseMessage("createError");
                return(new UmbracoLatchResponse(false, errorMessage));
            }

            var latchOperation = new LatchOperation
            {
                Name            = operation.Name,
                OperationId     = response.Data["operationId"] as string,
                Type            = operation.Type,
                Action          = operation.Action,
                ApplyToAllUsers = operation.ApplyToAllUsers,
                ApplyToAllNodes = operation.ApplyToAllNodes
            };

            latchRepo.InsertOperation(latchOperation, operation.Users, operation.Nodes);

            var successMessage = GetResponseMessage("createSuccess");

            return(new UmbracoLatchResponse(true, successMessage));
        }
        public UmbracoLatchResponse Create(LatchOperationRequestModel operation)
        {
            var response = latchOperationSvc.CreateOperation(operation);

            return(response);
        }
Example #5
0
        public UmbracoLatchResponse EditOperation(int operationId, LatchOperationRequestModel operation)
        {
            if (!OperationExists(operationId))
            {
                var errorMessage = GetResponseMessage("notFound");
                return(new UmbracoLatchResponse(false, errorMessage));
            }

            var currentOperation = latchRepo.GetOperationByIdIncludingRelationships(operationId);

            var operationNameHasChanged = !currentOperation.Name.Equals(operation.Name, StringComparison.InvariantCultureIgnoreCase);

            if (operationNameHasChanged)
            {
                var application = GetApplication();
                var latch       = new Latch(application.ApplicationId, application.Secret);
                var response    = latch.UpdateOperation(currentOperation.OperationId, operation.Name);

                if (response.Error != null)
                {
                    var errorMessage = GetResponseMessage("error" + response.Error.Code);
                    return(new UmbracoLatchResponse(false, errorMessage));
                }
            }

            var userIdsToAdd    = new List <int>();
            var userIdsToRemove = new List <int>();

            if (!currentOperation.ApplyToAllUsers && operation.ApplyToAllUsers)
            {
                userIdsToRemove.AddRange(currentOperation.UserIds);
            }
            else if (operation.Users.Any())
            {
                if (currentOperation.UserIds == null || !currentOperation.UserIds.Any())
                {
                    userIdsToAdd.AddRange(operation.Users);
                }
                else if (!Enumerable.SequenceEqual(currentOperation.UserIds, operation.Users))
                {
                    userIdsToAdd.AddRange(operation.Users.Where(x => !currentOperation.UserIds.Contains(x)));
                    userIdsToRemove.AddRange(currentOperation.UserIds.Where(x => !operation.Users.Contains(x)));
                }
            }

            var nodeIdsToAdd    = new List <int>();
            var nodeIdsToRemove = new List <int>();

            if (!currentOperation.ApplyToAllNodes && operation.ApplyToAllNodes)
            {
                nodeIdsToRemove.AddRange(currentOperation.NodeIds);
            }
            else if (operation.Nodes.Any())
            {
                if (currentOperation.NodeIds == null || !currentOperation.NodeIds.Any())
                {
                    nodeIdsToAdd.AddRange(operation.Nodes);
                }
                else if (!Enumerable.SequenceEqual(currentOperation.NodeIds, operation.Nodes))
                {
                    nodeIdsToAdd.AddRange(operation.Nodes.Where(x => !currentOperation.NodeIds.Contains(x)));
                    nodeIdsToRemove.AddRange(currentOperation.NodeIds.Where(x => !operation.Nodes.Contains(x)));
                }
            }

            currentOperation.Name            = operation.Name;
            currentOperation.Type            = operation.Type;
            currentOperation.Action          = operation.Action;
            currentOperation.ApplyToAllUsers = operation.ApplyToAllUsers;
            currentOperation.ApplyToAllNodes = operation.ApplyToAllNodes;

            latchRepo.EditOperation(currentOperation, userIdsToAdd, userIdsToRemove, nodeIdsToAdd, nodeIdsToRemove);

            var successMessage = GetResponseMessage("editSuccess");

            return(new UmbracoLatchResponse(true, successMessage));
        }