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.

eZ debug

Timing: Jan 18 2025 15:09:28
Script start
Timing: Jan 18 2025 15:09:28
Module start 'layout'
Timing: Jan 18 2025 15:09:28
Module start 'content'
Timing: Jan 18 2025 15:09:29
Module end 'content'
Timing: Jan 18 2025 15:09:29
Script end

Main resources:

Total runtime1.2286 sec
Peak memory usage4,096.0000 KB
Database Queries74

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0066 587.9063152.6250
Module start 'layout' 0.00660.0038 740.531339.4453
Module start 'content' 0.01031.2168 779.9766635.0234
Module end 'content' 1.22710.0014 1,415.000016.7891
Script end 1.2285  1,431.7891 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00340.2749160.0002
Check MTime0.00130.1076160.0001
Mysql Total
Database connection0.00080.066910.0008
Mysqli_queries1.162894.6455740.0157
Looping result0.00080.0633720.0000
Template Total1.187696.720.5938
Template load0.00220.181920.0011
Template processing1.185396.478220.5927
Template load and register function0.00020.014410.0002
states
state_id_array0.00150.124010.0015
state_identifier_array0.00160.129020.0008
Override
Cache load0.00200.1606430.0000
Sytem overhead
Fetch class attribute can translate value0.00100.078030.0003
Fetch class attribute name0.00170.134390.0002
XML
Image XML parsing0.00080.068030.0003
class_abstraction
Instantiating content class attribute0.00000.001690.0000
General
dbfile0.00090.0735160.0001
String conversion0.00000.000740.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
8content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
14content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
8content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 33
 Number of unique templates used: 6

Time used to render debug report: 0.0001 secs