Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Element authorization improvements #11808

Merged
merged 6 commits into from
Aug 21, 2022

Conversation

brandonkelly
Copy link
Member

This PR adds element authorization-check methods and events to the Elements service, with the same names as their craft\base\Element counterparts:

  • craft\services\Elements::canView() + EVENT_AUTHORIZE_VIEW
  • craft\services\Elements::canSave() + EVENT_AUTHORIZE_SAVE
  • craft\services\Elements::canDuplicate() + EVENT_AUTHORIZE_DUPLICATE
  • craft\services\Elements::canDelete() + EVENT_AUTHORIZE_DELETE
  • craft\services\Elements::canDeleteForSite() + EVENT_AUTHORIZE_DELETE_FOR_SITE
  • craft\services\Elements::canCreateDrafts() + EVENT_AUTHORIZE_CREATE_DRAFTS

There are two notable differences between these and the authorization methods/events on craft\base\Element:

  • The methods’ $user argument is optional. If null, it will default to the currently logged-in user, or return false if there isn’t one.
  • AuthorizationCheckEvent::$authorized is set to null by default, and the methods will only defer to the element’s corresponding canX() method if an event handler hasn’t explicitly set it to true or false. Which means event handlers have the ability to prevent users from being authorized for certain actions, as opposed to only being able to authorize them (see [4.x]: Issue with `Element::EVENT_AUTHORIZE_DELETE` ? #11759).

All internal authorization checks have been updated to call the new Elements service methods. And the old events on craft\base\Element are now deprecated, since they’re redundant and less useful.

Finally, there’s new global Twig functions that wrap the new Elements service methods, making it a lot easier to check if the current user is authorized to do something with an element: (#11797)

{% if not canView(entry) %}
    {% redirect siteUrl %}
{% endif %}

@linear
Copy link

linear bot commented Aug 21, 2022

@brandonkelly brandonkelly marked this pull request as ready for review August 21, 2022 18:09
@brandonkelly brandonkelly requested a review from a team as a code owner August 21, 2022 18:09
@brandonkelly brandonkelly force-pushed the feature/dev-984-element-authorization-improvements branch from 7a3db40 to 211d290 Compare August 21, 2022 18:09
@brandonkelly brandonkelly merged commit 2627ac5 into 4.3 Aug 21, 2022
@brandonkelly brandonkelly deleted the feature/dev-984-element-authorization-improvements branch August 21, 2022 18:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant