PHP code example of imiphp / imi-access-control

1. Go to this page and download the library: Download imiphp/imi-access-control library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

imiphp / imi-access-control example snippets


[
    'components'    =>  [
        // 引入本组件
        'AccessControl'    =>  'Imi\AC',
    ],
]

use Imi\AC\AccessControl\Operation;

Operation::create('权限名称');

// 权限代码不传或为null,则和权限名称相同,不可重复
Operation::create('权限名称', '权限代码');

// 指定父级ID、排序索引
Operation::create('权限名称', '权限代码', $parentId, $index, '介绍');

use Imi\AC\AccessControl\Role;

// 与 Operation::create 一样,不多做说明了
$role = Role::create('权限名称', '权限代码', '介绍');

// 支持ID、Code两种模式
$role = new Role('权限ID');
$role = new Role('权限代码', 'code');
$roleInfo = $role->getRoleInfo(); // $roleInfo->id/code/name/description

// 数组,成员为 \Imi\AC\Model\Operation 类型
$operations = $role->getOperations();

// 树形结构,成员为 \Imi\AC\Model\Filter\OperationTreeItem 类型,$item->children 为其下一级角色,同样为 \Imi\AC\Model\Filter\OperationTreeItem 类型
$operationTree = $role->getOperationTree();

$role->addOperations('code1', 'code2'); // 只在当前基础上增加这两个权限

$role->setOperations('code1', 'code2'); // 将角色权限设置为仅有这两个权限

$role->removeOperations('code1', 'code2');

$result = $role->hasOperations('code1', 'code2');

use Imi\AC\AccessControl\Member;

$memberId = 1;
$member = new Member(1);

$roles = $member->getRoles();

$member->addRoles('code1', 'code2'); // 只在当前基础上增加这两个角色

$member->setRoles('code1', 'code2'); // 将用户角色设置为仅有这两个角色

$member->removeRoles('code1', 'code2');

$result = $member->hasRoles('code1', 'code2');

// 数组,成员为 \Imi\AC\Model\Operation 类型
$operations = $member->getOperations();

// 树形结构,成员为 \Imi\AC\Model\Filter\OperationTreeItem 类型,$item->children 为其下一级角色,同样为 \Imi\AC\Model\Filter\OperationTreeItem 类型
$operationTree = $member->getOperationTree();

$member->addOperations('code1', 'code2'); // 只在当前基础上增加这两个权限

$member->setOperations('code1', 'code2'); // 将角色权限设置为仅有这两个权限

$member->removeOperations('code1', 'code2');

$result = $member->hasOperations('code1', 'code2');