|
| 1 | +<h1 align="center"> |
| 2 | + ThinkPHP 6.0 Authorization |
| 3 | +</h1> |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <strong>Think-authz 是一个专为ThinkPHP6.0打造的授权(角色和权限控制)工具</strong> |
| 7 | +</p> |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + <a href="https://travis-ci.org/php-casbin/think-authz"> |
| 11 | + <img src="https://travis-ci.org/php-casbin/think-authz.svg?branch=master" alt="Build Status"> |
| 12 | + </a> |
| 13 | + <a href="https://coveralls.io/github/php-casbin/think-authz"> |
| 14 | + <img src="https://coveralls.io/repos/github/php-casbin/think-authz/badge.svg" alt="Coverage Status"> |
| 15 | + </a> |
| 16 | + <a href="https://packagist.org/packages/casbin/think-authz"> |
| 17 | + <img src="https://poser.pugx.org/casbin/think-authz/v/stable" alt="Latest Stable Version"> |
| 18 | + </a> |
| 19 | + <a href="https://packagist.org/packages/casbin/think-authz"> |
| 20 | + <img src="https://poser.pugx.org/casbin/think-authz/downloads" alt="Total Downloads"> |
| 21 | + </a> |
| 22 | + <a href="https://packagist.org/packages/casbin/think-authz"> |
| 23 | + <img src="https://poser.pugx.org/casbin/think-authz/license" alt="License"> |
| 24 | + </a> |
| 25 | +</p> |
| 26 | + |
| 27 | +它基于 [Casbin](https://github.com/php-casbin/php-casbin), 一个强大的、高效的开源访问控制框架,它支持基于各种访问控制模型的权限管理。 |
| 28 | + |
| 29 | +在这之前,你需要了解 `Casbin` 的相关知识. |
| 30 | + |
| 31 | +* [安装](#安装) |
| 32 | +* [用法](#用法) |
| 33 | + * [快速开始](#快速开始) |
| 34 | + * [使用 Enforcer Api](#使用-enforcer-api) |
| 35 | + * [Using a middleware](#using-a-middleware) |
| 36 | + * [basic Enforcer Middleware](#basic-enforcer-middleware) |
| 37 | + * [HTTP Request Middleware ( RESTful is also supported )](#http-request-middleware--restful-is-also-supported-) |
| 38 | + * [Using commands](#using-commands) |
| 39 | + * [Cache](#using-cache) |
| 40 | +* [感谢](#thinks) |
| 41 | +* [License](#license) |
| 42 | + |
| 43 | +## 安装 |
| 44 | + |
| 45 | +使用`composer`安装: |
| 46 | + |
| 47 | +``` |
| 48 | +composer require casbin/think-authz |
| 49 | +``` |
| 50 | + |
| 51 | +注册服务,在应用的全局公共文件service.php中加入: |
| 52 | + |
| 53 | +```php |
| 54 | +return [ |
| 55 | + // ... |
| 56 | + |
| 57 | + tauthz\TauthzService::class, |
| 58 | +]; |
| 59 | +``` |
| 60 | + |
| 61 | +发布配置文件和数据库迁移文件: |
| 62 | + |
| 63 | +``` |
| 64 | +php think tauthz:publish |
| 65 | +``` |
| 66 | + |
| 67 | +这将自动生成 `config/tauthz-rbac-model.conf` 和 `config/tauthz.php` 文件。 |
| 68 | + |
| 69 | + |
| 70 | +执行迁移工具(确保数据库配置信息正确): |
| 71 | + |
| 72 | +``` |
| 73 | +php think migrate:run |
| 74 | +``` |
| 75 | + |
| 76 | +这将创将创建名为 `rules` 的表。 |
| 77 | + |
| 78 | + |
| 79 | +## 用法 |
| 80 | + |
| 81 | +### 快速开始 |
| 82 | + |
| 83 | +安装成功后,可以这样使用: |
| 84 | + |
| 85 | +```php |
| 86 | + |
| 87 | +use tauthz\facade\Enforcer; |
| 88 | + |
| 89 | +// adds permissions to a user |
| 90 | +Enforcer::addPermissionForUser('eve', 'articles', 'read'); |
| 91 | +// adds a role for a user. |
| 92 | +Enforcer::addRoleForUser('eve', 'writer'); |
| 93 | +// adds permissions to a rule |
| 94 | +Enforcer::addPolicy('writer', 'articles','edit'); |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +You can check if a user has a permission like this: |
| 99 | + |
| 100 | +```php |
| 101 | +// to check if a user has permission |
| 102 | +if (Enforcer::enforce("eve", "articles", "edit")) { |
| 103 | + // permit eve to edit articles |
| 104 | +} else { |
| 105 | + // deny the request, show an error |
| 106 | +} |
| 107 | + |
| 108 | +``` |
| 109 | + |
| 110 | +### 使用 Enforcer Api |
| 111 | + |
| 112 | +It provides a very rich api to facilitate various operations on the Policy: |
| 113 | + |
| 114 | +Gets all roles: |
| 115 | + |
| 116 | +```php |
| 117 | +Enforcer::getAllRoles(); // ['writer', 'reader'] |
| 118 | +``` |
| 119 | + |
| 120 | +Gets all the authorization rules in the policy.: |
| 121 | + |
| 122 | +```php |
| 123 | +Enforcer::getPolicy(); |
| 124 | +``` |
| 125 | + |
| 126 | +Gets the roles that a user has. |
| 127 | + |
| 128 | +```php |
| 129 | +Enforcer::getRolesForUser('eve'); // ['writer'] |
| 130 | +``` |
| 131 | + |
| 132 | +Gets the users that has a role. |
| 133 | + |
| 134 | +```php |
| 135 | +Enforcer::getUsersForRole('writer'); // ['eve'] |
| 136 | +``` |
| 137 | + |
| 138 | +Determines whether a user has a role. |
| 139 | + |
| 140 | +```php |
| 141 | +Enforcer::hasRoleForUser('eve', 'writer'); // true or false |
| 142 | +``` |
| 143 | + |
| 144 | +Adds a role for a user. |
| 145 | + |
| 146 | +```php |
| 147 | +Enforcer::addRoleForUser('eve', 'writer'); |
| 148 | +``` |
| 149 | + |
| 150 | +Adds a permission for a user or role. |
| 151 | + |
| 152 | +```php |
| 153 | +// to user |
| 154 | +Enforcer::addPermissionForUser('eve', 'articles', 'read'); |
| 155 | +// to role |
| 156 | +Enforcer::addPermissionForUser('writer', 'articles','edit'); |
| 157 | +``` |
| 158 | + |
| 159 | +Deletes a role for a user. |
| 160 | + |
| 161 | +```php |
| 162 | +Enforcer::deleteRoleForUser('eve', 'writer'); |
| 163 | +``` |
| 164 | + |
| 165 | +Deletes all roles for a user. |
| 166 | + |
| 167 | +```php |
| 168 | +Enforcer::deleteRolesForUser('eve'); |
| 169 | +``` |
| 170 | + |
| 171 | +Deletes a role. |
| 172 | + |
| 173 | +```php |
| 174 | +Enforcer::deleteRole('writer'); |
| 175 | +``` |
| 176 | + |
| 177 | +Deletes a permission. |
| 178 | + |
| 179 | +```php |
| 180 | +Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected). |
| 181 | +``` |
| 182 | + |
| 183 | +Deletes a permission for a user or role. |
| 184 | + |
| 185 | +```php |
| 186 | +Enforcer::deletePermissionForUser('eve', 'articles', 'read'); |
| 187 | +``` |
| 188 | + |
| 189 | +Deletes permissions for a user or role. |
| 190 | + |
| 191 | +```php |
| 192 | +// to user |
| 193 | +Enforcer::deletePermissionsForUser('eve'); |
| 194 | +// to role |
| 195 | +Enforcer::deletePermissionsForUser('writer'); |
| 196 | +``` |
| 197 | + |
| 198 | +Gets permissions for a user or role. |
| 199 | + |
| 200 | +```php |
| 201 | +Enforcer::getPermissionsForUser('eve'); // return array |
| 202 | +``` |
| 203 | + |
| 204 | +Determines whether a user has a permission. |
| 205 | + |
| 206 | +```php |
| 207 | +Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false |
| 208 | +``` |
| 209 | + |
| 210 | +### Using a middleware |
| 211 | + |
| 212 | +敬请期待... |
| 213 | + |
| 214 | +#### basic Enforcer Middleware |
| 215 | + |
| 216 | + |
| 217 | + |
| 218 | +#### HTTP Request Middleware ( RESTful is also supported ) |
| 219 | + |
| 220 | + |
| 221 | +``` |
| 222 | +``` |
| 223 | + |
| 224 | +### Using artisan commands |
| 225 | + |
| 226 | +敬请期待... |
| 227 | + |
| 228 | +### Using cache |
| 229 | + |
| 230 | +敬请期待... |
| 231 | + |
| 232 | +## 感谢 |
| 233 | + |
| 234 | +[Casbin](https://github.com/php-casbin/php-casbin) . You can find the full documentation of Casbin [on the website](https://casbin.org/). |
| 235 | + |
| 236 | +## License |
| 237 | + |
| 238 | +This project is licensed under the [Apache 2.0 license](LICENSE). |
0 commit comments