Freedom UI , show field based on user role

How can i hide / show a field based on the user role in freedom UI with business rules or with js code?

Thank you

Like 2

Like

6 comments
Best reply

You can do this with code on a Freedom UI page. 

1) Make sure you add "@creatio-devkit/common" to the page as sdk

2) First add an attribute to the viewModelConfig. I'll call the attribute "IsUserInRole since we'll set it to true/false if the user is in the role. 

viewModelConfig: /**SCHEMA_VIEW_MODEL_CONFIG*/{
    "attributes": {
        "IsUserInRole": {}
    }
}/**SCHEMA_VIEW_MODEL_CONFIG*/

3) Bind the attribute to the visible property of the control by adding the following to the control in the viewModelDiff

"visible": "$IsUserInRole"

4) Now when the view model is initialized, basically the Freedom UI equivalent of the onEntityInitialized on 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:

{
    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
        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 attribute
        request.$context.IsUserInRole = results.length > 0;
    }
}

I didn't test that code, but it should be pretty close. If anything you might need to play with the filter for the model query.

Ryan

Hello,

 

Unfortunately, there is no way to add visibility to the field based on user role via Section Wizard.

 

But we've registered it in our R&D team backlog for consideration and implementation in future application releases.

 

Thank you for helping us to improve our product. 

Bogdan,

but is it possible to calculate page parameter based on Operation permission? And then use this parameter in business rule?

You can do this with code on a Freedom UI page. 

1) Make sure you add "@creatio-devkit/common" to the page as sdk

2) First add an attribute to the viewModelConfig. I'll call the attribute "IsUserInRole since we'll set it to true/false if the user is in the role. 

viewModelConfig: /**SCHEMA_VIEW_MODEL_CONFIG*/{
    "attributes": {
        "IsUserInRole": {}
    }
}/**SCHEMA_VIEW_MODEL_CONFIG*/

3) Bind the attribute to the visible property of the control by adding the following to the control in the viewModelDiff

"visible": "$IsUserInRole"

4) Now when the view model is initialized, basically the Freedom UI equivalent of the onEntityInitialized on 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:

{
    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
        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 attribute
        request.$context.IsUserInRole = results.length > 0;
    }
}

I didn't test that code, but it should be pretty close. If anything you might need to play with the filter for the model query.

Ryan

Thank you. That helps a lot.

Ryan Farley,



I have tried a similar case, where on saving a record writing a validation to check whether it has unique "Code".

 

request: "crt.SaveRecordRequest",handler: async (request, next) => {
// Add any code to execute *before* the save here
 
var accountModel = await sdk.Model.create("Account");
 
const filter = new sdk.FilterGroup();
var codeValue = await request.$context.StringAttribute_enupz4g;
await filter.addSchemaColumnFilterWithParameter(sdk.ComparisonType.Equal, "SMCode", codeValue);
 
	const newFilter = Object.assign({}, filter);
	newFilter.items = filter.items;
 
	const accounts = await accountModel.load({
		attributes: ["Id"],
		parameters: [{
		type: sdk.ModelParameterType.PrimaryColumnValue,
		value: newFilter
		}]
	});
	if(accounts.length > 0){
		isSave = false;
		//Show warning Message
		request.$context.executeRequest({
		type: "crt.ShowDialogRequest",
		$context: request.$context,
		dialogConfig: {
		data: {
		message: "Code already already exists",
		actions: [{
			key: "OK",
			config: {
			color: "primary",
			caption: "OK"
			}
		}]
		}
		}
	});
	}
else{
return next.handle(request);
}
}



I followed your code to check any account has the similar code. Seems like, there is an issue in the filter it throws the below error.







Can you help me in adding the proper filter in this Freedom UI? Or provide a sample code to add filters to retrive data from an entity.



Regards,

Adharsh S

Adharsh,

Change this part: 

const accounts = await accountModel.load({
	attributes: ["Id"],
	parameters: [{
		type: sdk.ModelParameterType.PrimaryColumnValue,
		value: newFilter
	}]
});

To this: 

const accounts = await accountModel.load({
	attributes: ["Id"],
	parameters: [{
		type: sdk.ModelParameterType.Filter,
		value: newFilter
	}]
});

Note, the difference in type. You're specifying a filter, not providing a primary column value. 

Ryan

Show all comments