Restrict content to the content authors

Add this code to the read permissions of your collection to restrict entries by owner:

<?php
if ($context->user) {
    $context->options['filter']['_by'] = $context->user['_id'];
}

Add this code to the read permissions of your collection to restrict entries by owner, except admins:

<?php
if ($context->user && $context->user['group'] != 'admin') {
    $context->options['filter']['_by'] = $context->user['_id'];
}

Filtering + Hiding fields for non-admins can be done this way:

<?php
if ($context->user && $context->user['group'] != 'admin') {
    // filter by admin-boolean
    $context->options['filter']['admin_boolean'] = false;
    // hide field for non-admin --> or use field acl instead
    $context->options['fields']['admin_boolean'] = false;
}

I tried to test the solution above, but I have some weird bugs with boolean fields right now. I have to check in the next days, if I broke my test cockpit or if it is a real bug…


edit: I tried it again with a fresh installation and it worked.

Instead of adding $context->options['fields']['admin_boolean'] = false; to the read permissions, you can add permissions in the context menu of your field to group “admin”. It has the same effect.

For some more permission options, you may have a look at

and

1 Like