Apply authorization to childcollection items based on parent property

Apply authorization to childcollection items based on parent property

Old forum URL: forums.lhotka.net/forums/t/11419.aspx


Mr X posted on Sunday, June 17, 2012

I am trying to authorize editing child collection's items based on the value of the root object.  Can't seem to find any example in the samples projects nor any mentions in the Csla 4 books. I found many examples on how to authorize modifying properties or executing methods but they all refer to the current object, not a child collection or one of its items.  In this case, the methods/properties to authorize reside in another object. I tried adding a rule in the child but can't read the parent. What am I missing?

JonnyBee replied on Monday, June 18, 2012

Something like this should do it (for Csla 4.3.10) :

    public class CheckParentProperty : Csla.Rules.AuthorizationRule
    {
        public IPropertyInfo ParentProperty { getset; }
 
        public CheckParentProperty(AuthorizationActions action, IMemberInfo element, IPropertyInfo parentProperty)
            : base(action, element)
        {
            ParentProperty = parentProperty;
        }
 
 
        public CheckParentProperty(AuthorizationActions action, IPropertyInfo parentProperty)
            : base(action)
        {
            ParentProperty = parentProperty;
        }
 
        public override bool CacheResult
        {
            get { return false; }
        }
 
        protected override void Execute(AuthorizationContext context)
        {
            var target = context.Target;
            if (target == null)
            {
                context.HasPermission = true;
                return;
            }
 
// navigate to parent list             var list = ((Csla.Core.BusinessBase) target).Parent;             if (list == null)             {                 context.HasPermission = true;                 return;             } // get the parent of the List using MethodCaller and reflection
            var parentBO = MethodCaller.CallPropertyGetter(list, "Parent");
// Get the value (assume it is string here)             var parentValue = (string) ReadProperty(parentBO, ParentProperty);             context.HasPermission = (parentValue == "xyz");         }     }

Copyright (c) Marimer LLC