добавил политику на новости и кеширование доступных городов

This commit is contained in:
developer 2026-05-07 16:25:57 +08:00
parent e784abd65f
commit ef147fbfdb
4 changed files with 157 additions and 29 deletions

View File

@ -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'));

View File

@ -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,31 +127,35 @@ function GetAvailableComplexes()
function GetAvailableCities()
{
$user = auth()->user();
if ($user->hasRole(Role::SUPER_ADMIN)) {
return City::all();
}
if ($user->hasRole(Role::CITY_MANAGER)) {
$cititesOfManager = CityManager::where('user_id', $user->id)->pluck('city_id');
if ($cititesOfManager->count()) {
return City::whereIn('id', $cititesOfManager)->get();
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();
}
if ($user->hasRole(Role::CITY_MANAGER)) {
$cititesOfManager = CityManager::where('user_id', $user->id)->pluck('city_id');
if ($cititesOfManager->count()) {
return City::whereIn('id', $cititesOfManager)->get();
}
return false;
}
if ($user->hasRole(Role::COMPANY_ADMIN)) {
$adminCompany = AdminCompanyOfUser();
if ($adminCompany->count()) {
return City::where('id', $adminCompany->city_id)->get();
}
return false;
}
if ($user->hasRole(Role::AGENT)) {
$agentCompany = AgentCompanyOfUser();
if ($agentCompany->count()) {
return City::where('id', $agentCompany->city_id)->get();
}
return false;
}
return false;
}
if ($user->hasRole(Role::COMPANY_ADMIN)) {
$adminCompany = AdminCompanyOfUser();
if ($adminCompany->count()) {
return City::where('id', $adminCompany->city_id)->get();
}
return false;
}
if ($user->hasRole(Role::AGENT)) {
$agentCompany = AgentCompanyOfUser();
if ($agentCompany->count()) {
return City::where('id', $agentCompany->city_id)->get();
}
return false;
}
return false;
});
/*$agents = [];
if ($adminCompany = AdminCompanyOfUser()) {
$agents = Agent::where('company_id', $adminCompany->id);
@ -168,4 +171,4 @@ function GetAvailableCities()
$agents->with('user:id,name');
return $agents->get();*/
}
}
}

View 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;
}
}

View File

@ -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';