-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
157 additions
and
0 deletions.
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,24 @@ | ||
<?php | ||
|
||
namespace Orangesix\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Orangesix\Service\Log\LogService; | ||
|
||
class LogDB | ||
{ | ||
/** | ||
* Realiza gestão do Log via banco de dados | ||
* @param Request $request | ||
* @param Closure $next | ||
* @return mixed | ||
* @throws \Illuminate\Contracts\Container\BindingResolutionException | ||
*/ | ||
public function handle(Request $request, Closure $next): mixed | ||
{ | ||
$ignore = config('logging.db_route_ignore'); | ||
(app()->make(LogService::class))->dispatch(empty($ignore) ? [] : $ignore); | ||
return $next($request); | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace Orangesix\Models\Log; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class LogModel extends Model | ||
{ | ||
/** @var string */ | ||
protected $table = 'log'; | ||
|
||
/** @var string */ | ||
protected $connection = 'mysql'; | ||
|
||
public function __construct(array $attributes = []) | ||
{ | ||
parent::__construct($attributes); | ||
$this->table = env('LOG_DB', 'log'); | ||
$this->connection = env('DB_CONNECTION', 'mysql'); | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Orangesix\Repository\Log; | ||
|
||
use Orangesix\Models\Log\LogModel; | ||
use Orangesix\Repository\Repository; | ||
use Orangesix\Repository\RepositoryDataBase; | ||
|
||
/** | ||
* Repository - Log | ||
*/ | ||
class LogRepository implements Repository | ||
{ | ||
use RepositoryDataBase; | ||
|
||
/** @var LogModel */ | ||
private LogModel $model; | ||
|
||
public function __construct(LogModel $model) | ||
{ | ||
$this->model = $model; | ||
} | ||
} |
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,89 @@ | ||
<?php | ||
|
||
namespace Orangesix\Service\Log; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Orangesix\Service\ServiceBase; | ||
use Orangesix\Repository\Log\LogRepository; | ||
|
||
/** | ||
* Repository - LOG | ||
*/ | ||
class LogService extends ServiceBase | ||
{ | ||
public function __construct(LogRepository $repository) | ||
{ | ||
parent::__construct($repository); | ||
} | ||
|
||
/** | ||
* Realiza o disparo do LOG | ||
* @return void | ||
*/ | ||
public function dispatch(array $ignore = []): void | ||
{ | ||
try { | ||
DB::connection(env('DB_CONNECTION', 'mysql'))->getPdo(); | ||
|
||
if (Schema::hasTable(env('LOG_DB', 'log'))) { | ||
$this->log($ignore); | ||
} | ||
} catch (\Exception $exception) { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Realiza a validação dos dados LOG | ||
* @param Request $request | ||
* @return array | ||
*/ | ||
public function validated(Request $request): array | ||
{ | ||
$data = $request->validate([ | ||
'log_id_usuario' => '', | ||
'log_usuario' => '', | ||
'log_rota' => 'required', | ||
'log_ip' => 'required', | ||
'log_action' => 'required', | ||
'log_dados' => '', | ||
]); | ||
|
||
$now = date('Y-m-d H:i'); | ||
if ($this->repository->getModel()::query() | ||
->where('log_id_usuario', '=', $data['log_id_usuario']) | ||
->where('log_usuario', '=', $data['log_usuario']) | ||
->where('log_rota', '=', $data['log_rota']) | ||
->where('log_dados', '=', $data['log_dados']) | ||
->whereRaw("DATE_FORMAT(created_at, '%Y-%m-%d %H:%i') = '{$now}'") | ||
->exists()) { | ||
throw new \Exception('Log já existe'); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Realiza a gestão do LOG | ||
* @return void | ||
*/ | ||
private function log(array $ignore = []): void | ||
{ | ||
$user = Auth::user(); | ||
$dados = \request()->all(); | ||
$rota = \request()->route()->getName(); | ||
if (!in_array($rota, $ignore)) { | ||
$this->manager(new Request([ | ||
'log_id_usuario' => $user?->id, | ||
'log_usuario' => $user?->nome, | ||
'log_rota' => \request()->route()->getName(), | ||
'log_ip' => \request()->ip(), | ||
'log_action' => \request()->method(), | ||
'log_dados' => empty($dados) || (isset($dados['iframe']) && $dados['iframe'] == '1') ? null : json_encode($dados) | ||
])); | ||
} | ||
} | ||
} |