我试图检查一个用户是否属于一个名为tellers的组使用它 功能。我似乎无法做对
function isMember($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($this->DB_HOST, $this->DB_USER, $this->DB_PASS)
or die("Could not connect : " . mysql_error());
mysql_select_db($this->DB_NAME) or die("Could not select database");
// extract all groups user's a member of
$query = "Select q.title FROM (#__user_usergroup_map as p)
LEFT JOIN (#__usergroups as q)
on p.group_id=q.id
WHERE p.user_id=$userid;";
// remove prefix etc.
$SQL = $this->cleanSQL($query);
$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;
}
这不起作用;它从某个地方失败了。 如果某个成员是某个特定组的成员,是否有一个快捷方式?
答案 0 :(得分:5)
有一个捷径。
in_array($group_id, $user->getAuthorisedGroups() )
因此,您只需知道您的组ID并拥有用户对象。