用户登录到前端 - 自动登录到后端的插件


User logs into frontend - plugin to automatically be logged into backend

我需要创建一个系统插件(没有身份验证插件!),其中自动登录到前端的用户也会登录到后端。(用户有权通过/admin 登录后端。

我尝试通过您在下面看到的非常基本的代码来执行此操作,结果是积极的,但是如果我转到后端,用户仍然需要登录。

在会话表中设置了后端会话行,但"guest"字段设置为 1 而不是 0,用户 ID 设置为 0 而不是正确的 id。

如何做到这一点?

function onAfterInitialise() {
if(JFactory::getUser()->get('id')) {  // logged in? 
    $credentials = array();
    $credentials['username'] = "walter"; // hardcoded first
    $credentials['password'] = "123"; // hardcoded first
    $options = array();
    $options['action'] = 'core.login.admin'; 
    $result = $app->login($credentials, $options);  // this seams to work
    if (!($result instanceof Exception)) {
        $app->redirect("www.bummer.de");
    } 
}

除此之外,这是一个非常糟糕的主意,正如这个问题中提到的,Joomla!被实现为两个应用程序,前端(/index.php)和后端应用程序(/administrator/index.php)。

在提供的代码中,您没有显示初始化$app的位置,所以我猜它可能类似于$app->JFactory::getApplication('site');.

要登录到管理应用程序,您需要获取它而不是前端客户端应用程序,例如

$adminApp->JFactory::getApplication('administrator');
$result = $adminApp->login($credentials, $options);

注意:这是未经测试的代码,只是输入到堆栈溢出...应该是对的。