How can I restrict an external script to users that are logged in as an Administrator?

722 Views Asked by At

I am creating a new page that is getting redirected from the Administrator page in Joomla 2.5.The page is getting displayed when i type the URL in browser . I need to restrict the view such that it can be visible only when the administrator logs into his account . Can you help me on this ?

This is my code:

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$app = JFactory::getApplication('site');
$user = JFactory::getUser();
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';

//$app->isAdmin() always through null value

print_r($user);

echo $user->username;

When i login from my site this code works perfectly but when i login using administrator interface this code display's null value. i don't know why. is there any mistake in it? I get this output when i login using Administrator interface:

   JUser Object
   (
       [isRoot:protected] => 
       [id] => 0
       [name] => 
       [username] => 
       [email] => 
       [password] => 
       [password_clear] => 
       [usertype] => 
       [block] => 
       [sendEmail] => 0
       [registerDate] => 
       [lastvisitDate] => 
       [activation] => 
       [params] => 
       [groups] => Array
       (
        )

       [guest] => 1
       [lastResetTime] => 
       [resetCount] => 

when i login as a site user from my site (member login) i get this:

       JUser Object
      (
          [isRoot:protected] => 
          [id] => 2
          [name] => name of user
          [username] => username
          [email] => [email protected]
          [password] => 695c263968014c89bbf3159aa4:YoBWR6uzmUBMcqfj5hPzCIp7a6maYd
          [password_clear] => 
          [usertype] => 
          [block] => 0
          [sendEmail] => 1
          [registerDate] => 2014-11-24
          [lastvisitDate] => 2014-11-25 15:29:45
          [activation] => 
          [params] => {}
          [groups] => Array
          (
              [2] => 2
          )

          [guest] => 0

I suffering with this problem for last 8 days. can you help me with this..

1

There are 1 best solutions below

12
On BEST ANSWER

Be sure you're loading Joomla to start with (See this question at Joomla Stackexchange),

Update Added complete script.

Save this code in a php file located in the root of your Joomla folder. It will work, and you'll see the output. From there, you can adapt it's location and such to work with whatever you're trying to accomplish.

if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}
defined('DS') or define('DS', DIRECTORY_SEPARATOR);
$app = JFactory::getApplication('site');
$user = JFactory::getUser();
$groups = $user->groups;

if ($app->isSite()) echo 'Running from Joomla Front End site<Br/>';
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';
if($user->id) {
    echo $user->username.' is logged in<Br/>';
    if (isset($groups[8])) echo " - User is a Super User <Br/>";
    if (isset($groups[7])) echo " - User is an Administrator <Br/>";
    if (isset($groups[6])) echo " - User is an Manager <Br/>";
}else{
    echo 'Not logged in<Br/>';
}

if(!isset($groups[7])) die("You must be an administrator to run this");

VARIATION FOR USE IN \administrator

if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}

//$app = JFactory::getApplication('site');
$app = JFactory::getApplication('administrator');
if ($app->isSite()) echo 'Running from Joomla Front End site<Br/>';
if ($app->isAdmin()) echo 'Running from Joomla Administrator site <Br/>';

$user = JFactory::getUser();
if($user->id) {
    echo $user->username.' is logged in<Br/>';
    $groups = $user->groups;
    if(isset($groups[8])) {
        die("You are a Super User - only Administrators can run this");
        // Do your superuser coding here
    }elseif(isset($groups[7])) {
        die("You are an administrator - you can run this");
        // Do your admin coding here
    }
}else{
    echo 'Not logged in<Br/>';
}