Skip to content

Instantly share code, notes, and snippets.

@valorin
Created September 15, 2021 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save valorin/c7830b9e300db3ef977c11657659ac7b to your computer and use it in GitHub Desktop.
Save valorin/c7830b9e300db3ef977c11657659ac7b to your computer and use it in GitHub Desktop.
<?php
namespace App\Policies;
use App\Models\Tip;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class TipPolicy
{
use HandlesAuthorization;
// Admins and Moderators can fully manage Tips
public function before(User $user, $ability)
{
if ($user->isAdmin() || $user->isTipModerator()) {
return true;
}
}
// Users can see a list of Tips
public function viewAny(User $user)
{
return true;
}
// Users can only view their own Tips (Admins/Mods can view all)
public function view(User $user, Tip $tip)
{
return $user->id === $tip->user_id;
}
// Users cannot create Tips (only Admins/Mods can)
public function create(User $user)
{
return false;
}
// Users can update Tips
public function update(User $user, Tip $tip)
{
return $this->view($user, $tip);
}
// Users can soft delete Tips
public function delete(User $user, Tip $tip)
{
return true;
}
// Users can restore Tips
public function restore(User $user, Tip $tip)
{
return $this->delete($user, $tip);
}
// Users cannot force delete Tips (only Admins/Mods can)
public function forceDelete(User $user, Tip $tip)
{
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment