|
1 | | -# easyswoole-permission |
2 | | -An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole. |
| 1 | +<h1 align="center">easyswoole-permission</h1> |
| 2 | +<p align="center">An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.</p> |
| 3 | + |
| 4 | +## Installing |
| 5 | + |
| 6 | +Require this package in the `composer.json` of your easyswoole project. This will download the package. |
| 7 | + |
| 8 | +```shell |
| 9 | +$ composer require |
| 10 | +``` |
| 11 | + |
| 12 | +## Usage |
| 13 | + |
| 14 | +add mysql configuration to `dev.php`: |
| 15 | +```php |
| 16 | +/*################ MYSQL CONFIG ##################*/ |
| 17 | + |
| 18 | +'MYSQL' => [ |
| 19 | + 'host' => '127.0.0.1', |
| 20 | + 'port' => 3306, |
| 21 | + 'user' => 'root', |
| 22 | + 'password' => 'root', |
| 23 | + 'database' => 'easyswoole', |
| 24 | + 'timeout' => 5, |
| 25 | + 'charset' => 'utf8mb4', |
| 26 | +] |
| 27 | +``` |
| 28 | + |
| 29 | +add mysql configuration to `EasySwooleEvent.php`: |
| 30 | + |
| 31 | +```php |
| 32 | +use EasySwoole\ORM\Db\Connection; |
| 33 | +use EasySwoole\ORM\DbManager; |
| 34 | + |
| 35 | +public static function initialize() |
| 36 | +{ |
| 37 | + ... |
| 38 | + $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL')); |
| 39 | + DbManager::getInstance()->addConnection(new Connection($config)); |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +Before using it, you need to create a table named `casbin_rules` for Casbin to store the policy. |
| 44 | + |
| 45 | +Take mysql as an example: |
| 46 | + |
| 47 | +```sql |
| 48 | +CREATE TABLE if not exists `casbin_rules` ( |
| 49 | + `id` BigInt(20) unsigned NOT NULL AUTO_INCREMENT, |
| 50 | + `ptype` varchar(255) DEFAULT NULL, |
| 51 | + `v0` varchar(255) DEFAULT NULL, |
| 52 | + `v1` varchar(255) DEFAULT NULL, |
| 53 | + `v2` varchar(255) DEFAULT NULL, |
| 54 | + `v3` varchar(255) DEFAULT NULL, |
| 55 | + `v4` varchar(255) DEFAULT NULL, |
| 56 | + `v5` varchar(255) DEFAULT NULL, |
| 57 | + `create_time` timestamp NULL DEFAULT NULL, |
| 58 | + `update_time` timestamp NULL DEFAULT NULL, |
| 59 | + PRIMARY KEY (`id`) |
| 60 | +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; |
| 61 | +``` |
| 62 | + |
| 63 | +Then you can start like this: |
| 64 | + |
| 65 | +```php |
| 66 | +use Easyswolle\Permission\Casbin; |
| 67 | +use Easyswolle\Permission\Config; |
| 68 | + |
| 69 | +$config = new Config(); |
| 70 | +$casbin = new Casbin($config); |
| 71 | + |
| 72 | +// adds permissions to a user |
| 73 | +$casbin->addPermissionForUser('eve', 'articles', 'read'); |
| 74 | +// adds a role for a user. |
| 75 | +$casbin->addRoleForUser('eve', 'writer'); |
| 76 | +// adds permissions to a rule |
| 77 | +$casbin->addPolicy('writer', 'articles', 'edit'); |
| 78 | +``` |
| 79 | + |
| 80 | +You can check if a user has a permission like this: |
| 81 | + |
| 82 | +```php |
| 83 | +// to check if a user has permission |
| 84 | +if ($casbin->enforce('eve', 'articles', 'edit')) { |
| 85 | + // permit eve to edit articles |
| 86 | +} else { |
| 87 | + // deny the request, show an error |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Using Enforcer Api |
| 92 | + |
| 93 | +It provides a very rich api to facilitate various operations on the Policy: |
| 94 | + |
| 95 | +Gets all roles: |
| 96 | + |
| 97 | +```php |
| 98 | +Enforcer::getAllRoles(); // ['writer', 'reader'] |
| 99 | +``` |
| 100 | + |
| 101 | +Gets all the authorization rules in the policy.: |
| 102 | + |
| 103 | +```php |
| 104 | +Enforcer::getPolicy(); |
| 105 | +``` |
| 106 | + |
| 107 | +Gets the roles that a user has. |
| 108 | + |
| 109 | +```php |
| 110 | +Enforcer::getRolesForUser('eve'); // ['writer'] |
| 111 | +``` |
| 112 | + |
| 113 | +Gets the users that has a role. |
| 114 | + |
| 115 | +```php |
| 116 | +Enforcer::getUsersForRole('writer'); // ['eve'] |
| 117 | +``` |
| 118 | + |
| 119 | +Determines whether a user has a role. |
| 120 | + |
| 121 | +```php |
| 122 | +Enforcer::hasRoleForUser('eve', 'writer'); // true or false |
| 123 | +``` |
| 124 | + |
| 125 | +Adds a role for a user. |
| 126 | + |
| 127 | +```php |
| 128 | +Enforcer::addRoleForUser('eve', 'writer'); |
| 129 | +``` |
| 130 | + |
| 131 | +Adds a permission for a user or role. |
| 132 | + |
| 133 | +```php |
| 134 | +// to user |
| 135 | +Enforcer::addPermissionForUser('eve', 'articles', 'read'); |
| 136 | +// to role |
| 137 | +Enforcer::addPermissionForUser('writer', 'articles','edit'); |
| 138 | +``` |
| 139 | + |
| 140 | +Deletes a role for a user. |
| 141 | + |
| 142 | +```php |
| 143 | +Enforcer::deleteRoleForUser('eve', 'writer'); |
| 144 | +``` |
| 145 | + |
| 146 | +Deletes all roles for a user. |
| 147 | + |
| 148 | +```php |
| 149 | +Enforcer::deleteRolesForUser('eve'); |
| 150 | +``` |
| 151 | + |
| 152 | +Deletes a role. |
| 153 | + |
| 154 | +```php |
| 155 | +Enforcer::deleteRole('writer'); |
| 156 | +``` |
| 157 | + |
| 158 | +Deletes a permission. |
| 159 | + |
| 160 | +```php |
| 161 | +Enforcer::deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected). |
| 162 | +``` |
| 163 | + |
| 164 | +Deletes a permission for a user or role. |
| 165 | + |
| 166 | +```php |
| 167 | +Enforcer::deletePermissionForUser('eve', 'articles', 'read'); |
| 168 | +``` |
| 169 | + |
| 170 | +Deletes permissions for a user or role. |
| 171 | + |
| 172 | +```php |
| 173 | +// to user |
| 174 | +Enforcer::deletePermissionsForUser('eve'); |
| 175 | +// to role |
| 176 | +Enforcer::deletePermissionsForUser('writer'); |
| 177 | +``` |
| 178 | + |
| 179 | +Gets permissions for a user or role. |
| 180 | + |
| 181 | +```php |
| 182 | +Enforcer::getPermissionsForUser('eve'); // return array |
| 183 | +``` |
| 184 | + |
| 185 | +Determines whether a user has a permission. |
| 186 | + |
| 187 | +```php |
| 188 | +Enforcer::hasPermissionForUser('eve', 'articles', 'read'); // true or false |
| 189 | +``` |
| 190 | + |
| 191 | +See [Casbin API](https://casbin.org/docs/en/management-api) for more APIs. |
0 commit comments