[SOLVED] Restrict out based on boolean


I have a collection with a “visible” boolean switch. I would like to filter the output to only show entries with visible=true.

I know this can be achieved using php code in the blue read permission section.
But I don’t how to achieve this.

Maybe something like this

if( ! $item['visible']) {
    return false;

Thank you in advance.

Why don’t you just use the API in order to filter out entries? Thats what it is made for.

If you want to do it by the read permission try something like

if (isset($context->user) && $context->user['group'] !== 'admin') {
    $context->options['filter']['visible'] = true;

Thank you for the quick response.

The reason why I want to filter server-side is as to not leak unannoucent content.
If I just used https://cms/endpoint?filter[visible]=true they could also request the endpoint without the filter.

I tried to create an token with this get variable set but I don’t think this works or is supposed to.

you could also create a custom route:

create a php file in: /config/api/<your route name here>.php

with content like this:

    return cockpit('collections')->find('collection_name', [
        'filter' => function($doc) { // make only visible entries being returned
            return $doc['visible'];

then you can call http://YOUR_COCKPIT_BACKEND_URL/api/<your route name here>