Skip to content

Commit 45d8a7d

Browse files
committed
Session authorization is different
1 parent f4f67c0 commit 45d8a7d

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

src/Http/Controllers/PreviewController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CraftCms\Cms\Http\Controllers;
66

7+
use Craft;
78
use craft\helpers\ElementHelper;
89
use CraftCms\Cms\Http\EnforcesPermissions;
910
use CraftCms\Cms\Http\Middleware\HandleTokenRequest;
@@ -25,9 +26,9 @@
2526
public function createToken(Request $request, RouteTokens $tokens, RouteToken $tokenData): JsonResponse|RedirectResponse
2627
{
2728
match (true) {
28-
isset($tokenData->draftId) => $this->requirePermission("previewDraft:{$tokenData->draftId}"),
29-
isset($tokenData->revisionId) => $this->requirePermission("previewRevision:{$tokenData->revisionId}"),
30-
default => $this->requirePermission("previewElement:{$tokenData->getCanonicalId()}"),
29+
isset($tokenData->draftId) => $this->requireSessionAuthorization("previewDraft:{$tokenData->draftId}"),
30+
isset($tokenData->revisionId) => $this->requireSessionAuthorization("previewRevision:{$tokenData->revisionId}"),
31+
default => $this->requireSessionAuthorization("previewElement:{$tokenData->getCanonicalId()}"),
3132
};
3233

3334
$token = $tokens->createPreviewToken([
@@ -85,7 +86,7 @@ public function preview(Request $request, Kernel $kernel, RouteToken $tokenData)
8586
}
8687

8788
$element->previewing = true;
88-
\Craft::$app->getElements()->setPlaceholderElement($element);
89+
Craft::$app->getElements()->setPlaceholderElement($element);
8990
}
9091

9192
/** @var \Illuminate\Support\Uri $originalUri */

src/Http/Controllers/StructuresController.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace CraftCms\Cms\Http\Controllers;
66

7+
use Craft;
78
use craft\base\ElementInterface;
89
use CraftCms\Cms\Http\EnforcesPermissions;
910
use CraftCms\Cms\Http\RespondsWithFlash;
@@ -36,15 +37,15 @@ public function __construct(
3637
'siteId' => ['required', 'integer'],
3738
]);
3839

39-
$this->requirePermission("editStructure:$structureId");
40+
$this->requireSessionAuthorization("editStructure:$structureId");
4041

4142
abort_if(
4243
is_null($this->structure = $structures->getStructureById($structureId)),
4344
404,
4445
'Structure not found.'
4546
);
4647

47-
$elementsService = \Craft::$app->getElements();
48+
$elementsService = Craft::$app->getElements();
4849

4950
abort_if(
5051
is_null($elementType = $elementsService->getElementTypeById($elementId)),
@@ -77,10 +78,10 @@ public function moveElement(): Response
7778
$prevElementId = $this->request->input('prevId');
7879

7980
if ($prevElementId) {
80-
$prevElement = \Craft::$app->getElements()->getElementById($prevElementId, null, $this->element->siteId);
81+
$prevElement = Craft::$app->getElements()->getElementById($prevElementId, null, $this->element->siteId);
8182
$success = $this->structures->moveAfter($this->structure->id, $this->element, $prevElement);
8283
} elseif ($parentElementId) {
83-
$parentElement = \Craft::$app->getElements()->getElementById($parentElementId, null, $this->element->siteId);
84+
$parentElement = Craft::$app->getElements()->getElementById($parentElementId, null, $this->element->siteId);
8485
$success = $this->structures->prepend($this->structure->id, $this->element, $parentElement);
8586
} else {
8687
$success = $this->structures->prependToRoot($this->structure->id, $this->element);

src/Http/EnforcesPermissions.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ protected function enforceEditEntryPermissions(Entry $entry, bool $duplicate = f
3939
abort_unless($canSave, 403, 'User is not authorized to perform this action.');
4040
}
4141

42+
protected function requireSessionAuthorization(string $permission): void
43+
{
44+
if (! Craft::$app->getSession()->checkAuthorization($permission)) {
45+
abort(403, 'User is not authorized to perform this action.');
46+
}
47+
}
48+
4249
protected function requirePermission(string $permission): void
4350
{
4451
if (! $user = Auth::user()) {

src/Structure/Data/Structure.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace CraftCms\Cms\Structure\Data;
66

7-
use Illuminate\Support\Facades\Auth;
7+
use Craft;
88
use Spatie\LaravelData\Dto;
99

1010
final class Structure extends Dto
@@ -17,6 +17,6 @@ public function __construct(
1717

1818
public function isSortable(): bool
1919
{
20-
return Auth::user()?->can("editStructure:{$this->id}");
20+
return Craft::$app->getSession()->checkAuthorization("editStructure:{$this->id}");
2121
}
2222
}

0 commit comments

Comments
 (0)