繁体   English   中英

joomla3在joomla之外访问当前用户对象

[英]joomla3 access current user object outside joomla

我想将登录的用户名插入变量中,以便在页面中使用,该页面在我的Joomla注入的根目录中,并通过包装器/ iFrame显示。

在主页中设置cookie无效,使用$ user =&JFactory :: getUser();访问用户对象的推荐方法也不起作用。 在下面的代码之后(下面的链接)。 有任何想法吗?

define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE',$_SERVER['DOCUMENT_ROOT'].DS. basename(dirname(__DIR__)) );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();

//optional use depend on requirement 
jimport( 'joomla.user.user');
jimport( 'joomla.session.session');
jimport( 'joomla.user.authentication');

在Joomla之外访问会话数据

使用该代码,您将创建和初始化新的Joomla应用程序,因此您没有任何“当前”用户。 您必须通过代码执行登录,这可以完成,但是不幸的是需要纯文本用户名和密码。

$login_result = $mainframe->login(array(
    'username' => $username,
    'password' => $password
));

if (!$login_result)
{
    throw new \Exception("Unable to set current user!", 401);
}

之后,新的Joomla应用程序将拥有一个当前用户,您可以执行通常的JFactory::getUser()

如果没有用户名和密码(如我所愿),则有两种选择:

  • 使用Plug Master User插件,以便您可以使用管理员密码登录任何用户
  • 创建一个自定义的身份验证插件,在确保登录请求来自您的外部页面之后,该身份验证插件无需验证密码即可进行身份验证

谢谢大家帮助。 Elin提出的解决方案是将用户名放在url中。 为此,它涉及Joomla核心的破解,因为模板替代似乎不适用于此文件。

查找这个文件

组件\\ com_wrapper \\视图\\ wrapper \\ tmpl \\ default.php

//Insert line below line after this
defined('_JEXEC') or die;

$user = JFactory::getUser();

围绕44号线

//change 
src="<?php echo $this->escape($this->wrapper->url);

//to this
src="<?php echo $this->escape($this->wrapper->url). "?userId=" . $user->id; ?>"


//then in the file in the iframe

$userid2 = $_GET["userId"];

//or if you want to store the user name in a session variable

$userid3 = $_SESSION['userId'];

暂无
暂无

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

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