Check if user is in group

I’m pulling my hair out of frustration (well, almost) but I can’t get something to work right.

I’ve set up a ‘groups’ page with groups as sub-pages. Those groups can be restricted, so only users added to a ‘user’ structure field can access this page. What I would like to do is return a collection of these restricted pages where the current user (the logged in user) has access to, so where his username is found in the structure field of the group.

The most important part of the blueprint:

group_users:
    label: Users who have access to this group
    type: structure
    style: table
    fields:
        single_user:
            label: Gebruiker
            type: user

And in the page .txt file, the user is added to the field:

Group-users:

- 
  single_user: admin

What I do now (but doesn’t work) to get a collection of the groups with the current user in the user field was the suggestion in this topic: https://forum.getkirby.com/t/can-i-use-filterby-on-a-structure-field/1137:


// Fetch all pages with restricted access
$restrictedPages = $site->page('the-page/groups')->children()->filterBy('restricted', '==', 'true');

// So far so good, I used ->count() to check the returned collection and this works!

// Then, filter these pages to only have the ones where the current user is found in
$pagesWithUserAccess = $restrictedPages->filter(function($p){
    // Somehow, I need to define Kirby as well :)
    $kirby = kirby();
    $structure_field 	 = $p->group_users()->toStructure();
    
    return $structure_field->single_user(  $kirby->site()->user()->current()->username() );

    // I've also tried:
    // return $structure_field->single_user() == $kirby->site()->user()->current()->username();
});

The problem is that it doesn’t return any groups, even tho the user is added to some of the groups. Can someone point me in the right direction of what I’m doing wrong here?

TL;DR : I need to get a collection of subpages where a structurefield 'group_users has a field user that matches the current user visiting the page :slight_smile: .

The main issue here is that there can be multiple “single user” values per group (since you are using a structure field). Also you are using an incorrect syntax for the current user:

// Fetch all pages with restricted access
$restrictedPages = $site->page('the-page/groups')->children()->filterBy('restricted', '==', 'true');

// Get current username
$currentUser = site()->user()->username();

// Filter the pages by access to the current user
$pagesWithUserAccess = $restrictedPages->filter(function($p) use($currentUser) {
  $structure = $p->group_users()->toStructure();
  
  // Check if the structure contains an item where `single_user` is `$currentUser`
  return ($structure->findBy('single_user', $currentUser))? true : false;
});

This is why I love Kirby and the Kirby support. Within 5 minutes on a freakin’ Sunday you’ll get the solution to your problem or issues.

Thanks @lukasbestle ! :smiley:

5 Likes