Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed a bug where JavaScript and CSS registered by utility pages weren’t executed when navigating between utility
pages, and weren’t cleaned up when navigating away. ([#18978](https://github.com/craftcms/cms/issues/18978))
- Fixed a bug where custom element authorization methods weren’t respected by Laravel element policies. ([#18983](https://github.com/craftcms/cms/pull/18983))

## 6.0.0-alpha.5 - 2026-05-27

Expand Down
36 changes: 36 additions & 0 deletions src/Element/Policies/ElementPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
use CraftCms\Cms\Auth\Events\ElementAuthorizing;
use CraftCms\Cms\Element\Contracts\ElementInterface;
use CraftCms\Cms\Element\Contracts\NestedElementInterface;
use CraftCms\Cms\Element\Element;
use CraftCms\Cms\Field\Contracts\ElementContainerFieldInterface;
use CraftCms\Cms\Site\Models\Site;
use CraftCms\Cms\Support\Facades\Sites;
use CraftCms\Cms\User\Elements\User;
use Illuminate\Support\Facades\Gate;
use ReflectionMethod;

class ElementPolicy
{
Expand All @@ -28,6 +30,17 @@ class ElementPolicy
'duplicateAsDraft',
];

private const array ELEMENT_AUTHORIZATION_METHODS = [
'view' => 'canView',
'save' => 'canSave',
'delete' => 'canDelete',
'duplicate' => 'canDuplicate',
'copy' => 'canCopy',
'createDrafts' => 'canCreateDrafts',
'deleteForSite' => 'canDeleteForSite',
'duplicateAsDraft' => 'canDuplicateAsDraft',
];

/**
* Runs before all ability checks.
* Returns true/false to short-circuit, or null to continue.
Expand Down Expand Up @@ -76,12 +89,35 @@ public function saveCanonical(User $user, ElementInterface $element): bool
public function __call(string $method, array $arguments): bool
{
if (in_array($method, self::ABILITIES, true)) {
[$user, $element] = $arguments + [null, null];

if (
$user instanceof User &&
$element instanceof ElementInterface &&
$this->hasCustomElementAuthorizationMethod($element, $method)
) {
return $element->{self::ELEMENT_AUTHORIZATION_METHODS[$method]}($user);
}

return false;
}

throw new BadMethodCallException("Method {$method} does not exist.");
}

private function hasCustomElementAuthorizationMethod(ElementInterface $element, string $ability): bool
{
$method = self::ELEMENT_AUTHORIZATION_METHODS[$ability] ?? null;

if (! $method || ! method_exists($element, $method)) {
return false;
}

return new ReflectionMethod($element, $method)
->getDeclaringClass()
->getName() !== Element::class;
}

private function checkSiteAuthorization(User $user, ElementInterface $element): ?bool
{
if (! $siteId = $element->siteId) {
Expand Down
28 changes: 28 additions & 0 deletions tests/Feature/Cp/ElementHtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use CraftCms\Cms\Cp\Events\ElementCardHtmlResolving;
use CraftCms\Cms\Cp\Events\ElementChipHtmlResolving;
use CraftCms\Cms\Cp\Html\ElementHtml;
use CraftCms\Cms\Element\Element;
use CraftCms\Cms\User\Elements\User;
use Illuminate\Support\Facades\Event;

Expand Down Expand Up @@ -72,6 +73,19 @@

expect($html)->toBe('<div>overridden-chip</div>');
});

it('links custom element labels when the element authorizes viewing', function () {
$element = new ElementHtmlTestElement;
$element->id = 123;
$element->title = 'Payment #123';

$html = $this->elementHtml->elementChipHtml($element, [
'hyperlink' => true,
]);

expect($html)->toContain('<a class="label-link" href="')
->and($html)->toContain('/custom-elements/123');
});
});

describe('elementCardHtml', function () {
Expand Down Expand Up @@ -101,3 +115,17 @@
expect($html)->toBe('<div>overridden-card</div>');
});
});

class ElementHtmlTestElement extends Element
{
#[Override]
public function canView(User $user): bool
{
return true;
}

protected function cpEditUrl(): ?string
{
return "custom-elements/$this->id";
}
}
Loading