コード例 #1
0
        private void Validate(PageUpsertViewModel model)
        {
            if (model?.Type?.Id.HasValue == true && EnumHelper.GetPageTypeById(model.Type.Id.Value) == PageType.Content)
            {
                var url = this.GetUrl(model.PermanentLink);
                if (url.IsNotNullOrEmpty() && Url.IsLocalUrl(url))
                {
                    var absoluteUrl = this.GetUrl(model.PermanentLink, true);
                    var uri         = new Uri(absoluteUrl);
                    var httpContext = new HttpContext(
                        new HttpRequest(null, uri.GetLeftPart(UriPartial.Path), uri.Query),
                        new HttpResponse(new System.IO.StringWriter()));
                    var baseHttpContext = new HttpContextWrapper(httpContext);

                    var routeData = Url.RouteCollection.GetRouteData(baseHttpContext);
                    if (routeData?.RouteHandler != null)
                    {
                        try
                        {
                            var httpHandler = routeData.RouteHandler.GetHttpHandler(new RequestContext(baseHttpContext, routeData));
                            httpHandler.ProcessRequest(httpContext);

                            if (baseHttpContext.Response.StatusCode == HttpStatusCode.OK.GetHashCode())
                            {
                                var addError = true;
                                if (model.Id.HasValue)
                                {
                                    List <Page> pages;
                                    using (ContextManager.NewConnection())
                                    {
                                        pages = cmsService.SearchPages(new PageQueryModel {
                                            PermanentLink = model.PermanentLink
                                        });
                                    }

                                    addError = pages?.Count(
                                        item => item.PermanentLink?.Equals(
                                            model.PermanentLink,
                                            StringComparison.InvariantCultureIgnoreCase) == true &&
                                        item.Id == model.Id) != 1;
                                }

                                if (addError)
                                {
                                    ModelState.AddModelError(string.Empty, string.Format(Resource.PageWithLinkAlreadyExistMessage, model.PermanentLink));
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            }
        }
コード例 #2
0
        public void UpsertPostInvalidModelFromFormData()
        {
            ////Arrange
            var model = new PageUpsertViewModel();

            cmsController.Arrange(s => s.Url.Action(Arg.IsAny <string>())).Returns("Preview");
            ////Act
            var result = cmsController.Upsert(model);

            SimulateValidation(model, cmsController);
            ////Assert
            cmsController.ModelState.IsValid.Should().Be(false);
        }
コード例 #3
0
        public ActionResult Upsert(PageUpsertViewModel model, bool preview = false, string sessionId = null)
        {
            if (sessionId.IsNotNullOrEmpty())
            {
                model = sessionStorageService.Get <PageUpsertViewModel>(sessionId);

                ModelState.Clear();
                ValidateModel(model);
            }

            // Validate parent id
            if (model.Id.HasValue && model.ParentId.HasValue)
            {
                List <Page> parentPages;
                using (ContextManager.NewConnection())
                {
                    parentPages = cmsService.GetParentPages(model.Id.Value);
                }

                if (parentPages?.Any(item => item.ParentId == model.ParentId) == true)
                {
                    ModelState.AddModelError("ParentId", string.Format(Resource.InvalidFieldValue, Resource.ParentPage));
                }
            }

            // Custom page validation - add errors to model state
            Validate(model);

            var isAjax = Request.IsAjaxRequest();

            if (ModelState.IsValid)
            {
                if (preview)
                {
                    sessionStorageService.Upsert(model);
                    var url = Url.Action("Preview", new { sessionId = model.UniqueId });
                    if (isAjax)
                    {
                        this.SetAjaxResponseRedirectUrl(url);
                        return(new EmptyResult());
                    }

                    return(Redirect(url));
                }

                var dbModel = Mapper.Map <Page>(model);
                try
                {
                    using (var transaction = ContextManager.NewTransaction())
                    {
                        cmsService.Upsert(dbModel);
                        transaction.Commit();
                    }

                    this.ShowMessage(MessageType.Success, Resource.ChangesSuccessfullySaved);

                    var redirectUrl = Url.Action("Index");
                    if (isAjax)
                    {
                        this.SetAjaxResponseRedirectUrl(redirectUrl);
                        return(new EmptyResult());
                    }

                    return(Redirect(redirectUrl));
                }
                catch (UserException exc)
                {
                    ModelState.AddModelError(string.Empty, exc.Message);
                }
            }

            InitBreadcrumb(
                string.Format(
                    model?.Id.HasValue == true
                        ? string.Format(Resource.Editing, model.Title)
                        : string.Format(Resource.Creating, Resource.Page.ToLower())));

            return(isAjax
                ? PartialView(model)
                : View(model) as ActionResult);
        }