File manager - Edit - /var/www/html/pcadigital/api/app/Console/Commands/GenerateAuthorizationSettings.php
Back
<?php namespace App\Console\Commands; use Error; use Illuminate\Support\Arr; use Illuminate\Console\Command; /** * Command to setup all modules, permissions and roles that this application needs to work correctly */ class GenerateAuthorizationSettings extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'config:authorization'; /** * The console command description. * * @var string */ protected $description = 'Setup all modules, permissions and roles from this application.'; /** * Execute the console command. * * @return int */ public function handle() { $this->setupModules(); $this->setupPermissions(); return Command::SUCCESS; } private array $modules; private array $permissions; private string $accessToken; // private array $authorization = []; private array $authorization = [ 'modules' => [ [ 'id' => null, 'name' => 'Usuários', 'permissions' => [ 'Insert category', 'Update category', 'Get category', 'Insert sub category', 'Update sub category', 'Get sub category', 'Finish occurrence', 'Insert comment', 'Insert historic', 'Send historic to citizen' ] ] ] ]; private function authenticate(): void { $url = env('AUTH_SERVICE_URL') . '/api/login'; $response = authServiceClient('post', $url, [ 'grant_type' => 'password', 'client_id' => env('PASSPORT_CLIENT_ID'), 'client_secret' => env('PASSPORT_SECRET'), 'username' => '67108857049', 'password' => 'angra123', 'scope' => '', ]); if ($response->status() < 300) { $this->accessToken = $response->json()['access_token']; } } private function setupModules(): void { if (empty($this->accessToken)) { $this->authenticate(); } /** * necessary modules */ $modules = array_column($this->authorization['modules'], 'name'); /** * check if this modules already exists */ $url = env('AUTH_SERVICE_URL') . '/api/module'; $response = authServiceClient('get', $url, null, [ 'Authorization' => 'Bearer ' . $this->accessToken ]); if (!isset($response['data'])) { throw new Error('Data does not exist on response.'); } /** * defining modules */ $this->modules = array_column($response['data'], 'name'); /** * defining id from modules on authorization */ foreach($response['data'] as $m) { foreach($this->authorization['modules'] as $key => $module) { if (strtoupper(trim($module['name'])) == strtoupper(trim($m['name']))) { $this->authorization['modules'][$key]['id'] = $m['id']; } } } /** * creating modules */ $url = env('AUTH_SERVICE_URL') . '/api/module/store-many'; $modulesToCreate = array_diff($modules, $this->modules); if (count($modulesToCreate) > 0) { $request = [ 'app_id' => env('CLIENT_APP_KEY'), 'modules' => [] ]; foreach($modulesToCreate as $m) { $request['modules'][] = [ 'name' => $m ]; } $response = authServiceClient('post', $url, $request, [ 'Authorization' => 'Bearer ' . $this->accessToken ]); if ($response->status() > 300) { throw new Error(json_encode($response->json())); } } } private function setupPermissions(): void { if (empty($this->accessToken)) { $this->authenticate(); } /** * necessary permissions */ $permissions = array_merge(...array_column($this->authorization['modules'], 'permissions')); /** * check if this permissions already exists */ $url = env('AUTH_SERVICE_URL') . '/api/permission'; $response = authServiceClient('get', $url, null, [ 'Authorization' => 'Bearer ' . $this->accessToken ]); if (!isset($response['data'])) { throw new Error('Data does not exist on response.'); } $this->permissions = array_column($response['data']['permissions'], 'name'); /** * creating permissions */ $url = env('AUTH_SERVICE_URL') . '/api/permission/store-many'; $permissionsToCreate = array_diff($permissions, $this->permissions); if (count($permissionsToCreate) > 0) { foreach($this->authorization['modules'] as $m) { $request = [ 'module_id' => $m['id'], 'permissions' => [] ]; foreach($permissionsToCreate as $p) { if (in_array($p, $m['permissions'])) { $request['permissions'][] = [ 'name' => $p ]; } } if (count($request['permissions']) > 0) { $response = authServiceClient('post', $url, $request, [ 'Authorization' => 'Bearer ' . $this->accessToken ]); if ($response->status() > 300) { throw new Error(json_encode($response->json())); } } } } } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.02 |
proxy
|
phpinfo
|
Settings