-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tenant): adicionando atalho dev resetar senhas e criar tenant (#116
) * 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
- Loading branch information
1 parent
16e4614
commit c4fdbe4
Showing
5 changed files
with
275 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
class AdicionaTenantDev extends CommandDev | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'dev:add-tenant {--tenants=*}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Add a new tenant to the database Central'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$handle = parent::handle(); | ||
|
||
if ($handle === false) { | ||
return false; | ||
} | ||
|
||
if (!$this->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')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
|
||
class CommandBase extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'command:name'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Command description'; | ||
|
||
protected $tenant; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
|
||
$this->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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
class CommandDev extends CommandBase | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'dev:new_command'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Description command base'; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
if (env('APP_ENV') == 'production') { | ||
$this->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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Tenant; | ||
use App\Models\User; | ||
use Hash; | ||
|
||
class ResetPasswordDev extends CommandDev | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'dev:reset-password {--secret=*} {--tenants=*}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Reset the password for all users'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
$handle = parent::handle(); | ||
|
||
if ($handle === false) { | ||
return false; | ||
} | ||
|
||
if (!$this->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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters