Skip to main content

PermissionManager

Loads and serves permission catalogs from PHP files, grouped by context and optional model scope.

Namespace

JobMetric\Rolix\Services\PermissionManager

Facade

use JobMetric\Rolix\Facades\Permission;

Container binding: rolix.permission.

Constant

PermissionManager::SYSTEM_MODEL_KEY; // '__system__'

Used internally when $model is null (system-wide permissions).

File Format

Each permission file returns an associative array of permission key → translation key:

<?php

return [
'view' => 'permissions/hero/view',
'edit' => 'permissions/hero/edit',
'settings.open' => 'permissions/hero/settings/open',
];

Boot Loading

On application boot, Rolix:

  1. Loads every config/permissions/*.php (basename = context)
  2. Dispatches RegisterPathPermissionEvent
  3. Registers paths from listeners via addPermissionFile()

Methods

addPermissionFile()

public function addPermissionFile(
string $context,
string $path,
?string $model = null
): void
ParameterDescription
$contextContext name (e.g. hero)
$pathAbsolute path to PHP file
$modelFQCN for model-scoped catalog, or null for system

Throws: InvalidArgumentException if file missing, not an array, or entries are not strings.

Permission::addPermissionFile('hero', resource_path('permissions/hero.php'));
Permission::addPermissionFile(
'hero',
resource_path('permissions/hero-tenant.php'),
Tenant::class
);

getPermissions()

public function getPermissions(
?string $context = null,
string $view = 'assoc',
?string $model = null
): array
$viewResult
assoc (default)perm => lang (or nested by context)
flatlist of permission keys
langlist of lang keys
flat_langlist of {perm, lang} or context map
treenested tree (ignores $context, uses model scope)
Permission::getPermissions('hero');
Permission::getPermissions('hero', 'flat');
Permission::getPermissions(null, 'tree', Tenant::class);

hasPermission()

Whether a permission key exists in a context catalog (definition lookup — not user authorization).

public function hasPermission(
string $context,
string $permission,
?string $model = null
): bool
Permission::hasPermission('hero', 'view');

For user checks use HasRole::hasPermission().


getContextPermission()

public function getContextPermission(?string $model = null): array

Returns: list of context names for the model scope.


getFlatPermissions() / getLangPermissions() / getFlatLangPermissions() / getAssocPermissions()

Typed helpers used by getPermissions() views. Prefer the unified getPermissions() API unless you need a specific shape.


getPermissionTree()

Build a nested tree from dotted permission keys for a model scope.

public function getPermissionTree(?string $model = null): array

Returns: list of nodes:

[
[
'perm' => 'hero',
'lang' => '...',
'children' => [
['perm' => 'hero.view', 'lang' => '...', 'children' => []],
],
],
]

Parent of a.b.c is a.b when that key exists.


getPermissionTreeForType()

public function getPermissionTreeForType(string $type): array

Ensures the role type exists, then builds the tree for that type's bound model (null for system types).

Throws: RoleTypeNotFoundException

Permission::getPermissionTreeForType('system');
Permission::getPermissionTreeForType('tenant');