Skip to content

Commit

Permalink
feat(tenant): adicionando atalho dev resetar senhas e criar tenant (#116
Browse files Browse the repository at this point in the history
)

* 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
CeruttiMaicon authored Aug 17, 2024
1 parent 16e4614 commit c4fdbe4
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 1 deletion.
59 changes: 59 additions & 0 deletions app/Console/Commands/AdicionaTenantDev.php
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'));
}
}
}
97 changes: 97 additions & 0 deletions app/Console/Commands/CommandBase.php
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);
}
}
45 changes: 45 additions & 0 deletions app/Console/Commands/CommandDev.php
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;
}
}
}
74 changes: 74 additions & 0 deletions app/Console/Commands/ResetPasswordDev.php
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;
}
}
}
}
1 change: 0 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ public function getActivitylogOptions(): LogOptions
->logOnlyDirty()
->dontLogIfAttributesChangedOnly(
[
'password',
'remember_token',
'token',
'token_sessao',
Expand Down

0 comments on commit c4fdbe4

Please sign in to comment.