Show/Hide "Add" buttons by permissions

Permissions and “add”-buttons…

Hi,

I’m fiddling with the permissions and wonder if my conclusion is true.
I would like to remove the “Add” button on top of the (clubs) list for the “clubmanager” user(role).
I’ve set up my user-roles (yml).

My default (Kirby) admin user can basically manage everything.

The “Admin” will add the clubs.

Then the clubs are assigned to the users with user-role “clubmanager”.

Now when a clubmanager logs in I want the “Add” club button to be invisible since a clubmanager cannot add clubs this is done already by the “Admin”.

I updated my user(role) blueprint for “clubmanager” saying:

permissions:
  access:
    settings: false
    users: false
  pages:
    create: false

But this will remove the “Add” button AND all subsequent “Add” buttons in a selected clubs page for editing. We don’t want that because a “clubmanager” should be able to add “Courts”, “Trainers” etc. to the specific “Club”.

I’ve tried to get the “Add” buttons to display for the child pages in a club by adding page specific options for “Courts” and “Trainers” like:

options:
  create:
    clubmanager: true

But this doesn’t change anything the “Add” button to add “Courts” or “Trainers” to a “Club: do not appear.

So I’ve removed the “pages: create: false” for the “clubmanager” user(role) and the “Add” buttons are visible again for Club(s) and subsequent child pages (“Courts”, “Trainers”) when in editing a club.

Then I added this to the “Club” page (club.yml) to prevent the addition of Clubs by the “Clubmanager”:

options:
  create:
    clubmanager: false

This obviously works but… I still can see the “Add” button to add a Club (while being logged in as a “clubmanager”) but the ultimate “Save” doesn’t work so that’s fine, I cannot add clubs!

I conclude that it is not possible to hide the “Add” button on top of the “Clubs” list AND show all subsequent “Add” buttons of the clubs child pages (other than with css).

So Is it possible to remove the “Add” button for a specific user role and keep the “Add” buttons on child-pages intact?

Sorry for the long explanation, hope it’s clear.

Thanks in advance.

That the Add button still show is unfortunately a bug, I think there is an issue on GitHub somewhere. You could hide the button via CSS as a workaround.

Thanks texnixe,

Just what I thought! :grinning_face:

Have nice day

just to be complete the added css to hide the add-button based upon userrole “clubmanager”:

/*
  Kirby adds a `data-role` attribute under the <body> tag for the logged-in user.
  We can use this to target elements specifically for the clubmanager role.
*/
div[data-role="clubmanager"][data-id="clubs"] .k-section-header .k-button[aria-label="Add"] {
  display: none !important;
}

A more secure alternative to hiding the button (but more work) would be a PHP blueprint, where you could set the create option of a section to false conditionally.

Thanks for this solution texnixe. I’ll look into this. When I succeed I’ll post it here.
(:-

Ok, as a follow up I’ve implemented the solution of hiding the “Add” button on top of a pages list based upon user-role. Below the files I used to remove the Add button on top of my clubs list in the controlpanel.

programmable blueprint:

<?php
// site/blueprints/pages/clubs.php (UPDATED)
?>

<?php

// Default the create permission to false
$canCreate = false;

// If a user is logged in AND is an admin, allow them to create

if ($user = kirby()->user()) {
    if ($user->isAdmin()) {
        $canCreate = true;
    }
}

return [
    'title'   => 'Clubs Section',
    'icon'    => 'archive',
    'pages'   => [
        'template' => 'club'
    ],
    'columns' => [
        'main' => [
            'width' => '1/1',
            'sections' => [
                'clublist' => [
                    'headline' => 'Clubs',
                    'type'     => 'pages',
                    'template' => 'club',
                    'info'     => "Players: {{ page.children.filterBy('template', 'player').count }} | Lessons: {{ page.children.filterBy('template', 'lesson').count }}",
                    // The query filters the list of displayed pages by calling our page model.
                    'query'    => 'page.getVisibleClubs',
                    // The create permission is now a simple, reliable boolean.
                    // This will correctly show/hide the "Add" button.
                    'create'   => $canCreate
                ]
            ]
        ]
    ]
];

Register the programmable blueprint in the plugins index.php:

<?php
// site/plugins/padeltime/index.php (UPDATED)
//
// This file registers clubs blueprint.
//
?>
<?php
use Kirby\Cms\App as Kirby;

Kirby::plugin('myplugins/padeltime', [
    'panel' => [
        'js' => 'index.js',
    ],
    'blueprints' => [
        'pages/clubs' => function () {
            $blueprintPath = __DIR__ . '/../../blueprints/pages/clubs.php';
            if (file_exists($blueprintPath)) {
                return include $blueprintPath;
            }
        }
    ]
]);