API16

JUserHelper/setUserGroups: Difference between revisions

From Joomla! Documentation

Doxiki (talk | contribs)
New page: ===Description=== Method to set the groups for a user. <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton<no...
 
m preparing for archive only
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
Method to set the groups for a user.
Method to set the groups for a user.


<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JUserHelper/setUserGroups|Edit Descripton]]<nowiki>]</nowiki>
</span>


{{Description:JUserHelper/setUserGroups}}
 
 


===Syntax===
===Syntax===
Line 83: Line 81:
</source>
</source>


<span class="editsection" style="font-size:76%;">
 
<nowiki>[</nowiki>[[SeeAlso:JUserHelper/setUserGroups|Edit See Also]]<nowiki>]</nowiki>
 
</span>
{{SeeAlso:JUserHelper/setUserGroups}}


===Examples===
===Examples===
<CodeExamplesForm />
=== Code Examples ===
<dpl>
<dpl>
  noresultsheader=\n
  noresultsheader=\n
  category=setUserGroups
  category=setUserGroups
  category=JUserHelper
  category=JUserHelper
  category=CodeExample
  namespace=CodeExample
  category=MethodExample
  category=MethodExample
  include=*
  include=*
  format= ,,,
  format= ,,,
</dpl>
</dpl>

Latest revision as of 02:16, 25 March 2017

Description

Method to set the groups for a user.



Syntax

static setUserGroups($userId, $groups)
Parameter Name Default Value Description
$userId $userId The id of the user.
$groups $groups An array of group ids to put the user in.

Returns

mixed Boolean true on success, on error.

Defined in

libraries/joomla/user/helper.php

Importing

jimport( 'joomla.user.helper' );

Source Body

public static function setUserGroups($userId, $groups)
{
        // Get the user object.
        $user = & JUser::getInstance((int) $userId);

        // Set the group ids.
        JArrayHelper::toInteger($groups);
        $user->groups = array_fill_keys(array_values($groups), null);

        // Get the titles for the user groups.
        $db = &JFactory::getDbo();
        $db->setQuery(
                'SELECT `id`, `title`' .
                ' FROM `#__usergroups`' .
                ' WHERE `id` = '.implode(' OR `id` = ', array_keys($user->groups))
        );
        $results = $db->loadObjectList();

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

        // Set the titles for the user groups.
        for ($i = 0, $n = count($results); $i < $n; $i++) {
                $user->groups[$results[$i]->id] = $results[$i]->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;
}



Examples

Code Examples