Query regarding hasSystemRole() and custom roles #5450

Open
opened 2026-02-05 10:04:34 +03:00 by OVERLORD · 1 comment
Owner

Originally created by @FideliusFalcon on GitHub (Oct 8, 2025).

Attempted Debugging

  • I have read the debugging page

Searched GitHub Issues

  • 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

Originally created by @FideliusFalcon on GitHub (Oct 8, 2025). ### 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 ```text ``` ### Hosting Environment Docker image: linuxserver/bookstack:25.07.20250804
OVERLORD added the 🐕 Support label 2026-02-05 10:04:34 +03:00
Author
Owner

@ssddanbrown commented on GitHub (Oct 11, 2025):

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 commented on GitHub (Oct 11, 2025): 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: ```blade {-- 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.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/BookStack#5450