Fork me on GitHub

How do I get the group for a user??  Bottom

  • Hell all long time no see, been crazy busy at werk with a huge portal project, anyway I am doing some brainstorming about a new module I want to create and I was looking through the API reference and couldn't find a call that would return the group a logged in user belonged to. The question then is how can I get that information inside the PN system???



    Thanks all
    -Sunadmn
  • Don't know of a specific API to do this, but I looked through the security API code, and found that the core obtains this information like so:

    Code

    // Get all groups that user is in
        $query = "SELECT $groupmembershipcolumn[gid]
                  FROM $groupmembershiptable
                  WHERE $groupmembershipcolumn[uid] IN ("
    . pnVarPrepForStore($uids) . ")";

        $result =& $dbconn->Execute($query);

        if ($dbconn->ErrorNo() != 0) {
            return array($userperms, $groupperms);
        }

        $usergroups[] = -1;
        if (!pnUserLoggedIn()) {
            // Unregistered GID
            $usergroups[] = 0;
        }
        while (list($gid) = $result->fields) {
            $result->MoveNext();
            $usergroups[] = $gid;
        }
        $usergroups = implode(",", $usergroups);


    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • Hmm ok well darn I was hoping to stream line this a bit via an API call, but I can't seem to find anything that gets back that info, guess I will have to do it the old way and call the DB manually then. Thanks for the quick answer HammerHead!!



    -SUNADMN
  • Maybe this should be a core API? Anyway, simply drop it into your module as an API and you get the same effect.

    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • With .760 you can call a groups module API function to get what group(s) a user belings too.

    Code

    $usersgroups = pnModAPIFunc('groups', 'user', 'getusergroups', array('uid' => $userid));


    As we gradaully get all modules to API compliance more modules will expose API's that can be used to save direct queries in your own code.

    -Mark

    --
    Visit My homepage and Zikula themes.
  • Mark that sounds awesome, on a side note since I am running .750 right now if I add code in my modules API call the get the gid how can I config ti to connect to the correct set of tables using:

    Code

    $dbconn =& pnDBGetConn(true);
    $pntable =& pnDBGetTables();


    I was under the assumtion that the pnDBGetTables() looked to the pntables file for the given module and then used that for it's query source, is this not correct??


    Thanks
    -SUNADMN
  • You'll need to assign directly from the $pntable array:

    Code

    $groupmembershiptable = $pntable['group_membership'];
        $groupmembershipcolumn = &$pntable['group_membership_column'];

        $grouppermtable = $pntable['group_perms'];
        $grouppermcolumn = &$pntable['group_perms_column'];


    --
    itbegins.co.uk - Zikula Consulting

    birtwistle.me.uk - Personal Blog


    Please read the Support Guide
  • Thank you HammerHead I wasn't so sure how to acomplish that I will try things out and let you know hwo it went.


    Thanks again
    -SUNADMN
  • here is a function that does it:

    Code

    /**
     * Get list of groups user belongs to
     *
     * @param   int     $userid     pn user id
     *
     * @return  array   array of arrays (group names and id's this user belongs to) / error array
     */

    function getGroupsForUser ($userId)
    {
        // Get database connection and tables definition
        list($db) = pnDBGetConn();
        $tables = pnDBGetTables();

        $grp = $tables['groups'];
        $grpc = &$tables['groups_column'];
        $gmem = $tables['group_membership'];
        $gmemc = &$tables['group_membership_column'];
       
        $sql = "SELECT $grpc[gid]   gid,
                      $grpc[name]   name "
    .
               "FROM $grp ".
               "LEFT JOIN $gmem ".
                    "ON $grpc[gid]=$gmemc[gid] ".
               "WHERE ".
                    "$gmemc[uid]=$userId";
       
        $db->SetFetchMode(ADODB_FETCH_ASSOC);          
        $result = $db->GetAll($sql);
                   
        if ($db->ErrorNo() != 0) {
            return error('getGroupId: ' . $db->ErrorMsg());
        }

        return($result);
       
    }


    Alexan

    www.cytopia.org
  • Removed useless quotes and added pnVarPrepForStore for the userid...

    Code

    function getGroupsForUser ($userId)
    {
        // Get database connection and tables definition
        list($db) = pnDBGetConn();
        $tables = pnDBGetTables();

        $grp   = $tables['groups'];
        $grpc  = &$tables['groups_column'];
        $gmem  = $tables['group_membership'];
        $gmemc = &$tables['group_membership_column'];
       
        $sql = "SELECT    $grpc[gid]  gid,
                          $grpc[name] name
                FROM      $grp
                LEFT JOIN $gmem
                ON        $grpc[gid]  = $gmemc[gid]
                WHERE     $gmemc[uid] = '"
    .pnVarPrepForStore($userId)."'";
       
        $db->SetFetchMode(ADODB_FETCH_ASSOC);          
        $result = $db->GetAll($sql);
                   
        if ($db->ErrorNo() != 0) {
            return error('getGroupId: ' . $db->ErrorMsg());
        }

        return($result);
       
    }


    wink
  • Thank you guys that looks like it will work just fine!!


    -SUNADMN

This list is based on users active over the last 60 minutes.