Re: Business Rule for Not Allowing Edit or Delete for a Specific Record

Re: Business Rule for Not Allowing Edit or Delete for a Specific Record

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


JCardina posted on Monday, November 05, 2012

Whups!  Have you tried this Jonny?  I subclassed my existing rule as per your example, made it the only delete rule and it now calls execute which is progress however Context.Target is *always* null so it does nothing.

Is this a problem for some reason because I've used my subclassed rule for Delete action but used the regular rule for all the other actions? Does this confuse the rule engine or something?  I.E.: 

 

  public static void AddObjectAuthorizationRules()        { 

            Csla.Rules.BusinessRules.AddRule(typeof(PartWarehouse), new Admin.IsAllowed(Csla.Rules.AuthorizationActions.GetObject, RootObjectTypes.PartWarehouse));

            Csla.Rules.BusinessRules.AddRule(typeof(PartWarehouse), new Admin.IsAllowed(Csla.Rules.AuthorizationActions.CreateObject, RootObjectTypes.PartWarehouse));

            Csla.Rules.BusinessRules.AddRule(typeof(PartWarehouse), new Admin.IsAllowed(Csla.Rules.AuthorizationActions.EditObject, RootObjectTypes.PartWarehouse));

                        //Instead of running regular delete rule we'll run the one that checks if its' the default warehouse *in addition* to checking regular permissions

            Csla.Rules.BusinessRules.AddRule(typeof(PartWarehouse), new Admin.IsAllowedOrIsSystemObjectAuthorizationRule(Csla.Rules.AuthorizationActions.DeleteObject,   RootObjectTypes.PartWarehouse, IsDefaultWarehouseProperty));

        }

 

And the rule itself:

  internal class IsAllowedOrIsSystemObjectAuthorizationRule : IsAllowed

    {

        private readonly IPropertyInfo _IsSystemObjectField;

 

        public IsAllowedOrIsSystemObjectAuthorizationRule( AuthorizationActions action, RootObjectTypes objectType, PropertyInfo<bool> IsSystemObjectBoolField)

            : base(action, objectType)

        {

            _IsSystemObjectField = IsSystemObjectBoolField;

        }

 

        public override bool CacheResult

        {

            get

            {

                return false;

            }

        }

 

 

        protected override void Execute(AuthorizationContext context)

        {

            // check the base rule

            base.Execute(context);

            if(!context.HasPermission) return;

 

            if(context.Target == null) return;//It's always null, this isn't working!

 

            var isSystem = (bool)ReadProperty(context.Target, _IsSystemObjectField);

            context.HasPermission = !isSystem;                     

        }

    }//eoc   

 

Copyright (c) Marimer LLC