繁体   English   中英

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()返回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)

对于这两个代码示例,我都有一个很好的用户对象。 $ user-> get('用户名'); 这给了我很好的用户名。 (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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM