Search code examples
phpsession-variablesmodxmodx-revolution

ModX: How to retrieve logged in web user's user group?


I am using the Login addon (http://rtfm.modx.com/display/ADDON/Login) and am able to get some info about the user using:

print_r($_SESSION);

But I am wondering how to get that users user group (name or ID). Would anyone know how to go about this? Thanks!

PS: what print_r($_SESSION); give me:

Array
(
    [modx.user.0.resourceGroups] => Array
    (
        [web] => Array ( ) 
    ) 
    [modx.user.0.attributes]     => Array 
    (
        [web] => Array
        (
            [modAccessContext] => Array
            (
                [web] => Array
                (
                    [0] => Array
                    (
                        [principal] => 0 
                        [authority] => 0 
                        [policy]    => Array ( [load] => 1 ) 
                     ) 
                  )
            ) 
            [modAccessResourceGroup]       => Array ( ) 
            [modAccessCategory]            => Array ( ) 
            [sources.modAccessMediaSource] => Array ( )
        )
    ) 
[modx.user.contextTokens] => Array ( [web] => 2 ) 
[modx.web.user.token]     => modx507e2d531fedb5.20134124_250c977bed72111.66166942 
[modx.web.session.cookie.lifetime] => 0
[modx.user.2.resourceGroups] => Array ( [web] => Array ( [0] => 1 ) ) 
[modx.user.2.attributes] => Array ( [web] => Array ( [modAccessContext] => Array ( [web] => Array ( [0] => Array ( [principal] => 2 [authority] => 9999 [policy] => Array ( [load] => 1 [list] => 1 [view] => 1 [save] => 1 [remove] => 1 [copy] => 1 [view_unpublished] => 1 ) ) ) ) 
[modAccessResourceGroup] => Array ( [1] => Array ( [0] => Array ( [principal] => 2 [authority] => 9999 [policy] => Array ( [load] => 1 [list] => 1 [view] => 1 ) ) ) ) 
[modAccessCategory] => Array ( ) [sources.modAccessMediaSource] => Array ( ) ) ) ) 

Solution

  • read this - http://www.shawnwilkerson.com/modx-revolution/2012/03/10/programmatically-working-with-the-moduser-object/ sample of code that you need:

    /*
    * modUser Groups
    */
    $arry = print_r($user->getUserGroups(), true);
    $modx->toPlaceHolder('user.groups', $arry);