update 20.10.2025
This commit is contained in:
parent
8c11130b5d
commit
a939cd51ef
616 changed files with 84821 additions and 4121 deletions
72
dev/app-bak/Policies/ModelPolicy.php
Normal file
72
dev/app-bak/Policies/ModelPolicy.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\User;
|
||||
use Illuminate\Auth\Access\HandlesAuthorization;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
abstract class ModelPolicy
|
||||
{
|
||||
use HandlesAuthorization;
|
||||
|
||||
abstract protected function getModelClass(): string;
|
||||
|
||||
public function viewAny(User $user)
|
||||
{
|
||||
return $user->can('view-any-' . $this->getModelClass());
|
||||
}
|
||||
|
||||
public function view(User $user, Model $model)
|
||||
{
|
||||
if ($user->can('view-' . $this->getModelClass())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($user->can('view-self-' . $this->getModelClass())) {
|
||||
return $this->isOwner($user, $model);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function create(User $user)
|
||||
{
|
||||
return $user->can('create-' . $this->getModelClass());
|
||||
}
|
||||
|
||||
public function update(User $user, Model $model)
|
||||
{
|
||||
if ($user->can('update-' . $this->getModelClass())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($user->can('update-self-' . $this->getModelClass())) {
|
||||
return $this->isOwner($user, $model);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function delete(User $user, Model $model)
|
||||
{
|
||||
if ($user->can('delete-' . $this->getModelClass())) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($user->can('delete-self-' . $this->getModelClass())) {
|
||||
return $this->isOwner($user, $model);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isOwner(User $user, Model $model): bool
|
||||
{
|
||||
if (!empty($user) && method_exists($model, 'user')) {
|
||||
return $user->getKey() === $model->getRelation('user')->getKey();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue