Showing or Hiding a Field if the Current User is a Member of a Role in a Creatio Freedom UI Page

Creatio has been expanding the capabilities of business rules on Freedom UI pages with each release. However, as of 8.0.10, you cannot add a rule to show or hide a field on a Freedom UI page based on whether the current user is a member of a specific role or not. However, it is possible to do this with code on the page.

First of all, make sure you add “@creatio-devkit/common” to the page as sdk.

Next, add an attribute to the viewModelConfigDiff. I’ll call the attribute “IsUserInRole since we’ll set it to true/false if the user is in the role. Note: If there is already a node for path attributes, you can just add your new attribute to the values of that existing node.

viewModelConfigDiff: /**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/[
	{
		"operation": "merge",
		"path": [
			"attributes"
		],
		"values": {
			"IsUserInRole": {}
		}
	}
]/**SCHEMA_VIEW_MODEL_CONFIG_DIFF*/,

Now, bind the attribute to the visible property of the control by adding the following to the control in the viewConfigDiff:

"visible": "$IsUserInRole"

When the view model is initialized, basically the Freedom UI equivalent of the onEntityInitialized for classic pages, do a query using the model to determine if the current user is in the role. We’ll use that result to set the attribute. The code would look as follows:

{
    request: "crt.HandleViewModelInitRequest",
    handler: async (request, next) => {
        await next?.handle(request);

        // get current user
        const sysValuesService = new sdk.SysValuesService();        
        const sysValues = await sysValuesService.loadSysValues();
        const currentUserContact = sysValues.userContact;
 
        // create model query and add filters
        userRoleModel = await sdk.Model.create("SysUserInRole");
        const filter = new sdk.FilterGroup();
        await filter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "SysRole.Name", "The Role Name Here");
        await filter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "SysUser.Contact", currentUserContact.value);
 
        // workaround for filters, will be fixed in 8.1
        const newFilter = Object.assign({}, filter);
        newFilter.items = filter.items;
 
        const results = await userRoleModel.load({
            attributes: ["Id"],
            parameters: [{
                type: sdk.ModelParameterType.Filter,
                value: newFilter
            }]
        });
 
        // now set the attribute
        request.$context.IsUserInRole = (results.length > 0);
    }
}

Note, don’t forget to change “The Role Name” in the code above to the actual role name. The end result will be that the field will only show if the current user is in that role.

Want content like this delivered to your inbox? Sign up for our newsletter!
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. His new passion for CRM is Creatio, formerly bpm'online. He loves C#, Javascript, web development, open source, and Linux. He also loves his hobby as an amateur filmmaker.

4 Comments

  1. Hi Ryan,

    Do you have the updated code for version 8.1.3, basically viewModelConfig is no longer part of the structure it is now viewModelConfigDiff which is confusing me where to put these codes. Thanks in advance.

    Reply
    • I updated the article to show the new approach for adding attributes to a page.
      Ryan

    • Hi Ryan,

      Thank you for your help. How about for viewConfigDiff. Could you please share the full structure, I am confised how to add this visible property into it.

    • If I am hiding a button, I can search for the button’s “element code” (name) in the viewConfigDiff. You’ll find the button’s (or any element) element code in the bottom of the properties window in the designer. Locate that in the viewConfigDiff. It would look something like this:

      {
      	"operation": "insert",
      	"name": "Button_ls78mb0",
      	"values": {
      		"type": "crt.Button",
      		"caption": "#ResourceString(Button_ls78mb0_caption)#",
      		"color": "default",
      		"disabled": false,
      		"size": "large",
      		"iconPosition": "only-text",
      		"visible": true,
      		"icon": null,
      	},
      	"parentName": "ActionButtonsContainer",
      	"propertyName": "items",
      	"index": 3
      },

      Notice the properties in the “values” part, just like it was for elements in the diff of classic pages. You can change the “visible” property of it to bind to the attribute, like this:

      {
      	"operation": "insert",
      	"name": "Button_ls78mb0",
      	"values": {
      		"type": "crt.Button",
      		"caption": "#ResourceString(Button_ls78mb0_caption)#",
      		"color": "default",
      		"disabled": false,
      		"size": "large",
      		"iconPosition": "only-text",
      		"visible": "$MyAttributeNameHere",
      		"icon": null,
      	},
      	"parentName": "ActionButtonsContainer",
      	"propertyName": "items",
      	"index": 3
      },

      The “$” is what bind’s that to the attribute, the equivalent of the { "bindTo": "MyAttributeNameHere" } for classic pages. If the element doesn’t have a “visible” property, just add one to it.

      Ryan

Submit a Comment

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