Authorization Rules on List to create child object

Authorization Rules on List to create child object

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


crackity_jones666 posted on Friday, January 13, 2012

Hello...

How about this typical situation

 public class Child : BusinessBase<Child>
    {
    }

    public class ChildList : BusinessListBase<ChildListChild>
    {
        
    }

    public class Parent : BusinessBase<Parent>
    {
        public static PropertyInfo<ChildList> ChildProperty =
         RegisterProperty<ChildList>(o => o.Children);

        public ChildList Children
        {
            get { return GetProperty(ChildProperty); }
            set { SetProperty(ChildProperty, value); }
        }
    }

As an example, I would like to create an authorization rule that forbids the addition of more than 1 child in the child list.

From what I know, I would not be able to add a CreateObject custom authorization rule on the Child class as I would not receive the target on the Execute method (quite normal, the instance would not be created yet).

I know that I could create a validation rule, but I want the action to be blocked right away. 

In fact,  my main objective is to bind an "Add a child" button availability on the CanAddNew property of the ViewModelBase, as this property is linked to the CreateObject authorization rule...

if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, itemType) && !isObjectBusy) CanAddNew = true;else CanAddNew = false;

One other option that I could have is to create a public method on the ChildList and override the CanExecute method.  Unfortunatly, no CanExecute method exists on BusinessListBase.

Is there any hope for such a wish?

Thanks in advance!

 

JonnyBee replied on Friday, January 13, 2012

Hi,

Already in the wish list: http://www.lhotka.net/cslabugs/edit_bug.aspx?id=945

 

crackity_jones666 replied on Monday, January 16, 2012

Thanks!

Any workaround suggested while waiting for this implementation? I tried with a CanWrite Autz rule on the parent class to fullfill my need for now, but the result may differ with the action I wan't to do (Add a new child vs. modify a child).

Thanks for your time...

JonnyBee replied on Monday, January 16, 2012

For now - until this is supported by Authz rules in CSLA, I would recommend the following solution:

  public class ChildList : BusinessListBase<ChildListChild>
  {
    protected override Child AddNewCore()
    {
      if (!CanAddChild()) 
        throw new SecurityException("Cannot add child object to list.");
 
      return base.AddNewCore();
    }
    public bool CanAddChild()     {       return this.Count == 0;     }   }
  public class ChildViewModel : ViewModel<ChildList>
  {
    protected override void OnSetProperties()
    {
      base.OnSetProperties();

if (this.Model != null)        CanAddNew = CanAddNew && this.Model.CanAddChild();     }   }

crackity_jones666 replied on Monday, January 16, 2012

Thanks!

Copyright (c) Marimer LLC