Skip to main content

Role Service

The Role service provides CRUD, hierarchy/path maintenance, rule sync, and tree listings for Rolix roles. Use the service class or the Facade.

Namespace

JobMetric\Rolix\Services\Role

Facade

use JobMetric\Rolix\Facades\Role;

Basic CRUD Operations

Inherited from AbstractCrudService (Package Core). Responses use the standard Package Core Response object (ok, message, data, …).

Store

$response = Role::store([
'type' => 'system',
'name' => 'Editor',
'description' => 'Can edit content',
'allow' => ['post.view', 'post.edit'],
'deny' => ['post.delete'],
'parent_id' => null,
'is_default' => false,
'is_super' => false,
'ordering' => 10,
'rules' => [
[
'driver' => 'time',
'payload' => ['from' => '09:00', 'to' => '18:00'],
],
],
]);

$role = $response->data; // RoleResource / model payload

Validation runs through StoreRoleRequest.

Show

$response = Role::show($id);
$response = Role::show($id, ['parent', 'children', 'rules', 'paths']);

Update

$response = Role::update($id, [
'allow' => ['post.view', 'post.edit', 'post.publish'],
'parent_id' => $newParentId,
]);

Uses UpdateRoleRequest.

Destroy

$response = Role::destroy($id);

Notes:

  • Super roles cannot be deleted (RoleIsSuperException)
  • Children are detached (parent_id nullified) and their paths rebuilt
  • Bumps global roles cache version and writes activity log

Query Helpers

Role::paginate($filters);
Role::all($filters);
Role::query(); // underlying query builder helpers from AbstractCrudService

Hierarchy & Paths

Hierarchical types (config / registry hierarchical => true) store ancestry in role_paths.

rebuildPaths()

public function rebuildPaths(Role $role): void

Rebuilds path rows for a role after structural changes. Called internally after store/update/destroy when needed.

syncRules()

public function syncRules(Role $role, ?array $rules): void

Replaces role_rules for the role. Each item:

[
'driver' => 'weekday',
'payload' => ['days' => [1, 2, 3, 4, 5]],
]

driver must be registered in RuleEvaluatorRegistry.

Tree APIs

flatTree()

OpenCart-style flat list with depth and path label.

public function flatTree(string $type, ?int $rootId = null): Collection
$nodes = Role::flatTree('system');
$subtree = Role::flatTree('system', $rootId);

foreach ($nodes as $role) {
// $role->depth, $role->path_label (when prepared by service)
}

nestedTree()

Nested array of roles with children.

public function nestedTree(string $type, ?int $rootId = null): array
$tree = Role::nestedTree('tenant');

Both methods call RoleTypeRegistry::ensure($type).

Side Effects

OperationSide effects
store / update / destroyActivityLogger, PermissionCache::bumpRoles(), DomainEvents
hierarchy changepath rebuild
rules in payloadsyncRules()