Content Hub Tip #15: Searching for user groups

 

Content Hub tips logo

Today, we had an issue with a script that checks if you're part of a specific User Group before executing the script. This script checked if you're part Superusers or Reviewers group. The first group, named Superusers, will be familiar to you. This is one of the internal groups that Content Hub ships with. The second group is a custom created group specifically created for this implementation.

So which part of the code failed in the script?

var usergroupsQuery = Query.CreateQuery(entities =>
                entities.Where(e => e.DefinitionName == UserGroupDefinitionName &&
                    (e.Identifier == SuperUsersUsergroupName || e.Identifier == ReviewerUsergroupName)));

The above line of searches for UserGroup definitions, that have the identifier of SuperUser or Reviewer (our custom group). This seems to work fine for SuperUsers, but when we tested with Reviewers, it failed. If you have some experience with Content Hub, you might spot the problem already.

If you haven't spotted the problem, no worries, we've got you covered. The problem with the code is that the assumption was made that the Identifier of custom made groups work the same way as the default groups. That's just plain wrong! Only the default groups have a specialized identifier. If we access the two user groups via the API we see the difference clearly.

Difference between user groups

On the left, we've the Superusers group and on the left is our custom made Reviewer group. As you can see, both have the GroupName the same, but the identifier is completely different. So the easiest fix was to change the query value of the ReviewerUsergroupName property in the code. But before we did that, we checked our other environments. In the image below, you can see the output of the QA environment. I expected the identifier to be the same, but unfortunately, it wasn't. That would mean that changing the selection value wouldn't work.

Reviewer group QA environment

The only thing that's the same across all instances is the value of the GroupName property. So let's use that for our query. 

var usergroupsQuery = Query.CreateQuery(entities => entities.Where(e =>
                e.DefinitionName == UserGroupDefinitionName &&
                (e.Identifier == SuperUsersUsergroupName ||
                 e.Property("GroupName") == ReviewerUsergroupName)));

In the code above we've changed the selection of our custom Group to check the GroupName property. This solves our problem. The selection now works as expected and across multiple environments.

Lessons learned:
  • Only default user groups have named identifiers
  • User groups across different environments don't share the same identifier. Each user group has a unique identifier for each environment.
  • Use the GroupName property to select a group across multiple environments.
Until next time!