PHPackages                             imiphp/imi-access-control - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. imiphp/imi-access-control

ActiveLibrary

imiphp/imi-access-control
=========================

imi 框架的权限控制组件

v2.1.6(2y ago)62502MulanPSL-2.0PHP

Since Apr 22Pushed 2y ago1 watchersCompare

[ Source](https://github.com/imiphp/imi-access-control)[ Packagist](https://packagist.org/packages/imiphp/imi-access-control)[ RSS](/packages/imiphp-imi-access-control/feed)WikiDiscussions 2.0 Synced 3d ago

READMEChangelog (10)DependenciesVersions (27)Used By (0)

imi-access-control
==================

[](#imi-access-control)

[![Latest Version](https://camo.githubusercontent.com/85b4c42656826e762ea96dbf3449a3c5ad1fa0a6aeb338a438c9bc9ff8f78b78/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d697068702f696d692d6163636573732d636f6e74726f6c2e737667)](https://packagist.org/packages/imiphp/imi-access-control)[![Php Version](https://camo.githubusercontent.com/4a5c2ab20974058a8bab53ecb30ac4c2e6bb961df6229b7386fdc097ab53dfa8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d372e342d627269676874677265656e2e737667)](https://secure.php.net/)[![Swoole Version](https://camo.githubusercontent.com/f077644cadc3b88104d75a54818d0e1462f910dc6d6dfd9939ea295fb11b4e2b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f73776f6f6c652d2533453d342e312e302d627269676874677265656e2e737667)](https://github.com/swoole/swoole-src)[![IMI License](https://camo.githubusercontent.com/571b0b4dc74d08801124791056468cb081fc1ce3f5dd81e978b258d9563568ca/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f696d697068702f696d692d6163636573732d636f6e74726f6c2e737667)](https://github.com/imiphp/imi-access-control/blob/master/LICENSE)

介绍
--

[](#介绍)

imi 框架的权限控制组件，不提供具体 API、管理界面，仅提供基础操作组件。

本组件中支持：角色关联操作，用户关联角色，用于关联操作。

用户除了角色赋予的操作权限以外，还可以单独赋予操作权限。

> 本仓库仅用于浏览，不接受 issue 和 Pull Requests，请前往：

Composer
--------

[](#composer)

本项目可以使用composer安装，遵循psr-4自动加载规则，在你的 `composer.json` 中加入下面的内容:

```
{
    "require": {
        "imiphp/imi-access-control": "~2.0.0"
    }
}
```

然后执行 `composer update` 安装。

使用
--

[](#使用)

在项目 `config/config.php` 中配置：

```
[
    '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();
```

#### 增加、设置权限

[](#增加设置权限-1)

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

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

#### 移除权限

[](#移除权限-1)

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

#### 判断用户是否拥有权限

[](#判断用户是否拥有权限)

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

免费技术支持
------

[](#免费技术支持)

QQ群：17916227 [![点击加群](https://camo.githubusercontent.com/75b53e353bb9e5064662e185a6d39f4bb88c4e45bd3a1240ddf599525edb6afa/68747470733a2f2f7075622e69647171696d672e636f6d2f7770612f696d616765732f67726f75702e706e67 "点击加群")](https://jq.qq.com/?_wv=1027&k=5wXf4Zq)，如有问题会有人解答和修复。

运行环境
----

[](#运行环境)

- [PHP](https://php.net/) &gt;= 7.4
- [Composer](https://getcomposer.org/) &gt;= 2.0
- [Swoole](https://www.swoole.com/) &gt;= 4.1.0

版权信息
----

[](#版权信息)

`imi-access-control` 遵循 MIT 开源协议发布，并提供免费使用。

捐赠
--

[](#捐赠)

[![](https://camo.githubusercontent.com/d6289b746e049310225a52ee7d4f9aab71cf9df63a3341cb037e7ec09b31ea1f/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f696d697068702f696d6940322e302f7265732f7061792e706e67)](https://camo.githubusercontent.com/d6289b746e049310225a52ee7d4f9aab71cf9df63a3341cb037e7ec09b31ea1f/68747470733a2f2f63646e2e6a7364656c6976722e6e65742f67682f696d697068702f696d6940322e302f7265732f7061792e706e67)

开源不求盈利，多少都是心意，生活不易，随缘随缘……

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 85% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~63 days

Recently: every ~76 days

Total

27

Last Release

938d ago

Major Versions

v1.0.4 → v2.0.02021-08-20

v1.0.6 → v2.0.22021-10-21

2.1.x-dev → 3.0.x-dev2023-10-20

PHP version history (3 changes)v1.0.4PHP &gt;=7.1.0

v2.0.0PHP &gt;=7.4.0

2.0.x-devPHP &gt;=7.4 &lt;8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/6f917bb42280d114c53cebadc2942a13ee03abe14971089f88895e266d637169?d=identicon)[Yurunsoft](/maintainers/Yurunsoft)

---

Top Contributors

[![Yurunsoft](https://avatars.githubusercontent.com/u/20104656?v=4)](https://github.com/Yurunsoft "Yurunsoft (34 commits)")[![Mjc960512](https://avatars.githubusercontent.com/u/41005585?v=4)](https://github.com/Mjc960512 "Mjc960512 (2 commits)")[![NHZEX](https://avatars.githubusercontent.com/u/14545600?v=4)](https://github.com/NHZEX "NHZEX (2 commits)")[![cyycler](https://avatars.githubusercontent.com/u/42498453?v=4)](https://github.com/cyycler "cyycler (1 commits)")[![web-flow](https://avatars.githubusercontent.com/u/19864447?v=4)](https://github.com/web-flow "web-flow (1 commits)")

### Embed Badge

![Health badge](/badges/imiphp-imi-access-control/health.svg)

```
[![Health](https://phpackages.com/badges/imiphp-imi-access-control/health.svg)](https://phpackages.com/packages/imiphp-imi-access-control)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
