Role and policy operators
This page is for designers who show something only to some users: an "Edit" link for editors, a members area link for signed-in members.
Contents
The role and policy operators
Seven operators of eZTemplateRoleOperator (lib/eztemplate/classes/eztemplateroleoperator.php), described in
Role and policy checks in templates:
if $current_user|has_role( 'Editor' ) ... /if
if $current_user|has_policy( 'content', 'edit' ) ... /if
if $current_user|has_any_role( array( 'Editor', 'Administrator' ) ) ... /ifThe piped value is ignored; without a user_id parameter the current user is checked.
Note
Before 6.0.15 these operators were not registered with the template engine, so a template using them got the warning "Operator 'has_role' is not registered". 6.0.15 registers all seven. On an older installation that reports the operator as unknown, use the fetch functions below, which work everywhere.
The fetch alternatives
if fetch( 'user', 'has_access_to', hash( 'module', 'content', 'function', 'edit' ) )
...
/if
if fetch( 'user', 'can_open', hash( 'uri', 'setup/info' ) )
<a href='setup/info'|ezurl>...</a>
/if(kernel/user/function_definition.php: has_access_to with module, function and an optional user_id;
can_open with a uri.)
Node permissions
For a specific node, the node itself knows: $node.can_edit, $node.can_create, $node.can_remove, $node.can_read
(Nodes, objects and attributes).
if $node.can_edit
<a href=concat( 'content/edit/', $node.contentobject_id )|ezurl>'Edit'|i18n( 'design/mytheme' )</a>
/ifShowing is not protecting
Hiding a link does not stop anyone from opening the URL. Access is enforced by roles and policies in the administration; templates only decide what to show.
Caching
Output that depends on the user's roles must be cached per role set: the view cache already varies by the user's roles;
a cache-block needs $current_user.role_id_list|implode( ',' ) in its keys (Cache-block).
Related pages
- Logic and comparison operators
- User and login templates
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.