如何将外部脚本限制为以管理员身份登录的用户?

时间:2014-11-19 12:24:16

标签: session joomla joomla2.5

我正在创建一个新页面,该页面将从Joomla 2.5中的“管理员”页面重定向。当我在浏览器中键入URL时,将显示该页面。我需要限制视图,使其仅在管理员登录其帐户时才可见。你能帮我解决这个问题吗?

这是我的代码:

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;

当我从我的网站登录时,此代码可以正常工作但我登录时使用 管理员界面此代码显示空值。我不知道为什么。有没有 错了吗? 当我使用管理员界面登录时,我得到此输出:

   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] => 

当我从我的网站(会员登录)以网站用户身份登录时,我得到了这个:

       JUser Object
      (
          [isRoot:protected] => 
          [id] => 2
          [name] => name of user
          [username] => username
          [email] => username@gmail.com
          [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

过去8天我遇到了这个问题。你能帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

确保你开始加载Joomla(参见Joomla Stackexchange的this question),

更新添加了完整的脚本。

将此代码保存在位于Joomla文件夹根目录的php文件中。它会工作,你会看到输出。从那里,你可以调整它的位置,以便与你想要完成的任何事情一起工作。

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

\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/>';
}