Skip to content

Commit 213dfca

Browse files
committed
Fix lock issue
1 parent c0633e3 commit 213dfca

File tree

1 file changed

+18
-27
lines changed

1 file changed

+18
-27
lines changed

yii2-adapter/legacy/behaviors/SessionBehavior.php

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use craft\web\Session;
1212
use craft\web\View;
1313
use CraftCms\Cms\Support\Json;
14+
use Illuminate\Support\Facades\Cache;
1415
use yii\base\Behavior;
1516
use yii\base\Exception;
1617
use yii\web\AssetBundle;
@@ -266,19 +267,14 @@ public function broadcastToJs(string|array $message): void
266267
*/
267268
public function authorize(string $action): void
268269
{
269-
$mutex = Craft::$app->getMutex();
270-
$locked = $mutex->acquire(self::AUTH_LOCK_NAME, 5);
271-
272-
$access = $this->owner->get($this->authAccessParam, []);
273-
274-
if (!in_array($action, $access, true)) {
275-
$access[] = $action;
276-
$this->owner->set($this->authAccessParam, $access);
277-
}
278-
279-
if ($locked) {
280-
$mutex->release(self::AUTH_LOCK_NAME);
281-
}
270+
Cache::lock(self::AUTH_LOCK_NAME)->block(5, function() use ($action) {
271+
$access = $this->owner->get($this->authAccessParam, []);
272+
273+
if (!in_array($action, $access, true)) {
274+
$access[] = $action;
275+
$this->owner->set($this->authAccessParam, $access);
276+
}
277+
});
282278
}
283279

284280
/**
@@ -288,20 +284,15 @@ public function authorize(string $action): void
288284
*/
289285
public function deauthorize(string $action): void
290286
{
291-
$mutex = Craft::$app->getMutex();
292-
$locked = $mutex->acquire(self::AUTH_LOCK_NAME, 5);
293-
294-
$access = $this->owner->get($this->authAccessParam, []);
295-
$index = array_search($action, $access, true);
296-
297-
if ($index !== false) {
298-
array_splice($access, $index, 1);
299-
$this->owner->set($this->authAccessParam, $access);
300-
}
301-
302-
if ($locked) {
303-
$mutex->release(self::AUTH_LOCK_NAME);
304-
}
287+
Cache::lock(self::AUTH_LOCK_NAME)->block(5, function() use ($action) {
288+
$access = $this->owner->get($this->authAccessParam, []);
289+
$index = array_search($action, $access, true);
290+
291+
if ($index !== false) {
292+
array_splice($access, $index, 1);
293+
$this->owner->set($this->authAccessParam, $access);
294+
}
295+
});
305296
}
306297

307298
/**

0 commit comments

Comments
 (0)