Disable components based on group membership


I've made and app that has view and update functionality. Fields are readonly by default and when clicking a button "Edit" it will make them editable.
However I don't want some people to edit certain fields, so I've created 2 user groups (Contracts-View and Contracts-Update).

How can I check if a user is member of the update group?
I've tried this code. (if Edit mode is enabled AND user is member of group update or admin AND field2 is populated, the field should be editable)

{{ EditMode.value == false | (EditMode.value == true && (current_user.groups == 'Admin' || current_user.groups == 'Contracts-update')  && txtCcode2.value == "")}}

This isn't working because the current_user.groups is an array of objects. How can I verify is the user is a member of a certain group?


For now I've found a solution that seems to work.
I've created a transformer with this code:

let groups = {{current_user.groups}}
let allow_update = false
let authorized = groups.filter(item => item.name === 'Contracts-update' || item.name === 'admin')
if (authorized.length > 0) {
  allow_update = true

return allow_update

I make the fields editable based on AllowUpdate.value == true.

Any other (better?) solutions available?

This gets all groups the user is in to support Workflow.
var groups = current_user.groups[0].name;
for (i = 1; i < current_user.groups.length; i++) {
  groups += ', ' + current_user.groups[i].name;

myGroups is a tempVar so I only run this when the page loads.

1 Like