|
1 | | -# hyperf-permission |
2 | | -An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf. |
| 1 | +<h1 align="center"> hyperf-permission </h1> |
| 2 | + |
| 3 | +<p align="center"> An authorization library that supports access control models like ACL, RBAC, ABAC in Hyperf..</p> |
| 4 | + |
| 5 | + |
| 6 | +## Installing |
| 7 | + |
| 8 | +Require this package in the `composer.json` of your Hyperf project. This will download the package. |
| 9 | + |
| 10 | +```shell |
| 11 | +$ composer require yi17310320725/hyperf-permission:dev-master -vvv |
| 12 | +``` |
| 13 | + |
| 14 | +To publish the config, run the vendor publish command: |
| 15 | + |
| 16 | +```shell |
| 17 | +$ php bin/hyperf.php vendor:publish yi17310320725/hyperf-permission |
| 18 | +``` |
| 19 | + |
| 20 | +This will create a new model config file named `config/autoload/casbin-rbac-model.conf`, a new permission config file named `config/autoload/permission.php` and new migrate file named `2020_07_22_213202_create_rules_table.php`. |
| 21 | + |
| 22 | +To migrate the migrations, run the migrate command: |
| 23 | + |
| 24 | +```shell |
| 25 | +$ php bin/hyperf.php migrate |
| 26 | +``` |
| 27 | + |
| 28 | +This will create a new table named `rules` . |
| 29 | + |
| 30 | +## Usage |
| 31 | + |
| 32 | +### Quick start |
| 33 | + |
| 34 | +Once installed you can do stuff like this: |
| 35 | + |
| 36 | +```php |
| 37 | +use Hyperf\Permission\Casbin; |
| 38 | + |
| 39 | +$casbin = new Casbin(); |
| 40 | + |
| 41 | +// adds permissions to a user |
| 42 | +$casbin->addPermissionForUser('eve', 'articles', 'read'); |
| 43 | +// adds a role for a user. |
| 44 | +$casbin->addRoleForUser('eve', 'writer'); |
| 45 | +// adds permissions to a rule |
| 46 | +$casbin->addPolicy('writer', 'articles', 'edit'); |
| 47 | +``` |
| 48 | + |
| 49 | +You can check if a user has a permission like this: |
| 50 | + |
| 51 | +```php |
| 52 | +// to check if a user has permission |
| 53 | +if ($casbin->enforce('eve', 'articles', 'edit')) { |
| 54 | + // permit eve to edit articles |
| 55 | +} else { |
| 56 | + // deny the request, show an error |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Using Enforcer Api |
| 61 | + |
| 62 | +It provides a very rich api to facilitate various operations on the Policy: |
| 63 | + |
| 64 | +Gets all roles: |
| 65 | + |
| 66 | +```php |
| 67 | +Enforcer::getAllRoles(); // ['writer', 'reader'] |
| 68 | +``` |
| 69 | + |
| 70 | +Gets all the authorization rules in the policy.: |
| 71 | + |
| 72 | +```php |
| 73 | +Enforcer::getPolicy(); |
| 74 | +``` |
| 75 | + |
| 76 | +Gets the roles that a user has. |
| 77 | + |
| 78 | +```php |
| 79 | +Enforcer::getRolesForUser('eve'); // ['writer'] |
| 80 | +``` |
| 81 | + |
| 82 | +Gets the users that has a role. |
| 83 | + |
| 84 | +```php |
| 85 | +Enforcer::getUsersForRole('writer'); // ['eve'] |
| 86 | +``` |
| 87 | + |
| 88 | +Determines whether a user has a role. |
| 89 | + |
| 90 | +```php |
| 91 | +Enforcer::hasRoleForUser('eve', 'writer'); // true or false |
| 92 | +``` |
| 93 | + |
| 94 | +Adds a role for a user. |
| 95 | + |
| 96 | +```php |
| 97 | +Enforcer::addRoleForUser('eve', 'writer'); |
| 98 | +``` |
| 99 | + |
| 100 | +Adds a permission for a user or role. |
| 101 | + |
| 102 | +```php |
| 103 | +// to user |
| 104 | +Enforcer::addPermissionForUser('eve', 'articles', 'read'); |
| 105 | +// to role |
| 106 | +Enforcer::addPermissionForUser('writer', 'articles','edit'); |
| 107 | +``` |
| 108 | + |
| 109 | +Deletes a role for a user. |
| 110 | + |
| 111 | +```php |
| 112 | +Enforcer::deleteRoleForUser('eve', 'writer'); |
| 113 | +``` |
| 114 | + |
| 115 | +Deletes all roles for a user. |
| 116 | + |
| 117 | +```php |
| 118 | +Enforcer::deleteRolesForUser('eve'); |
| 119 | +``` |
| 120 | + |
| 121 | +Deletes a role. |
| 122 | + |
| 123 | +```php |
| 124 | +Enforcer::deleteRole('writer'); |
| 125 | +``` |
| 126 | + |
| 127 | +Deletes a permission. |
| 128 | + |
| 129 | +```php |
| 130 | +Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected). |
| 131 | +``` |
| 132 | + |
| 133 | +Deletes a permission for a user or role. |
| 134 | + |
| 135 | +```php |
| 136 | +Enforcer::deletePermissionForUser('eve', 'articles', 'read'); |
| 137 | +``` |
| 138 | + |
| 139 | +Deletes permissions for a user or role. |
| 140 | + |
| 141 | +```php |
| 142 | +// to user |
| 143 | +Enforcer::deletePermissionsForUser('eve'); |
| 144 | +// to role |
| 145 | +Enforcer::deletePermissionsForUser('writer'); |
| 146 | +``` |
| 147 | + |
| 148 | +Gets permissions for a user or role. |
| 149 | + |
| 150 | +```php |
| 151 | +Enforcer::getPermissionsForUser('eve'); // return array |
| 152 | +``` |
| 153 | + |
| 154 | +Determines whether a user has a permission. |
| 155 | + |
| 156 | +```php |
| 157 | +Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false |
| 158 | +``` |
| 159 | + |
| 160 | +See [Casbin API](https://casbin.org/docs/en/management-api) for more APIs. |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +You can contribute in one of three ways: |
| 165 | + |
| 166 | +1. File bug reports using the [issue tracker](https://github.com/yi17310320725/hyperf-authz/issues). |
| 167 | +2. Answer questions or fix bugs on the [issue tracker](https://github.com/yi17310320725/hyperf-authz/issues). |
| 168 | +3. Contribute new features or update the wiki. |
| 169 | + |
| 170 | +_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._ |
| 171 | + |
| 172 | +## License |
| 173 | + |
| 174 | +Apache-2.0 |
0 commit comments