Forums / General / Fetching User Group in PHP

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....

eZ debug

Timing: Jan 30 2025 07:18:18
Script start
Timing: Jan 30 2025 07:18:18
Module start 'content'
Timing: Jan 30 2025 07:18:18
Module end 'content'
Timing: Jan 30 2025 07:18:18
Script end

Main resources:

Total runtime0.3284 sec
Peak memory usage8,192.0000 KB
Database Queries141

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0090 589.5234370.3047
Module start 'content' 0.00900.0142 959.82811,009.5156
Module end 'content' 0.02320.3052 1,969.34383,897.5625
Script end 0.3284  5,866.9063 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00451.3656200.0002
Check MTime0.00140.4263200.0001
Mysql Total
Database connection0.00060.181210.0006
Mysqli_queries0.227969.39881410.0016
Looping result0.00150.46371390.0000
Template Total0.304892.810.3048
Template load0.00080.258510.0008
Template processing0.303992.532710.3039
Override
Cache load0.00060.173310.0006
Sytem overhead
Fetch class attribute can translate value0.00190.565410.0019
XML
Image XML parsing0.00030.076710.0003
General
dbfile0.00872.6623200.0004
String conversion0.00000.002230.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs