1. Go to this page and download the library: Download kordy/auzo-tools 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/ */
// config/acl.php
return [
'before' => [
function($user, $ability) {
return $user->id == 1;
}
],
'abilities' => [
'post.update' => [
function($user, $ability, $model) { return $user->id == 3; },
['or' => function ($user, $ability, $model) { return $user->id == 2; }],
],
'post.destroy' => [
function ($user, $ability, $model) { return $user->id == 2; },
],
],
// use this to log or monitor authorization given to users
// you may not modify the result of the authorization check from an after callback
'after' => [
function ($user, $ability, $result, $arguments = null)
{
if ($result) {
\Log::info("Authorization Log: User $user->name ($user->email) is granted access to ability $ability at ".date('d-m-Y H:j'));
} else {
\Log::info("Authorization Log: User $user->name ($user->email) is forbidden to access ability $ability at ".date('d-m-Y H:j'));
}
},
],
];
// config/acl.php
return [
'before' => [
'App\MyPolicyClass@isAdmin'
],
'abilities' => [
'post.update' => [
'App\MyPolicyClass@postOwner',
['or' => 'App\MyPolicyClass@isModerator']
],
'post.destroy' => [
'App\MyPolicyClass@isModerator'
],
],
// use this to log or monitor authorization given to users
// you may not modify the result of the authorization check from an after callback
'after' => [
'App\MyPolicyClass@monitor'
],
];
namespace App;
class MyPolicyClass
{
/**
* Check if user is admin
*
* @param $user
* @param $ability
* @return bool
*/
public function isAdmin($user, $ability) {
return $user->id == 1;
}
/**
* Check if user is moderator
*
* @param $user
* @param $ability
* @return bool
*/
public function isModerator($user, $ability) {
return $user->role == 'moderator';
}
/**
* Check if user is post owner
*
* @param $user
* @param $ability
* @return bool
*/
public function postOwner($user, $ability, $post) {
if ($post instanceOf Post) {
return $user->id == $post->user_id;
}
// If middleware passed you the user request instead of the model
// instance, get the resource information from the request
if ($post === null || $post instanceof Request) {
$postId = request()->route('id');
$post = Post::find($postId);
return $user->id == $post->user_id;
}
}
/**
* Run authorization monitor, see storage/logs/laravel.log
*
* @param $user
* @param $ability
*/
public function monitor($user, $ability, $result, $arguments = null)
{
if ($result) {
\Log::info("Authorization Log: User $user->name ($user->email) is granted access to ability $ability at " . date('d-m-Y H:j'));
} else {
\Log::info("Authorization Log: User $user->name ($user->email) is forbidden to access ability $ability at " . date('d-m-Y H:j'));
}
}
}
// app/Providers/AppServiceProvider.php
public function boot()
{
// Load abilities to Laravel Gate
$abilities_policies = config('acl');
\AuzoToolsPermissionRegistrar::registerPermissions($abilities_policies);
}
$user->can('post.show', $post)
// or
$user->cannot('post.update', $post)
// or for current logged in user
Gate::allows('post.update', Post::findOrFail($postId));
$file_path = config_path('abilities/generated_abilities.json');
// This will faltten the output array
GenerateAbilities::fullCrudAbilities($model)->writeToFile($file_path);
// This will not faltten the output array
GenerateAbilities::fullCrudAbilities($model)->writeToFile($file_path, false);