1 Roles and Routing
stak administrator edited this page 2026-09-10 11:43:32 +00:00

Roles and Routing

The roles

Defined centrally in config/roles.php:

Role Meaning
pending Default role after self-registration. No access to any restricted area beyond a placeholder dashboard message.
role1, role2, role3 The three application-level roles, for whatever business logic you need.
admin Full access, including /admin/users (user management).

To add a new role, add it in two places:

  1. config/roles.php — so it shows up in the admin dropdowns.
  2. sql/database.sql (or an ALTER TABLE if the table already exists) — the role column is a MySQL ENUM, so the new value must be added there too:
    ALTER TABLE user
        MODIFY role ENUM('pending', 'role1', 'role2', 'role3', 'admin', 'role4')
        NOT NULL DEFAULT 'pending';
    

The routing table

config/routes.php is a single array mapping a URL path to a page file and a list of allowed roles:

'/dashboard' => [
    'file'  => 'pages/dashboard.php',
    'roles' => ['AUTH'],
],

Special role values

Value Effect
['ALL'] Public page. No login required at all.
['AUTH'] Login required, but any role is accepted.
['role1'], ['admin'], ... Login required, and the user's role must be in the list.
['role1', 'role2'] Login required; either role1 or role2 is accepted.

How the check is applied

In app/router.php:

$isPublic = in_array('ALL', $allowedRoles, true);

if (!$isPublic) {
    if (!isLoggedIn()) {
        redirectTo('/login');
    }
    if (!hasRole($allowedRoles)) {
        http_response_code(403);
        renderError('403 - Access denied');
        return;
    }
}

hasRole() (in app/auth.php) treats 'AUTH' as "any role is fine" and otherwise checks the session's role value against the allowed list.

Current routes

Path File Roles
/ pages/home.php ALL
/login pages/login.php ALL
/register pages/register.php ALL
/logout pages/logout.php ALL
/profile pages/profile.php AUTH
/dashboard pages/dashboard.php AUTH
/role1-area pages/role1_area.php role1
/admin/users pages/admin_users.php admin
/admin/users/new pages/admin_user_new.php admin
/admin/users/edit pages/admin_user_edit.php admin
/admin/users/delete pages/admin_user_delete.php admin

See Adding a New Page for how to add another entry.