File: D:/HostingSpaces/farmfun/reserveren.farmfun.be/app/Komma/Locations/LocationPolicy.php
<?php
namespace App\Komma\Locations;
use App\Komma\Base\Policy;
use App\Komma\Locations\Models\Location;
use App\Komma\Users\Models\KmsUser;
use App\Komma\Users\Models\KmsUserRole;
use Illuminate\Auth\Access\HandlesAuthorization;
final class LocationPolicy extends Policy
{
use HandlesAuthorization;
protected $modelClassName = Location::class;
/**
* Create a new policy instance.
*
* @return void
*/
public function __construct()
{
// parent::$DEBUG = LocationPolicy::class;
}
/**
* Determine if it is allowed to view all resources
*
* @param KmsUser|null $user
* @return bool
*/
public function index(KmsUser $user): bool
{
$result = $user->isAtLeast(KmsUserRole::Editor);
$this->debug('index', $result);
return $result;
}
/**
* Determine if it is allowed to view a specific resource
*
* @param KmsUser|null $user
* @return bool
*/
public function show(KmsUser $user, $modelToShow): bool
{
if ($user->role == KmsUserRole::Editor) { // Allows if the user has Editor role and the location is bind it
if ($user->location_id == $modelToShow->id) {
return true;
}
}
return parent::show($user, $modelToShow);
}
/**
* Determine if it is allowed show a form to edit a resource.
* This usually means that the user did view the model to be edited and got past
* the show authorisation. Then he edited a form to change the model and pressed
* save. After he pressed save he will trigger this edit authorisation
*
* @param KmsUser|null $user
* @return bool
*/
public function edit(KmsUser $user, $modelToEdit): bool
{
if ($user->role == KmsUserRole::Editor) { // Allows if the user has Editor role and the location is bind it
if ($user->location_id == $modelToEdit->id) {
return true;
}
}
return parent::edit($user, $modelToEdit);
}
/**
* Determine if it is allowed to update an existing resource after editing it
*
* @param KmsUser|null $user
* @return bool
*/
public function update(KmsUser $user, $modelToEdit): bool
{
if ($user->role == KmsUserRole::Editor) { // Allows if the user has Editor role and the location is bind it
if ($user->location_id == $modelToEdit->id) {
return true;
}
}
return parent::update($user, $modelToEdit);
}
}