|
9 | 9 | - **ABAC**: Model-level attribute filtering with JSON rules applied via global scopes |
10 | 10 | - **Automatic Data Filtering**: Global scopes filter data based on user's authorized organization nodes |
11 | 11 | - **Middleware**: Route-level permission, role, and organization scope protection |
12 | | -- **Policies**: Laravel Gate integration with OrganizationNode and Role policies |
| 12 | +- **Gate Integration**: `Gate::before` callback for Laravel authorization integration |
13 | 13 | - **Cache**: Configurable caching with automatic invalidation via observers |
14 | 14 | - **Super Admin**: Bypass all permission checks with configurable column |
15 | 15 | - **Events**: Lifecycle events for roles and permissions |
@@ -512,8 +512,8 @@ public static function getABACRules(): array |
512 | 512 | // Access the related organization node |
513 | 513 | $orgNode = $school->relatedAAuthOrganizationNode(); |
514 | 514 |
|
515 | | -// Get all records without organization scope filtering |
516 | | -$all = School::allWithoutAAuthOrganizationNodeScope(); |
| 515 | +// Get all records without organization scope filtering (instance method) |
| 516 | +$all = (new School)->allWithoutAAuthOrganizationNodeScope(); |
517 | 517 | </code-snippet> |
518 | 518 | @endverbatim |
519 | 519 |
|
@@ -567,64 +567,47 @@ public static function getABACRules(): array |
567 | 567 | </code-snippet> |
568 | 568 | @endverbatim |
569 | 569 |
|
570 | | -### Policies |
| 570 | +### Laravel Gate Integration |
571 | 571 |
|
572 | | -AAuth registers Laravel Gate policies for `OrganizationNode` and `Role` models via `Gate::policy()` in the service provider. A `Gate::before` callback also integrates AAuth with Laravel's built-in authorization and checks super admin status. |
573 | | - |
574 | | -**OrganizationNodePolicy** (`src/Policies/OrganizationNodePolicy.php`): |
| 572 | +AAuth integrates with Laravel's built-in authorization via a `Gate::before` callback registered in the service provider: |
575 | 573 |
|
576 | 574 | @verbatim |
577 | | -<code-snippet name="OrganizationNodePolicy" lang="php"> |
578 | | -// viewAny: requires 'view_organization_nodes' permission |
579 | | -// view: requires 'view_organization_nodes' + verifies node is accessible via AAuth::organizationNode($id) |
580 | | -// create: requires 'create_organization_nodes' permission |
581 | | -// update: requires 'update_organization_nodes' + verifies node accessibility |
582 | | -// delete: requires 'delete_organization_nodes' + verifies node accessibility |
583 | | - |
584 | | -// Usage with Laravel Gate: |
585 | | -Gate::allows('viewAny', OrganizationNode::class); |
586 | | -Gate::allows('view', $organizationNode); |
587 | | -Gate::allows('update', $organizationNode); |
588 | | -</code-snippet> |
589 | | -@endverbatim |
| 575 | +<code-snippet name="Gate::before Integration" lang="php"> |
| 576 | +// Registered in AAuthServiceProvider::boot() |
| 577 | +Gate::before(function ($user, $ability, $arguments = []) { |
| 578 | + $aauth = app('aauth'); |
| 579 | + |
| 580 | + // Super admin bypasses all permission checks |
| 581 | + if ($aauth->isSuperAdmin()) { |
| 582 | + return true; |
| 583 | + } |
590 | 584 |
|
591 | | -**RolePolicy** (`src/Policies/RolePolicy.php`): |
| 585 | + // Delegate to AAuth::can() for all Gate checks |
| 586 | + return $aauth->can($ability, ...$arguments) ?: null; |
| 587 | +}); |
592 | 588 |
|
593 | | -@verbatim |
594 | | -<code-snippet name="RolePolicy" lang="php"> |
595 | | -// viewAny: requires 'view_roles' permission |
596 | | -// view: requires 'view_roles' + for organization roles, checks scope access |
597 | | -// create: requires 'create_roles' permission |
598 | | -// update: requires 'update_roles' + for organization roles, checks scope access |
599 | | -// delete: requires 'delete_roles' + checks $role->deletable + for org roles, checks scope access |
600 | | - |
601 | | -// Organization role scope check: iterates user's organization nodes |
602 | | -// and verifies at least one node matches the role's organization_scope_id. |
603 | | -// System roles always pass the scope check. |
604 | | - |
605 | | -// Usage with Laravel Gate: |
606 | | -Gate::allows('update', $role); |
607 | | -Gate::allows('delete', $role); // also checks deletable attribute |
| 589 | +// This means standard Laravel Gate/Policy checks work with AAuth: |
| 590 | +Gate::allows('edit_something'); |
| 591 | +$user->can('edit_something'); |
| 592 | +@can('edit_something') ... @endcan |
608 | 593 | </code-snippet> |
609 | 594 | @endverbatim |
610 | 595 |
|
611 | | -**Gate::before callback**: The service provider registers a `Gate::before` that checks `isSuperAdmin()` (bypasses all checks if true) and then delegates to `AAuth::can()` for all Gate checks. |
612 | | - |
613 | 596 | ### Events |
614 | 597 |
|
615 | 598 | AAuth dispatches events for role and permission lifecycle: |
616 | 599 |
|
617 | 600 | @verbatim |
618 | 601 | <code-snippet name="Available Events" lang="php"> |
619 | | -use AuroraWebSoftware\AAuth\Events\RoleCreatedEvent; |
620 | | -use AuroraWebSoftware\AAuth\Events\RoleUpdatedEvent; |
621 | | -use AuroraWebSoftware\AAuth\Events\RoleDeletedEvent; |
622 | | -use AuroraWebSoftware\AAuth\Events\RoleAssignedEvent; |
623 | | -use AuroraWebSoftware\AAuth\Events\RoleRemovedEvent; |
624 | | -use AuroraWebSoftware\AAuth\Events\RoleSwitchedEvent; |
625 | | -use AuroraWebSoftware\AAuth\Events\PermissionAddedEvent; |
626 | | -use AuroraWebSoftware\AAuth\Events\PermissionUpdatedEvent; |
627 | | -use AuroraWebSoftware\AAuth\Events\PermissionRemovedEvent; |
| 602 | +use AuroraWebSoftware\AAuth\Events\RoleCreatedEvent; // Role $role |
| 603 | +use AuroraWebSoftware\AAuth\Events\RoleUpdatedEvent; // Role $role |
| 604 | +use AuroraWebSoftware\AAuth\Events\RoleDeletedEvent; // Role $role |
| 605 | +use AuroraWebSoftware\AAuth\Events\RoleAssignedEvent; // int $userId, Role $role, ?OrganizationNode $organizationNode |
| 606 | +use AuroraWebSoftware\AAuth\Events\RoleRemovedEvent; // int $userId, Role $role, ?OrganizationNode $organizationNode |
| 607 | +use AuroraWebSoftware\AAuth\Events\RoleSwitchedEvent; // int $userId, Role $newRole, ?Role $oldRole, ?OrganizationNode $organizationNode |
| 608 | +use AuroraWebSoftware\AAuth\Events\PermissionAddedEvent; // Role $role, string $permission, ?array $parameters |
| 609 | +use AuroraWebSoftware\AAuth\Events\PermissionUpdatedEvent; // Role $role, string $permission, ?array $parameters, ?array $oldParameters |
| 610 | +use AuroraWebSoftware\AAuth\Events\PermissionRemovedEvent; // Role $role, string $permission |
628 | 611 |
|
629 | 612 | // Listen to events in your EventServiceProvider or listener classes |
630 | 613 | </code-snippet> |
|
0 commit comments