This site has been archived and you can no longer log in or post new messages. For up-to-date community resources please visit

eZ Community » Learn » eZ Publish » Fetching User Objects with PHP - part 1

Fetching User Objects with PHP - part 1

Friday 24 September 2010 4:57:12 pm

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Step 4: Fetching multiple users

The same fetches you can use in your template files can also be used in your PHP scripts. You can also pull out all users and all users of a particular user group or those users that are currently logged in (amongst others). Since there are so many options for pulling out multiple users we will concentrate on the fetch methods in the next tutorial. For the remainder of this tutorial we will cover the other ways you can extract users from eZ Publish.

Please note that for any large website due to the amount of data being extracted if you are running the script through a template or through your web browser there is a very good chance the script will time out so care is needed when running these scripts. One solution is to run the scripts through a cronjob and allow the duration of the script to be extended, that way you can limit the script's use to when your server is at it's quietest.


Extracting all users

eZUser has a function to pull out all users currently enabled in your CMS. The code below pulls all of the users out and returns their name and email. You can use the code we have already looked at if you want to pull out other information.

$allUsers = eZUser::fetchContentList();
print_r( $allUsers[0] ); //demonstrating how the data is returned
foreach( $allUsers as $key => $user )
    $userObj = eZUser::fetch( $user['id'] );
    $cli->output( 'Element: ' . $key );
    $cli->output( 'Username: ' . $userObj->attribute('login') );
    $cli->output( 'Email: ' . $userObj->attribute('email') );
    $cli->output( 'Content Object ID: ' . $userObj->attribute( 'contentobject_id' ) );
    $cli->output( '**********');

You may notice a slight change in how we need to pull the users out through this example. The fetchContentList returns an array. This contains only a limited amount of information so then we need to lookup the user using a basic fetch function. We can then extract the information using the code we have already covered. Our example prints the first user object to demonstrate the difference in structure returned by fetchContentList :

displaying users list


Users by user role

A similar requirement to pulling out all users is to pull out all users for a specific role. So for instance, all users of the site will probably have the same role whereas admin users would not. To do this, we need to use the eZRole class. There is a function in the eZRole class that will return all users and user groups for a particular role. If we use this we can then display all users directly, or in the case of user groups we can then access their children, which will all be users.

As roles are usually assigned by group rather than individual users and so most of the time you will be dealing with User Groups. When we have the information we just display the name but you can use the code we’ve covered previously if you want to show more information :

$adminRole = eZRole::fetchByName( 'Administrator' );
$roleUsers = $adminRole->fetchUserByRole(); //this will return eZContentObjects within separate arrays
foreach( $roleUsers as $key => $userHolder )
    $object_type = $userHolder['user_object']->attribute( 'class_name' );
    if ( $object_type == 'user_group' ) //we will more than likely be dealing with a user group so we need to pull out the users from this
        $user_group = $userHolder['user_object']->attribute( 'main_node' ); //convert so we can access the children of the node
        foreach( $user_group->attribute( 'children' ) as $group_user ) //user_group will contain  eZContentObjectTreeNodes, let's just output the name (details above on accessing other fields).
            $cli->output( 'name: ' . $group_user->attribute( 'name' ) );
    else //if we have a user we are looking at a eZContentObject
        $cli->output( 'name: ' . $userHolder['user_object']->attribute('name') );

The key part to the script occurs once you;ve established whether you’re looking at a user or a user group. If you have a user, it is easy enough to show information almost directly since we are dealing with a eZContentObject (you can use the code from previous examples to display the user information).

For User Groups, we need to carry out additional steps. You will be aware that eZContentObjects do not have a hierarchy in eZ Publish, the hierarchy is established by nodes rather than objects. Therefore, to work out the users which sit under a particular user group (in other words the users who belong to the user group), we must first convert the eZUserObject to a node. The following code in the previous example performs this transformation and then iterates through the users below the group, displaying the name for each user :

$user_group = $userHolder['user_object']->attribute( 'main_node' ); //convert so we can access the children of the node
foreach( $user_group->attribute( 'children' ) as $group_user ) //user_group will contain  eZContentObjectTreeNodes, let's just output the name (details above on accessing other fields).
        $cli->output( 'name: ' . $group_user->attribute( 'name' ) );

Users currently logged in

The eZUser class also provides methods for providing a list of users who are currently logged in. It is very straightforward to use. The method returns an array of eZUser objects so you you can use the examples in the rest of this tutorial to extract more information.

$loggedInCount = eZUser::fetchLoggedInCount();
$loggedIn = eZUser::fetchLoggedInList( true );
$cli->output( 'Logged in Users: ' . $loggedInCount );
foreach( $loggedIn as $user )
    $cli->output( 'name: ' . $user->attribute( 'login' ) );
36 542 Users on board!

Tutorial menu


Printer Friendly version of the full article on one page with plain styles


Proudly Developed with from