Code: Select all
$user =& JFactory::getUser();
$user_type = $user->get('usertype');
What has it been replaced by?
And how does that work when users can be assigned to more then one usergroup?
Moderator: ooffick
Code: Select all
$user =& JFactory::getUser();
$user_type = $user->get('usertype');
Code: Select all
jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser(43);
print_r($groups);
Code: Select all
jimport( 'joomla.user.helper' );
$groups = JUserHelper::getUserGroups(43);
print_r($groups);
Code: Select all
$user =& JFactory::getUser();
$groups = isset($user->groups) ? $user->groups : array();
print_r($groups);
You mean that the array holds the values 1 and 2?carsten888 wrote:returns:Code: Select all
jimport( 'joomla.access.access' ); $groups = JAccess::getGroupsByUser(43); print_r($groups);
Array ( [0] => 1 [1] => 2 )
which is an array of all usergroup id's including those which are not in the usergroup_map table (the usergroups which checkbox go disabled in the user-manager, example: public)
Code: Select all
jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser(43, false);
print_r($groups);
Code: Select all
jimport( 'joomla.user.helper' );
$groups = JUserHelper::getUserGroups(43);
print_r($groups);
Code: Select all
JAccess::getGroupsByUser(43, false);
Code: Select all
jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser($Joomla_userId, false);
print_r($groups);
Code: Select all
//only visible for super-users (= group 8)
if(in_array(8, $groups)){
echo 'only visible for super-users';
}
Code: Select all
if(in_array(8, $groups) && in_array(1, $groups)){
echo 'only visible for users assigned to super-users AND public';
}
If you get it done please share a link to it so we can all see how well it works.Quickheads wrote:Carsten/Sovai,
Beautiful! Thanks for the quick responses!
I will give this a try. I believe this is the final missing piece in a google map project I am working on for my site! This will help me show different versions of the map to different user types.
Again thank you very much.
Warm regards,
Dan Yager
Code: Select all
<?php
/*
* Function isAdmin($userid, $group) returns true if logged-in user is a member of string or array $group
* Group names are case-insensitive. The array $privGroups contains the groups who will always be
* returned as admins
*
* Temporary (one hopes) fix for a deficiency in Joomla
*
*/
// Return true if $userid is a member of any of the $groups array, or is in $privGroups
function isAdmin($userid, $group){
$privGroups = array('Administrator', 'Super Users'); // Always return true for these
if(is_array($group))$mygroup = $group;
else $mygroup[0]=$group;
$j=0;
$n=count($mygroup);
for($i=count($mygroup); $i<($n+count($privGroups)); $i++) {
$mygroup[$i] = $privGroups[$j]; // Add "always" admins to argument admins
$j++;
}
// DB access credentials: $db, $dbhost, $login, and $pw need to be defined here
$link = mysql_connect($dbhost, $login, $pw)
or die("Could not connect : " . mysql_error());
mysql_select_db($db) or die("Could not select database");
// extract all groups user's a member of
$SQL = "Select q.title FROM (j16_user_usergroup_map as p)
LEFT JOIN (j16_usergroups as q)
on p.group_id=q.id
WHERE p.user_id=$userid;";
$res=mysql_query($SQL) or die('<p>Groups select query failed<br />' . mysql_error() . "<br />$SQL</p>");
if (mysql_num_rows($res) == 0) { // No groups? Then user clearly isn't a mamber of "$group"
if($termlink) mysql_close($link);
return false;
}
while ($line = mysql_fetch_array($res, MYSQL_ASSOC)) {
extract($line);
$userGroups[]=$title;
}
mysql_close($link); // Close link
for($i=0; $i<count($userGroups); $i++){
for($j=0; $j<count($mygroup); $j++) {
if(strtolower($userGroups[$i])==strtolower($mygroup[$j])) return true;
}
}
return false;
}
?>
Code: Select all
function check_group($group, $inherited){
$user =& JFactory::getUser();
$user_id = $user->get('id');
if($inherited){
//include inherited groups
jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser($user_id);
}else{
//exclude inherited groups
$user =& JFactory::getUser($user_id);
$groups = isset($user->groups) ? $user->groups : array();
}
$return = 0;
if(in_array($group, $groups)){
$return = true;
}
return $return;
}
Code: Select all
function check_group($group, $inherited){
$user =& JFactory::getUser();
$user_id = $user->get('id');
if($inherited){
//include inherited groups
jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser($user_id);
}
else {
//exclude inherited groups
$user =& JFactory::getUser($user_id);
$groups = isset($user->groups) ? $user->groups : array();
}
return (in_array($group, $groups))?true:0;
}
Code: Select all
<?php $varGroup = check_group('Registered', FALSE); ?>
Code: Select all
$user = & JFactory::getUser();
// See if we have a logged-in user
$notLoggedIn = ($user->guest) ? true : false; // Set variable indicating guest status
Thanks, this is helpful, how do I get the group of the current user?jimbrooking wrote:It's been a while but: If you look at carsten888's code and my followup, it seems the $group argument to the function is the id number of the group you're trying to check against, not its name.
However, if you're just trying to see if the Joomla user is registered or not, this code should work:Code: Select all
$user = & JFactory::getUser(); // See if we have a logged-in user $notLoggedIn = ($user->guest) ? true : false; // Set variable indicating guest status
Code: Select all
SELECT * FROM JOS_user_usergroup_map WHERE user_id='current logged-in-user-id' AND group_id='10';
Code: Select all
if (mysqli_num_rows($result) > 0 ) // found current user in group 10
{display content};
else
{don't do anything};
Thanks for the reply, I'm playing around with this now, this site is 3.3.6, maybe I posted in the wrong forum, but this was the only thread I could find regarding this.jimbrooking wrote:In Joomla 2.5: Look at the database table JOS_user_usergroup_map - two fields: user_id and group_id. Run a query likethenCode: Select all
SELECT * FROM JOS_user_usergroup_map WHERE user_id='current logged-in-user-id' AND group_id='10';
Code: Select all
if (mysqli_num_rows($result) > 0 ) // found current user in group 10 {display content}; else {don't do anything};
Code: Select all
<?php if(in_array(10, $User->groups)):?>