|
| 1 | +<h1 align="center"> |
| 2 | + CodeIgniter Permission |
| 3 | +</h1> |
| 4 | + |
| 5 | +<p align="center"> |
| 6 | + <strong>CodeIgniter Permission is an authorization library for the CodeIgniter4 framework.</strong> |
| 7 | +</p> |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + <a href="https://travis-ci.org/php-casbin/codeigniter-permission"> |
| 11 | + <img src="https://travis-ci.org/php-casbin/codeigniter-permission.svg?branch=master" alt="Build Status"> |
| 12 | + </a> |
| 13 | + <a href="https://coveralls.io/github/php-casbin/codeigniter-permission"> |
| 14 | + <img src="https://coveralls.io/repos/github/php-casbin/codeigniter-permission/badge.svg" alt="Coverage Status"> |
| 15 | + </a> |
| 16 | + <a href="https://packagist.org/packages/casbin/codeigniter-permission"> |
| 17 | + <img src="https://poser.pugx.org/casbin/codeigniter-permission/v/stable" alt="Latest Stable Version"> |
| 18 | + </a> |
| 19 | + <a href="https://packagist.org/packages/casbin/codeigniter-permission"> |
| 20 | + <img src="https://poser.pugx.org/casbin/codeigniter-permission/downloads" alt="Total Downloads"> |
| 21 | + </a> |
| 22 | + <a href="https://packagist.org/packages/casbin/codeigniter-permission"> |
| 23 | + <img src="https://poser.pugx.org/casbin/codeigniter-permission/license" alt="License"> |
| 24 | + </a> |
| 25 | +</p> |
| 26 | + |
| 27 | +It's based on [Casbin](https://github.com/php-casbin/php-casbin), an authorization library that supports access control models like ACL, RBAC, ABAC. |
| 28 | + |
| 29 | +All you need to learn to use `Casbin` first. |
| 30 | + |
| 31 | +* [Installation](#installation) |
| 32 | +* [Usage](#usage) |
| 33 | + * [Quick start](#quick-start) |
| 34 | + * [Using Enforcer Api](#using-enforcer-api) |
| 35 | + * [Multiple enforcers](#multiple-enforcers) |
| 36 | + * [Cache](#using-cache) |
| 37 | +* [Thinks](#thinks) |
| 38 | +* [License](#license) |
| 39 | + |
| 40 | +## Installation |
| 41 | + |
| 42 | +Require this package in the `composer.json` of your Laravel project. This will download the package. |
| 43 | + |
| 44 | +``` |
| 45 | +composer require casbin/codeigniter-permission |
| 46 | +``` |
| 47 | + |
| 48 | +To migrate the migrations, run the migrate command: |
| 49 | + |
| 50 | +``` |
| 51 | +php spark migrate -n "Casbin\CodeIgniter" |
| 52 | +``` |
| 53 | + |
| 54 | +This will create a new table named `rules` |
| 55 | + |
| 56 | + |
| 57 | +## Usage |
| 58 | + |
| 59 | +### Quick start |
| 60 | + |
| 61 | +Once installed you can do stuff like this: |
| 62 | + |
| 63 | +```php |
| 64 | + |
| 65 | +$enforcer = \Config\Services::enforcer(); |
| 66 | + |
| 67 | +// adds permissions to a user |
| 68 | +$enforcer->addPermissionForUser('eve', 'articles', 'read'); |
| 69 | +// adds a role for a user. |
| 70 | +$enforcer->addRoleForUser('eve', 'writer'); |
| 71 | +// adds permissions to a rule |
| 72 | +$enforcer->addPolicy('writer', 'articles','edit'); |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +You can check if a user has a permission like this: |
| 77 | + |
| 78 | +```php |
| 79 | +// to check if a user has permission |
| 80 | +if ($enforcer->enforce("eve", "articles", "edit")) { |
| 81 | + // permit eve to edit articles |
| 82 | +} else { |
| 83 | + // deny the request, show an error |
| 84 | +} |
| 85 | + |
| 86 | +``` |
| 87 | + |
| 88 | +### Using Enforcer Api |
| 89 | + |
| 90 | +It provides a very rich api to facilitate various operations on the Policy: |
| 91 | + |
| 92 | +Gets all roles: |
| 93 | + |
| 94 | +```php |
| 95 | +$enforcer->getAllRoles(); // ['writer', 'reader'] |
| 96 | +``` |
| 97 | + |
| 98 | +Gets all the authorization rules in the policy.: |
| 99 | + |
| 100 | +```php |
| 101 | +$enforcer->getPolicy(); |
| 102 | +``` |
| 103 | + |
| 104 | +Gets the roles that a user has. |
| 105 | + |
| 106 | +```php |
| 107 | +$enforcer->getRolesForUser('eve'); // ['writer'] |
| 108 | +``` |
| 109 | + |
| 110 | +Gets the users that has a role. |
| 111 | + |
| 112 | +```php |
| 113 | +$enforcer->getUsersForRole('writer'); // ['eve'] |
| 114 | +``` |
| 115 | + |
| 116 | +Determines whether a user has a role. |
| 117 | + |
| 118 | +```php |
| 119 | +$enforcer->hasRoleForUser('eve', 'writer'); // true or false |
| 120 | +``` |
| 121 | + |
| 122 | +Adds a role for a user. |
| 123 | + |
| 124 | +```php |
| 125 | +$enforcer->addRoleForUser('eve', 'writer'); |
| 126 | +``` |
| 127 | + |
| 128 | +Adds a permission for a user or role. |
| 129 | + |
| 130 | +```php |
| 131 | +// to user |
| 132 | +$enforcer->addPermissionForUser('eve', 'articles', 'read'); |
| 133 | +// to role |
| 134 | +$enforcer->addPermissionForUser('writer', 'articles','edit'); |
| 135 | +``` |
| 136 | + |
| 137 | +Deletes a role for a user. |
| 138 | + |
| 139 | +```php |
| 140 | +$enforcer->deleteRoleForUser('eve', 'writer'); |
| 141 | +``` |
| 142 | + |
| 143 | +Deletes all roles for a user. |
| 144 | + |
| 145 | +```php |
| 146 | +$enforcer->deleteRolesForUser('eve'); |
| 147 | +``` |
| 148 | + |
| 149 | +Deletes a role. |
| 150 | + |
| 151 | +```php |
| 152 | +$enforcer->deleteRole('writer'); |
| 153 | +``` |
| 154 | + |
| 155 | +Deletes a permission. |
| 156 | + |
| 157 | +```php |
| 158 | +$enforcer->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected). |
| 159 | +``` |
| 160 | + |
| 161 | +Deletes a permission for a user or role. |
| 162 | + |
| 163 | +```php |
| 164 | +$enforcer->deletePermissionForUser('eve', 'articles', 'read'); |
| 165 | +``` |
| 166 | + |
| 167 | +Deletes permissions for a user or role. |
| 168 | + |
| 169 | +```php |
| 170 | +// to user |
| 171 | +$enforcer->deletePermissionsForUser('eve'); |
| 172 | +// to role |
| 173 | +$enforcer->deletePermissionsForUser('writer'); |
| 174 | +``` |
| 175 | + |
| 176 | +Gets permissions for a user or role. |
| 177 | + |
| 178 | +```php |
| 179 | +$enforcer->getPermissionsForUser('eve'); // return array |
| 180 | +``` |
| 181 | + |
| 182 | +Determines whether a user has a permission. |
| 183 | + |
| 184 | +```php |
| 185 | +$enforcer->hasPermissionForUser('eve', 'articles', 'read'); // true or false |
| 186 | +``` |
| 187 | + |
| 188 | +See [Casbin API](https://casbin.org/docs/en/management-api) for more APIs. |
| 189 | + |
| 190 | +### Multiple enforcers |
| 191 | + |
| 192 | +If you need multiple permission controls in your project, you can configure multiple enforcers. |
| 193 | + |
| 194 | +In the `Config\Enforcer.php` file, it should be like this: |
| 195 | + |
| 196 | +```php |
| 197 | + |
| 198 | +namespace Config; |
| 199 | + |
| 200 | +use Casbin\CodeIgniter\Config as BaseConfig; |
| 201 | +use Casbin\CodeIgniter\Adapters\DatabaseAdapter; |
| 202 | + |
| 203 | +class Enforcer extends BaseConfig |
| 204 | +{ |
| 205 | + /* |
| 206 | + * Default Enforcer driver |
| 207 | + * |
| 208 | + * @var string |
| 209 | + */ |
| 210 | + public $default = 'basic'; |
| 211 | + |
| 212 | + public $basic = [ |
| 213 | + /* |
| 214 | + * Casbin model setting. |
| 215 | + */ |
| 216 | + 'model' => [ |
| 217 | + // Available Settings: "file", "text" |
| 218 | + 'config_type' => 'file', |
| 219 | + |
| 220 | + 'config_file_path' => __DIR__.'/lauthz-rbac-model.conf', |
| 221 | + |
| 222 | + 'config_text' => '', |
| 223 | + ], |
| 224 | + |
| 225 | + /* |
| 226 | + * Casbin adapter . |
| 227 | + */ |
| 228 | + 'adapter' => DatabaseAdapter::class, |
| 229 | + |
| 230 | + /* |
| 231 | + * Database setting. |
| 232 | + */ |
| 233 | + 'database' => [ |
| 234 | + // Database connection for following tables. |
| 235 | + 'connection' => '', |
| 236 | + |
| 237 | + // Rule table name. |
| 238 | + 'rules_table' => 'rules', |
| 239 | + ], |
| 240 | + |
| 241 | + 'log' => [ |
| 242 | + // changes whether Casbin will log messages to the Logger. |
| 243 | + 'enabled' => false, |
| 244 | + |
| 245 | + // Casbin Logger |
| 246 | + 'logger' => \Casbin\CodeIgniter\Logger::class, |
| 247 | + ], |
| 248 | + |
| 249 | + 'cache' => [ |
| 250 | + // changes whether Casbin will cache the rules. |
| 251 | + 'enabled' => false, |
| 252 | + |
| 253 | + // cache Key |
| 254 | + 'key' => 'rules', |
| 255 | + |
| 256 | + // ttl int|null |
| 257 | + 'ttl' => 24 * 60, |
| 258 | + ], |
| 259 | + ]; |
| 260 | + |
| 261 | + public $second = [ |
| 262 | + 'model' => [ |
| 263 | + // ... |
| 264 | + ], |
| 265 | + |
| 266 | + 'adapter' => DatabaseAdapter::class, |
| 267 | + // ... |
| 268 | + ]; |
| 269 | +} |
| 270 | + |
| 271 | +``` |
| 272 | + |
| 273 | +Then you can choose which enforcers to use. |
| 274 | + |
| 275 | +```php |
| 276 | +$enforcer->guard('second')->enforce("eve", "articles", "edit"); |
| 277 | +``` |
| 278 | + |
| 279 | +### Using cache |
| 280 | + |
| 281 | +Authorization rules are cached to speed up performance. The default is off. |
| 282 | + |
| 283 | +Sets your own cache configs in `Config\Enforcer.php`. |
| 284 | + |
| 285 | +```php |
| 286 | +'cache' => [ |
| 287 | + // changes whether Casbin will cache the rules. |
| 288 | + 'enabled' => false, |
| 289 | + // cache Key |
| 290 | + 'key' => 'rules', |
| 291 | + // ttl int|null |
| 292 | + 'ttl' => 24 * 60, |
| 293 | +] |
| 294 | +``` |
| 295 | + |
| 296 | +## Thinks |
| 297 | + |
| 298 | +[Casbin](https://github.com/php-casbin/php-casbin) in Laravel. You can find the full documentation of Casbin [on the website](https://casbin.org/). |
| 299 | + |
| 300 | +## License |
| 301 | + |
| 302 | +This project is licensed under the [Apache 2.0 license](LICENSE). |
0 commit comments