From c4fdbe4f899a697b525753933c4efe1819d0ab3f Mon Sep 17 00:00:00 2001 From: Maicon Cerutti Date: Sat, 17 Aug 2024 00:25:19 -0300 Subject: [PATCH] feat(tenant): adicionando atalho dev resetar senhas e criar tenant (#116) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: adicionado novos comandos para importação de bancos de dados, criação de tenants e reset de senhas para ambiente de desenvolvimento * chore: ajustes php cs fixer --- app/Console/Commands/AdicionaTenantDev.php | 59 +++++++++++++ app/Console/Commands/CommandBase.php | 97 ++++++++++++++++++++++ app/Console/Commands/CommandDev.php | 45 ++++++++++ app/Console/Commands/ResetPasswordDev.php | 74 +++++++++++++++++ app/Models/User.php | 1 - 5 files changed, 275 insertions(+), 1 deletion(-) create mode 100644 app/Console/Commands/AdicionaTenantDev.php create mode 100644 app/Console/Commands/CommandBase.php create mode 100644 app/Console/Commands/CommandDev.php create mode 100644 app/Console/Commands/ResetPasswordDev.php diff --git a/app/Console/Commands/AdicionaTenantDev.php b/app/Console/Commands/AdicionaTenantDev.php new file mode 100644 index 00000000..d0d9fb06 --- /dev/null +++ b/app/Console/Commands/AdicionaTenantDev.php @@ -0,0 +1,59 @@ +option('tenants')) { + $tenants[] = $this->ask('What is the tenant id?'); + } else { + $tenants = $this->option('tenants'); + } + + foreach ($tenants as $tenant) { + if (\App\Models\Tenant::find($tenant)) { + $this->error("Tenant {$tenant} already exists!"); + + continue; + } + + $tenant = \App\Models\Tenant::create([ + 'id' => $tenant, + ]); + + $this->info("Tenant {$tenant->id} added successfully!"); + + $tenant->domains()->create(['domain' => $tenant->id . '.' . env('APP_HOST')]); + + $this->info('Default domain added successfully!'); + $this->info("{$tenant->id}" . '.' . env('APP_HOST')); + } + } +} diff --git a/app/Console/Commands/CommandBase.php b/app/Console/Commands/CommandBase.php new file mode 100644 index 00000000..22e11b30 --- /dev/null +++ b/app/Console/Commands/CommandBase.php @@ -0,0 +1,97 @@ +tenant = null; + } + + public function formatProgress($tenant) + { + return "cron - tenant($tenant): PROCESSO $this->nomeProcesso %current%/%max% [%bar%] %percent:3s%% estimated: %estimated:-6s% - current: %elapsed:-3s% \n"; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + // + } + + /** + * @param mixed $nomeComando + * @param mixed $mensagem + */ + protected function infoComando($nomeComando, $mensagem): void + { + if (env('APP_ENV') == 'test') { + return; + } + $this->info('cron - ' . $mensagem . ': ' . $nomeComando); + } + + /** + * @param mixed $nomeComando + * @param mixed $tenant + * @param mixed $mensagem + */ + protected function processoComando($nomeComando, $tenant, $mensagem): void + { + if (env('APP_ENV') == 'test') { + return; + } + $this->info('cron - tenant(' . $tenant . '): ' . $mensagem . ' PROCESSO ' . $nomeComando); + } + + /** + * @param mixed $nomeComando + * @param mixed $tenant + * @param mixed $mensagem + */ + protected function processoComandoCancelado($nomeComando, $tenant, $mensagem): void + { + if (env('APP_ENV') == 'test') { + return; + } + $this->warn('cron - tenant(' . $tenant . '): ' . $mensagem . ' PROCESSO ' . $nomeComando); + } + + /** + * @param mixed $nomeComando + * @param mixed $tenant + * @param mixed $seeder + */ + protected function execSeederComando($nomeComando, $tenant, $seeder): void + { + if (env('APP_ENV') == 'test') { + return; + } + $this->info('cron - tenant(' . $tenant . '): seeder: ' . $seeder . ' PROCESSO ' . $nomeComando); + } +} diff --git a/app/Console/Commands/CommandDev.php b/app/Console/Commands/CommandDev.php new file mode 100644 index 00000000..e4fc2756 --- /dev/null +++ b/app/Console/Commands/CommandDev.php @@ -0,0 +1,45 @@ +error('It is not possible to run this command in the production environment'); + + return false; + } + + if (!(env('APP_DEBUG')) || env('APP_ENV') == 'staging') { + $this->error('It is not possible to run this command in this environment'); + + return false; + } + } +} diff --git a/app/Console/Commands/ResetPasswordDev.php b/app/Console/Commands/ResetPasswordDev.php new file mode 100644 index 00000000..b9918911 --- /dev/null +++ b/app/Console/Commands/ResetPasswordDev.php @@ -0,0 +1,74 @@ +option('secret')) { + $secret = $this->ask('What is the new password?'); + } else { + $secret = $this->option('secret')[0]; + } + + $tenants = $this->option('tenants') == [] + ? Tenant::pluck('id')->toArray() + : $this->option('tenants'); + + foreach ($tenants as $tenant) { + tenancy()->initialize($tenant); + + $this->processoComando('update_passwords', $tenant, 'INICIO'); + + try { + $total = User::count(); + $total = $total > 0 ? $total : 1; + $total = ceil($total / 100); + $bar = $this->output->createProgressBar($total); + $bar->start(); + + User::chunk(100, function ($users) use ($secret, $bar) { + foreach ($users as $user) { + $user->password = Hash::make($secret); + $user->save(); + } + $bar->advance(); + }); + $this->processoComando('update_passwords', $tenant, 'FIM'); + } catch (\Throwable $error) { + $this->processoComando('update_passwords', $tenant, 'ERRO'); + throw $error; + } + } + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 42ddec22..66b8b1f4 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -126,7 +126,6 @@ public function getActivitylogOptions(): LogOptions ->logOnlyDirty() ->dontLogIfAttributesChangedOnly( [ - 'password', 'remember_token', 'token', 'token_sessao',