добавил политику на новости и кеширование доступных городов
This commit is contained in:
parent
e784abd65f
commit
ef147fbfdb
@ -15,6 +15,7 @@ class CreateModelAndMigrationCommand extends Command
|
||||
protected $signature = 'module:create {name}
|
||||
{--momi=}
|
||||
{--create_model=}
|
||||
{--create_policy=} {--model=}
|
||||
{--create_migration=}
|
||||
{--add_migration=}
|
||||
';
|
||||
@ -35,6 +36,9 @@ public function handle()
|
||||
$this->input->setOption('create_model', $this->option('momi'));
|
||||
$this->input->setOption('create_migration', $this->option('momi'));
|
||||
}
|
||||
if ($this->option('create_policy')) {
|
||||
$this->createPolicy();
|
||||
}
|
||||
if ($this->option('create_model')) {
|
||||
$this->createModel();
|
||||
}
|
||||
@ -63,6 +67,25 @@ private function createModel()
|
||||
|
||||
}
|
||||
|
||||
private function createPolicy()
|
||||
{
|
||||
try
|
||||
{
|
||||
$path = trim($this->argument('name'));
|
||||
$policy = Str::singular(class_basename($this->option('create_policy')));
|
||||
$model = Str::singular(class_basename($this->option('model')));
|
||||
$this->call('make:policy', [
|
||||
'name' => 'App\\Modules\\' . $path . '\\Policies\\' . $policy,
|
||||
'--model' => $model
|
||||
]);
|
||||
}
|
||||
catch (\Exception $e)
|
||||
{
|
||||
$e->getMessage();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private function createMigration()
|
||||
{
|
||||
$path = trim($this->argument('name'));
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
use Modules\Contracts\Models\ContractStatus;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
use Modules\Main\Models\Company\Company;
|
||||
use Modules\Main\Models\Company\CompanyAdmin;
|
||||
@ -64,8 +65,7 @@ function GetAvailableAgents($resultType = 'Collection')
|
||||
return $agents->get();
|
||||
} else {
|
||||
return $agents;
|
||||
}
|
||||
;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -96,8 +96,7 @@ function GetAvailableCompanies($resultType = 'Collection')
|
||||
return $companies->get();
|
||||
} else {
|
||||
return $companies;
|
||||
}
|
||||
;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -128,6 +127,7 @@ function GetAvailableComplexes()
|
||||
function GetAvailableCities()
|
||||
{
|
||||
$user = auth()->user();
|
||||
return Cache::remember('helper_available_cities_of_user_' . $user->id, now()->addMinutes(10), function () use ($user) {
|
||||
if ($user->hasRole(Role::SUPER_ADMIN)) {
|
||||
return City::all();
|
||||
}
|
||||
@ -153,6 +153,9 @@ function GetAvailableCities()
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
|
||||
/*$agents = [];
|
||||
if ($adminCompany = AdminCompanyOfUser()) {
|
||||
$agents = Agent::where('company_id', $adminCompany->id);
|
||||
|
||||
96
app/Modules/Post/Policies/PostPolicy.php
Normal file
96
app/Modules/Post/Policies/PostPolicy.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,7 @@
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Livewire\Livewire;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
|
||||
class ModuleServiceProvider extends ServiceProvider
|
||||
{
|
||||
@ -25,6 +26,11 @@ public function boot()
|
||||
$this->registerLivewire();
|
||||
}
|
||||
|
||||
protected function registerPolicies()
|
||||
{
|
||||
Gate::policy(\Modules\Post\Models\Post::class, \Modules\Post\Policies\PostPolicy::class);
|
||||
}
|
||||
|
||||
protected function registerViews()
|
||||
{
|
||||
$moduleViewsPath = __DIR__ . '/../Views';
|
||||
|
||||
Loading…
Reference in New Issue
Block a user