0

I want to insert the logged in username into a variable for use in a page which is in the root directory of my Joomla instillation and displayed through a wrapper/iFrame.

Setting cookies in the main page does not work nor does the recommended way of accessing the user object with $user =& JFactory::getUser(); after the code below (link below). Any ideas?

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

Accessing session data outside Joomla

Community
  • 1
  • 1
  • If I understand you correctly, you don't actually have or want to have authentication to the new application, you just want the specific information which is the user name right? It's tricky because you could have 20 users at once, but if that is all you really need I think I would cache the name and then retrieve the cache. The thing you will have to do is make sure that the cache name is unique maybe by using a token and passing that token into the iframe url request. – Elin Jul 05 '15 at 18:18
  • Thanks for your reply. You are correct that the authentication has already been done by Joomla. When a user logs into Joomla it is easy find the current user details with the lines of code below. The problem is that I cannot seem to find a way of passing this information to the page in the iFrame. $user = JFactory::getUser(); echo "$user->name"; – Fear Ialtóg Jul 05 '15 at 20:12

2 Answers2

0

With that code you are creating and initializing a new Joomla application, so you don't have any "current" user. You have to perform the login via code, which can be done but unfortunately requires the plain-text user and password.

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

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

After this, the new Joomla application will have a current user and you can do the usual JFactory::getUser()

If you don't have username and password (as I hope) you have two choices:

  • use the Plug Master User plugin so you can login any user with your administrator password
  • create a custom authentication plugin that, after making sure the login request comes from your external page, allows authentication without checking the password
Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30
  • You might be able to share a session rather than reauthenticating. But passing information into an iframe is not simple. – Elin Jul 05 '15 at 18:02
  • Thanks for your answer. The user is logged in using Joomla and the user object is available while inside Joomla. The problem I have is in trying to get something like the user id on to the page in the iframe. – Fear Ialtóg Jul 05 '15 at 20:15
  • Ok, I understand now. The problem is not really related to Joomla, rather is "how to share data between two applications?". As @Elin said, you may be able to share session, or you can use a totally different approach and use a custom cookie or local storage. – Francesco Abeni Jul 05 '15 at 20:59
  • http://stackoverflow.com/questions/536538/pass-value-to-iframe-from-a-window has some suggestions. I think the best is to add `'&user=' . $name` to the url and then since you are in Joomla in the other application get the name from the input object. – Elin Jul 06 '15 at 01:38
  • @Elin Thanks. That proved to be the solution. – Fear Ialtóg Jul 08 '15 at 09:37
0

Thanks guys for your help. The solution as proposed by Elin is to put the user name in the url. To do this it involves a hack of the Joomla core as template overrides do not appear to work for this file.

Find this file

components\com_wrapper\views\wrapper\tmpl\default.php

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

$user = JFactory::getUser();

Around line number 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'];