Determining if a User has a Specific Role in bpm’online

In bpm’online roles are used to determine what a user is allowed to do. There are organizational roles and functional roles. Functional roles are specific things a user can do, and an organizational role is a grouping of those functional roles for a specific type of user in the organization. If you’ve come to bpm’online from Infor CRM, think of functional roles as “secured actions” and organizational roles as “roles” in Infor CRM. In this article, we will be taking a look at how to programmatically determine if a user is in a specific organizational role. The idea being you can do something based on this information, such as show or hide the lead qualify button, as outlined in my last article.

In bpm’online, client-side queries are performed using the EntitySchemaQuery (ESQ) objects (Note: ESQ queries can also be performed server-side as well in processes or configuration services). ESQ queries are easy to use and understand and allow you to perform queries against the object model and security/entity schema events, etc will all apply. We’ll use an ESQ query from our client-side code to query the SysUserInRole entity with a join to SysRole, allowing us to query based on the role name.

First of all, we’ll need to know who the current user is. This code will allow us to determine that:

Terrasoft.SysValue.CURRENT_USER.value

This will provide the current user’s ID. Note, this is the user ID value from the SysAdminUnit for the user, not the user’s contact ID (which cab be obtained using Terrasoft.SysValue.CURRENT_USER_CONTACT.value). For this purpose, we need the SysAdminUnit Id for the user since that is what is stored in the SysUserInRole. See my last post for more on this topic.

Once we have that, we can form the ESQ query (redundant “query” lol) as follows:

// Check if the user is in the "Accounting" role
// this could be any role name, for example "All employees"
var roleName = "Accounting";

// create the query for SysUserInRole 
var esq = Ext.create("Terrasoft.EntitySchemaQuery", {
    rootSchemaName: "SysUserInRole"
});
esq.addColumn("SysRole");

// add filter for current user
esq.filters.add("UserFilter", Terrasoft.createColumnFilterWithParameter(
    Terrasoft.ComparisonType.EQUAL, "SysUser", Terrasoft.SysValue.CURRENT_USER.value
));
// add filter for role name, joining to SysRole (which is where the name is stored)
esq.filters.add("RoleFilter", Terrasoft.createColumnFilterWithParameter(
    Terrasoft.ComparisonType.EQUAL, "SysRole.Name", roleName
));
// now get the results
esq.getEntityCollection(function(result) {
    if (!result.success || result.collection.getItems().length === 0) {
        // the user is *not* in the role 
        // do something here if needed
    }
    else {
        // the user *is* in the role
        // do whatever is needed here
    }
}, this);

Something to note about ESQ queries, they are asynchronous. In the query above, the getEntityCollection will execute asynchronously, executing the callback function for the results. That just means that you can’t stick this in a function that returns some true/false result, you need to act on the data returned, when it is returned.

Similarly, if you wanted a list of all roles the user is in, you could use this query:

var esq = Ext.create("Terrasoft.EntitySchemaQuery", {
    rootSchemaName: "SysUserInRole"
});
esq.addColumn("SysRole");
esq.filters.add("UserFilter", Terrasoft.createColumnFilterWithParameter(
    Terrasoft.ComparisonType.EQUAL, "SysUser", Terrasoft.SysValue.CURRENT_USER.value
));
esq.getEntityCollection(function(result) {
    if (!result.success || result.collection.getItems().length === 0) {
        return;
    }
    
    // iterate through all roles returned for the current user
    result.collection.each(function(item) { 
    	var role = item.get("SysRole");
    	var roleName = role.displayValue;
    	var roleId = role.value;
    	this.console.log(roleName);
    });
}, this);

At some point in the future I will have a more in depth article on working with EntitySchemaQuery.

ABOUT THE AUTHOR

Ryan Farley

Ryan Farley is the Director of Development for Customer FX and creator of slxdeveloper.com. He's been blogging regularly about SalesLogix, now Infor CRM, since 2001 and believes in sharing with the community. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe To Our Newsletter

Join our mailing list to receive the latest Infor CRM (Saleslogix) news and product updates!

You have Successfully Subscribed!