How to disable Removebutton per instance in ProjectTracker?

How to disable Removebutton per instance in ProjectTracker?

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


simon posted on Tuesday, January 10, 2012

I want to disable the RemoveButton per Instance.

In the sample (05-WpfSl-111028) the Removebutton in ProjectList View binding to ProjectList ViewModel's CanRemove:

 

      public new bool CanRemove   {

        get { return Csla.Rules.BusinessRules.HasPermission(Csla.Rules.AuthorizationActions.DeleteObject, typeof(ProjectTracker.Library.ProjectEdit)); }

      }

 

I add some code into ProjectEdit Model 's 

 

 protected override void AddBusinessRules()

    {

      this.BusinessRules.AddRule(new MyEditRule(AuthorizationActions.EditObject));

    }

 

         private class MyEditRule : Csla.Rules.AuthorizationRule

        {

            public MyEditRule(Csla.Rules.AuthorizationActions action)

                : base(action)

            {

            }

            protected override void Execute(AuthorizationContext context)

            {

 

                var target = (ProjectEdit)context.Target;

 

                if (target != null)

                {

                    context.HasPermission = target.Name != "Update ProjectTracker";

                }

            }

        }

but it does not work correctly.

how disable the RemoveButton per instance?

 now i only use following code to disable the button in my code.

private RoleInfo  focusedRow;

        public RoleInfo FocusedRow

        {

            get { return focusedRow; }

            set

            {

                if (focusedRow != value)

                {

                    focusedRow = value;

                    OnPropertyChanged("FocusedRow");

                    this.DeleteCommand.RaiseCanExecuteChanged();

                }

            }

        }

this.DeleteCommand = new DelegateCommand<RoleInfo>(OnDeleteExcuted,OnCanDelete);

 private bool OnCanDelete(RoleInfo info)

        {

            if (info == null || info.Name == "Administrators")

                return false;

            else

                return Csla.Rules.BusinessRules.HasPermission(Csla.Rules.AuthorizationActions.DeleteObject, typeof(Role));

        }

 

 but instantiate the Role Model. above code does not work correctly. because below code will run and context.Target is null.

  private class RoleDeleteRule : Csla.Rules.AuthorizationRule

        {

            public RoleDeleteRule(Csla.Rules.AuthorizationActions action) : base(action)

            {  }

            protected override void Execute(AuthorizationContext context)

            {

               var target = (Role)context.Target;

                if (target != null)

                {

                    context.HasPermission = target.Name != "Administrators";

                }

            }

 

 

 

 

simon replied on Tuesday, January 10, 2012

I got it from the book.

Finally, for the edit and delete actions you might have an object instance you can supply to the rule:

bool canEdit = Csla.Rules.BusinessRules.HasPermission( Csla.Rules.AuthorizationActions.EditObject, obj);

Copyright (c) Marimer LLC