Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/php-casbin/symfony-permission

Use Casbin in Symfony Framework, Casbin is a powerful and efficient open-source access control library.
https://github.com/php-casbin/symfony-permission

abac access-control acl auth authz casbin permission rbac symfony-casbin

Last synced: 2 months ago
JSON representation

Use Casbin in Symfony Framework, Casbin is a powerful and efficient open-source access control library.

Awesome Lists containing this project

README

        

symfony-permission


An authorization library that supports access control models like ACL, RBAC, ABAC in Symfony.

## Installing

Require this package in the `composer.json` of your easyswoole project. This will download the package.

```shell
$ composer require
```

## Usage

First, you can install Doctrine.

```bash
$ composer require symfony/orm-pack
```

Configuring the Database, you can find and customize this inside `.env`:
```env
# customize this line!
DATABASE_URL="mysql://db_user:[email protected]:3306/db_name?serverVersion=5.7"

# to use sqlite:
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/app.db"

# to use postgresql:
# DATABASE_URL="postgresql://db_user:[email protected]:5432/db_name?serverVersion=11&charset=utf8"
```

And you can create migration file to generate database table, :

*migrations/Version20200823135629.php*
```php
addSql('CREATE TABLE casbin_rules (id INT AUTO_INCREMENT NOT NULL, ptype VARCHAR(255) DEFAULT NULL, v0 VARCHAR(255) DEFAULT NULL, v1 VARCHAR(255) DEFAULT NULL, v2 VARCHAR(255) DEFAULT NULL, v3 VARCHAR(255) DEFAULT NULL, v4 VARCHAR(255) DEFAULT NULL, v5 VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}

public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE casbin_rules');
}
}
```

execute your migrations:
```shell
$ php bin/console doctrine:migrations:migrate
```

Then you can start like this:

```php
use Easyswolle\Permission\Casbin;
use Easyswolle\Permission\Config;

$config = new Config();
$config->setUrl($_ENV['DATABASE_URL']);
$casbin = new Casbin($config);

// adds permissions to a user
$casbin->addPermissionForUser('eve', 'articles', 'read');
// adds a role for a user.
$casbin->addRoleForUser('eve', 'writer');
// adds permissions to a rule
$casbin->addPolicy('writer', 'articles', 'edit');
```

You can check if a user has a permission like this:

```php
// to check if a user has permission
if ($casbin->enforce('eve', 'articles', 'edit')) {
// permit eve to edit articles
} else {
// deny the request, show an error
}
```

### Using Enforcer Api

It provides a very rich api to facilitate various operations on the Policy:

Gets all roles:

```php
$casbin->getAllRoles(); // ['writer', 'reader']
```

Gets all the authorization rules in the policy.:

```php
$casbin->getPolicy();
```

Gets the roles that a user has.

```php
$casbin->getRolesForUser('eve'); // ['writer']
```

Gets the users that has a role.

```php
$casbin->getUsersForRole('writer'); // ['eve']
```

Determines whether a user has a role.

```php
$casbin->hasRoleForUser('eve', 'writer'); // true or false
```

Adds a role for a user.

```php
$casbin->addRoleForUser('eve', 'writer');
```

Adds a permission for a user or role.

```php
// to user
$casbin->addPermissionForUser('eve', 'articles', 'read');
// to role
$casbin->addPermissionForUser('writer', 'articles','edit');
```

Deletes a role for a user.

```php
$casbin->deleteRoleForUser('eve', 'writer');
```

Deletes all roles for a user.

```php
$casbin->deleteRolesForUser('eve');
```

Deletes a role.

```php
$casbin->deleteRole('writer');
```

Deletes a permission.

```php
$casbin->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
```

Deletes a permission for a user or role.

```php
$casbin->deletePermissionForUser('eve', 'articles', 'read');
```

Deletes permissions for a user or role.

```php
// to user
$casbin->deletePermissionsForUser('eve');
// to role
$casbin->deletePermissionsForUser('writer');
```

Gets permissions for a user or role.

```php
$casbin->getPermissionsForUser('eve'); // return array
```

Determines whether a user has a permission.

```php
$casbin->hasPermissionForUser('eve', 'articles', 'read'); // true or false
```

See [Casbin API](https://casbin.org/docs/en/management-api) for more APIs.