PHP code example of flixtechs-labs / laravel-authorizer
1. Go to this page and download the library: Download flixtechs-labs/laravel-authorizer 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/ */
flixtechs-labs / laravel-authorizer example snippets
namespace App\Policies;
use App\Enums\PostState;
use App\Models\Post;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;
class PostPolicy
{
use HandlesAuthorization;
/**
* Determine whether the user can view any models.
*
* @param User $user
* @return Response|bool
*/
public function viewAny(User $user): Response|bool
{
return $user->can('view all posts');
}
/**
* Determine whether the user can view the model.
*
* @param User|null $user
* @param Post $post
* @return Response|bool
*/
public function view(?User $user, Post $post): Response|bool
{
return $user->can('view post')
}
/**
* Determine whether the user can create models.
*
* @param User $user
* @return Response|bool
*/
public function create(User $user): Response|bool
{
return $user->can('create post');
}
/**
* Determine whether the user can update the model.
*
* @param User $user
* @param Post $post
* @return Response|bool
*/
public function update(User $user, Post $post): Response|bool
{
return $user->can('update post');
}
/**
* Determine whether the user can delete the model.
*
* @param User $user
* @param Post $post
* @return Response|bool
*/
public function delete(User $user, Post $post): Response|bool
{
return $user->can('delete post');
}
/**
* Determine whether the user can restore the model.
*
* @param User $user
* @param Post $post
* @return Response|bool
*/
public function restore(User $user, Post $post): Response|bool
{
return $user->can('restore post');
}
/**
* Determine whether the user can permanently delete the model.
*
* @param User $user
* @param Post $post
* @return Response|bool
*/
public function forceDelete(User $user, Post $post): Response|bool
{
return $user->can('force delete post');
}
}
namespace App\Controllers;
use App\Models\Post;
public function __construct()
{
$this->authorizeResource(Post::class, 'post');
}
public function update(UpdatePostRequest $request, Post $post)
{
$this->authorize('update', $post);
}