https://github.com/devvoh/rights
Rights is a simple bitmask-based rights system, using binary strings to represent rights.
https://github.com/devvoh/rights
acl parable php7 php71 php72 php73 policy rights
Last synced: 2 months ago
JSON representation
Rights is a simple bitmask-based rights system, using binary strings to represent rights.
- Host: GitHub
- URL: https://github.com/devvoh/rights
- Owner: devvoh
- License: mit
- Created: 2019-03-23T11:03:42.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-26T19:49:30.000Z (almost 5 years ago)
- Last Synced: 2025-01-19T18:43:59.023Z (4 months ago)
- Topics: acl, parable, php7, php71, php72, php73, policy, rights
- Language: PHP
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Rights
[](https://packagist.org/packages/parable-php/rights)
Rights is a simple bitmask-based rights system, using binary strings to represent rights. This allows for easy storage and fast handling.
## Install
Php 7.1+ and [composer](https://getcomposer.org) are required.
```bash
$ composer require parable-php/rights
```## Usage
To create a rights instance with CRUD rights:
```php
$rights = new Rights();$rights->add('create', 'read', 'update', 'delete');
```The binary string for 4 rights is `0000` (all disabled) -> `1111` (all enabled).
```php
$rights->has('0001', 'create'); // true, since last position is 1
$rights->has('0001', 'read'); // false, since before-last is 0
```Rights are represented in the binary string in reverse order. You can add as many rights as you want.
You can also combine two rights strings together, keeping the high values. This can be used to combine a user's individual rights and the rights set for that user's user group.
```php
$combined = $rights->combine('1000', '0001'); // $combined = 1001
```## API
- `add(string ...$names): void` - add a right by name
- `getAll(): int[]` - get all rights (`[string => int]`)
- `getNames(): string[]` - get all rights' names
- `get(string $name): ?int` - get specific right value by name
- `can(string $provided, string $name): bool` - check if `$provided` string can take action `$name`
- `combine(string ...$rights): string` - combine two strings, keeping the highest
- `getRightsFromNames(string ...$names): string` - create binary string from rights passed
- `getNamesFromRights(string $rights): string[]` - get enabled rights from binary string## Contributing
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at [devvoh.com](https://devvoh.com).
## License
Rights is licensed under the MIT license.