### Run Installation Commands for Laravel 10 & 11 Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt These commands set up necessary database tables for queue batches and notifications, publish Filament actions migrations, and media library migrations. Finally, it installs the user roles permissions package and compiles Filament assets. ```bash # For Laravel 11 php artisan make:queue-batches-table php artisan make:notifications-table # For Laravel 10 php artisan queue:batches-table php artisan notifications:table # For both versions php artisan vendor:publish --tag=filament-actions-migrations php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" php artisan users-roles-permissions:install php artisan filament:assets ``` -------------------------------- ### Install Filament Users Roles Permissions Package Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Installs the Filament Users Roles Permissions package using Composer. This is the initial step to integrate the package into your Laravel project. ```shell composer require cwsps154/users-roles-permissions ``` -------------------------------- ### Customize User Resource in Filament Panel Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt This PHP code shows how to customize the User resource provided by the Filament Users Roles Permissions package. By extending the core resource, you can add your own customizations. The example demonstrates how to register your custom UserResource within the Filament panel configuration. ```php plugins([ UsersRolesPermissionsPlugin::make() ->setUserResource(UserResource::class) ]); ``` -------------------------------- ### Check User Online Status (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Provides examples for checking a user's online status, retrieving their last seen timestamp, and verifying if their account is active. This functionality relies on the User model and its associated methods like isOnline() and properties like last_seen and is_active. The last_seen property returns a Carbon datetime instance. ```php isOnline()) { echo "User is active"; } else { echo "User is offline"; } // Get last seen timestamp $lastSeen = $user->last_seen; // Carbon datetime instance echo "Last seen: " . $lastSeen->diffForHumans(); // Check if user account is active if ($user->is_active) { // User can access the system } else { // User account is disabled } ``` -------------------------------- ### Automatic Middleware Application (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Explains that the plugin automatically registers three middleware: HaveAccess, IsActive, and IsOnline. These are applied to all authenticated Filament routes without manual intervention. The description details the behavior of each middleware: HaveAccess checks route permissions, IsActive verifies account activity, and IsOnline tracks user status. ```php // The plugin automatically registers three middleware: // 1. HaveAccess - Checks route-based permissions // 2. IsActive - Verifies user account is active // 3. IsOnline - Tracks user activity and online status // These are applied to all authenticated Filament routes // No manual middleware registration required // Middleware behavior: // - HaveAccess: Checks if user has permission for current route, sends warning notification if denied // - IsActive: Redirects to login with error notification if account is inactive // - IsOnline: Updates last_seen timestamp and sets 2-minute cache for online status ``` -------------------------------- ### Run Laravel Migrations and Publish Assets Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Executes commands to create necessary database tables for queue batches, notifications, Filament actions, and Spatie MediaLibrary. It also publishes Filament assets. ```shell # for laravel 11 php artisan make:queue-batches-table php artisan make:notifications-table //ensure these queues and notifications migrates are published # for laravel 10 php artisan queue:batches-table php artisan notifications:table # for both php artisan vendor:publish --tag=filament-actions-migrations //publish filament import and export migrations php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-migrations" //publish spatie media provider php artisan users-roles-permissions:install php artisan filament:assets ``` -------------------------------- ### Manual Middleware Usage in Routes (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Illustrates how to manually apply middleware in Laravel routes. It shows the usage of IsActive and IsOnline middleware for a group of routes and HaveAccess middleware for a specific admin route. The HaveAccess middleware automatically checks permissions based on the route name. Dependencies include the CWSPS154 middleware classes. ```php group(function () { Route::get('/dashboard', [DashboardController::class, 'index']); Route::get('/profile', [ProfileController::class, 'show']); }); // HaveAccess middleware checks permissions automatically based on route name Route::middleware([HaveAccess::class])->group(function () { Route::get('/admin/users', [UserController::class, 'index']) ->name('admin.resources.users.index'); }); ``` -------------------------------- ### Access User Roles and Permissions (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Demonstrates how to retrieve and check user roles and permissions, manage role-permission associations, and create new roles. It utilizes the User and Role models from the CWSPS154 package. Dependencies include the App\Models\User, CWSPS154\UsersRolesPermissions\Models\Role, and CWSPS154\UsersRolesPermissions\Models\Permission classes. ```php role; // Check if user has specific role if ($user->role_id && $user->role->identifier === Role::ADMIN) { // User is an admin } // Get all permissions for a role $role = Role::with('permissions')->find(1); foreach ($role->permissions as $permission) { echo $permission->name; } // Check if role has all permissions if ($role->all_permission) { // This role has access to everything } // Get available permissions for role assignment $permissionOptions = Role::getPermissionOptions(); // Returns: ['permission_id' => 'Permission Name', ...] // Create new role with permissions $role = Role::create([ 'role' => 'Content Manager', 'identifier' => 'content-manager', 'all_permission' => false, 'is_active' => true, ]); $role->permissions()->attach(['permission-id-1', 'permission-id-2']); ``` -------------------------------- ### Working with Permission Model (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Details operations related to the Permission model, including retrieving permissions for a specific panel, creating hierarchical permissions (parent and child), fetching permissions with their relationships, and an overview of automatic permission caching. It uses the Permission model and Illuminate\Support\Facades\Cache. Cache keys follow a specific format and are automatically cleared on model changes. ```php where('status', true) ->get(); // Create hierarchical permissions $parentPermission = Permission::create([ 'name' => 'Product Management', 'identifier' => 'product', 'panel_ids' => ['admin'], 'route' => null, 'parent_id' => null, 'status' => true, ]); $childPermission = Permission::create([ 'name' => 'View Products', 'identifier' => 'view-product', 'panel_ids' => ['admin'], 'route' => 'resources.products.index', 'parent_id' => $parentPermission->id, 'status' => true, ]); // Get permission with parent/children relationships $permission = Permission::with('parent', 'children')->find($permissionId); $parent = $permission->parent; $children = $permission->children; // Permission caching (automatic) // Cache key format: permissions_for_panel_{identifier}_{panel_id} // Cache cleared automatically on create/update/delete $cacheKey = Permission::$cacheKeyPrefix . '_view-product_admin'; $cachedPermission = Cache::get($cacheKey); ``` -------------------------------- ### Implement User Model Interfaces Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Ensures your `User` model implements the required interfaces for Filament and Spatie MediaLibrary. These interfaces are `HasMedia`, `HasAvatar`, and `FilamentUser`. ```php use Spatie\MediaLibrary\HasMedia; use Filament\Models\Contracts\HasAvatar; use Filament\Models\Contracts\FilamentUser; class User extends Authenticatable { // ... other uses and traits implements HasMedia, HasAvatar, FilamentUser // ... rest of your User model ``` -------------------------------- ### Check User Permissions with Laravel Gates (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Demonstrates how to check user permissions using Laravel's Gate facade. It shows how to check for a single permission ('view-blog') and multiple permissions, where access is granted if the user has any of the specified permissions ('create-blog', 'edit-blog'). The `HAVE_ACCESS_GATE` constant is used for checking permissions. ```php plugins([ UsersRolesPermissionsPlugin::make() ->canViewAnyUser(true) ->canCreateUser(false) ->canEditUser(true) ->canDeleteUser(false) ]); // Using gate ability with permission identifier $panel->plugins([ UsersRolesPermissionsPlugin::make() ->canViewAnyUser( UsersRolesPermissionsServiceProvider::HAVE_ACCESS_GATE, 'view-user' ) ->canCreateUser( UsersRolesPermissionsServiceProvider::HAVE_ACCESS_GATE, 'create-user' ) ]); // Using closure for custom logic $panel->plugins([ UsersRolesPermissionsPlugin::make() ->canViewAnyUser(function () { return auth()->user()->email === 'admin@example.com'; }) ->canDeleteUser(function () { return auth()->user()->role->identifier === 'super-admin'; }) ]); ``` -------------------------------- ### Configure Custom Permissions (PHP) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Defines custom permissions for different sections of the application, such as 'blog' management. It allows for hierarchical permissions with child permissions like 'view-blog', 'create-blog', etc. This configuration is typically stored in `config/cwsps-permissions.php` and integrates with Filament panels. ```php hasPlugin(UsersRolesPermissionsServiceProvider::$name)) { $panel_ids[] = $panel->getId(); } } return [ 'blog' => [ 'name' => 'Blog Management', 'panel_ids' => $panel_ids, 'route' => null, 'status' => true, 'children' => [ 'view-blog' => [ 'name' => 'View Blog Posts', 'panel_ids' => $panel_ids, 'route' => 'resources.blog-posts.index', 'status' => true, ], 'create-blog' => [ 'name' => 'Create Blog Post', 'panel_ids' => $panel_ids, 'route' => null, 'status' => true, ], 'edit-blog' => [ 'name' => 'Edit Blog Post', 'panel_ids' => $panel_ids, 'route' => null, 'status' => true, ], 'delete-blog' => [ 'name' => 'Delete Blog Post', 'panel_ids' => $panel_ids, 'route' => null, 'status' => true, ], ], ], ]; ``` -------------------------------- ### Publish Language Files Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Publishes language files for the project, including translations for fields like 'mobile' used by `propaganistas/laravel-phone`. This ensures proper localization. ```shell php artisan lang:publish ``` -------------------------------- ### Configure User Model for Filament Package Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt This PHP code snippet shows how to configure your User model to work with the Filament Users Roles Permissions package. It requires extending the Authenticatable class, implementing HasMedia, HasAvatar, and FilamentUser interfaces, and using the HasRole trait. It also defines fillable properties, hidden fields, and casts for relevant attributes. ```php 'datetime', 'password' => 'hashed', 'last_seen' => 'datetime', 'is_active' => 'boolean', ]; } } ``` -------------------------------- ### Configure User Model Fillable, Hidden, and Casts Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Defines the `$fillable`, `$hidden`, and `$casts` properties for the `User` model. These are standard Laravel Eloquent model configurations essential for security and data handling. ```php /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ 'name', 'email', 'mobile', 'password', 'role_id', 'last_seen', 'is_active' ]; /** * The attributes that should be hidden for serialization. * * @var array */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', 'last_seen' => 'datetime', 'is_active' => 'boolean', ]; } ``` -------------------------------- ### Use Permissions in Blade Templates (Blade) Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt Illustrates how to use the `@can` directive in Blade templates to conditionally display content based on user permissions. It shows checking for a single permission ('create-blog') and multiple permissions ('edit-blog', 'delete-blog'), where the content is shown if any of the listed permissions are held by the user. ```blade @can(\CWSPS154\UsersRolesPermissions\UsersRolesPermissionsServiceProvider::HAVE_ACCESS_GATE, 'create-blog') Create New Post @endcan @can(\CWSPS154\UsersRolesPermissions\UsersRolesPermissionsServiceProvider::HAVE_ACCESS_GATE, ['edit-blog', 'delete-blog']) @endcan ``` -------------------------------- ### Integrate UsersRolesPermissionsPlugin into Filament PanelProvider Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Adds the UsersRolesPermissionsPlugin to your Filament PanelProvider. This enables the package's functionality within your Filament application. It requires the `databaseNotifications` and `databaseTransactions` methods to be called beforehand if you intend to use export files and optional transactions respectively. ```php use CWSPS154\UsersRolesPermissions\UsersRolesPermissionsPlugin; $panel->databaseNotifications() //need to see the export files for the permission ->databaseTransactions() //optional ->plugins([ UsersRolesPermissionsPlugin::make() ]); //required to enable this extension ``` -------------------------------- ### Register UsersRolesPermissionsPlugin in Filament Panel Source: https://context7.com/cwsps154/users-roles-permissions/llms.txt This PHP code demonstrates how to register the UsersRolesPermissionsPlugin within your Filament Panel configuration. It's essential for enabling the package's features within your Filament admin panel. The plugin requires the 'databaseNotifications' middleware to be enabled. ```php default() ->id('admin') ->path('admin') ->databaseNotifications() // Required for export notifications ->databaseTransactions() // Optional ->plugins([ UsersRolesPermissionsPlugin::make() ]); } } ``` -------------------------------- ### Customize UserResource in Filament PanelProvider Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Allows you to specify a custom UserResource for the plugin within your Filament PanelProvider. This is useful if you have a custom UserResource that extends the core one. ```php use CWSPS154\UsersRolesPermissions\UsersRolesPermissionsPlugin; use App\Filament\Resources\UserResource; // Assuming your custom UserResource is here $panel->plugins([ UsersRolesPermissionsPlugin::make()->setUserResource(UserResource::class) ]); ``` -------------------------------- ### Apply HasRole Trait to User Model Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Applies the `HasRole` trait to your `User` model. This trait provides the necessary methods for role management within the package. ```php use CWSPS154\UsersRolesPermissions\Models\HasRole; class User extends Authenticatable { use HasRole; // ... rest of your User model ``` -------------------------------- ### Add Mobile Field Translation Source: https://github.com/cwsps154/users-roles-permissions/blob/main/README.md Adds a translation for the 'phone' attribute within your language files. This is necessary if you are using the `propaganistas/laravel-phone` package and need to provide user-friendly error messages for the mobile field. ```php 'phone' => 'The :attribute field must be a valid number.', ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.