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

时间:2017-03-27 19:26:07

标签: php joomla

我只想将用户设置为特定组。我尝试过很多东西但没有帮助。一个解决方案适用于我,但我认为它已被弃用,因为在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()返回false。

更新 此代码在$ user-> save()。

上也返回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)

使用这两个代码示例,我得到了良好的用户对象。 $用户>获得('用户名');这给了我很好的用户名。 (Joomla 3.6.5)

2 个答案:

答案 0 :(得分:1)

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

$user =JFactory::getUser(joe);

应该是:

$user =JFactory::getUser(5);

其中5是用户名为#34; joe"的用户名。您无法使用函数JFactory::getUser按用户名获取用户。

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

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

希望这有帮助。

答案 1 :(得分:1)

正如@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;