How do i check if a user has access to view a term entity?

Created on 9 April 2025, 9 days ago

Problem/Motivation

I'm trying to figure out how i can check if a user has access to view a term, I've set up permissions so that my group members can view terms from within the group the belong to and this is working as expected. But I’d like to manually do this check in custom code, I've had a look through this modules code and also the groups module code but haven’t been to find anything. Is this something that someone can help me with please?

πŸ’¬ Support request
Status

Active

Version

3.1

Component

Code

Created by

πŸ‡¬πŸ‡§United Kingdom lincoln-batsirayi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @lincoln-batsirayi
  • πŸ‡§πŸ‡ͺBelgium lobsterr

    Hi, you need to check if user has permission "view group_term:tags entity" (in this case I have tags taxonomy vocabulary enabled as a plugin in my group type). You can do it like this:

    $group->hasPermission('view group_term:tags entity', $account);
    

    The same way you can check any other permissions like edit, delete:

    $group->hasPermission('update any group_term:tags entity', $account);
    
  • πŸ‡¬πŸ‡§United Kingdom lincoln-batsirayi

    Hi @lobsterr thanks for the reply, so yes i can confirm that this works when checking against the actual group entity, but I’d like to do this check against one of the taxonomy tags for example.

    So through the following

    $group_relationships_tags = GroupRelationship::loadByPluginId('group_term: tags');
    

    i can get a list of all the relationship entities, in the resulting array i can see that a relationship does exist which connects that individual taxonomy term to the group.

    I was wondering if it's possible to get more specific and do the access check based on those individual terms?

  • πŸ‡§πŸ‡ͺBelgium lobsterr

    I think you are looking for this method 'loadByEntity' :

    This an example how you can use it.

    $group_relationships = GroupRelationship::loadByEntity($term);
    
    foreach($group_relationships  as $group_relationship) {
       $group = $group_relationship->getGroup();
       if ($group->hasPermission('view group_term:tags entity', $account)) {
           // Do something here.
       }
    }
    
  • πŸ‡¬πŸ‡§United Kingdom lincoln-batsirayi

    Ok great! I was able to make it work this way, thanks so much for your help @lobsterr

  • πŸ‡§πŸ‡ͺBelgium lobsterr

    Great!

Production build 0.71.5 2024