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

https://github.com/thomiceli/voters

Check and validate complex specific user permissions
https://github.com/thomiceli/voters

permission php user voter

Last synced: 8 months ago
JSON representation

Check and validate complex specific user permissions

Awesome Lists containing this project

README

          

# Voters

Useful to check complex user permissions.
###### Inspired from [Symfony voter system](https://symfony.com/doc/current/security/voters.html).

### Installation

```shell
$ composer require thomas-miceli/voters
```

### Usage

To check a specific permission of an user action `$attribute` (e.g. view or edit) with or without a `$subject` (e.g. an article), every registered voters will be called and will be checked to see if they can vote for the permission.
Every eligible voters will vote whether the user can have the permission or not.

Let's setup permissions for our blog.

#### Create the Permission object

```php
addVoter(new ArticleVoter); // register the voter for this permission object
$user = ...
$article = ...

$permission->can($user, ArticleVoter::VIEW, $article); // check if our user can view the article
$permission->can($user, ArticleVoter::EDIT, $article); // check if our user can edit the article
```

#### Create the voter

```php
getAuthor() === $user;
}

// the user has not the permission for other attributes
return false;
}
}
```

### Strategies

It's possible to set different strategies to determine the permission when multiple voters are eligible to vote for an attribute.

```php
canVote($attribute, $subject)) {
$vote = $voter->vote($user, $attribute, $subject);
//ConsoleLogger::debug($voter, $vote, $attribute, $user, $subject);

if ($vote) {
++$approvals;
}
}
}
return $approvals > 3;
}
}
```

...then use it.

```php