Zikula: A Flexible Open Source Content Management System
home | forum | contact us

Dizkus

Bottom
Detect the user group
  • Posted: 06.07.2006, 14:58
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    Hello
    I need to "detect" or know in what group is a user that is actually login and visiting my mod. for make different results in the user-view part of my module
    I mean:
    exampleuser is in examplegroup.
    thanks in advance!



    edited by: SiCk949, Jul 06, 2006 - 09:58 AM
  • Posted: 06.07.2006, 15:43
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    Firstly bear in mind that users can be part of many groups - not just one.

    You can obtain a list of the groups a user is a member of via the following API where the uid variable is the user id to obtain the group memberships for.

    Code

    $usergroups = pnModAPIFunc('Groups', 'user', 'getusergroups', array('uid' => $uid));


    If you need to obtain the user id from a given username use the following API where uname contains the user name.

    Code

    $uid = pnUserGetIDFromName($uname);


    -Mark
  • Posted: 06.07.2006, 15:46
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    thanks mark.
    I will test it now.
  • Posted: 06.07.2006, 16:06
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    mmm how to get the actual logged "$uname"?
    I don't know these functions :s
    I read about pnUserGetAll, but only for uname?

    edit
    Finally I fixed with that:

    Code

    if (pnUserLoggedIn()) {
        $name = pnUserGetVar('name');
        if (isset($name)) {
            $uid = pnUserGetIDFromName($name);
            $gruposuser = pnModAPIFunc('Groups', 'user', 'getusergroups', array('uid' => $uid));
            // ASSING THE FIRST? GROUP TO A VAR
        }
    }


    But, well, I need the group because I wanna show a HTML block for a concrete group. Then, I ask if the actual group user is equal to the groupname that i wanna get access. But, how to do? i don't know what the array contains :S



    edited by: SiCk949, Jul 06, 2006 - 11:25 AM
  • Posted: 06.07.2006, 16:13
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    I'd assumed that you'd already got the user name. In which case ignore the bit about converting username's to userid's. Simply use the following API call to get the user id directly

    Code

    $uid = pnUserGetVar('uid');


    -Mark
  • Posted: 06.07.2006, 16:27
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    I used the previous post script, my problem now is in my previous post
    I thought about comparing each array field with the group-that-have-access name
    Thanks in advance markwest!



    edited by: SiCk949, Jul 06, 2006 - 11:32 AM
  • Posted: 06.07.2006, 16:33
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    Please don't edit posts like that. It makes it difficult to follow the converstation now and renders the discussion useless for people looking at this thread later.

    The groups API call returns an array of group records (id and name). You'd need to loop through the array checking the group name against your known group.

    -Mark
  • Posted: 06.07.2006, 16:38
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    Ok, sorry :(
    But, how to show it? its a multidimensional or an asociative array?
    (sorry for my english and for my low knows about this)
  • Posted: 06.07.2006, 16:53
     
    rank:
    Moderator Moderator
    registered:
     March 2002
    Status:
    offline
    last visit:
    26.08.08
    Posts:
    7720
    If you want to look at the contents of an array for debugging purposes then use var_dump. I'd suggest reviewing the PHP manual for available functions.

    -Mark
  • Posted: 06.07.2006, 17:59
     
    SiCk949
    rank:
    Freshman Freshman
    registered:
     June 2006
    Status:
    offline
    last visit:
    30.10.06
    Posts:
    28
    Well, its strange. I use this function

    Code

    <?php
    function smarty_function_sacargrupo($args)
    {
    if (pnUserLoggedIn()) {
        $name = pnUserGetVar('name');
        if (isset($name)) {
            $uid = pnUserGetIDFromName($name);
            $gruposuser = pnModAPIFunc('Groups', 'user', 'getusergroups', array('uid' => $uid));
            $grupo = $gruposuser[0]['name']; // I will use here a "in_array", for compare strings, and for that this func. recive arguments.
            var_dump($gruposuser); // only for check results
        }
    }
    return $name;
    }
    ?>


    and i call it on a pnrender template. It works correctly. When I visit the module with the admin user, all its ok, I recieve:

    Code

    array(1) { [0]=>  array(2) { ["gid"]=>  string(1) "2" ["name"]=>  string(6) "Admins" } } Admin


    But i had created a new user and insert it in a new group. When i visit with this user logged i recieve:

    Code

    array(0) { }

    In the DB the user and group are created correctly and linked correctly on group_membership table...
    The same for another user on another group. It seems this works only with the administrator, dont know why :S

    The problem is that it not capture the name with "pnUserGetVar('name')". It do with the admin user (dump show: string(5) "Admin" Admin) but with another users it shows: string(0) ""

    thanks!!!
    .
    .
    .
    .
    .
    edit:
    I fixed it (for users that read this later).
    I only used the id for recover the user group and all works correctly (i hope xD!)

    Code

    function smarty_function_sacargrupo($nombreg)
    {
        if (pnUserLoggedIn()) {
            extract($nombreg);
            $uid = pnUserGetVar('uid');
            $gruposuser = pnModAPIFunc('Groups', 'user', 'getusergroups', array('uid' => $uid));
            for ($i=0;$i<(count($gruposuser));$i++)
            {
                if ($nombreg == ($gruposuser[$i]["name"])) {
                  return true;
                  //return $gruposuser[$i]["name"];
                }
            }
        }
    return false;
    }

    I check it and no problems detected.



    edited by: SiCk949, Jul 06, 2006 - 01:56 PM
  • Posted: 24.08.2006, 09:20
     
    wvoigt
    rank:
    Freshman Freshman
    registered:
     June 2005
    Status:
    offline
    last visit:
    22.03.07
    Posts:
    5
    I found I could do this in a template with the following code:

    Code

    <!--[pnmodapifunc modname=Groups func=getusergroups uid=$uid assign=thegroups]-->
    <ul>
    <!--[foreach from=$thegroups item=thegroup]-->
        <li><a href=<!--[pnmodurl modname="Groups" type="admin" func="groupmembership" gid=$thegroup.gid|pnvarprepfordisplay]-->>
         <!--[$thegroup.name|pnvarprepfordisplay]--></a></li>
    <!--[/foreach]-->
    </ul>

    You just need to make sure $uid is set with the uid of the person you want to know what groups they belong too.
    I've used this in a modified Members_list view so I can see the groups of each person that has registered at the site.

    --
    - Wendel

Extensions Moderation

Main Menu

Extensions Database

Documentation

Development

Login

Donate to Zikula