Blogs

eXpress App Framework Team

Applying Security to State Machine module

     

In this blog entry will discuss how to restrict transition to certain states for specific system roles. Do not be put off by the title, by the end of the post you will surely agree that this is not as complicated as it sounds!

In the following example, control over transition to the Completed state is to be restricted to administrators. To make this happen we need to create a custom function criteria operator that will enumerate all user roles and check their name against this function’s argument. This is demonstrated below,

public class IsAllowedToRoleOperator : ICustomFunctionOperator {

    public const string OperatorName = "IsAllowedToRole";

    #region ICustomFunctionOperator Members

    public object Evaluate(params object[] operands) {

        if (!(operands != null && operands.Length == 1 && operands[0] is string)) {

            throw new ArgumentException("IsAllowedToRole operator should have one paraneter - string roleName.");

        }

        var roleName = (string)operands[0];

        bool result = false;

        var userWithRoles = SecuritySystem.CurrentUser as IUserWithRoles;

        if (userWithRoles != null) {

            foreach (IRole role in userWithRoles.Roles) {

                if (role.Name == roleName) {

                    result = true;

                    break;

                }

            }

        }

        return result;

    }

 

    public string Name {

        get { return OperatorName; }

    }

 

    public Type ResultType(params Type[] operands) {

        return typeof(bool);

    }

    #endregion

}

 

After implementing the operator we still need to register it, for example in a custom module.

public override void CustomizeTypesInfo(DevExpress.ExpressApp.DC.ITypesInfo typesInfo) {

    base.CustomizeTypesInfo(typesInfo);

    if (CriteriaOperator.GetCustomFunction(IsAllowedToRoleOperator.OperatorName) == null) {

        CriteriaOperator.RegisterCustomFunction(new IsAllowedToRoleOperator());

    }

}

 

Note: In future versions these custom operators will be registered to the core. Thus they will appear in all relevant UIs - this sure sounds like the DX way!

The next step is to set the TargetObjectCriteria of the Completed state to,

image

When a non administrator tries to perform the transition as shown,

image

then a validation exception will be raised,

image

Using this approach the state machine designer is capable at runtime of restricting transition to certain states. Moreover applying different types of Security schemas is as easy as providing different versions of our custom function criteria operator.

We would appreciate your feedback on this post. Has it been useful to you? Feel free to contact us with any further questions

Related Links
Online documentation
Blog posts

Published Jul 22 2011, 09:09 AM by Apostolis Bekiaris (DevExpress)
Technorati tags: 11.1 v2011.1, state machine, XAF
Bookmark and Share

Comments

 

christy pirumova said:

Tolis, i've searched the documentation for the IsAllowedToRole operator after your webinar about state machine module :)

you have mentioned it there and i've got an impression that it's a built in one

i liked the idea of such operator and am glad you have posted its definition here

thanks!

July 22, 2011 2:51 PM
 

M. Brekhof said:

Very good Tolis, thank you. Would it also be possible to not even show the state transition to Completed if the user has no rights?

July 23, 2011 5:00 AM
 

Apostolis Bekiaris (DevExpress) said:

Thanks for your comments!.

@M. Brekhof Of course it is, I already included the solution in my next post. Stay tuned!

July 26, 2011 9:38 AM
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.