[Craft 4] Allow null for $user
param in element authorization check methods
#11797
-
This is a very minor thing, but it'd be great if it was possible to pass The use case is simply to reduce a little bit of boilerplate code, in situations where the current user may or may not be logged in. I.e. instead of this: {% if not currentUser or not entry.canView(currentUser) %}
{% redirect siteUrl %}
{% endif %} ...it'd be nice to just be able to do this: {% if not entry.canView(currentUser) %}
{% redirect siteUrl %}
{% endif %} Obviously, the can-methods should just return |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Wouldn’t be able to do this without changing the Maybe something we can make nicer with a Twig-specific tag/function/test though? |
Beta Was this translation helpful? Give feedback.
-
Just added new element authorization global Twig functions for Craft 4.3 ( {% if not canView(entry) %}
{% redirect siteUrl %}
{% endif %} See details in the PR (#11808). |
Beta Was this translation helpful? Give feedback.
Just added new element authorization global Twig functions for Craft 4.3 (
canView()
,canSave()
, etc.), which haveelement
anduser
arguments, butuser
is optional and defaults to the current user. So you will be able to do this:See details in the PR (#11808).