BookStack icon indicating copy to clipboard operation
BookStack copied to clipboard

Query regarding hasSystemRole() and custom roles

Open FideliusFalcon opened this issue 3 months ago • 1 comments

Attempted Debugging

  • [x] I have read the debugging page

Searched GitHub Issues

  • [x] I have searched GitHub for the issue.

Describe the Scenario

Hi,

I'm working on a custom theme and trying to show specific navigation links based on user roles.

I've found that hasSystemRole() works perfectly for the default admin role, but it consistently fails for other roles, including the default 'Editor' and any custom roles I create.

Here is a snippet of my test code from a custom header file, which demonstrates the issue:

{{-- This link displays correctly when the user is an admin --}}
@if(auth()->user()->hasSystemRole('admin'))
    <li>
        <a href="#" role="menuitem" class="icon-item">
            @icon('lock') <div>Admin Link</div>
        </a>
    </li>
@endif

{{-- This link does NOT display, even when the user has the 'Editor' role --}}
@if(auth()->user()->hasSystemRole('editor'))
    <li>
        <a href="#" role="menuitem" class="icon-item">
            @icon('edit') <div>Editor Link</div>
        </a>
    </li>
@endif

{{-- This link also does NOT display for my custom 'Test' role --}}
@if(auth()->user()->hasSystemRole('test'))
    <li>
        <a href="#" role="menuitem" class="icon-item">
            @icon('info') <div>Test Link</div>
        </a>
    </li>
@endif

My question is: Is the hasSystemRole() method intended to be used with roles other than admin? Given that it works for admin, I'm trying to understand what might be preventing it from recognizing other default and custom roles.

Thanks for your great work on BookStack!

Exact BookStack Version

v25.07.20250804

Log Content


Hosting Environment

Docker image: linuxserver/bookstack:25.07.20250804

FideliusFalcon avatar Oct 08 '25 08:10 FideliusFalcon

Hi @FideliusFalcon,

My question is: Is the hasSystemRole() method intended to be used with roles other than admin?

It's only meant for a couple of in-build roles which have special capabilities (admin and the guest/public role). Otherwise, all other roles are treated generically like any other, and are identified by id instead of string.

The equivalent for your @if statements would be:

{-- Add this towards the top of your template --}
@php
$roleMap = [
    'admin'  => 1,
    'editor' => 2,
    'test'   => 5,
];
@endphp

{-- The if statements --}
@if(user()->hasRole($roleMap['test']))

You'll need to configure that $roleMap to your instance. You could just use the ids directly in ->hasRole() but I thought the map would be nicer. It's also possible to dynamically query the ids by name, but I'd advise against that since names are not treated as unique.

I've used user() instead of auth()->user(). user() is a BookStack specific function, which will always return a user (returning the guest default if not logged in) wheras auth()->user() could return null if no authed user session is active.

ssddanbrown avatar Oct 11 '25 13:10 ssddanbrown