API16

JUserHelper/addUserToGroup

From Joomla! Documentation

< API16:JUserHelper
Revision as of 17:36, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Method to add a user to a group. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<nowik...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

The "API16" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.

Description[edit]

Method to add a user to a group.

[Edit Descripton]

Template:Description:JUserHelper/addUserToGroup

Syntax[edit]

static addUserToGroup($userId, $groupId)
Parameter Name Default Value Description
$userId $userId The id of the user.
$groupId $groupId The id of the group.

Returns[edit]

mixed Boolean true on success, on error.

Defined in[edit]

libraries/joomla/user/helper.php

Importing[edit]

jimport( 'joomla.user.helper' );

Source Body[edit]

public static function addUserToGroup($userId, $groupId)
{
        // Get the user object.
        $user = new JUser((int) $userId);

        // Add the user to the group if necessary.
        if (!array_key_exists($groupId, $user->groups))
        {
                // Get the title of the group.
                $db     = &JFactory::getDbo();
                $db->setQuery(
                        'SELECT `title`' .
                        ' FROM `#__usergroups`' .
                        ' WHERE `id` = '. (int) $groupId
                );
                $title = $db->loadResult();

                // Check for a database error.
                if ($db->getErrorNum()) {
                        return new JException($db->getErrorMsg());
                }

                // If the group does not exist, return an exception.
                if (!$title) {
                        return new JException(JText::_('Access_Usergroup_Invalid'));
                }

                // Add the group data to the user object.
                $user->groups[$groupId] = $title;

                // Store the user object.
                if (!$user->save()) {
                        return new JException($user->getError());
                }
        }

        // Set the group data for any preloaded user objects.
        $temp = & JFactory::getUser((int) $userId);
        $temp->groups = $user->groups;

        // Set the group data for the user object in the session.
        $temp = & JFactory::getUser();
        if ($temp->id == $userId) {
                $temp->groups = $user->groups;
        }

        return true;
}

[Edit See Also] Template:SeeAlso:JUserHelper/addUserToGroup

Examples[edit]

<CodeExamplesForm />