kenepa / resource-lock
Filament Resource Lock is a Filament plugin that adds resource locking functionality to your site.
Installs: 49 658
Dependents: 2
Suggesters: 0
Security: 0
Stars: 91
Watchers: 6
Forks: 22
Open Issues: 7
Requires
- php: ^8.1
- filament/filament: ^3.0
- illuminate/contracts: ^9.0|^10.0|^11.0|^12.0
- spatie/laravel-package-tools: ^1.15.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.0|^8.1
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0|^8.0|^9.0|^10.0
- pestphp/pest: ^2.0|^3.7
- pestphp/pest-plugin-laravel: ^2.0|^3.1
- pestphp/pest-plugin-livewire: ^2.0|^3.0
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0|^2.0
- phpstan/phpstan-phpunit: ^1.0|^2.0
- spatie/laravel-ray: ^1.26
- tightenco/duster: ^1.1|^3.1
README

Filament Resource Lock is a Filament plugin that adds resource locking functionality to your site. When a user begins editing a resource, Filament Resource Lock automatically locks the resource to prevent other users from editing it at the same time. The resource will be automatically unlocked after a set period of time, or when the user saves or discards their changes.
Installation
Plugin Version | Filament Version | PHP Version |
---|---|---|
1.x | 2.x | > 8.0 |
2.x | 3.x | > 8.1 |
3.x | 3.x | > 8.1 |
You can install the package via composer:
composer require kenepa/resource-lock
Then run the installation command to publish and run migration(s)
php artisan resource-lock:install
Register plugin with a panel
use Kenepa\ResourceLock\ResourceLockPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->plugin(ResourceLockPlugin::make()); }
Upgrade guides
Upgrade to 2.x
Notice - Upgrading to Version 2.1.x :
In case you have published the config, make sure to update the following in your config:'resource' => [ 'class' => \Kenepa\ResourceLock\Resources\LockResource::class, ],
Upgrade from 2.x to 3.x
Breaking Changes
- Plugin-based Customization: Icons, labels, model classes, and access gates now configured via the plugin.
- Timeout unit changed: Now uses seconds instead of minutes
Usage
The Filament Resource Lock package enables you to lock a resource and prevent other users from editing it at the same time. Currently, this package only locks the EditRecord page and the edit modal when editing a simple modal resource. Follow the steps below to add locks to your resources.
Add Locks to your model
The first step is to add the HasLocks trait to the model of your resource. The HasLocks trait enables the locking functionality on your model.
// Post.php use Kenepa\ResourceLock\Models\Concerns\HasLocks; class Post extends Model { use HasFactory; use HasLocks; protected $table = 'posts'; protected $guarded = []; }
Add Locks to your EditRecord Page
The second step is to add the UsesResourceLock trait to your EditRecord page. The UsesResourceLock trait enables the locking function on your edit page.
// EditPost.php use Kenepa\ResourceLock\Resources\Pages\Concerns\UsesResourceLock; class EditPost extends EditRecord { use UsesResourceLock; protected static string $resource = PostResource::class; }
Simple modal Resource
If your resource is a simple modal resource, you'll need to use the UsesSimpleResourceLock trait instead.
// ManagePosts.php use Kenepa\ResourceLock\Resources\Pages\Concerns\UsesSimpleResourceLock; class ManagePosts extends ManageRecords { use UsesSimpleResourceLock; protected static string $resource = PostResource::class; }
And that's it! Your resource is now able to be locked. Refer to the documentation below for more information on how to configure the locking functionality.
Use polling to detect presence (SPA mode)
To make the resource locking feature possible for SPA we polling based detection to check if a user is still editing the resource. This is disabled by default but you can enable it in the config:
use Kenepa\ResourceLock\ResourceLockPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->plugin(ResourceLockPlugin::make() ->usesPollingToDetectPresence() ->presencePollingInterval(10)); }
Resource Lock manager
The package also provides a simple way to manage and view all your active and expired locks within your app. And it also provides a way to quickly unlock all resources or specific locks.
Configuration
Access
You can restrict the access to the Unlock button or to the resource manager by adjusting the access variable. Enabling the "limited" key and setting it to true allows you to specify either a Laravel Gate class or a permission name from the Spatie Permissions package.
use Kenepa\ResourceLock\ResourceLockPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->limitedAccessToResourceLockManager() ->gate('unlock') }
Example
// Example using gates // More info about gates: https://laravel.com/docs/authorization#writing-gates Gate::define('unlock', function (User $user, Post $post) { return $user->email === 'admin@mail.com'; }); // Example using spatie permission package Permission::create(['name' => 'unlock']);
Using custom models
Sometimes, you may have a customized implementation for the User model in your application, or you may want to use a custom class for the ResourceLock functionality. In such cases, you can update the configuration file to specify the new class you want to use. This will ensure that the ResourceLock functionality works as expected with the new implementation.
use Kenepa\ResourceLock\ResourceLockPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->userModel(\App\Models\CustomUser::class) ->resourceLockModel(\App\Models\CustomResourceLock::class); }
Displaying the user who has locked the resource
This package uses actions which allows you to implement your own custom logic. An action class is nothing more than a simple class with a method that executes some logic. Learn more about actions
To create a custom action, first create a file within your project and name
it CustomGetResourceLockOwnerAction.php
, for
example. In this file, create a new class that extends the GetResourceLockOwnerAction
class and override the
execute
method to return the desired identifier. For example:
// CustomGetResourceLockOwnerAction.php namespace App\Actions; use Kenepa\ResourceLock\Actions\GetResourceLockOwnerAction; class CustomResourceLockOwnerAction extends GetResourceLockOwnerAction { public function execute($userModel): string|null { return $userModel->email; } }
Next, register your custom action within the your plugin configuration:
use Kenepa\ResourceLock\ResourceLockPlugin; use Filament\Panel; public function panel(Panel $panel): Panel { return $panel // ... ->plugin( ResourceLockPlugin::make() ->resourceLockOwnerAction(\Kenepa\ResourceLock\Actions\CustomGetResourceLockOwnerAction::class) ); }
Overriding default functionality
If you need some custom functionality beyond what the traits provide, you can override the functions that they use. For example, if you want to change the URL that the "Return" button redirects to, you can override the resourceLockReturnUrl() function. By default, this button takes you to the index page of the resource, but you can change it to whatever URL you want by adding your custom implementation in the resourceLockReturnUrl() function.
For instance, if you want the "Return" button to redirect to https://laracasts.com, you can override the function as follows:
public function resourceLockReturnUrl(): string { return 'https://laracasts.com'; }
Now the return url will redirect to laracasts.com
This will change the behavior of the "Return" button to redirect to the provided URL.
Publishing migrations, configuration and view
php artisan vendor:publish --tag="resource-lock-migrations"
php artisan migrate
You can publish and run the migrations with:
php artisan vendor:publish --tag="resource-lock-migrations"
php artisan migrate
Optionally, you can publish the views using
Note: Publishing Blade views can introduce breaking changes into your app. If you're interested in how to stay safe, see this article by Dan Harrin.
php artisan vendor:publish --tag="resource-lock-views"
Coming soon
- Locked status indicator for table rows
- Polling
- Optimistic Locking
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.