55 lines
1.1 KiB
PHP
55 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
abstract class BasicPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
protected $entity;
|
|
|
|
/**
|
|
* Check if user has ability to manage entity before evaluating any other policy method
|
|
* @param User $user
|
|
* @param $ability
|
|
*
|
|
* @return bool|null
|
|
*/
|
|
public function before(User $user, $ability)
|
|
{
|
|
|
|
// check if user can operate on given entity
|
|
if (!$this->manage($user)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Can user manage given entity?
|
|
*
|
|
* @param User $user
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function manage(User $user)
|
|
{
|
|
return $user->hasAbility($this->entity . '-management');
|
|
}
|
|
|
|
/**
|
|
* Determine whether the user can delete the content.
|
|
*
|
|
* @param \App\Models\User $user
|
|
* @param Model $model
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function delete(User $user, Model $model)
|
|
{
|
|
return $user->hasAbility('delete-' . $this->entity);
|
|
}
|
|
}
|