Download the PHP package authority-php/authority without Composer
On this page you can find all versions of the php package authority-php/authority. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package authority
Authority
A simple and flexible activity/resource based authorization system for PHP
Installation via Composer
Add Authority to your composer.json file to require Authority
And install via composer
composer install
Further installation information is available in docs/install.md
Introduction
Authority is an authorization system for PHP that focuses more on the concept of activities and resources rather than roles. Using different user roles is still completely possible and often needed, but rather than determining functionality based on roles throughout your app, Authority allows you to simply check if a user is allowed to perform an action on a given resource or activity.
Let's take an example of editing a Post $post
.
First we'll use standard role-based authorization checks for roles that may be able to delete a post
While this certainly works, it is highly prone to needing changes, and could get quite large as roles increase.
Let's instead see how simply checking against an activity on a resourse looks.
Instead of littering the codebase with several conditionals about user roles, we only need to write out a conditional that reads like "if the current user can edit this post".
Default behavior
Two important default behaviors of Authority to keep in mind
- Unspecified rules are denied - if you check a rule that has not been set, Authority will deny the activity.
- Rules are evaluated in order of declaration - last rule takes precedence.
Basic usage
Authority is intended to be instantiated once per application (though supports multiple instances). It works well with an IoC (Inversion of Control) container that supports singleton access, like Laravel's IoC, or by using standard dependency injection. You may assign rules prior to your app authorizing resources, or add at any time.
The Authority constructor requires at least one argument - the object that represents the current user. We'll cover the second optional argument later.
If we run the above script, we will see:
I can read about any user based on class!
I can read about another user!
I can delete my own user, so you see me :)
Intermediate Usage
Coming soon
Advanced Usage
Coming soon