简体   繁体   中英

Opencart custom module permission

I'm writing a module/plugin for opencart, and it would be nice to auto-create the user groups permissions.

Once the module is installed, the user must go into user permission control and manually add the permission. Its a unnecessary step, that would be very nice to jump.

As i'm currently using VqMod to do some changes (like adding extra items to the admin menu), it would be possible to add the route to the ignore route list in permission control class, but i don't think this is a nice option and i prefer to insert the permissions if possible.

I tried to add permissions with the user_group model in my constructor, but adding that to my constructor didn't work. The class is probably not instantiated before permission.

$this->load->model('user/user_group');

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

This code should be placed in the install() method of your controller. This will then be run when your mod is installed under EXTENSIONS > MODULES in the administrator area. Just add this to your controller and it should work (untested)

protected function install() {
    $this->load->model('user/user_group');

    $this->model_user_user_group->addPermission($this->user->getId(), 'access', 'test/import');
    $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'test/import');
}

In any case you need to create a module to add permission.

My solution is file - controller/module/test_import.php

<? php  class ControllerModuleTestimport extends Controller {

    public function index() { 
        $this->redirect($this->url->link('test/import', 'token=' . $this->session->data['token'], 'SSL'));        
    }

public function install() {  
        $this->load->model('user/user_group');
        $this->model_user_user_group->addPermission($this->user->getId(), 'access', 'test/import');
        $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'test/import');
    }
} ?>

/language/english/module/test_import.php

<?php
// Heading
$_['heading_title']       = 'test_import';
?>

And go by link /admin/index.php?route=extension/module/install&token=&extension=test_import

Normally You install the module/extension from within the modules overview , so under ADMIN -> Extensions -> Modules. Here the user has to have modify permission on the extension/modules to be able to install/uninstall/modify the modules.

Now if the controller of Your extension has the install method it will be called from within admin/controller/extension/module.php::install() method. Anyway, You do not need to add the permission manually as they are already added by the mentioned method (check lines 115-118 of admin/controller/extension/module.php ):

        $this->load->model('user/user_group');

        $this->model_user_user_group->addPermission($this->user->getId(), 'access', 'module/' . $this->request->get['extension']);
        $this->model_user_user_group->addPermission($this->user->getId(), 'modify', 'module/' . $this->request->get['extension']);

Thus IMHO, no action is needed if the new module is placed within OC's modules and is installed by normal way. If not (so it lays elsewhere) I guess You have missed something or You are not following the OC's standards or we are not talking about the extension/module then...

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