Skip to content

Commit f6850b5

Browse files
committed
Fixed #17694
1 parent d092850 commit f6850b5

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Fixed a bug where reference tags that only referenced an entry’s slug weren’t resolving.
6+
- Fixed a race condition that could cause “User is not authorized” errors in the control panel. ([#17694](https://github.com/craftcms/cms/issues/17694))
67

78
## 4.16.8 - 2025-07-25
89

src/behaviors/SessionBehavior.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
class SessionBehavior extends Behavior
2626
{
27+
private const AUTH_LOCK_NAME = 'authAccess';
28+
2729
/**
2830
* @var string|null The session variable name used to store the authorization keys for the current session.
2931
* @see authorize()
@@ -266,12 +268,19 @@ public function broadcastToJs(string|array $message): void
266268
*/
267269
public function authorize(string $action): void
268270
{
271+
$mutex = Craft::$app->getMutex();
272+
$locked = $mutex->acquire(self::AUTH_LOCK_NAME, 5);
273+
269274
$access = $this->owner->get($this->authAccessParam, []);
270275

271276
if (!in_array($action, $access, true)) {
272277
$access[] = $action;
273278
$this->owner->set($this->authAccessParam, $access);
274279
}
280+
281+
if ($locked) {
282+
$mutex->release(self::AUTH_LOCK_NAME);
283+
}
275284
}
276285

277286
/**
@@ -281,13 +290,20 @@ public function authorize(string $action): void
281290
*/
282291
public function deauthorize(string $action): void
283292
{
293+
$mutex = Craft::$app->getMutex();
294+
$locked = $mutex->acquire(self::AUTH_LOCK_NAME, 5);
295+
284296
$access = $this->owner->get($this->authAccessParam, []);
285297
$index = array_search($action, $access, true);
286298

287299
if ($index !== false) {
288300
array_splice($access, $index, 1);
289301
$this->owner->set($this->authAccessParam, $access);
290302
}
303+
304+
if ($locked) {
305+
$mutex->release(self::AUTH_LOCK_NAME);
306+
}
291307
}
292308

293309
/**

0 commit comments

Comments
 (0)