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 userconst 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.1const 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.
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 userconst 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.1const 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.
This is a very common requirement for all clients, this really should be added to the no code Page Designer. Similarly with Operation permissions to be used in visibility conditions.