Skip to content

Commit

Permalink
Merge pull request #3 from kenepa/release_ready
Browse files Browse the repository at this point in the history
Release ready
  • Loading branch information
Jehizkia authored Apr 6, 2023
2 parents 78a71fd + 7d5706c commit 3cc3dbc
Show file tree
Hide file tree
Showing 14 changed files with 290 additions and 16 deletions.
35 changes: 31 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ the [EditRecord](https://filamentphp.com/docs/2.x/admin/resources/editing-record
a [simple modal resource.](https://filamentphp.com/docs/2.x/admin/resources/getting-started#simple-modal-resources)
Follow the steps below to add locks to your resources.

### Add Locks to your modal
### Add Locks to your model

The first step is to add the HasLocks trait to the modal of your resource. The HasLocks trait enables the locking
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.

```php
Expand Down Expand Up @@ -95,13 +95,21 @@ class ListExamples extends ManageRecords
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.

## Resource Lock manager

<img style="width: 100%; max-width: 100%;" alt="filament-shield-art" src="https://raw.githubusercontent.com/kenepa/Kenepa/main/art/ResourceLock/filament-resource-lock-manager.png" >

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

<img style="width: 100%; max-width: 100%;" alt="filament-shield-art" src="https://raw.githubusercontent.com/kenepa/Kenepa/main/art/ResourceLock/filament-locked.png" >

You can restrict the access to the **Unlock** button by adjusting the access variable. Enabling the "limited" key and
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](https://github.com/spatie/laravel-permission).

Expand All @@ -119,11 +127,24 @@ the [Spatie Permissions package](https://github.com/spatie/laravel-permission).
*/

'unlocker' => [
'limited_access' => false,
'limited_access' => true,
'gate' => 'unlock-resource'
],
```

Example

```php

// Example using gates
Gate::define('unlock-resource', function (User $user, Post $post) {
return $user->email === '[email protected]';
});

// Example using spatie permission package
Permission::create(['name' => 'unlock-resource']);
```

### Using custom models

Sometimes, you may have a customized implementation for the User model in your application, or you may want to use a
Expand Down Expand Up @@ -199,6 +220,12 @@ Optionally, you can publish the views using
php artisan vendor:publish --tag="resource-lock-views"
```

## Coming soon

- Locked status indicator for table rows
- Polling
- Displaying which users has locked a resource

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kenepa/resource-lock",
"description": "This is my package resource lock",
"description": "Filament Resource Lock is a Filament plugin that adds resource locking functionality to your site.",
"keywords": [
"Kenepa",
"laravel",
Expand Down
37 changes: 29 additions & 8 deletions config/resource-lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@

/*
|--------------------------------------------------------------------------
| Resource Unlocker
| Resource Unlocker Button
|--------------------------------------------------------------------------
|
| The unlocker configuration specifies whether limited access is enabled for
| the resource lock feature. If limited access is enabled, only specific
| users or roles will be able to unlock locked resources.
| the resource unlock button. If limited access is enabled, only specific
| users or roles will be able to unlock locked resources directly from
| the modal.
|
*/

Expand All @@ -34,6 +35,25 @@
// 'gate' => ''
],

/*
|--------------------------------------------------------------------------
| Resource Lock Manager
|--------------------------------------------------------------------------
|
| The resource lock manager provides a simple way to view all resource locks
| of your application. It provides several ways to quickly unlock all or
| specific resources within your app.
|
*/

'manager' => [
'navigation_label' => 'Resource Lock Manager',
'navigation_group' => 'Ticket',
'navigation_sort' => 1,
'limited_access' => true,
'gate' => 'manager'
],

/*
|--------------------------------------------------------------------------
| Lock timeout (in minutes)
Expand All @@ -49,15 +69,16 @@

/*
|--------------------------------------------------------------------------
| Throw Forbidden Exception
| Check Locks before saving
|--------------------------------------------------------------------------
|
| The throw_forbidden_exception configuration specifies whether a 403 forbidden
| exception should be thrown if a tech-savvy user is able to bypass the locked
| resource modal and attempt to save the resource.
| The check_locks_before_saving configuration specifies whether a lock of a resource will be checked
| before saving a resource if a tech-savvy user is able to bypass the locked
| resource modal and attempt to save the resource. In some cases you may want to turns this off.
| It's recommended to keep this on.
|
*/

'throw_forbidden_exception' => true,
'check_locks_before_saving' => true,
];

10 changes: 10 additions & 0 deletions resources/lang/de/manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'active' => 'Aktiv',
'expired' => 'Abgelaufen',
'unlock' => 'Entsperren',
'unlocked' => 'Entsperrter Ressourcen',
'unlocked_selected' => 'Ausgewählte Ressourcen entsperrt',
'unlock_all' => 'Alle Ressourcen entsperren'
];
10 changes: 10 additions & 0 deletions resources/lang/en/manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'active' => 'Active',
'expired' => 'Expired',
'unlock' => 'Unlock',
'unlocked' => 'Unlocked resource',
'unlocked_selected' => 'Unlocked selected resources',
'unlock_all' => 'Unlock all resources'
];
10 changes: 10 additions & 0 deletions resources/lang/es/manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'active' => 'Activo',
'expired' => 'Expirado',
'unlock' => 'Desbloquear',
'unlocked' => 'Recurso desbloqueado',
'unlocked_selected' => 'Recursos seleccionados desbloqueados',
'unlock_all' => 'Desbloquear todos los recursos'
];
10 changes: 10 additions & 0 deletions resources/lang/fr/manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'active' => 'Actif',
'expired' => 'Expiré',
'unlock' => 'Déverrouiller',
'unlocked' => 'Ressource déverrouillée',
'unlocked_selected' => 'Ressources sélectionnées déverrouillées',
'unlock_all' => 'Déverrouiller toutes les ressources'
];
10 changes: 10 additions & 0 deletions resources/lang/nl/manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
'active' => 'Actief',
'expired' => 'Verlopen',
'unlock' => 'Ontgrendelen',
'unlocked' => 'Ontgrendelde resource',
'unlocked_selected' => 'Geselecteerde resource ontgrendeld',
'unlock_all' => 'Alle resources ontgrendelen'
];
8 changes: 8 additions & 0 deletions src/Models/ResourceLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Kenepa\ResourceLock\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand All @@ -20,4 +21,11 @@ public function lockable(): MorphTo
{
return $this->morphTo();
}

public function isExpired(): bool
{
$expiredDate = (new Carbon($this->updated_at))->addMinutes(config('resource-lock.lock_timeout'));

return Carbon::now()->greaterThan($expiredDate);
}
}
3 changes: 2 additions & 1 deletion src/ResourceLockServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Filament\Facades\Filament;
use Filament\PluginServiceProvider;
use Illuminate\Support\Facades\Blade;
use Kenepa\ResourceLock\Resources\ResourceLockResource;
use Livewire\Livewire;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
use Spatie\LaravelPackageTools\Package;
Expand All @@ -14,7 +15,7 @@ class ResourceLockServiceProvider extends PluginServiceProvider
public static string $name = 'resource-lock';

protected array $resources = [
// CustomResource::class,
ResourceLockResource::class,
];

public function configurePackage(Package $package): void
Expand Down
9 changes: 7 additions & 2 deletions src/Resources/Pages/Concerns/UsesResourceLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ public function resourceLockObserverUnlock()
*/
public function save(bool $shouldRedirect = true): void
{
if (config('resource-lock.throw_forbidden_exception', true)) {
abort_unless($this->record->isLocked() && $this->record->isLockedByCurrentUser(), 403);
if (config('resource-lock.check_locks_before_saving', true)) {
$this->record->refresh();
if ($this->record->isLocked() && !$this->record->isLockedByCurrentUser()) {
$this->checkIfResourceLockHasExpired($this->record);
$this->lockResource($this->record);
return;
}
}

parent::save($shouldRedirect);
Expand Down
12 changes: 12 additions & 0 deletions src/Resources/Pages/Concerns/UsesSimpleResourceLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public function mountTableAction(string $name, ?string $record = null)
$this->lockResource($this->resourceRecord);
}

public function callMountedTableAction(?string $arguments = null) {
if (config('resource-lock.check_locks_before_saving', true)) {
$this->resourceRecord->refresh();
if ($this->resourceRecord->isLocked() && !$this->resourceRecord->isLockedByCurrentUser()) {
$this->checkIfResourceLockHasExpired($this->resourceRecord);
$this->lockResource($this->resourceRecord);
return;
}
}
parent::callMountedTableAction($arguments);
}

public function resourceLockObserverUnload()
{
$this->resourceRecord->unlock();
Expand Down
Loading

0 comments on commit 3cc3dbc

Please sign in to comment.