Constable Authorization Engine 2.0 BETA

RuleBase.State Property

[This is preliminary documentation and subject to change.]

Sets or returns the State associated with this authorization rule.

[Visual Basic]
Overridable Public Property State As State
[C#]
public virtual State State {get; set;}

Example

[VB.NET]
Imports System
Imports System.Diagnostics
Imports System.Security.Principal

Imports LaMarvin.Constable
Imports LaMarvin.Constable.Model
Imports LaMarvin.Constable.Principal

Public Class RuleBaseSetStateSample

  ''' 
  ''' Demonstrates the new capability of directly associating an existing
  ''' authorization rule with a new state by setting the ActionRule.State
  ''' or PropertyRule.State properties.
  ''' 
  Public Shared Sub Main()
    ' Set up a simple authorization policy programmatically.
    Dim policy As AuthorizationPolicy = New AuthorizationPolicy("battleshipGame")
    policy.Description = "FMI see the Battleships game at http://www.miniclip.com/"

    ' The game roles.
    policy.Roles.AddNew("User")
    policy.Roles.AddNew("Computer")

    ' The game states.
    policy.States.AddNew("shipDeployment") ' initial state - deploying ships on the board
    policy.States.AddNew("startedUserMove") ' game has been started, the user should move As 
    policy.States.AddNew("startedComputerMove") ' game has been started, the computer should move As 
    policy.States.AddNew("wonByUser") ' the game has been won by the user As 
    policy.States.AddNew("wonByComputer") ' the game has been won by the computer As 

    ' These are the game actions.
    policy.Actions.AddNew("Start")
    policy.Actions.AddNew("Move")
    policy.Actions.AddNew("Stop")

    ' The initial state is shipDeployment; the Computer starts the game by executing the Start action.
    policy.ActionRules.AddNew("Start", "Computer", "shipDeployment", "startedUserMove")

    ' The game is in progress; the Move action executed by the Computer transitions the game from
    ' the startedComputerMove state to the startedUserMove state.
    policy.ActionRules.AddNew("Move", "Computer", "startedComputerMove", "startedUserMove")

    ' The game is in progress; the Move action executed by the User transitions the game from
    ' the startedUserMove state to the startedComputerMove state.
    policy.ActionRules.AddNew("Move", "User", "startedUserMove", "startedComputerMove")

    ' The Computer decides who wins the game and ends it by executing the Stop action
    ' while the game is running (i.e. is in the startedUserMove or startedComputerMove states).
    policy.ActionRules.AddNew("Stop", "Computer", "startedUserMove", "wonByUser")
    policy.ActionRules.AddNew("Stop", "Computer", "startedComputerMove", "wonByComputer")

    ' Our principals representing the User and Computer roles.
    Dim user As IPrincipal =  New GenericPrincipal(New GenericIdentity("Jack User"),new String(){"User"})
    Dim computer As IPrincipal =  New GenericPrincipal(New GenericIdentity("Compaq nx7010"),new String(){"Computer"})

    ' The Computer starts the game.
    policy.CurrentState = policy.States("shipDeployment")
    policy.CurrentPrincipal = computer
    policy.ExecuteAction("Start")

    ' Now the User should move; the Computer cannot.
    Debug.Assert(Not policy.IsActionExecutable("Move"))
    policy.CurrentPrincipal = user
    policy.ExecuteAction("Move")

    ' Now the Computer should move; the User cannot.
    Debug.Assert(Not policy.IsActionExecutable("Move"))

    ' Now it gets a bit tricky:
    ' We're in the startedComputerMove state and the current principal is still the User.
    ' We'll dynamically change the rule for the Move command executed by the User to be
    ' allowed even in the startedComputerMove state. 
    Dim moveUserRule As ActionRule = policy.ActionRules.Lookup("Move", "User", "startedUserMove")
    moveUserRule.State = policy.States("startedComputerMove")
    Debug.Assert(policy.IsActionExecutable("Move"))
    policy.ExecuteAction("Move")
  End Sub
End Class

[C#]
using System;
using System.Diagnostics;
using System.Security.Principal;

using LaMarvin.Constable;
using LaMarvin.Constable.Model;
using LaMarvin.Constable.Principal;

public class RuleBaseSetStateSample
{

  /// 
  /// Demonstrates the new capability of directly associating an existing
  /// authorization rule with a new state by setting the ActionRule.State
  /// or PropertyRule.State properties.
  /// 
  public static void Main()
  {
    // Set up a simple authorization policy programmatically.
    AuthorizationPolicy policy = new AuthorizationPolicy("battleshipGame");
    policy.Description = @"FMI see the Battleships game at http://www.miniclip.com/";
    
    // The game roles.
    policy.Roles.AddNew("User");
    policy.Roles.AddNew("Computer");

    // The game states.
    policy.States.AddNew("shipDeployment"); // initial state - deploying ships on the board
    policy.States.AddNew("startedUserMove");  // game has been started, the user should move
    policy.States.AddNew("startedComputerMove"); // game has been started, the computer should move
    policy.States.AddNew("wonByUser");  // the game has been won by the user
    policy.States.AddNew("wonByComputer");  // the game has been won by the computer
    
    // These are the game actions.
    policy.Actions.AddNew("Start");
    policy.Actions.AddNew("Move");
    policy.Actions.AddNew("Stop");

    // The initial state is shipDeployment; the Computer starts the game by executing the Start action.
    policy.ActionRules.AddNew("Start", "Computer", "shipDeployment", "startedUserMove");

    // The game is in progress; the Move action executed by the Computer transitions the game from
    // the startedComputerMove state to the startedUserMove state.
    policy.ActionRules.AddNew("Move", "Computer", "startedComputerMove", "startedUserMove");

    // The game is in progress; the Move action executed by the User transitions the game from
    // the startedUserMove state to the startedComputerMove state.
    policy.ActionRules.AddNew("Move", "User", "startedUserMove", "startedComputerMove");
    
    // The Computer decides who wins the game and ends it by executing the Stop action
    // while the game is running (i.e. is in the startedUserMove or startedComputerMove states).
    policy.ActionRules.AddNew("Stop", "Computer", "startedUserMove", "wonByUser");
    policy.ActionRules.AddNew("Stop", "Computer", "startedComputerMove", "wonByComputer");

    // Our principals representing the User and Computer roles.
    IPrincipal user = new GenericPrincipal(new GenericIdentity("Jack User"), new string[]{"User"});
    IPrincipal computer = new GenericPrincipal(new GenericIdentity("Compaq nx7010"), new string[]{"Computer"});

    // The Computer starts the game.
    policy.CurrentState = policy.States["shipDeployment"];
    policy.CurrentPrincipal = computer;
    policy.ExecuteAction("Start");

    // Now the User should move; the Computer cannot.
    Debug.Assert(!policy.IsActionExecutable("Move"));
    policy.CurrentPrincipal = user;
    policy.ExecuteAction("Move");

    // Now the Computer should move; the User cannot.
    Debug.Assert(!policy.IsActionExecutable("Move"));

    // Now it gets a bit tricky:
    // We're in the startedComputerMove state and the current principal is still the User.
    // We'll dynamically change the rule for the Move command executed by the User to be
    // allowed even in the startedComputerMove state. 
    ActionRule moveUserRule = policy.ActionRules.Lookup("Move", "User", "startedUserMove");
    moveUserRule.State = policy.States["startedComputerMove"];
    Debug.Assert(policy.IsActionExecutable("Move"));
    policy.ExecuteAction("Move");
  }
}

See Also

RuleBase Class | LaMarvin.Constable.Model Namespace