1. Go to this page and download the library: Download authority-php/authority library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
authority-php / authority example snippets
if ($user->hasRole('admin') || $user->hasRole('moderator') || $user->hasRole('editor')) {
// Can perform actions on resource
$post->delete();
}
if ($authority->can('edit', $post)) {
// Can perform actions on resource
$post->delete();
}
use Authority\Authority;
// Assuming you have your current user stored
// in $currentUser, with the id property of 1
$authority = new Authority($currentUser);
/*
* Let's assign an alias to represent a group of actions
* so that we don't have to handle each action individually each time
*/
$authority->addAlias('manage', ['create', 'update', 'index', 'read', 'delete']);
// Let's allow a User to see all other User resources
$authority->allow('read', 'User');
/*
* Now let's restrict a User to managing only hiself or herself through
* the use of a conditional callback.
*
* Callback Parameters:
* $self is always the current instance of Authority so that we always
* have access to the user or other functions within the scope of the callback.
* $user here will represent the User object we'll pass into the can() method later
*/
$authority->allow('manage', 'User', function($self, $user) {
// Here we'll compare id's of the user objects - if they match, permission will
// be granted, else it will be denied.
return $self->user()->id === $user->id;
});
// Now we can check to see if our rules are configured properly
$otherUser = (object) ['id' => 2];
if ($authority->can('read', 'User')) {
echo 'I can read about any user based on class!';
}
if ($authority->can('read', $otherUser)) {
echo 'I can read about another user!';
}
if ($authority->can('delete', $otherUser)) {
echo 'I cannot edit this user so you will not see me :(';
}
if ($authority->can('delete', $user)) {
echo 'I can delete my own user, so you see me :)';
}
" "authority-php/authority": "dev-master"
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.