From ef147fbfdb348a406e26793ea321e5fdf10420d1 Mon Sep 17 00:00:00 2001 From: developer Date: Thu, 7 May 2026 16:25:57 +0800 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=BE=D0=BB=D0=B8=D1=82=D0=B8=D0=BA=D1=83=20=D0=BD=D0=B0?= =?UTF-8?q?=20=D0=BD=D0=BE=D0=B2=D0=BE=D1=81=D1=82=D0=B8=20=D0=B8=20=D0=BA?= =?UTF-8?q?=D0=B5=D1=88=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20?= =?UTF-8?q?=D0=B4=D0=BE=D1=81=D1=82=D1=83=D0=BF=D0=BD=D1=8B=D1=85=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=80=D0=BE=D0=B4=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CreateModelAndMigrationCommand.php | 23 +++++ app/Modules/Main/Helpers/helper.php | 61 ++++++------ app/Modules/Post/Policies/PostPolicy.php | 96 +++++++++++++++++++ .../Post/Providers/ModuleServiceProvider.php | 6 ++ 4 files changed, 157 insertions(+), 29 deletions(-) create mode 100644 app/Modules/Post/Policies/PostPolicy.php diff --git a/app/Console/Commands/CreateModelAndMigrationCommand.php b/app/Console/Commands/CreateModelAndMigrationCommand.php index af6c4d3..25ab7bd 100644 --- a/app/Console/Commands/CreateModelAndMigrationCommand.php +++ b/app/Console/Commands/CreateModelAndMigrationCommand.php @@ -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')); diff --git a/app/Modules/Main/Helpers/helper.php b/app/Modules/Main/Helpers/helper.php index b448a9e..8d76a56 100644 --- a/app/Modules/Main/Helpers/helper.php +++ b/app/Modules/Main/Helpers/helper.php @@ -1,5 +1,6 @@ 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();*/ } -} \ No newline at end of file +} diff --git a/app/Modules/Post/Policies/PostPolicy.php b/app/Modules/Post/Policies/PostPolicy.php new file mode 100644 index 0000000..24bfbb5 --- /dev/null +++ b/app/Modules/Post/Policies/PostPolicy.php @@ -0,0 +1,96 @@ +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; + } +} diff --git a/app/Modules/Post/Providers/ModuleServiceProvider.php b/app/Modules/Post/Providers/ModuleServiceProvider.php index ea2d533..2afcc28 100644 --- a/app/Modules/Post/Providers/ModuleServiceProvider.php +++ b/app/Modules/Post/Providers/ModuleServiceProvider.php @@ -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';