Пример #1
0
        private void AssertBasics <T, TPersisted>(ContentItemBasic <T> result, TPersisted content)
            where T : ContentPropertyBasic
            where TPersisted : IContentBase
        {
            Assert.AreEqual(content.Id, result.Id);

            var ownerId = content.CreatorId;

            if (ownerId != 0)
            {
                Assert.IsNotNull(result.Owner);
                Assert.AreEqual(Constants.Security.SuperUserId, result.Owner.UserId);
                Assert.AreEqual("Administrator", result.Owner.Name);
            }
            else
            {
                Assert.IsNull(result.Owner); // because, 0 is no user
            }

            Assert.AreEqual(content.ParentId, result.ParentId);
            Assert.AreEqual(content.UpdateDate, result.UpdateDate);
            Assert.AreEqual(content.CreateDate, result.CreateDate);
            Assert.AreEqual(content.Name, result.Name);
            Assert.AreEqual(content.Properties.Count(), result.Properties.Count(x => x.Alias.StartsWith("_umb_") == false));
        }
Пример #2
0
        private void AssertContentItem <T>(ContentItemBasic <ContentPropertyDto> result, T content)
            where T : IContentBase
        {
            AssertBasics(result, content);

            foreach (var p in content.Properties)
            {
                AssertProperty(result, p);
            }
        }
        /// <summary>
        /// Ensure the content exists
        /// </summary>
        /// <param name="postedItem"></param>
        /// <param name="actionContext"></param>
        /// <returns></returns>
        protected virtual bool ValidateExistingContent(ContentItemBasic <ContentPropertyBasic, TPersisted> postedItem, HttpActionContext actionContext)
        {
            if (postedItem.PersistedContent == null)
            {
                var message = string.Format("content with id: {0} was not found", postedItem.Id);
                actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                return(false);
            }

            return(true);
        }
        private void AssertBasicProperty <T, TPersisted>(ContentItemBasic <T, TPersisted> result, Property p)
            where T : ContentPropertyBasic
            where TPersisted : IContentBase
        {
            var pDto = result.Properties.SingleOrDefault(x => x.Alias == p.Alias);

            Assert.IsNotNull(pDto);
            Assert.AreEqual(p.Alias, pDto.Alias);
            Assert.AreEqual(p.Id, pDto.Id);

            Assert.IsTrue(p.Value == null ? pDto.Value == string.Empty : pDto.Value == p.Value);
        }
 private void AssertBasics <T, TPersisted>(ContentItemBasic <T, TPersisted> result, TPersisted content)
     where T : ContentPropertyBasic
     where TPersisted : IContentBase
 {
     Assert.AreEqual(content.Id, result.Id);
     Assert.AreEqual(0, result.Owner.UserId);
     Assert.AreEqual("Administrator", result.Owner.Name);
     Assert.AreEqual(content.ParentId, result.ParentId);
     Assert.AreEqual(content.UpdateDate, result.UpdateDate);
     Assert.AreEqual(content.CreateDate, result.CreateDate);
     Assert.AreEqual(content.Name, result.Name);
     Assert.AreEqual(content.Properties.Count(), result.Properties.Count(x => x.Alias.StartsWith("_umb_") == false));
 }
Пример #6
0
            protected override bool ValidateProperties(ContentItemBasic <ContentPropertyBasic, IMember> postedItem, HttpActionContext actionContext)
            {
                var propertiesToValidate = postedItem.Properties.ToList();
                var defaultProps         = Constants.Conventions.Member.GetStandardPropertyTypeStubs();
                var exclude = defaultProps.Select(x => x.Value.Alias).ToArray();

                foreach (var remove in exclude)
                {
                    propertiesToValidate.RemoveAll(property => property.Alias == remove);
                }

                return(ValidateProperties(propertiesToValidate.ToArray(), postedItem.PersistedContent.Properties.ToArray(), actionContext));
            }
        private void AssertProperty <TPersisted>(ContentItemBasic <ContentPropertyDto, TPersisted> result, Property p)
            where TPersisted : IContentBase
        {
            AssertBasicProperty(result, p);

            var pDto = result.Properties.SingleOrDefault(x => x.Alias == p.Alias);

            Assert.IsNotNull(pDto);
            Assert.AreEqual(p.PropertyType.Mandatory, pDto.IsRequired);
            Assert.AreEqual(p.PropertyType.ValidationRegExp, pDto.ValidationRegExp);
            Assert.AreEqual(p.PropertyType.Description, pDto.Description);
            Assert.AreEqual(p.PropertyType.Name, pDto.Label);
            Assert.AreEqual(ApplicationContext.Services.DataTypeService.GetDataTypeDefinitionById(p.PropertyType.DataTypeDefinitionId), pDto.DataType);
            Assert.AreEqual(PropertyEditorResolver.Current.GetByAlias(p.PropertyType.PropertyEditorAlias), pDto.PropertyEditor);
        }
        private void AssertDisplayProperty <T, TPersisted>(ContentItemBasic <T, TPersisted> result, Property p, ApplicationContext applicationContext)
            where T : ContentPropertyDisplay
            where TPersisted : IContentBase
        {
            AssertBasicProperty(result, p);

            var pDto = result.Properties.SingleOrDefault(x => x.Alias == p.Alias);

            Assert.IsNotNull(pDto);

            //pDto.Alias = p.Alias;
            //pDto.Description = p.PropertyType.Description;
            //pDto.Label = p.PropertyType.Name;
            //pDto.Config = applicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(p.PropertyType.DataTypeDefinitionId);
        }
Пример #9
0
            /// <summary>
            /// This ensures that the internal membership property types are removed from validation before processing the validation
            /// since those properties are actually mapped to real properties of the IMember.
            /// This also validates any posted data for fields that are sensitive.
            /// </summary>
            /// <param name="postedItem"></param>
            /// <param name="actionContext"></param>
            /// <returns></returns>
            protected override bool ValidateProperties(ContentItemBasic <ContentPropertyBasic, IMember> postedItem, HttpActionContext actionContext)
            {
                var propertiesToValidate = postedItem.Properties.ToList();
                var defaultProps         = Constants.Conventions.Member.GetStandardPropertyTypeStubs();
                var exclude = defaultProps.Select(x => x.Value.Alias).ToArray();

                foreach (var remove in exclude)
                {
                    propertiesToValidate.RemoveAll(property => property.Alias == remove);
                }

                var httpCtx = actionContext.Request.TryGetHttpContext();

                if (httpCtx.Success == false)
                {
                    actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, "No http context");
                    return(false);
                }
                var umbCtx = httpCtx.Result.GetUmbracoContext();

                //if the user doesn't have access to sensitive values, then we need to validate the incoming properties to check
                //if a sensitive value is being submitted.
                if (umbCtx.Security.CurrentUser.HasAccessToSensitiveData() == false)
                {
                    var sensitiveProperties = postedItem.PersistedContent.ContentType
                                              .PropertyTypes.Where(x => postedItem.PersistedContent.ContentType.IsSensitiveProperty(x.Alias))
                                              .ToList();

                    foreach (var sensitiveProperty in sensitiveProperties)
                    {
                        var prop = propertiesToValidate.FirstOrDefault(x => x.Alias == sensitiveProperty.Alias);

                        if (prop != null)
                        {
                            //this should not happen, this means that there was data posted for a sensitive property that
                            //the user doesn't have access to, which means that someone is trying to hack the values.

                            var message = string.Format("property with alias: {0} cannot be posted", prop.Alias);
                            actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, new InvalidOperationException(message));
                            return(false);
                        }
                    }
                }

                return(ValidateProperties(propertiesToValidate, postedItem.PersistedContent.Properties.ToList(), actionContext));
            }
Пример #10
0
 // Umbraco.Code.MapAll -Edited -Updater -Alias
 private void Map(IMedia source, ContentItemBasic <ContentPropertyBasic> target, MapperContext context)
 {
     target.ContentTypeAlias = source.ContentType.Alias;
     target.CreateDate       = source.CreateDate;
     target.Icon             = source.ContentType.Icon;
     target.Id              = source.Id;
     target.Key             = source.Key;
     target.Name            = source.Name;
     target.Owner           = _commonMapper.GetOwner(source, context);
     target.ParentId        = source.ParentId;
     target.Path            = source.Path;
     target.Properties      = context.MapEnumerable <Property, ContentPropertyBasic>(source.Properties);
     target.SortOrder       = source.SortOrder;
     target.State           = null;
     target.Trashed         = source.Trashed;
     target.Udi             = Udi.Create(Constants.UdiEntityType.Media, source.Key);
     target.UpdateDate      = source.UpdateDate;
     target.VariesByCulture = source.ContentType.VariesByCulture();
 }
Пример #11
0
            /// <summary>
            /// We need to manually validate a few things here like email and login to make sure they are valid and aren't duplicates
            /// </summary>
            /// <param name="postedItem"></param>
            /// <param name="actionContext"></param>
            /// <returns></returns>
            protected override bool ValidatePropertyData(ContentItemBasic <ContentPropertyBasic, IMember> postedItem, HttpActionContext actionContext)
            {
                var memberSave = (MemberSave)postedItem;

                if (memberSave.Username.IsNullOrWhiteSpace())
                {
                    actionContext.ModelState.AddPropertyError(
                        new ValidationResult("Invalid user name", new[] { "value" }),
                        string.Format("{0}login", Constants.PropertyEditors.InternalGenericPropertiesPrefix));
                }

                if (memberSave.Email.IsNullOrWhiteSpace() || new EmailAddressAttribute().IsValid(memberSave.Email) == false)
                {
                    actionContext.ModelState.AddPropertyError(
                        new ValidationResult("Invalid email", new[] { "value" }),
                        string.Format("{0}email", Constants.PropertyEditors.InternalGenericPropertiesPrefix));
                }

                //default provider!
                var membershipProvider = Core.Security.MembershipProviderExtensions.GetMembersMembershipProvider();

                var validEmail = ValidateUniqueEmail(memberSave, membershipProvider, actionContext);

                if (validEmail == false)
                {
                    actionContext.ModelState.AddPropertyError(
                        new ValidationResult("Email address is already in use", new[] { "value" }),
                        string.Format("{0}email", Constants.PropertyEditors.InternalGenericPropertiesPrefix));
                }

                var validLogin = ValidateUniqueLogin(memberSave, membershipProvider, actionContext);

                if (validLogin == false)
                {
                    actionContext.ModelState.AddPropertyError(
                        new ValidationResult("Username is already in use", new[] { "value" }),
                        string.Format("{0}login", Constants.PropertyEditors.InternalGenericPropertiesPrefix));
                }

                return(base.ValidatePropertyData(postedItem, actionContext));
            }
            public string Resolve(IContent source, ContentItemBasic <ContentPropertyBasic> destination, string destMember, ResolutionContext context)
            {
                // invariant = only 1 name
                if (!source.ContentType.VariesByCulture())
                {
                    return(source.Name);
                }

                // variant = depends on culture
                var culture = context.Options.GetCulture();

                // if there's no culture here, the issue is somewhere else (UI, whatever) - throw!
                if (culture == null)
                {
                    throw new InvalidOperationException("Missing culture in mapping options.");
                }

                // if we don't have a name for a culture, it means the culture is not available, and
                // hey we should probably not be mapping it, but it's too late, return a fallback name
                return(source.CultureInfos.TryGetValue(culture, out var name) && !name.Name.IsNullOrWhiteSpace() ? name.Name : $"({source.Name})");
            }
Пример #13
0
 // Umbraco.Code.MapAll -Alias
 private void Map(IContent source, ContentItemBasic <ContentPropertyBasic> target, MapperContext context)
 {
     target.ContentTypeAlias = source.ContentType.Alias;
     target.CreateDate       = source.CreateDate;
     target.Edited           = source.Edited;
     target.Icon             = source.ContentType.Icon;
     target.Id              = source.Id;
     target.Key             = source.Key;
     target.Name            = GetName(source, context);
     target.Owner           = _commonMapper.GetOwner(source, context);
     target.ParentId        = source.ParentId;
     target.Path            = source.Path;
     target.Properties      = context.MapEnumerable <Property, ContentPropertyBasic>(source.Properties);
     target.SortOrder       = source.SortOrder;
     target.State           = _basicStateMapper.Map(source, context);
     target.Trashed         = source.Trashed;
     target.Udi             = Udi.Create(source.Blueprint ? Constants.UdiEntityType.DocumentBlueprint : Constants.UdiEntityType.Document, source.Key);
     target.UpdateDate      = GetUpdateDate(source, context);
     target.Updater         = _commonMapper.GetCreator(source, context);
     target.VariesByCulture = source.ContentType.VariesByCulture();
 }
Пример #14
0
        public void To_Content_Item_Simple()
        {
            Content content = _contentBuilder
                              .AddContentType()
                              .AddPropertyGroup()
                              .AddPropertyType()
                              .Done()
                              .Done()
                              .Done()
                              .WithCreatorId(Constants.Security.SuperUserId)
                              .Build();

            ContentItemBasic <ContentPropertyBasic> result = _sut.Map <IContent, ContentItemBasic <ContentPropertyBasic> >(content);

            AssertBasics(result, content);

            foreach (IProperty p in content.Properties)
            {
                AssertBasicProperty(result, p);
            }
        }
            public DateTime Resolve(IContent source, ContentItemBasic <ContentPropertyBasic> destination, DateTime destMember, ResolutionContext context)
            {
                // invariant = global date
                if (!source.ContentType.VariesByCulture())
                {
                    return(source.UpdateDate);
                }

                // variant = depends on culture
                var culture = context.Options.GetCulture();

                // if there's no culture here, the issue is somewhere else (UI, whatever) - throw!
                if (culture == null)
                {
                    throw new InvalidOperationException("Missing culture in mapping options.");
                }

                // if we don't have a date for a culture, it means the culture is not available, and
                // hey we should probably not be mapping it, but it's too late, return a fallback date
                var date = source.GetUpdateDate(culture);

                return(date ?? source.UpdateDate);
            }
        private void AssertBasicProperty <T, TPersisted>(ContentItemBasic <T, TPersisted> result, Property p)
            where T : ContentPropertyBasic
            where TPersisted : IContentBase
        {
            var pDto = result.Properties.SingleOrDefault(x => x.Alias == p.Alias);

            Assert.IsNotNull(pDto);
            Assert.AreEqual(p.Alias, pDto.Alias);
            Assert.AreEqual(p.Id, pDto.Id);

            if (p.Value == null)
            {
                Assert.AreEqual(pDto.Value, string.Empty);
            }
            else if (p.Value is decimal)
            {
                Assert.AreEqual(pDto.Value, ((decimal)p.Value).ToString(NumberFormatInfo.InvariantInfo));
            }
            else
            {
                Assert.AreEqual(pDto.Value, p.Value.ToString());
            }
        }
 /// <summary>
 /// Ensure all of the ids in the post are valid
 /// </summary>
 /// <param name="postedItem"></param>
 /// <param name="actionContext"></param>
 /// <returns></returns>
 protected virtual bool ValidateProperties(ContentItemBasic <ContentPropertyBasic, TPersisted> postedItem, HttpActionContext actionContext)
 {
     return(ValidateProperties(postedItem.Properties.ToArray(), postedItem.PersistedContent.Properties.ToArray(), actionContext));
 }
        /// <summary>
        /// Validates the data for each property
        /// </summary>
        /// <param name="postedItem"></param>
        /// <param name="actionContext"></param>
        /// <returns></returns>
        /// <remarks>
        /// All property data validation goes into the modelstate with a prefix of "Properties"
        /// </remarks>
        protected virtual bool ValidatePropertyData(ContentItemBasic <ContentPropertyBasic, TPersisted> postedItem, HttpActionContext actionContext)
        {
            foreach (var p in postedItem.ContentDto.Properties)
            {
                var editor = p.PropertyEditor;
                if (editor == null)
                {
                    var message = string.Format("The property editor with alias: {0} was not found for property with id {1}", p.DataType.PropertyEditorAlias, p.Id);
                    LogHelper.Warn <ContentItemValidationHelper <TPersisted, TModelSave> >(message);
                    //actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
                    //return false;
                    continue;
                }

                //get the posted value for this property
                var postedValue = postedItem.Properties.Single(x => x.Alias == p.Alias).Value;

                //get the pre-values for this property
                var preValues = p.PreValues;

                //TODO: when we figure out how to 'override' certain pre-value properties we'll either need to:
                // * Combine the preValues with the overridden values stored with the document type property (but how to combine?)
                // * Or, pass in the overridden values stored with the doc type property separately

                foreach (var result in editor.ValueEditor.Validators.SelectMany(v => v.Validate(postedValue, preValues, editor)))
                {
                    actionContext.ModelState.AddPropertyError(result, p.Alias);
                }

                //Now we need to validate the property based on the PropertyType validation (i.e. regex and required)
                // NOTE: These will become legacy once we have pre-value overrides.
                if (p.IsRequired)
                {
                    foreach (var result in p.PropertyEditor.ValueEditor.RequiredValidator.Validate(postedValue, "", preValues, editor))
                    {
                        actionContext.ModelState.AddPropertyError(result, p.Alias);
                    }
                }

                if (p.ValidationRegExp.IsNullOrWhiteSpace() == false)
                {
                    //We only want to execute the regex statement if:
                    // * the value is null or empty AND it is required OR
                    // * the value is not null or empty
                    //See: http://issues.umbraco.org/issue/U4-4669

                    var asString = postedValue as string;

                    if (
                        //Value is not null or empty
                        (postedValue != null && asString.IsNullOrWhiteSpace() == false)
                        //It's required
                        || (p.IsRequired))
                    {
                        foreach (var result in p.PropertyEditor.ValueEditor.RegexValidator.Validate(postedValue, p.ValidationRegExp, preValues, editor))
                        {
                            actionContext.ModelState.AddPropertyError(result, p.Alias);
                        }
                    }
                }
            }

            return(actionContext.ModelState.IsValid);
        }
Пример #19
0
 protected bool Equals(ContentItemBasic other) => Id == other.Id;