Joomla 3.x 使用 PHP 将用户设置为特定组

Joomla 3.x set User to a specific group with PHP

我只想将用户设置到特定组。我尝试了很多东西但没有帮助。一种解决方案对我有用,但我认为它已被弃用,因为在 Joomla 的管理页面中我看到其他组。

我的代码:

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', $_SERVER[ 'DOCUMENT_ROOT' ] );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );

$user =JFactory::getUser(joe); //joe is the username

$GroupJoomla = array('14'=>14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); //Method one, which I have found on documentations
print_r ( $user->get('groups')); //Succesfully sets, BUT if I check on the Joomla's admin page, the user is still on other group. Maybe this is a deprecated function and the Joomla didnt use it now.

$user->groups = $GroupJoomla; 
$user->save(); //Method two. Unfortunatelly this is always returns to false for me, dont know why. There is no error message. (Apache error log also empty)
print_r ($user->getAuthorisedGroups()); //This is the good to get the user's groups. This is correct.

if ($user->save() === false)
    {

        throw new Exception($user->getError(), 500); // This gives me this error: Error displaying the error page: Application Instantiation Error: Application Instantiation Error
    }

我做错了什么? :/

我没有 Joomfish,所以这不是 $user->save() returns false 的原因。

更新: 此代码在 $user->save() 上也是 returns false。

$uid = 1748;
$user =JFactory::getUser($uid); //joe is the username

$GroupJoomla = array(14); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); 
$user->groups = $GroupJoomla; 
$user->save(); 
jimport( 'joomla.access.access' );//Call the Access Class

/*If the below code is not there it wont save.*/
if ($user->save() === false){ //This is gives me false somehow

       throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit; //Receives Group id 15 (Not 14)

通过这两个代码示例,我得到了良好的用户对象。 $user->get('username');这给了我好的用户名。 (Joomla 3.6.5)

快速浏览一下您的代码,我认为这一行:

$user =JFactory::getUser(joe);

应该是:

$user =JFactory::getUser(5);

其中 5 是用户名为 "joe" 的用户的 ID。您无法使用函数 JFactory::getUser.

按用户名获取用户

或者,如果你真的想通过用户名获取用户,那么你应该使用:

$userId = JUserHelper::getUserId('joe'); //note that there are quotes around joe (in your code you omitted the quote)
$user =JFactory::getUser($userId);

希望这对您有所帮助。

正如@itoctopus 所说,您需要输入 id 而不是用户名,但还有一些其他错误需要注意,例如启动组变量。应该是

$GroupJoomla = array(14);

如果不止一组

$GroupJoomla = array(13,14);

然后获取您可以使用的组

$groups = JAccess::getGroupsByUser($id, false);

最后你的代码将是

<?php

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', 'C:\xampp\htdocs\joomla' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );
$uid = 404;
$user =JFactory::getUser($uid); //joe is the username

$GroupJoomla = array(4,6); //I want to change it's group to 14
$user->set('groups',$GroupJoomla); 
$user->groups = $GroupJoomla; 
$user->save(); 
jimport( 'joomla.access.access' );//Call the Access Class

/*If the below code is not there it wont save.*/
if ($user->save() === false){

        throw new Exception($user->getError(), 500);
}
$groups = JAccess::getGroupsByUser($uid, false);
print_r($groups); exit;