|
1 |
| -# cake-casbin |
| 1 | +# Cake-Casbin |
| 2 | + |
| 3 | +[](https://packagist.org/packages/casbin/cake-adapter) |
| 4 | +[](https://packagist.org/packages/casbin/cake-adapter) |
| 5 | +[](https://packagist.org/packages/casbin/cake-adapter) |
| 6 | + |
2 | 7 | Use Casbin in CakePHP Framework, Casbin is a powerful and efficient open-source access control library.
|
| 8 | + |
| 9 | +### Installation |
| 10 | + |
| 11 | +Require this package in the `composer.json` of your CakePHP project. This will download the package. |
| 12 | + |
| 13 | +``` |
| 14 | +composer require casbin/cake-adapter |
| 15 | +``` |
| 16 | + |
| 17 | +create config file `config/casbin.php` for Casbin: |
| 18 | + |
| 19 | +```php |
| 20 | +<?php |
| 21 | + |
| 22 | +return [ |
| 23 | + 'Casbin' => [ |
| 24 | + /* |
| 25 | + * Cake-casbin model setting. |
| 26 | + */ |
| 27 | + 'model' => [ |
| 28 | + // Available Settings: "file", "text" |
| 29 | + 'config_type' => 'file', |
| 30 | + 'config_file_path' => __DIR__.'/casbin-model.conf', |
| 31 | + 'config_text' => '', |
| 32 | + ], |
| 33 | + |
| 34 | + // Cake-casbin adapter . |
| 35 | + 'adapter' => '\CasbinAdapter\Cake\Adapter', |
| 36 | + |
| 37 | + /* |
| 38 | + * Cake-casbin database setting. |
| 39 | + */ |
| 40 | + 'database' => [ |
| 41 | + // Database connection for following tables. |
| 42 | + 'connection' => '', |
| 43 | + // CasbinRule tables and model. |
| 44 | + 'casbin_rules_table' => '', |
| 45 | + ], |
| 46 | + ], |
| 47 | +]; |
| 48 | +``` |
| 49 | + |
| 50 | +create a new model config file named `config/casbin-model.conf`. |
| 51 | + |
| 52 | +``` |
| 53 | +[request_definition] |
| 54 | +r = sub, obj, act |
| 55 | +
|
| 56 | +[policy_definition] |
| 57 | +p = sub, obj, act |
| 58 | +
|
| 59 | +[policy_effect] |
| 60 | +e = some(where (p.eft == allow)) |
| 61 | +
|
| 62 | +[matchers] |
| 63 | +m = r.sub == p.sub && r.obj == p.obj && r.act == p.act |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | +### Usage |
| 68 | + |
| 69 | +```php |
| 70 | + |
| 71 | +$sub = 'alice'; // the user that wants to access a resource. |
| 72 | +$obj = 'data1'; // the resource that is going to be accessed. |
| 73 | +$act = 'read'; // the operation that the user performs on the resource. |
| 74 | + |
| 75 | +$casbin = new \CasbinAdapter\Cake\Casbin(); |
| 76 | + |
| 77 | +if (true === $casbin->enforce($sub, $obj, $act)) { |
| 78 | + // permit alice to read data1 |
| 79 | +} else { |
| 80 | + // deny the request, show an error |
| 81 | +} |
| 82 | +``` |
| 83 | + |
| 84 | +### Define your own model.conf |
| 85 | + |
| 86 | +You can modify the config file named `config/casbin-model.conf` |
| 87 | + |
| 88 | +### Learning Casbin |
| 89 | + |
| 90 | +You can find the full documentation of Casbin [on the website](https://casbin.org/). |
0 commit comments