正在验证管理员是否已登录OpenCart


Validating whether an admin is logged in or not in OpenCart

我目前正在开发一个API,以从我们的数据库中检索JSON形式的数据。我决定在检索数据之前强制进行验证。

因为我使用的是OpenCart,所以为我提供了一个名为isLogged()的函数。

然而,据我所知,有两个isLogged():

  1. 用户的isLogged(),即管理员,在此路径下:

    opencart/upload/system/library/user.php

  2. 客户的isLogged(),在此路径下:

    opencart/upload/system/library/customer.php

isLogged()的使用(据我目前所知)取决于我的控制器在哪里。我目前正在/opencart/upload/catalog/controller/api/order.php下开发,根据定义,它在customer侧。因此,我不能使用$this->user->isLogged(),而必须使用$this->customer->isLogged(),这与管理员验证的观点背道而驰。

如果我尝试使用$this->user->isLogged(),我会得到以下错误:

PHP Fatal error: Call to a member function isLogged() on a non-object in /vagrant/opencart/upload/catalog/controller/api/order.php

在不将控制器移动到admin的情况下,如何验证管理员是否已登录?还是我的分析完全错了?

编辑:看来我误解了项目要求。这与OpenCart的用户验证无关,而是来自我们另一个内部系统的用户验证。

$this->session->data['isAdminLogin'] = 0;
    if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) { 
        $this->session->data['token'] = md5(mt_rand());
        $this->session->data['isAdminLogin'] = 1;
        if (isset($this->request->post['redirect'])) {
            $this->redirect($this->request->post['redirect'] . '&token=' . $this->session->data['token']);
        } else {
            $this->redirect($this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'));
        }
    }

将公共/login.php索引函数代码替换为以下代码。现在用这个检查adminlogin

$this->session->data['isAdminLogin']

版本2.XX

使用在/index.php 中插入此代码

// User
$registry->set('user', new User($registry));

并使用此代码检查登录管理

    //Checks to see if the logged in user has permission to view or edit a particular admin page.
    $this->user->isLogged();
    //Checks to see if the admin user is logged into their account.
    $this->user->getId();
    //Gets the ID number of the administrator account.
    $this->user->getUserName();

Opencart 1.5.x

例如,如果您希望页面的访问者不看到仅用于管理的语言选择器,您将输入/controlog/controller/common/header.php

      // Show if logged in as admin
        $this->load->library('user');
        $this->user = new User($this->registry);

在"受保护的函数索引(){"之后说并保存它。

然后打开你的模板:

/catalog/view/theme/*/template/common/header.tpl

<?php echo $language; ?>

您将替换:

<?php  if($this->user->isLogged()){echo $language;} ?>

准备好了。。。

// In Version 3.X
// logged in user id
echo (int)$this->user->getId();
// OpenCart 3 (ocStore 3.0.3.7)
$user = new User($this->registry);
$this->registry->set('user', $user);
if ($this->user->isLogged()) {
  // code
}