【问题标题】:Joomla 3.x set User to a specific group with PHPJoomla 3.x 使用 PHP 将用户设置为特定组
【发布时间】:2017-03-27 19:26:07
【问题描述】:

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

【问题讨论】:

    标签: php joomla


    【解决方案1】:

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

    $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;
    

    【讨论】:

    • 感谢您的帮助!我已经改变了我的阵列,但没有帮助。我已将您的整个代码复制到我的服务器并替换为 Joe 的 ID,但仍然相同。 $user->save() 返回 false,因此无法保存用户修改。我已经注释掉了异常行。该用户仍在其他组中。我已经用你的代码更新了我的问题
    • @csib 复制整个代码后,您更改了基本路径还是保持不变?不要注释掉任何行并尝试使用给定的代码。它对我来说很好。我已经在我的服务器上进行了测试。
    • 查看我的问题(我已更新)。我已将其更改为定义('JPATH_BASE',$_SERVER['DOCUMENT_ROOT'])然后将 UID 更改为 1748。然后当我调用它时,我收到错误显示错误页面:应用程序实例化错误:应用程序实例化错误
    • @csib 获取硬编码的基本路径,输入并检查。如果您的路径不正确,它将永远无法工作。检查这个页面,我已经回答了类似的问题stackoverflow.com/questions/43079935/…
    【解决方案2】:

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

    $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);
    

    希望这会有所帮助。

    【讨论】:

    • 在我的测试中,$user =JFactory::getUser(joe) 工作正常,引号不知何故 joomla 什么也没返回,这就是我删除引号的原因。没有它们,Joomla 通过用户名找到用户,我得到了对象。顺便说一句,我已将代码更改为使用 ID,但没有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 2014-02-03
    • 2019-10-08
    相关资源
    最近更新 更多