Skip to content

Commit

Permalink
Merge pull request #15 from tv2regionerne/fix/lock-expiration-check
Browse files Browse the repository at this point in the history
Fix/lock expiration check
  • Loading branch information
Sylvester Damgaard authored May 16, 2024
2 parents 260e6d5 + d264970 commit bb7b29c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Listeners/LockListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Tv2regionerne\StatamicLocks\Listeners;

use Illuminate\Support\Carbon;
use Illuminate\Validation\ValidationException;
use Statamic\Events;
use Statamic\Facades\Site;
use Statamic\Facades\User;
use Statamic\Support\Str;
use Tv2regionerne\StatamicLocks\Models\LockModel;

class LockListener
Expand All @@ -15,6 +17,13 @@ public function handle($event)
$itemId = false;
$itemType = false;

$path = request()->path();

// ignore locks on API requests
if (Str::of($path)->startsWith('api')) {
return;
}

if ($event instanceof Events\AssetSaving) {
$itemId = $event->asset->id() ?? false;
$itemType = 'asset';
Expand All @@ -38,9 +47,11 @@ public function handle($event)

if ($lock = LockModel::where(['item_id' => $itemId, 'item_type' => $itemType, 'site' => Site::current()->handle()])->first()) {
if (! $lock->user() || ! $user || $lock->user()->id() != $user->id()) {
throw ValidationException::withMessages([
'locked' => __('This item is locked'),
]);
if ($lock->updated_at > Carbon::now()->subMinutes(config('statamic-locks.clear_locks_after', 5))) {
throw ValidationException::withMessages([
'locked' => __('This item is locked'),
]);
}
}
}
}
Expand Down

0 comments on commit bb7b29c

Please sign in to comment.