Permission
extends Service
in package
In-memory registry of permissions declared by services.
Tsugi.php calls assemble() in the same initialization that registers routes. Definitions are not scanned from disk and not stored in the database. has() answers from the logged-in user's grants, loaded once into the session cache.
Table of Contents
Constants
- GRANT_CACHE : mixed = 'permissions'
- Session cache slot for one user's active grants.
- GRANT_CACHE_SECONDS : mixed = 300
- How long a loaded grant set stays in the session, in seconds.
- SERVICES : array<int, Service>> = [\Tsugi\Services\Courses\CoursesService::class]
- Services whose permissions() are merged. Explicit, like the controller list in Tsugi.php.
Properties
- $registry : array<string, array<string, mixed>>|null
Methods
- assemble() : array<string, array<string, mixed>>
- Merge permissions() from the service list into one registry.
- cacheClear() : mixed
- Drop the cached grants for this session.
- cacheRefresh() : mixed
- Drop the cached grants and load the current user's grants again.
- has() : mixed
- True when the current user holds an active grant for a registered permission.
- permissions() : array<string, array<string, mixed>>
- Permission codes this service owns.
- registry() : array<string, array<string, mixed>>
- Registry installed by assemble() during startup.
- activeGrants() : array<int, array<string, true>>
- Active grants for $uid, keyed by scope id then permission code.
Constants
GRANT_CACHE
Session cache slot for one user's active grants.
private
mixed
GRANT_CACHE
= 'permissions'
GRANT_CACHE_SECONDS
How long a loaded grant set stays in the session, in seconds.
private
mixed
GRANT_CACHE_SECONDS
= 300
SERVICES
Services whose permissions() are merged. Explicit, like the controller list in Tsugi.php.
private
array<int, Service>>
SERVICES
= [\Tsugi\Services\Courses\CoursesService::class]
Properties
$registry
private
static array<string, array<string, mixed>>|null
$registry
= null
Methods
assemble()
Merge permissions() from the service list into one registry.
public
static assemble([array<int, Service>>|null $services = null ]) : array<string, array<string, mixed>>
Startup calls assemble() with no arguments and keeps that result. A caller can pass a list to merge that list instead; a passed list does not replace the startup registry. A repeated permission code aborts the merge.
Parameters
- $services : array<int, Service>>|null = null
Return values
array<string, array<string, mixed>>cacheClear()
Drop the cached grants for this session.
public
static cacheClear() : mixed
The next has() loads them again. Call this after a change that should be visible before the five minutes are up.
cacheRefresh()
Drop the cached grants and load the current user's grants again.
public
static cacheRefresh() : mixed
A logged-out session is only cleared. A missing connection leaves the cache empty so the next check can try again.
has()
True when the current user holds an active grant for a registered permission.
public
static has(string $permission[, int|null $scope_id = null ][, bool $fresh = false ]) : mixed
Unknown codes are denied. The scope defaults to the system row (scope_id 1). A grant is active when it is not revoked and not past expires_at. Site-admin bypass does not belong here.
The user's active grants are read once and kept in the session cache for five minutes. Login and logout clear that cache. cacheClear() drops it. cacheRefresh() drops it and loads the current user again. Pass $fresh to reload before answering. Display can use the cache. A write reloads, then checks.
Parameters
- $permission : string
- $scope_id : int|null = null
- $fresh : bool = false
-
Reload this user's grants before the check.
permissions()
Permission codes this service owns.
public
static permissions() : array<string, array<string, mixed>>
Keys are stable permission identifiers. Values describe the permission for application screens. Unknown codes are denied later, when a grant is checked.
Return values
array<string, array<string, mixed>>registry()
Registry installed by assemble() during startup.
public
static registry() : array<string, array<string, mixed>>
Return values
array<string, array<string, mixed>>activeGrants()
Active grants for $uid, keyed by scope id then permission code.
private
static activeGrants(int $uid) : array<int, array<string, true>>
A session hit is returned as stored. A miss loads every active grant for that user. A missing connection or a failed query is not stored, so the next check can try again.
Parameters
- $uid : int