2023-02-22 20:21:36 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Policies;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
|
|
|
trait OwnershipTrait
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Ownership of single item condition
|
2023-02-25 21:12:10 +01:00
|
|
|
*
|
2023-02-22 20:21:36 +01:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isOwnerOf(User $user, mixed $item)
|
|
|
|
{
|
|
|
|
return $user->id === $item->user_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ownership of collection condition
|
2023-02-25 21:12:10 +01:00
|
|
|
*
|
2023-02-22 20:21:36 +01:00
|
|
|
* @template TKey of array-key
|
|
|
|
* @template TValue
|
2023-02-25 21:12:10 +01:00
|
|
|
*
|
2023-02-22 20:21:36 +01:00
|
|
|
* @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue> $items
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isOwnerOfEach(User $user, $items)
|
|
|
|
{
|
|
|
|
foreach ($items as $item) {
|
|
|
|
if (! $this->isOwnerOf($user, $item)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|