简体   繁体   中英

Opencart Permission Denied - Cannot add permissions

I am extending opencart and I am building a custom controller for catalog for uploading products via CSV. I am getting this error when I try and view catalog/upload You do not have permission to access this page, please refer to your system administrator. .

I figured I would need to change the permissions in the db, and found a function to do so

$this->model_user_user_group->addPermission($this->user->getId(), 'access', 'catalog/upload'); $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'catalog/upload');

I put this in a module built for the sole purpose of addiing these permissions when it installs

<?php
    class ControllerModuleInstl extends Controller {
        public function install() {
            $this->load->model('user/user_group');
            $this->model_user_user_group->addPermission($this->user->getId(), 'access', 'catalog/upload');
            $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'catalog/upload');

            if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
                $this->model_setting_setting->editSetting('instl', $this->request->post);       

                $this->session->data['success'] = $this->language->get('text_success');

                $this->redirect($this->url->link('extension/module', 'token=' . $this->session->data['token'], 'SSL'));
            }
        }
    }
?>

I click install, I get a success message (no page redirect however), when I check the DB the permissions have not been changed.

I find this hard to debug because when usually debugging Ill use echo and such, but obviously you can't do this kind of thing with an application of this size (I usually just write small scripts), what is the best way to debug opencart, step through it, and also can anyone tell me why my permissions are not being changed?

Thanks!

Go to your admin dashboard -> system -> user groups

There are two options which are access permissions and modify permissions.

You have to access your new permission there.

I hope this will help you.

Here is how i fixed this problem when i faced it. Opencart stores it permission by serializing a php array to the database. write a small utility to grab that record and deserialize it and then add your permissions to it then serialize and write it back.

To answer your simpler question you can use:

$this->log->write('message');

in place of echo. It will send the message to the admin panel's error log in Opencart.

You know function "addPermission" of Opencart, it set params with "user group id" not about "user id" you set it above. So:

You can get user group id with:

$user_group_id = $this->user->getGroupId();

After that, you addPermission "access/modify" for this user group of user.

$this->model_user_user_group->addPermission($user_group_id, 'access', 'catalog/upload');

$this->model_user_user_group->addPermission($user_group_id, 'modify', 'catalog/upload');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM