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
- Host: GitHub
- URL: https://github.com/thomiceli/voters
- Owner: thomiceli
- License: gpl-3.0
- Created: 2020-06-10T23:18:20.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-04-28T21:20:43.000Z (about 5 years ago)
- Last Synced: 2025-01-16T10:32:31.865Z (over 1 year ago)
- Topics: permission, php, user, voter
- Language: PHP
- Homepage: https://packagist.org/packages/thomas-miceli/voters
- Size: 42 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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