|
1 |
| -# webman-permission |
| 1 | +# webman casbin plugin |
| 2 | + |
| 3 | +[](https://packagist.org/packages/tinywan/casbin) |
| 4 | +[](https://packagist.org/packages/tinywan/casbin) |
| 5 | +[](https://packagist.org/packages/tinywan/casbin) |
| 6 | +[](https://packagist.org/packages/tinywan/casbin) |
| 7 | +[]() |
| 8 | +[]() |
| 9 | + |
2 | 10 | An authorization library that supports access control models like ACL, RBAC, ABAC for webman plugin
|
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +- [ThinkORM](https://www.workerman.net/doc/webman/db/others.html) |
| 15 | +- [PHP-DI](https://github.com/PHP-DI/PHP-DI) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +```sh |
| 20 | +composer require tinywan/casbin |
| 21 | +``` |
| 22 | + |
| 23 | +## Configure |
| 24 | + |
| 25 | +### 1、DI |
| 26 | + |
| 27 | +configure `config/container.php`,Its final content is as follows: |
| 28 | + |
| 29 | +```php |
| 30 | +$builder = new \DI\ContainerBuilder(); |
| 31 | +$builder->addDefinitions(config('dependence', [])); |
| 32 | +$builder->useAutowiring(true); |
| 33 | +return $builder->build(); |
| 34 | +``` |
| 35 | + |
| 36 | +### 2、Database configuration |
| 37 | + |
| 38 | +(1)修改数据库 `thinkorm` 配置 |
| 39 | + |
| 40 | +(2)创建 `casbin_rule` 数据表 |
| 41 | + |
| 42 | +```sql |
| 43 | +CREATE TABLE `casbin_rule` ( |
| 44 | + `id` BIGINT ( 20 ) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 45 | + `ptype` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 46 | + `v0` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 47 | + `v1` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 48 | + `v2` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 49 | + `v3` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 50 | + `v4` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 51 | + `v5` VARCHAR ( 128 ) NOT NULL DEFAULT '', |
| 52 | + PRIMARY KEY ( `id` ) USING BTREE, |
| 53 | + KEY `idx_ptype` ( `ptype` ) USING BTREE, |
| 54 | + KEY `idx_v0` ( `v0` ) USING BTREE, |
| 55 | + KEY `idx_v1` ( `v1` ) USING BTREE, |
| 56 | + KEY `idx_v2` ( `v2` ) USING BTREE, |
| 57 | + KEY `idx_v3` ( `v3` ) USING BTREE, |
| 58 | + KEY `idx_v4` ( `v4` ) USING BTREE, |
| 59 | + KEY `idx_v5` ( `v5` ) USING BTREE |
| 60 | +) ENGINE = INNODB CHARSET = utf8mb4 COMMENT = '策略规则表'; |
| 61 | +``` |
| 62 | +(3)配置 `config/redis` 配置 |
| 63 | + |
| 64 | +## 重启webman |
| 65 | + |
| 66 | +``` |
| 67 | +php start.php restart |
| 68 | +``` |
| 69 | +或者 |
| 70 | +``` |
| 71 | +php start.php restart -d |
| 72 | +``` |
| 73 | + |
| 74 | +## 用法 |
| 75 | + |
| 76 | +### 快速开始 |
| 77 | + |
| 78 | +安装成功后,可以这样使用: |
| 79 | + |
| 80 | +```php |
| 81 | +use Tinywan\Casbin\Permission; |
| 82 | + |
| 83 | +// adds permissions to a user |
| 84 | +Permission::addPermissionForUser('eve', 'articles', 'read'); |
| 85 | +// adds a role for a user. |
| 86 | +Permission::addRoleForUser('eve', 'writer'); |
| 87 | +// adds permissions to a rule |
| 88 | +Permission::addPolicy('writer', 'articles','edit'); |
| 89 | +``` |
| 90 | + |
| 91 | +你可以检查一个用户是否拥有某个权限: |
| 92 | + |
| 93 | +```php |
| 94 | +if (Permission::enforce("eve", "articles", "edit")) { |
| 95 | + echo '恭喜你!通过权限认证'; |
| 96 | +} else { |
| 97 | + echo '对不起,您没有该资源访问权限'; |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +更多 `API` 参考 [Casbin API](https://casbin.org/docs/en/management-api) 。 |
| 102 | + |
| 103 | +## 感谢 |
| 104 | + |
| 105 | +[Casbin](https://github.com/php-casbin/php-casbin),你可以查看全部文档在其 [官网](https://casbin.org/) 上。 |
0 commit comments