Appearance
Authorization Actions
The AuthorizeAction
enum defines standard actions used for authorization throughout Module Manager. These actions are used with the authorizeAction()
helper and PolicyChecker
facade to provide a type-safe way to check permissions.
Available Actions
php
use ModuleManager\ModuleManager\Enums\AuthorizeAction;
Action | Value | Description |
---|---|---|
VIEW_ANY | viewAny | View a list or collection of resources |
VIEW | view | View a single resource |
CREATE | create | Create a new resource |
UPDATE | update | Update an existing resource |
UPDATE_ANY | updateAny | Update multiple resources at once |
DELETE | delete | Delete a single resource |
DELETE_ANY | deleteAny | Delete multiple resources at once |
RESTORE | restore | Restore a soft-deleted resource |
FORCE_DELETE | forceDelete | Permanently delete a resource |
REORDER | reorder | Reorder resources in a collection |
Usage Examples
With authorizeAction() Helper
php
use ModuleManager\ModuleManager\Enums\AuthorizeAction;
use Modules\Blog\Models\Post;
// Check permission for a class
$canCreate = authorizeAction(AuthorizeAction::CREATE, Post::class);
// Check permission for a model instance
$post = Post::find(1);
$canUpdate = authorizeAction(AuthorizeAction::UPDATE, $post);
// Use in conditional logic
if (authorizeAction(AuthorizeAction::DELETE, $post)) {
// User is authorized to delete this post
$post->delete();
}
With PolicyChecker Facade
php
use ModuleManager\ModuleManager\Enums\AuthorizeAction;
use ModuleManager\ModuleManager\Facades\PolicyChecker;
use Modules\Blog\Models\Post;
// Basic check
$canView = PolicyChecker::from(AuthorizeAction::VIEW, $post)->validate();
// Multiple checks - ALL must pass (logical AND)
$canManage = PolicyChecker::from(AuthorizeAction::CREATE, Post::class)
->and(AuthorizeAction::UPDATE, $post)
->validate();
// Multiple checks - ANY should pass (logical OR)
$hasAccess = PolicyChecker::from(AuthorizeAction::VIEW, $post)
->or(AuthorizeAction::UPDATE, $post)
->anyOf()
->validate();
In Blade Templates
php
@authorizeAction(AuthorizeAction::CREATE, \Modules\Blog\Models\Post::class)
<button>Create Post</button>
@endauthorizeAction
{{-- Or with a model instance --}}
@authorizeAction(AuthorizeAction::UPDATE, $post)
<a href="{{ route('posts.edit', $post) }}">Edit Post</a>
@endauthorizeAction
Benefits
Using the AuthorizeAction
enum instead of string values provides several advantages:
- Type Safety - Compile-time checking prevents typos and errors
- Code Completion - IDE auto-completion shows available options
- Refactoring Support - Easy to find all uses of a particular action
- Standardization - Consistent naming across your application