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
Watch
GitHub Core
Show your support for Zikula! Sign up at Github account and watch the Core project!
GitHub Modules
- internetking created topic »password problem« 25. May
- mesteele101 responded to »ERR (3): E_USER_ERROR: Smarty error: [in pagesvar:pagesitem2en line XXX]…« 25. May
- mazdev responded to »Pages 2.5.0 and updating - Page not found« 25. May
- ehdwma created topic »Hide "Register new account" and change template to 3 col« 25. May
- mesteele101 responded to »Zikula 1.3.3 - Selecting a category in Pages not working« 25. May
- mdee created topic »How to implement returnpage ?« 25. May
- nestormateo responded to »Fillters in Clip« 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
Detect the user group
-
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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
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
--
Visit My homepage and Zikula themes. -
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 22
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
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 -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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
--
Visit My homepage and Zikula themes. -
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 22
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 -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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
--
Visit My homepage and Zikula themes. -
- Rank: Team Member
- Registered: Mar 18, 2002
- Last visit: Oct 21, 2009
- Posts: 6606
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
--
Visit My homepage and Zikula themes. -
**unknown user**
- Rank: Registered User
- Registered: Mar 16, 2002
- Last visit: Oct 21, 2009
- Posts: 22
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:
But i had created a new user and insert it in a new group. When i visit with this user logged i recieve:
Code
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 -
- Rank: Registered User
- Registered: Jun 20, 2005
- Last visit: May 27, 2010
- Posts: 21
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
- Moderated by:
- Support
