Constable Authorization Engine 2.0 BETA

RuleBase.Role Property

[This is preliminary documentation and subject to change.]

Sets or returns the Role associated with this authorization rule.

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

Exceptions

Exception Type Condition
ArgumentNullException An attempt was made to set the property to a a null reference (Nothing in Visual Basic) value.
DuplicateAuthorizationRuleException An attempt was made to set the property to a Role instance that would make this authorization rule duplicate.
ConstableException The Role instance being set cannot be associated with the authorization rule for some other reason; most likely the Role instance belongs to a different AuthorizationPolicy, or it doesn't belong to any AuthorizationPolicy at all.

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 RuleBaseSetRoleSample

  ''' 
  ''' Demonstrates the new capability of directly associating an existing
  ''' authorization rule with a new role by setting the ActionRule.Role
  ''' (or PropertyRule.Role) property.
  ''' 
  Public Shared Sub Main()
    ' Set up a simple authorization policy programmatically.
    Dim policy As New AuthorizationPolicy
    policy.States.AddNew("Default")

    ' These are our two roles.
    Dim userRole As Role = policy.Roles.AddNew("User")
    Dim adminRole As Role = policy.Roles.AddNew("Admin")

    ' Actions will be Logoff - available to both roles.
    policy.Actions.AddNew("Logoff")
    policy.ActionRules.AddNew("Logoff", "User")
    policy.ActionRules.AddNew("Logoff", "Admin")

    ' The Shutdown action is initially available only to Admin.
    policy.Actions.AddNew("Shutdown")
    Dim shutdownRule As ActionRule = policy.ActionRules.AddNew("Shutdown", "Admin")

    ' Now associate the policy with a principal in the User role and assert
    ' some obvious facts about the policy.
    policy.CurrentPrincipal = New ExtendedPrincipal(New GenericIdentity("John Doe"), New String() {"User"})
    Debug.Assert(policy.IsActionExecutable("Logoff"))
    Debug.Assert(Not policy.IsActionExecutable("Shutdown"))

    ' Now associate the Shutdown action rule with the User role and check
    ' it is now executable.
    shutdownRule.Role = userRole
    Debug.Assert(policy.IsActionExecutable("Shutdown"))
  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 RuleBaseSetRoleSample
{

  /// 
  /// Demonstrates the new capability of directly associating an existing
  /// authorization rule with a new role by setting the ActionRule.Role
  /// (or PropertyRule.Role) property.
  /// 
  public static void Main()
  {
    // Set up a simple authorization policy programmatically.
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.States.AddNew("Default");
    
    // These are our two roles.
    Role userRole = policy.Roles.AddNew("User");
    Role adminRole = policy.Roles.AddNew("Admin");

    // Actions will be Logoff - available to both roles.
    policy.Actions.AddNew("Logoff");
    policy.ActionRules.AddNew("Logoff", "User");
    policy.ActionRules.AddNew("Logoff", "Admin");

    // The Shutdown action is initially available only to Admin.
    policy.Actions.AddNew("Shutdown");
    ActionRule shutdownRule = policy.ActionRules.AddNew("Shutdown", "Admin");

    // Now associate the policy with a principal in the User role and assert
    // some obvious facts about the policy.
    policy.CurrentPrincipal = new ExtendedPrincipal(new GenericIdentity("John Doe"), new string[] {"User"});
    Debug.Assert(policy.IsActionExecutable("Logoff"));
    Debug.Assert(!policy.IsActionExecutable("Shutdown"));

    // Now associate the Shutdown action rule with the User role and check
    // it is now executable.
    shutdownRule.Role = userRole;
    Debug.Assert(policy.IsActionExecutable("Shutdown"));
  }
}

See Also

RuleBase Class | LaMarvin.Constable.Model Namespace