Beispiel #1
0
        /// <summary>
        /// Outputs the correct messages in Notifications, validation and notice board based on the NotificationState
        /// </summary>
        /// <param name="state"></param>
        /// <param name="model"></param>
        protected virtual void NotifyForProcess(NotificationState state, TEditorModel model)
        {
            switch (state)
            {
            case NotificationState.Save:
                Notifications.Add(new NotificationMessage(
                                      "Content.Save.Message".Localize(this),
                                      "Content.Save.Title".Localize(this),
                                      NotificationType.Success));
                break;

            case NotificationState.Publish:
                Notifications.Add(new NotificationMessage(
                                      "Publish.SingleSuccess.Message".Localize(this, new { model.Name }, encode: false),
                                      "Publish.Title".Localize(this),
                                      NotificationType.Success));
                break;

            case NotificationState.Unpublish:
                Notifications.Add(new NotificationMessage(
                                      "Unpublish.SingleSuccess.Message".Localize(this, new { model.Name }, encode: false),
                                      "Unpublish.Title".Localize(this),
                                      NotificationType.Success));
                break;

            case NotificationState.SaveUnauthorized:
                var msgSave = new NotificationMessage(
                    "Content.SaveUnathorized.Message".Localize(this, new { model.Name }, encode: false),
                    "Content.SaveUnathorized.Title".Localize(this),
                    NotificationType.Error);
                //model.NoticeBoard.Add(msg);
                ModelState.AddDataValidationError(msgSave.Message);
                Notifications.Add(msgSave);
                break;

            case NotificationState.PublishUnathorized:
                var msgPublish = new NotificationMessage(
                    "Publish.SingleUnathorized.Message".Localize(this, new { model.Name }, encode: false),
                    "Publish.Title".Localize(this),
                    NotificationType.Error);
                ModelState.AddDataValidationError(msgPublish.Message);
                //model.NoticeBoard.Add(msg);
                Notifications.Add(msgPublish);
                break;

            case NotificationState.UnpublishedUnauthorized:
                var msgUnpublish = new NotificationMessage(
                    "Unpublish.SingleUnathorized.Message".Localize(this, new { model.Name }, encode: false),
                    "Unpublish.Title".Localize(this),
                    NotificationType.Error);
                //model.NoticeBoard.Add(msgUnpublish);
                ModelState.AddDataValidationError(msgUnpublish.Message);
                Notifications.Add(msgUnpublish);
                break;

            default:
                throw new ArgumentOutOfRangeException("state");
            }
        }
        /// <summary>
        /// Helper method to retreive the from/to entities and validate if the move/copy is allowed
        /// </summary>
        /// <param name="selectedItemId"></param>
        /// <param name="toId"></param>
        /// <param name="performSave"></param>
        protected JsonResult ProcessMoveCopy(
            HiveId selectedItemId,
            HiveId toId,
            Func <TypedEntity, TypedEntity, IGroupUnit <IContentStore>, Tuple <string, EntityPathCollection, string> > performSave)
        {
            using (var uow = Hive.Create <IContentStore>())
            {
                var copyFrom = uow.Repositories.Get <TypedEntity>(selectedItemId);
                if (copyFrom == null)
                {
                    ModelState.AddDataValidationError(string.Format("No content found for id: {0}", selectedItemId));
                }

                var parentEntity = uow.Repositories.Get <TypedEntity>(toId);
                if (parentEntity == null)
                {
                    ModelState.AddDataValidationError(string.Format("No content found for id: {0}", toId));
                }

                //if it's not the root node, need to validate the move there
                if (ContentController.VirtualRootNodeId != toId)
                {
                    //Validate that we are allowed to go here
                    //TODO: We manually load the EntitySchema again here to ensure it has the correct provider id, pending a fix to have deep-level objects
                    //have their id remapped when loaded from Hive
                    var entitySchema = uow.Repositories.Schemas.Get <EntitySchema>(copyFrom.EntitySchema.Id);
                    if (entitySchema == null)
                    {
                        ModelState.AddDataValidationError(string.Format("Could not find the document type for the selected content, with id: {0}", parentEntity.EntitySchema.Id));
                    }

                    if (!ModelState.IsValid)
                    {
                        return(ModelState.ToJsonErrors());
                    }

                    var toDocType = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map <EntitySchema, DocumentTypeEditorModel>(parentEntity.EntitySchema);
                    if (!toDocType.AllowedChildIds.Contains(entitySchema.Id, new HiveIdComparer(true)))
                    {
                        ModelState.AddDataValidationError("The current node is not allowed under the chosen node because of its type");
                        return(ModelState.ToJsonErrors());
                    }
                }

                //need to clear route engine cache, since we're moving/copying we need to clear all routingn cache
                this.BackOfficeRequestContext.RoutingEngine.ClearCache(clearAll: true);

                var result = performSave(copyFrom, parentEntity, uow);

                return(new CustomJsonResult(new
                {
                    operation = result.Item3,
                    path = result.Item2.ToJson(),
                    success = true,
                    msg = result.Item1
                }.ToJsonString));
            }
        }
        //[RebelAuthorize(Permissions = new[] { FixedPermissionIds.Hostnames })]
        public virtual JsonResult HostnameForm(HostnamesModel model)
        {
            Mandate.ParameterNotEmpty(model.Id, "Id");

            //need to validate the hostnames, first need to remove the invalid required field of the main host name field as this is only required for client side validation
            ModelState.RemoveAll(x => x.Key == "NewHostname");
            foreach (var h in model.AssignedHostnames)
            {
                if (!Regex.IsMatch(h.Hostname, @"^([\w-\.:]+)$", RegexOptions.IgnoreCase))
                {
                    ModelState.AddDataValidationError(string.Format("{0} is an invalid host name", h));
                }
                else if (h.Hostname.Contains(":") && !Regex.IsMatch(h.Hostname.Split(':')[1], @"^\d+$"))
                {
                    ModelState.AddDataValidationError(string.Format("{0} is an invalid port number", h.Hostname.Split(':')[1]));
                }
                //check if the hostname already exists in the collection
                if (BackOfficeRequestContext.RoutingEngine.DomainList.ContainsHostname(h.Hostname))
                {
                    //check if that hostname is assigned to a different node
                    if (BackOfficeRequestContext.RoutingEngine.DomainList[h.Hostname].ContentId != model.Id)
                    {
                        ModelState.AddDataValidationError(string.Format("{0} is already assigned to node id {1}", h.Hostname, BackOfficeRequestContext.RoutingEngine.DomainList[h.Hostname].ContentId));
                    }
                }
                if (model.AssignedHostnames.Count(x => x.Hostname == h.Hostname) > 1)
                {
                    ModelState.AddDataValidationError(string.Format("{0} is a duplicate entry in the submitted list", h));
                }
                if (!ModelState.IsValid)
                {
                    return(ModelState.ToJsonErrors());
                }
            }

            using (var uow = Hive.Create <IContentStore>())
            {
                //get the content entity for the hostname assignment
                var entity = uow.Repositories.Get <TypedEntity>(model.Id);
                if (entity == null)
                {
                    throw new NullReferenceException("Could not find entity with id " + model.Id);
                }

                //map the hostname entities from the model
                var hostnames = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map <IEnumerable <Hostname> >(model);
                //need to remove the hostnames that no longer exist
                var assignedHostnames = uow.Repositories.GetChildren <Hostname>(FixedRelationTypes.HostnameRelationType, model.Id);
                foreach (var a in assignedHostnames.Where(x => !hostnames.Select(h => h.Id).Contains(x.Id)))
                {
                    uow.Repositories.Delete <Hostname>(a.Id);
                }

                uow.Repositories.AddOrUpdate(hostnames);
                uow.Complete();

                //clears the domain cache
                BackOfficeRequestContext.RoutingEngine.ClearCache(clearDomains: true, clearGeneratedUrls: true);

                var successMsg = "Hostname.Success.Message".Localize(this, new
                {
                    Count = model.AssignedHostnames.Count,
                    Name  = entity.GetAttributeValue(NodeNameAttributeDefinition.AliasValue, "Name")
                });
                Notifications.Add(new NotificationMessage(successMsg, "Hostname.Title".Localize(this), NotificationType.Success));

                return(new CustomJsonResult(new
                {
                    success = true,
                    notifications = Notifications,
                    msg = successMsg
                }.ToJsonString));
            }
        }