eZUser and access checks
Part 8, reference 9. eZUser (kernel/classes/datatypes/ezuser/ezuser.php) is a user account. Every request has a
current user; on a public page that is usually the anonymous user. Access is granted through roles and their
policies (Sections, roles and access for builders), and
an extension declares its own policy functions (Policy functions and access).
The current user
| Method (static) | Returns |
|---|---|
eZUser::currentUser() | the user of this request |
eZUser::currentUserID() | the ID |
eZUser::fetch( $id, $asObject = true ), fetchByName( $login, ... ), fetchByEmail( $email, ... ) | other users |
| Method | Returns |
|---|---|
isAnonymous() | true for the anonymous user |
isLoggedIn() | true when signed in |
contentObject() | the user's content object (name, photo ...) |
groups( $asObject = false ) | the user's groups |
roles() | the user's roles |
hasAccessTo()
function hasAccessTo( $module, $function = false )returns an array whose accessWord is one of:
| accessWord | Meaning |
|---|---|
yes | full access |
no | no access |
limited | access with limitations (sections, classes, subtrees ...), listed in policies |
$user = eZUser::currentUser();
$access = $user->hasAccessTo( 'myext', 'manage' );
if ( $access['accessWord'] === 'no' )
{
return $Module->handleError( eZError::KERNEL_ACCESS_DENIED, 'kernel' );
}Run in a command line script as the anonymous user, hasAccessTo( 'content', 'read' ) returned limited and
hasAccessTo( 'setup', 'setup' ) returned no.
For a policy function without limitations (most extension functions), treat anything but yes as no access unless
your function declares limitations and you evaluate them.
Access to content
For content, use the object's and node's own checks, which evaluate all limitations:
| Method | On |
|---|---|
canRead(), canEdit( ... ), checkAccess( $functionName, ... ) | eZContentObject |
canRead(), canEdit(), canCreate(), canRemove() | eZContentObjectTreeNode |
subTreeByNodeID() and the content fetch functions apply read access themselves.
In templates
{if fetch( 'user', 'has_access_to', hash( 'module', 'myext', 'function', 'manage' ) )}
<a href={'/myext/manage'|ezurl}>{'Manage'|i18n( 'extension/myext' )}</a>
{/if}fetch( 'user', 'current_user' ) gives the current user. In admin menus, menu.ini [MenuAccessSettings] hides links a
user cannot open (Context menus and admin integration).
Rules
- Check access in the view, even if the link to it is hidden: anyone can type a URL.
- Never compare user IDs or group names in code to grant access; grant it through a policy function, so administrators can assign it.
- Signing a user in from code (
eZUser::loginUser()) is for login handlers; an ordinary extension never does it.
Demo mode. eZUser::store() keeps the stored password of an administrator while EXP_DEMO_MODE=1; ask expDemoMode::protectsPassword( $user ) before changing a password so the user gets the reason. Demo mode for developers.
Related pages
- Previous: eZContentClass and its attributes
- Next: eZHTTPTool: requests, sessions and redirects
- Security in extensions
- 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.