简体   繁体   中英

joomla not showing full map of user group php

So I have made some custom user groups in Joomla from backend. I put the registered users under these custom groups. Groups are like this-

Public
|— Users
|—|— Auditor
|—|— Custom_group1
|—|—|— Custom_group2
|—|—|—|— Custom_group3
|—|—|—|—|— Administrator
|—|—|—|—|—|— Super User
|—|— Custom_group4

So, say that user_b is under group Custom_group4 and also under Custom_group3 ,where the ids of custom group 3 and 4 are 2 and 11 respectively(say), The MySql database would look like-

Table: #__users

id       name           username           email               password     
523     User A          user_a          user_a@mysite.com      blablabla...
524     User B          user_b          user_b@mysite.com      blablabla... 

Table: #__user_usergroup_map

user_id(Foreign Key to #__users.id)       group_id(Foreign Key to #__usergroups.id)
    523                                       8
    524                                       2
    524                                       11

You can see there are two groups assigned for user id 524. Now I have this PHP code to get the group id for user(user id-524)

        $user = JFactory::getUser();
        $userId = $user->id;
        $db = JFactory::getDbo();
        $recursive = False;

        // Build the database query to get the rules for the asset.
        $query  = $db->getQuery(true);
        $query->select('ug.group_id');
        $query->from('#__user_usergroup_map AS ug');
        $query->leftJoin('#__users AS a ON a.id = ug.user_id');
        $query->where('a.id = '.(int) $userId);

        // Execute the query and load the rules from the result.
        $db->setQuery($query);
        $result = $db->loadResult();
        print_r((array)$result);

of which, I get the output-

Array([0] => 2)

but I want it to be Array([0] => 2, [1]=> 11)

Can anyone help me?

Solved.

I looked upon and find a similar function here , using the similar query(better as expected).

Here is part of that code-

        $query  = $db->getQuery(true);
        $query->select($recursive ? 'b.id' : 'a.id');
        $query->from('#__user_usergroup_map AS map');
        $query->where('map.user_id = '.(int) $userId);
        $query->leftJoin('#__usergroups AS a ON a.id = map.group_id');

        // If we want the rules cascading up to the global asset node we need a self-join.
        if ($recursive) {
                $query->leftJoin('#__usergroups AS b ON b.lft <= a.lft AND b.rgt >= a.rgt');
        }

        // Execute the query and load the rules from the result.
        $db->setQuery($query);
        $result = $db->loadResultArray();

The only difference lies in the way of obtaining table information.

I would now simple use this as-

jimport( 'joomla.access.access' );
$groups = JAccess::getGroupsByUser(526, False); //526 is the user_id for user b
print_r($groups);

and this did what I want-

Array ( [0] => 2 [1] => 11 )

Thanks google.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM