Fetching User Group in PHP

Author Message

John Smith

Wednesday 18 August 2010 4:22:00 am

Need some help to fetch "User group + attributes" in PHP when user is logged in.

I have userobject with the following code:

$userObject = $user->attribute( 'contentobject' );

Carlos Revillo

Wednesday 18 August 2010 7:24:53 am

You mean getting the groups to which the user belongs? i mean, you need to get names as 'Guest Accounts' or 'Administrator users'?

if so, you probably can get the assigned nodes of the user and getting their parents... it's that what you need?

John Smith

Wednesday 18 August 2010 7:39:08 am

Cheers Carlos,

For User class, I can use the following code to get FirstName, Last Name and Email.

$userObject = $user->attribute( 'contentobject' );
$userMap = $userObject->dataMap();
$firstName = $userMap['first_name']->content();
$lastName = $userMap['last_name']->content();
$email = $user->attribute( 'email' );

I need some help to fetch the usergroup id / name and few other attributes in the same way.

Can you please guide.

Carlos Revillo

Wednesday 18 August 2010 8:09:26 am

First of all, you need to note that a user can 'belong' to several groups, because a user object can have several locations. supposing you're working only with the main location, you can do something like

$userObject = $user->attribute( 'contentobject' );
$userMainNode = $userObject->attribute( 'main_node' );
$userGroup = $userMainNode->attribute( 'parent' );
$userGroupName = $userGroup->attribute( 'name' ); // and this will be the name of the user group

once you have $userGroup, you can easily get its dataMap and play with it.

Note also you don't need all those lines. you can do it just like

$userGroupName = eZUser::currentUser()->attribute( 'contentobject' )->attribute( 'main_node' )->attribute( 'parent' )->attribute( 'name' );

hope this works for you.

John Smith

Wednesday 18 August 2010 9:36:34 am

Thanks for explaining and kind help

With the following code

$userGroupMap = eZUser::currentUser()->attribute( 'contentobject' )->attribute( 'main_node' )->attribute( 'parent' )->dataMap();
$relation_ObjectID = $userGroupMap['relation_link']->content();

Print_r "$relation_objectID" gives me the following

Array
{
  [relation_list] => Array
   {
     [0] => Array
      (
       [identifier] =>
       [priority] => 1
       [in_trash] =>
       [contentobject_id] => 241
       [contentobject_version] => 13
       [node_id] => 238
       [parent_node_id] => 203
       [contentclass_id] => 37
       [contentclass_identifier] => xxx_xxxxx
       [is_modified] =>
       [contentobject_remote_id] => b484e7365a7b9eb3b02447e24f34dc3d
       )
   )
)

Would you kindly explain me how to get the value of node_id which is 238 and then the node "name".

Thanks

Thiago Campos Viana

Wednesday 18 August 2010 10:02:40 am

Hi

How about?

/* first fetch the user content object attribute */
$user=eZUser::currentUser();

/* dump user groups array */
var_dump( $user->attribute( 'groups' ) );

eZ Publish Certified Developer: http://auth.ez.no/certification/verify/376924

Twitter: http://twitter.com/tcv_br

Carlos Revillo

Wednesday 18 August 2010 10:20:30 am

Thiago has added another good option.

Regarding yr question i understand you need to get the node with node_id = 238?

then you can

$node_id = <span>$relation_ObjectID['relation_list'][0]['node_id'];
$searchednodename = eZContentObjectTreeNode::fetch( $node_id )->attribute( 'name' );

hope it helps.

John Smith

Thursday 19 August 2010 3:46:25 am

Sorted. Thanks guys....

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.