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
Watch
GitHub Core
Show your support for Zikula! Sign up at Github account and watch the Core project!
GitHub Modules
- mesteele101 responded to »ERR (3): E_USER_ERROR: Smarty error: [in pagesvar:pagesitem2en line XXX]…« 07:01 AM
- mazdev responded to »Pages 2.5.0 and updating - Page not found« 06:41 AM
- ehdwma created topic »Hide "Register new account" and change template to 3 col« 06:27 AM
- mesteele101 responded to »Zikula 1.3.3 - Selecting a category in Pages not working« 01:29 AM
- mdee created topic »How to implement returnpage ?« 01:00 AM
- nestormateo responded to »Fillters in Clip« 24. May
- damon responded to »Can the Updated Version Check be Turned Off (Z 1.3)« 24. May
Zikula Blog
- Anatomy of Open Source Projects on Mar 07
- Continuous Review on Mar 01
- Not Invented Here on Feb 24
- How to Contribute Your Code at Github on Jan 13
- 10 Steps to Coding-Nirvana: Tips for Successful Module Writing on Nov 12
- Submitting Bug Report Tickets That Get Results on Aug 17
- Cozi Tricks #1: Syntax Highlighting on Aug 07
Login
How do I get the group for a user??
-
- Rank: Legend
- Registered: Dec 11, 2002
- Last visit: Oct 21, 2009
- Posts: 11674
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 -
**unknown user**
- Rank: Softmore
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 413
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 -
- Rank: Legend
- Registered: Dec 11, 2002
- Last visit: Oct 21, 2009
- Posts: 11674
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 -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
With .760 you can call a groups module API function to get what group(s) a user belings too.
Code
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. -
**unknown user**
- Rank: Softmore
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 413
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 -
- Rank: Legend
- Registered: Dec 11, 2002
- Last visit: Oct 21, 2009
- Posts: 11674
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 -
- Rank: Freshman
- Registered: Nov 29, 2004
- Last visit: May 28, 2009
- Posts: 69
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 -
**unknown user**
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 1097
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);
}
-
- Rank: Freshman
- Registered: Nov 29, 2004
- Last visit: May 28, 2009
- Posts: 69
- Moderated by:
- Support
