lk.zachem.info/app/Modules/Post/Policies/PostPolicy.php

97 lines
2.4 KiB
PHP

<?php
namespace Modules\Post\Policies;
use Illuminate\Auth\Access\Response;
use Modules\Post\Models\Post;
use Modules\User\Models\User;
class PostPolicy
{
public function before(User $user, string $ability): bool|null
{
if ($user->isAdmin()) {
return true;
}
return null;
}
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user): bool
{
//
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Post $post): bool
{
if ($postCities = $post->cities->pluck('id')->toArray()) {
$availableCities = GetAvailableCities()->pluck('id')->toArray();
if (array_intersect($postCities, $availableCities)) {
return true;
}
}
return false;
}
/**
* Determine whether the user can create models.
*/
public function create(User $user): bool
{
if ($user->isCityManager()) {
return true;
}
return false;
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Post $post): bool
{
$citiesOfManager = \Modules\CityManager\Models\CityManager::where('user_id', $user->id)->pluck('city_id')->toArray();
if ($postCities = $post->cities->pluck('id')->toArray()) {
if (array_intersect($postCities, $citiesOfManager)) {
return true;
}
}
return false;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Post $post): bool
{
$citiesOfManager = \Modules\CityManager\Models\CityManager::where('user_id', $user->id)->pluck('city_id')->toArray();
if ($postCities = $post->cities->pluck('id')->toArray()) {
if (array_intersect($postCities, $citiesOfManager)) {
return true;
}
}
return false;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Post $post): bool
{
return false;
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Post $post): bool
{
return false;
}
}