theGreenCabbage
theGreenCabbage

Reputation: 4845

Validating whether an admin is logged in or not in OpenCart

I am currently working on an API to retrieve data in the form of JSON from our database. I've decided to force validation before the retrieval of data.

Because I am using OpenCart, I am offered the function called isLogged().

However, from what I am aware, there are two isLogged():

  1. isLogged() for the user, which is the admin, under this path:

    opencart/upload/system/library/user.php

  2. isLogged() for the customer, under this path:

    opencart/upload/system/library/customer.php

The use of isLogged() is (as far as I know right now) dependent on where my Controller is. I am currently developing under /opencart/upload/catalog/controller/api/order.php, which by definition is in the customer side. So, I cannot use $this->user->isLogged(), and instead, must use $this->customer->isLogged(), which is against the point of admin validation.

If I try to use $this->user->isLogged(), I get the following error:

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

How do I, without moving my Controller to admin, validate whether an admin has logged in or not? Or am I totally wrong in my analysis?

EDIT: Looks like I misunderstood the project requirement. This has nothing to do with OpenCart's user validation, but the user validation from another one of our internal systems.

Upvotes: 1

Views: 4012

Answers (5)

// OpenCart 3 (ocStore 3.0.3.7)

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

if ($this->user->isLogged()) {
  // code
}

Upvotes: 0

Kamlesh
Kamlesh

Reputation: 6135

// In Version 3.X

// logged in user id

echo (int)$this->user->getId();

Upvotes: 0

Gábor Honffy
Gábor Honffy

Reputation: 11

Opencart 1.5.x

For example, if you want your page's visitors not to see the language selector only for admin, you will enter /catalog/controller/common/header.php

                      

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

        $this->user = new User($this->registry);

say after "protected function index () {" and save it.

Then open your template:

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

<?php echo $language; ?>

you are going to replace:

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

And ready ...

Upvotes: 1

ScyllaDarwin
ScyllaDarwin

Reputation: 121

in version 2.XX

use insert this code in /index.php

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

and use this code for check login admin

    //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();

Upvotes: 1

Tanmoy
Tanmoy

Reputation: 1035

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

Replace the common/login.php index function code with this. Now check adminlogin with this

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

Upvotes: 2

Related Questions