Rights is a simple bitmask-based rights system, using binary strings to represent rights. This allows for easy storage and fast handling.
Php 7.1+ and composer are required.
$ composer require parable-php/rights
To create a rights instance with CRUD rights:
$rights = new Rights();
$rights->add('create', 'read', 'update', 'delete');
The binary string for 4 rights is 0000
(all disabled) -> 1111
(all enabled).
$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.
$combined = $rights->combine('1000', '0001'); // $combined = 1001
add(string ...$names): void
- add a right by namegetAll(): int[]
- get all rights ([string => int]
)getNames(): string[]
- get all rights' namesget(string $name): ?int
- get specific right value by namecan(string $provided, string $name): bool
- check if$provided
string can take action$name
combine(string ...$rights): string
- combine two strings, keeping the highestgetRightsFromNames(string ...$names): string
- create binary string from rights passedgetNamesFromRights(string $rights): string[]
- get enabled rights from binary string
Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.
Rights is licensed under the MIT license.