1

I am working on a component where I want to show all users of a specific usergroup. Right now I found two solutions for this but I'm not feeling comfortable with both of them.

Solution 1

$usersID = JAccess::getUsersByGroup(3);
$users = array();
foreach($usersID as $cUserID)
{
    $users[] = JFactory::getUser($cUserID);
}

This one seems to produce two database queries every time JFactory::getUser($cUserID) is called. I really don't want this.

Solution 2

function inside model

function getUsers()
{
    if(!isset($this->users))
    {
        $groupID = 3;

        $db    = JFactory::getDbo();
        $query = $db->getQuery(true);

        $select = array( 'users.id', 'users.name');
        $where = $db->quoteName('map.group_id') . ' = ' . $groupID;

        $query
            ->select($select)
            ->from( $db->quoteName('#__user_usergroup_map', 'map') )
            ->leftJoin( $db->quoteName('#__users', 'users') . ' ON (map.user_id = users.id)' )
            ->where($where);

        $db->setQuery($query);
        $this->users = $db->loadObjectList();
    }

    return $this->users;
}

This one works like a charm but I feel there should be a "more Joomla! way" of doing this. I don't like working on their tables.

Right now I'm going with solution 2 but i really wonder if there is some better way to do it.

Tom
  • 188
  • 8
  • You can, but that means creating your own registry. You want this. If you want this, you have to create new tabel, a new ACL, alterate the login form. For security reasons, I would advise doing, but it is time consuming. – Jose Marques Apr 10 '17 at 00:58
  • Hey Jose, I don't think I'am going to mess around with ACL and stuff. Getting all users of a user group just sounds so basic for me that i thought there might be something like $userGroups->getUsers(id); where Joomla cares about security and consistency etc. – Tom Apr 19 '17 at 12:50
  • Yes you can create with a user group, but how are you going to do it? In the meantime, do not forget to create a component to manage all in the back office. One advice, creates a record table and a table for the user group. This way you can simplify your component and increase the security of your site.Any questions you have, ask me, I'll try to help. – Jose Marques Apr 19 '17 at 16:50
  • @Tom Thank you for posting your solutions I used the second one and right from the gecko it worked. After reading through several forums this one is the one that did the trick. – purple11111 Aug 27 '20 at 12:16

0 Answers0