# Filtering pages by user

**URL:** <https://forum.getkirby.com/t/filtering-pages-by-user/6716>\
**Category:** Questions\
**Tags:** v2\
**Created:** [February 22, 2017, 7:46pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716 "2017-02-22T19:46:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![grantpalin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/grantpalin/32/41_2.png) [@grantpalin](https://forum.getkirby.com/u/grantpalin)\
**Post date:** [February 22, 2017, 7:46pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/1 "2017-02-22T19:46:09Z")

</div>

I have a number of pages within the same parent, each of which has a user field to identify the user who created the page:

```
  user:
    label: User
    type: user
    required: true

```

These users are also in a specific role such that they can only create pages of this type, and no other. I’ve managed to get some permissions going to this end.

```
<?php
// https://getkirby.com/docs/panel/permissions
// https://getkirby.com/docs/panel/roles
return [
    'name' => 'Sales',
    'default' => false,
    'panel' => true,
    'permissions' => [
        '*' => true,
        'panel.access' => true,
        'panel.access.options' => false,
        'panel.access.users' => false,
        'panel.page.*' => false,
        'panel.page.create' => function() {
            return $this->target()->page()->template() === 'partners'; // allow creating children of the Partners node
        },
        'panel.page.read' => function() {
          return
            'partners' === $this->target()->page()->template()
            || (
              ('partner' === $this->target()->page()->template())
              && 'dshannon' === $this->username()
              );
        },
        'panel.page.update' => function() {
          return $this->target()->page()->template() === 'partner'; // allow creating children of the Partners node
        },
        'panel.widget.*' => false,
        'panel.widget.pages' => true,
        'panel.site.update' => false,
        'panel.user.*' => false,
    ]
];

```

What I’d like is to filter the pages list to only show those created by the currently logged in user. My attempt so far from the above is:

```
'panel.page.read' => function() {
    return
        'partners' === $this->target()->page()->template()
        || (
            ('partner' === $this->target()->page()->template())
                && ???? === $this->username()
            );
},

```

The first portion of the conditional allows the user to access the parent page in the panel. The second portion, the first part of it, shows the pages within the parent. Fine. The last portion is where I attempt filtering by the current username.

[https://getkirby.com/docs/cheatsheet/permissions/panel-page-read](https://getkirby.com/docs/cheatsheet/permissions/panel-page-read) Is pretty brief and only gives an example using the template field. I’ve tried doing `$this->target()->page()->user() === $this->username()` to no avail. Is this possible?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 22, 2017, 8:10pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/2 "2017-02-22T20:10:12Z")

</div>

Try

```auto
'panel.page.read' => function() {
    return
        'partners' === $this->target()->page()->template()
        || (
            ('partner' === $this->target()->page()->template())
                && $this->target()->data()['userfield'] === $this->username()
            );
   },

```

---

<div class="post-metadata">

**Author:** ![grantpalin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/grantpalin/32/41_2.png) [@grantpalin](https://forum.getkirby.com/u/grantpalin)\
**Post date:** [February 23, 2017, 6:28pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/3 "2017-02-23T18:28:49Z")

</div>

```
'panel.page.read' => function() {
    if ('partners' === $this->target()->page()->template()) return true;

    return 'partner' === $this->target()->page()->template() && $this->target()->data()['userfield'] === $this->username();
},

```

No workie unfortunately. I tried hardcoding a known user name in place of `$this->username()`, but still no pages show up. Conversely if I remove the whole right half of the final return statement, all pages do appear.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 23, 2017, 8:19pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/4 "2017-02-23T20:19:04Z")

</div>

Maybe it should be

```auto
$this->site()->user()->username()

```

The docs might be wrong here, I’m not sure, not tested.

---

<div class="post-metadata">

**Author:** ![grantpalin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/grantpalin/32/41_2.png) [@grantpalin](https://forum.getkirby.com/u/grantpalin)\
**Post date:** [February 23, 2017, 9:56pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/5 "2017-02-23T21:56:15Z")

</div>

Just tried that, still nothing. `$this->username` may be correct as I’ve seen it mentioned more than once ([1](https://getkirby.com/docs/developer-guide/advanced/hooks), [2](https://getkirby.com/docs/panel/permissions)). Replacing that code altogether with a known username (for which there should be at least one result) still produced no result. Starting to seem to me that the issue is with how to reference a field within a page, if that’s even possible. I haven’t yet been able to find any list of what data should be available via `$this->target->data()['...']`.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 23, 2017, 11:08pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/6 "2017-02-23T23:08:52Z")

</div>

I did a test and this should work:

```auto
<?php
// https://getkirby.com/docs/panel/permissions
// https://getkirby.com/docs/panel/roles
return [
    'name' => 'Editor',
    'default' => false,
    'panel' => true,
    'permissions' => [
        '*' => true,
        'panel.access' => true,
        'panel.access.options' => false,
        'panel.access.users' => false,
        'panel.page.*' => false,
        'panel.page.create' => function() {
            return $this->target()->page()->template() === 'partners'; // allow creating children of the Partners node
        },
        'panel.page.read' => function() {
          return $this->target()->page()->template() == 'partners' || $this->target()->page()->template() == 'partner' && $this->target()->page()->userfield() == $this->username();

        },
        'panel.page.update' => function() {
          return $this->target()->page()->template() === 'partner'; // allow creating children of the Partners node
        },
        'panel.widget.*' => false,
        'panel.widget.pages' => true,
        'panel.site.update' => false,
        'panel.user.*' => false,
    ]
];

```

---

<div class="post-metadata">

**Author:** ![grantpalin](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/grantpalin/32/41_2.png) [@grantpalin](https://forum.getkirby.com/u/grantpalin)\
**Post date:** [March 1, 2017, 5:39pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/7 "2017-03-01T17:39:01Z")

</div>

Still no good. I’ve poked and prodded at this permission, but I can still only get an all-or-nothing result, not just the few that there should be per user.

Appreciate the help, but I can live with keeping this permission open seeing as I can still lock down other permissions.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 1, 2017, 8:00pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/8 "2017-03-01T20:00:18Z")

</div>

Hm, I did in fact test this in a Starterkit before I ventured out with this permission. But I’m only human…

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 4:52pm UTC](https://forum.getkirby.com/t/filtering-pages-by-user/6716/9 "2025-02-06T16:52:33Z")

</div>


