Skip to content

Commit

Permalink
2024 08 28 melhorias (#44)
Browse files Browse the repository at this point in the history
* fix: retirado do README o texto sobre versão beta.

* fix: melhorado o layout e arquivos do processo de configuração inicial da ferramenta

* feat: Criado na home as Sessoes ativas

* feat: Melhorado as configurações de segurança da sessão

* fix: melhorado a utilização de variavel na PagesController

* fix: melhorado a ducumentacao de metodos do Session
  • Loading branch information
arthusantiago authored Sep 1, 2024
1 parent f976f6e commit e9ed15e
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 6 deletions.
4 changes: 3 additions & 1 deletion config/app.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,11 @@
'engine' => 'DatabaseSession',
'model' => 'Sessions'
],
'timeout' => 480,
'timeout' => 240,
'cookie' => '__Secure-KAW',
'ini' => [
'session.sid_length' => 40,
'session.sid_bits_per_character' => 6,
'session.cookie_secure' => true,
'session.cookie_samesite' => 'Strict',
'session.cookie_httponly' => true,
Expand Down
18 changes: 17 additions & 1 deletion src/Controller/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

use App\Model\Table\LogsTable;
use App\Model\Table\IpsBloqueadosTable;
use App\Model\Table\SessionsTable;
use Cake\Core\Configure;

/**
* Static content controller
Expand All @@ -38,6 +40,20 @@ public function home()
->find('ultimosBloqueados')
->toArray();

$this->set(compact('logs', 'ipsBloqueados'));
$sessoes = (new SessionsTable)
->find('all')
->select(['expires', 'created', 'user_agent'])
->contain(['Users' => ['fields' => ['username']]])
->orderDesc('sessions.created');

$sessoesAtivas = [];
$timeout = Configure::read('Session.timeout');
foreach ($sessoes as $sessao) {
if ($sessao->estaAtiva($timeout)) {
$sessoesAtivas[] = $sessao;
}
}

$this->set(compact('logs', 'ipsBloqueados', 'sessoesAtivas'));
}
}
28 changes: 28 additions & 0 deletions src/Model/Entity/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,26 @@ class Session extends Entity
*/
public bool $esteDispositivo = false;

/**
* Propriedade virtual 'navegador'. Para acessar: $session->navegador
*
* @see https://book.cakephp.org/4/en/orm/entities.html#accessors-mutators
* @access protected
* @return string
*/
protected function _getNavegador()
{
$navegadorSessao = $this->buscaNaString($this->navegadores, $this->user_agent) ?? 'Desconhecido';
return ucfirst($navegadorSessao);
}

/**
* Propriedade virtual 'sistema_operacional'. Para acessar: $session->sistema_operacional
*
* @see https://book.cakephp.org/4/en/orm/entities.html#accessors-mutators
* @access protected
* @return string
*/
protected function _getSistemaOperacional()
{
$sistOperaSessao = $this->buscaNaString($this->sistemasOperacionais, $this->user_agent) ?? 'Desconhecido';
Expand All @@ -96,4 +110,18 @@ protected function buscaNaString(array $itensProcurados, string $string): string
}
return false;
}

/**
* Verifica se a sessão excedeu o tempo de inatividade
*
* @access public
* @param int $maxTimeInactiv Tempo máximo de inatividade
* @return bool Retorna TRUE quando a sessão é considerada ativa, FALSE caso contrário.
*/
public function estaAtiva(int $maxTimeInactiv)
{
// Time Without user Interaction = (Current time - Time of Last Interaction)
$twi = time() - $this->expires;
return $twi >= $maxTimeInactiv ? false : true ;
}
}
6 changes: 6 additions & 0 deletions src/Model/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ class User extends Entity
'root'
];

public function usernameEncurtado(int $tamanho = 15): string
{
$complemento = strlen($this->username) > $tamanho ? '(...)' : '';
return substr($this->username, 0, $tamanho) . $complemento;
}

/**
* @param string $password
* @see https://book.cakephp.org/4/en/orm/entities.html#accessors-mutators
Expand Down
16 changes: 12 additions & 4 deletions templates/Pages/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</div>
</div>
<div class='row'>
<div class="col-sm-4 mb-3">
<h2>Atividades suspeitas</h2>
<div class="col-sm mb-3">
<h3>Atividades suspeitas</h3>
<?php if ($logs): ?>
<ul class="list-group">
<?php foreach($logs as $log): ?>
Expand All @@ -25,8 +25,8 @@
<p>Aparentemente tranquilo. Continue atento &#128373;&#127997;</p>
<?php endif; ?>
</div>
<div class="col-sm-auto mb-3">
<h2>Últimos IPs bloqueados</h2>
<div class="col-sm mb-3">
<h3>Últimos IPs bloqueados</h3>
<?php if ($ipsBloqueados): ?>
<ul class="list-group">
<?php foreach($ipsBloqueados as $ip): ?>
Expand All @@ -45,4 +45,12 @@
<p>Nenhum IP bloqueado</p>
<?php endif; ?>
</div>
<div class="col-sm mb-3">
<h3>Sessões ativas</h3>
<ul class="list-group">
<?php foreach($sessoesAtivas as $sessao): ?>
<li class="list-group-item"><?=h($sessao->created . ' - ' . $sessao->user->usernameEncurtado() . ' - ' . $sessao->navegador)?></li>
<?php endforeach;?>
</ul>
</div>
</div>

0 comments on commit e9ed15e

Please sign in to comment.