Reference

Service and Facade API

On this page

BabeRuka\SystemRoles\Services\SystemRolesService#

The main entry point for checks. Static methods can be called directly. Instance methods are available through the container or the facade.

Static methods#

Method Returns Description
superAdminRoleId() int The configured super admin role id
isSuperAdminRole($roleId) bool Whether the id is the super admin role. 0 and negatives are false
roleIdForUser($userId) int The user's role id from user_roles.user_role, or 0
isSuperAdmin($user = null) bool Whether the user (default: the logged-in user) has the super admin role. false for guests
permissionsForRole($roleId) array `[in_name => 0
roleHas($roleId, string $permission) bool Whether the role has the permission switched on. Matches in_guard_name or in_name. Always true for the super admin
sessionKey(string $key) string $key with the configured session_prefix

Instance methods#

Method Returns Description
getUserRole(int $userId) ?UserRoles The user's user_roles row
updateUserRole(int $userId, array $data) ?UserRoles Fill and save the user's row. null if the user has none. Does not write history or refresh user_roles_in
createUserRole(array $data) UserRoles Create a user_roles row. Does not write history
role($roleId) ?SystemRoles Find a role
PHP
$service = app(\BabeRuka\SystemRoles\Services\SystemRolesService::class);
$service->getUserRole(42)?->role?->role_name;

To change a role with history and permission refresh, see Assigning Roles → Doing it in code.

SystemRoles facade#

BabeRuka\SystemRoles\Facades\SystemRoles resolves SystemRolesService from the container (binding systemroles). Every method above can be called through it:

PHP
use BabeRuka\SystemRoles\Facades\SystemRoles;

SystemRoles::isSuperAdmin();
SystemRoles::roleHas(3, 'invoice.approve');
SystemRoles::getUserRole(42);

With auto-discovery the alias SystemRoles is registered globally.

SystemClassesController (controller access checks)#

BabeRuka\SystemRoles\Http\Controllers\SystemRoles\SystemClassesController

Method Returns Description
checkSystemClassesInit($roleId, string $class) int 1 if the role may use the controller, else 0. Super admin always 1. $class may be short or fully qualified
checkAccess($roleId, string $class) true Same check; aborts with 403 when denied
chechAccess(...) true Deprecated alias of checkAccess
controllerInit() true Scan controllers into system_classes
addSystemClassIn($classId) int Create missing "revoke" rows for every role on one controller

SystemRolesController (session helpers)#

BabeRuka\SystemRoles\Http\Controllers\SystemRoles\SystemRolesController

Method Description
rolesArray($userId) Store role_id, role_name, role_guard_name, user_role in the session
userRole() Store and return ['role_id', 'role_name', 'role_guard_name'] for the logged-in user
userPermissions($roleId, $force = false) Store and return permissionsForRole() in the session. Uses the stored copy unless $force
checkRole() The logged-in user's role id
init_permissions($roleId) Fill in missing permission rows for a role. Returns how many were added
init_user_permissions($userId, $roleId) Rebuild a user's user_roles_in rows from a role
nextInSequence() (static) Next free in_sequence

Scanners#

Class Method Description
Services\SystemRouteScanner scanRoutes() Array of every route: uri, methods, name, action, middleware, prefix, controller
sync() Store routes in system_routes; returns ['created' => n, 'updated' => n]
Services\SystemClassesScanner scanControllers() Array of ['filename', 'namespace', 'classname'] for every PHP class in controller_directory

Middleware#

Alias Class Description
super_admin Http\Middleware\SuperAdminMiddleware Allows only the super admin. See Middleware
– Http\Middleware\SystemRolesAuthenticate Simple auth check (redirects to the login route). Not registered by default

Models#

All in BabeRuka\SystemRoles\Models. See Database Schema and Models.

SystemRoles, SystemRolesIn, UserRoles, UserRoleHistory, UserRolesIn, SystemClasses, SystemClassesIn, SystemRoutes, SystemRoutesIn, SystemMenus, SystemMenusIn, SystemMenuItems.