Quantcast
Channel: Optimizely Search & Navigation
Viewing all articles
Browse latest Browse all 6894

Access Rights for Properties

$
0
0

Hi All

I am using IMetadataAware to restrict access to certain properties on content types, although when using an example i found from Linus it doesnt seem to work if the property is located in the SystemTabNames.PageHeader tab

The code i am using is as follows :

    public class PropertyEditRestrictionAttribute : ValidationAttribute, IMetadataAware
    {
        public PropertyEditRestrictionAttribute(string[] allowedRoles)
        {
            AllowedRoles = allowedRoles;
        }
        public string[] AllowedRoles { get; set; }
        public void OnMetadataCreated(ModelMetadata metadata)
        {
            foreach (string role in AllowedRoles)
            {
                if (EPiServer.Security.PrincipalInfo.CurrentPrincipal.IsInRole(role))
                {
                    return;
                }
            }
            metadata.IsReadOnly = true;
        }
        public override string FormatErrorMessage(string name)
        {
            return "You do not have access to change " + name;
        }
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var contentData = validationContext.ObjectInstance as IContentData;
            if (contentData == null)
            {
                //This attribute only handles instances of IContentData.
                return ValidationResult.Success;
            }
            if (!contentData.Property[validationContext.MemberName].IsModified)
            {
                return ValidationResult.Success;
            }
            return base.IsValid(value, validationContext);
        }
        public override bool RequiresValidationContext
        {
            get
            {
                return true;
            }
        }
        public override bool IsValid(object value)
        {
            foreach (string role in AllowedRoles)
            {
                if (EPiServer.Security.PrincipalInfo.CurrentPrincipal.IsInRole(role))
                {
                    return true;
                }
            }
            return false;
        }
    }

And to restrict at Property Level i am doing

        [PropertyEditRestriction(new string[] { "WebEditors" })]

Just to confirm when i use on any other property not in that specific tab it works just fine. Any Help would be appreciated

Also for some bonus points is their anyway i can completely restrict access to SystemTabNames.PageHeader or Hide it ? Via admin mode i tried setting access right to administer only and it took no effect

Cheers

Minesh


Viewing all articles
Browse latest Browse all 6894

Trending Articles