Complete API documentation for AAuth package.
The main class for authorization operations.
public function __construct(?AAuthUserContract $user, ?int $roleId, ?string $panelId = null)Creates a new AAuth instance.
| Parameter | Type | Description |
|---|---|---|
$user |
AAuthUserContract|null |
The authenticated user |
$roleId |
int|null |
The role ID to use |
$panelId |
string|null |
Optional Filament panel ID |
Throws:
AuthenticationException- If user is nullMissingRoleException- If roleId is null or role not foundUserHasNoAssignedRoleException- If user doesn't have the specified role
Example:
$aauth = new AAuth(Auth::user(), Session::get('roleId'));public static function forPanel(AAuthUserContract $user, int $roleId, string $panelId): selfCreates AAuth instance for a specific Filament panel.
Example:
$aauth = AAuth::forPanel($user, 5, 'admin');public static function forCurrentPanel(AAuthUserContract $user, int $roleId): selfCreates AAuth instance with auto-detected Filament panel.
Example:
$aauth = AAuth::forCurrentPanel($user, 5);public static function detectCurrentPanelId(): ?stringDetects current Filament panel ID. Returns null if Filament is not installed or no panel is active.
public function can(string $permissionName, mixed ...$arguments): boolChecks if current role has the specified permission.
| Parameter | Type | Description |
|---|---|---|
$permissionName |
string |
Permission name to check |
$arguments |
mixed |
Optional arguments for parametrized permissions |
Example:
if (AAuth::can('edit.users')) {
// User has permission
}
// With parameters
if (AAuth::can('edit.users', ['max_count' => 10])) {
// User has parametrized permission
}public function canModel(string $permissionName, object $model): boolChecks permission with ABAC rules against a specific model instance.
Example:
$order = Order::find(1);
if (AAuth::canModel('view.order', $order)) {
// User can view this specific order
}public function passOrAbort(string $permissionName): voidChecks permission and aborts with 403 if not allowed.
Throws: HttpException with 403 status code
Example:
AAuth::passOrAbort('delete.users');
// If we get here, user has permissionpublic function permissions(): arrayReturns all permissions for the current role.
Returns: array of permission names
public function currentRole(): ?RoleReturns the current role model.
public function switchableRoles(): array|CollectionReturns all roles the user can switch to.
public function switchableRolesForPanel(string $panelId): CollectionReturns roles available for a specific panel.
public function switchableRolesForCurrentPanel(): CollectionReturns roles available for the current Filament panel.
public static function switchableRolesStatic(int $userId): array|CollectionStatic method to get switchable roles by user ID.
public static function switchableRolesForPanelStatic(int $userId, string $panelId): CollectionStatic method to get panel-specific roles by user ID.
public function getCurrentPanel(): ?stringReturns the current panel context (if set).
public function getPanelId(): ?stringReturns the role's panel_id from database.
public function isInPanel(string $panelId): boolChecks if currently in a specific panel.
Example:
if ($aauth->isInPanel('admin')) {
// We're in admin panel
}public function organizationNodes(bool $includeRootNode = false, ?string $modelType = null): CollectionReturns all accessible organization nodes.
| Parameter | Type | Description |
|---|---|---|
$includeRootNode |
bool |
Include root node in results |
$modelType |
string|null |
Filter by model type |
public function getAccessibleOrganizationNodes(
?int $minDepthFromRoot = null,
?int $maxDepthFromRoot = null,
?string $scopeName = null,
?int $scopeLevel = null,
bool $includeRootNode = false,
?string $modelType = null
): CollectionReturns organization nodes with depth and scope filtering.
| Parameter | Type | Description |
|---|---|---|
$minDepthFromRoot |
int|null |
Minimum depth (0-based) |
$maxDepthFromRoot |
int|null |
Maximum depth (0-based) |
$scopeName |
string|null |
Filter by scope name |
$scopeLevel |
int|null |
Filter by scope level |
$includeRootNode |
bool |
Include root node |
$modelType |
string|null |
Filter by model type |
Example:
// Get only level 1-2 nodes
$nodes = $aauth->getAccessibleOrganizationNodes(
minDepthFromRoot: 1,
maxDepthFromRoot: 2
);
// Get nodes with specific scope
$nodes = $aauth->getAccessibleOrganizationNodes(
scopeName: 'Region'
);public function organizationNodesQuery(bool $includeRootNode = false, ?string $modelType = null): BuilderReturns query builder for organization nodes (for custom queries).
public function organizationNode(int $nodeId, ?string $modelType = null): OrganizationNodeReturns a specific organization node if accessible.
Throws: InvalidOrganizationNodeException if not accessible
public function organizationNodeIds(): ?arrayReturns array of accessible organization node IDs.
public function descendant(int $rootNodeId, int $childNodeId): boolChecks if a node is descendant of another node.
public function ABACRules(string $modelType): ?arrayReturns ABAC rules for a specific model type.
public function loadAndCacheContext(): voidLoads and caches authorization context for the request.
public function clearContext(): voidClears the cached authorization context.
Service for managing roles and permissions.
public function createRole(array $data): RoleCreates a new role.
| Field | Type | Description |
|---|---|---|
name |
string |
Role name (min 3 chars) |
organization_scope_id |
int|null |
Scope ID (null for system roles) |
panel_id |
string|null |
Filament panel ID |
status |
string |
Role status |
public function updateRole(array $data, int $roleId): RoleUpdates an existing role.
public function attachPermissionToRole(string|array $permissionOrPermissions, int $roleId): boolAttaches permission(s) to a role.
public function detachPermissionFromRole(string|array $permissions, int $roleId): boolRemoves permission(s) from a role.
public function syncPermissionsOfRole(array $permissions, int $roleId): boolSyncs all permissions for a role (replaces existing).
public function attachSystemRoleToUser(array|int $roleIdOrIds, int $userId): arrayAttaches system role(s) to a user.
public function detachSystemRoleFromUser(array|int $roleIdOrIds, int $userId): intRemoves system role(s) from a user.
public function attachOrganizationRoleToUser(int $organizationNodeId, int $roleId, int $userId): boolAttaches organization role to user at specific node.
public function detachOrganizationRoleFromUser(int $userId, int $roleId, int $organizationNodeId): intRemoves organization role from user.
Service for managing organization structure.
public function createOrganizationScope(array $data): OrganizationScopeCreates a new organization scope.
public function createOrganizationNode(array $data): OrganizationNodeCreates a new organization node.
// Permission check
aauth_can(string $permission, ...$arguments): bool
// Create panel-aware instance (uses Auth::user() and Session::get('roleId') internally)
aauth_for_panel(?string $panelId = null): AAuth
// Get panel roles for current user
aauth_panel_roles(?string $panelId = null): Collection
// Check if in panel
aauth_in_panel(string $panelId): bool
// Get current panel ID
aauth_current_panel(): ?string{{-- Permission check --}}
@aauth_can('permission.name')
{{-- Content --}}
@endaauth_can
{{-- Panel context --}}
@panel('admin')
{{-- Admin panel content --}}
@endpanel
{{-- Panel permission check --}}
@aauth_panel_can('permission.name', 'admin')
{{-- Content --}}
@endaauth_panel_can| Exception | Description |
|---|---|
AuthorizationException |
Authorization failed |
InvalidOrganizationNodeException |
Invalid or inaccessible organization node |
InvalidOrganizationScopeException |
Invalid organization scope |
InvalidRoleException |
Invalid role |
InvalidRoleTypeException |
Invalid role type |
InvalidUserException |
Invalid user |
MissingRoleException |
Role not found |
OrganizationNodeAuthException |
Organization node authorization failed |
OrganizationScopesMismatchException |
Organization scopes mismatch |
UserHasNoAssignedRoleException |
User has no assigned role |