Permission Management

`If you want to remove a permission from init.php file then first remove that permission by command then remove the line and then add again.`

Define Permissions Rules

backend/extension/[module]/[customer]/init.php

<?php
// This method is important for CLI
public function getPermissions()
{
        // Here each permission key must have minimum 3 parts: 1. type = module, 2. extension-name, i.e. customer, 3. permission for
        return array(
                'module.customer.create' => false,
                'module.customer.update' => false,
                'module.customer.delete' => false,
        );
}

public function install()
{
        $this->addPermissions($this->getPermissions());
}

public function uninstall()
{
        $this->removePermissions($this->getPermissions());
}

Add / Remove Permission by Command

Read CLI page.

Manually Adjust User Role’s Permissions

  • Backend > Human Resources > Roles

  • Press “Permissions” button

  • Tick / Un-Tick fields > Update

Check for Permissions

Syntax: has_permission(key, userId);

<?php
if (has_permission('module.customer.create') {
        echo 'Premission granted!'
} else {
        echo 'Permission denied!'
}

if (has_permission('module.customer.create', $user_id=2) {
        echo 'Premission granted!'
} else {
        echo 'Permission denied!'
}