Adds the resource blocking feature to the MoonShine admin panel. When a user edits an entry, the resource is blocked so that other users cannot make changes at the same time.
Package version | MoonShine admin panel Version |
---|---|
^1.x | ^2.18.0 |
^2.x | ^3.x |
- Installation
- Usage
- Configuration
- Display of all locks
- Publishing configuration and language files
- License
The command to install:
composer require forest-lynx/moonshine-resource-lock
Then run the installation command:
php artisan resource-lock:install
The command will start migrations, and will offer to publish the configuration file and language files.
The resource-lock
package allows you to lock a resource and prevent it from being edited by other users.
To activate the resource lock, add the WithResourceLock
trait to the ModelResource
.
<?php
//...
use ForestLynx\MoonShine\Traits\WithResourceLock;
class PostResource extends ModelResource
{
use WithResourceLock;
//...
Now your resource may be blocked.
By default, the resource will be blocked for 10 minutes, or until it is saved, whichever comes first.
The lock time is regulated by the lock_time
parameter, which is specified in the configuration file, the value is specified in minutes.
By default, the modal window displays information about the user who blocked access to the resource.
The show_owner_modal
parameter (which is set to true
by default) in the configuration file is responsible for displaying user information.
The modal window displays only the name of the user who blocked access to the resource. To display other information, you can create your own class, which will be inherited from the ResourceLockOwnerAction
, and register it in the configuration file. This way, you can configure the display of additional information about the user who blocked access in the modal window.
For example:
<?php
namespace App\Actions;
//...
use ForestLynx\ResourceLock\Actions\ResourceLockOwnerAction;
class CustomActions extends ResourceLockOwnerAction
{
public function execute(Model|Authenticatable $user): ?string
{
return $user->email;
}
}
Then add it to the configuration file:
//...
-'resource_lock_owner' => \ForestLynx\MoonShine\Actions\ResourceLockOwnerAction::class
+'resource_lock_owner' => \App\Actions\CustomActions::class
//...
By default, when you click the Back button in the modal window of a blocked resource, you are redirected to the index page of the resource. However, you can change the URL of the redirect page by overriding the getReturnUrlResourceLock
method in your resource.
<?php
//...
class PostResource extends ModelResource
{
//...
use WithResourceLock;
//...
protected function getReturnUrlResourceLock(): string
{
return 'https://...';
}
//...
}
By default, the index page of the resource displays information that access to the resource has been blocked by another user. This is displayed as a special icon:
To hide this information on the index page of a resource, you can set the resource_lock_to_index_page
parameter to false
in the configuration file.
Depending on your needs, you can configure the display of information about the blocked resource on the index page of the resource by declaring the method in your resource isDisplayOnIndexPage()'. This method should return a boolean value of
trueor
false'.
Example:
<?php
//...
class PostResource extends ModelResource
{
//...
use WithResourceLock;
//...
public function isDisplayOnIndexPage(): bool
{
return false;
}
//...
}
Caution
So far, this only works for resources with display via 'TableBuilder'.
Note
To prevent editing of a blocked resource, the edit and delete buttons on the main page of the resource are removed from the list of actions. Instead, a button appears that opens information about the blocked resource in a special modal window.
In this package, you can configure the display of all blocked resources.
The name of the resource to use in the MoonShine admin panel menu:
ForestLynx\MoonShine\Resources\LockResource
.
Deleting an entry unlocks the resource.
To learn more about the available display options, refer to the documentation of the MoonShine admin panel Menu and ModelResource.
To clear the database table of all outdated resource lock records, run the command:
php artisan resource-lock:clear-old
To publish the configuration file, run the command:
php artisan vendor:publish --tag=resource-lock-config
Learn more about package configuration.
To publish language files, run the command:
php artisan vendor:publish --tag=resource-lock-lang