feat: implement JWT-based authentication system with repository, service, and middleware layers
This commit is contained in:
@@ -0,0 +1,151 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\Auth\Repository;
|
||||
|
||||
use KrothiumAPI\Database\DBManager;
|
||||
use KrothiumAPI\Database\Contracts\DriverInterface;
|
||||
|
||||
class AuthRepository {
|
||||
private DriverInterface $db;
|
||||
|
||||
public function __construct(?DriverInterface $db = null) {
|
||||
$this->db = $db ?? DBManager::getConnection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Localiza usuário ativo e não deletado por e-mail ou nome de usuário.
|
||||
* Retorna apenas colunas estritamente necessárias. NUNCA SELECT *.
|
||||
*
|
||||
* @param string $login E-mail ou nome_usuario
|
||||
* @return array|null
|
||||
*/
|
||||
public function findByLogin(string $login): ?array {
|
||||
$sql = "
|
||||
SELECT
|
||||
u.id,
|
||||
u.uuid,
|
||||
u.nome_completo,
|
||||
u.cpf,
|
||||
u.email,
|
||||
u.nome_usuario,
|
||||
u.senha_hash,
|
||||
u.ativo
|
||||
FROM usuario u
|
||||
WHERE (u.email = :login OR u.nome_usuario = :login)
|
||||
AND u.deleted_at IS NULL
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
return $this->db->fetchOne($sql, ['login' => $login]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Localiza dados cadastrais do usuário por UUID (sem expor id ou hash de senha).
|
||||
*
|
||||
* @param string $uuid
|
||||
* @return array|null
|
||||
*/
|
||||
public function findByUuid(string $uuid): ?array {
|
||||
$sql = "
|
||||
SELECT
|
||||
u.uuid,
|
||||
u.nome_completo,
|
||||
u.cpf,
|
||||
u.email,
|
||||
u.nome_usuario,
|
||||
u.ativo,
|
||||
u.created_at
|
||||
FROM usuario u
|
||||
WHERE u.uuid = :uuid
|
||||
AND u.deleted_at IS NULL
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
return $this->db->fetchOne($sql, ['uuid' => $uuid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Localiza dados cadastrais do usuário por ID (compatibilidade interna).
|
||||
*
|
||||
* @param int $id
|
||||
* @return array|null
|
||||
*/
|
||||
public function findById(int $id): ?array {
|
||||
$sql = "
|
||||
SELECT
|
||||
u.uuid,
|
||||
u.nome_completo,
|
||||
u.cpf,
|
||||
u.email,
|
||||
u.nome_usuario,
|
||||
u.ativo,
|
||||
u.created_at
|
||||
FROM usuario u
|
||||
WHERE u.id = :id
|
||||
AND u.deleted_at IS NULL
|
||||
LIMIT 1
|
||||
";
|
||||
|
||||
return $this->db->fetchOne($sql, ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lista empresas vinculadas ao usuário por UUID (retornando apenas e.uuid como identificador).
|
||||
* NUNCA SELECT *.
|
||||
*
|
||||
* @param string $usuarioUuid
|
||||
* @return array
|
||||
*/
|
||||
public function findEmpresasByUsuarioUuid(string $usuarioUuid): array {
|
||||
$sql = "
|
||||
SELECT
|
||||
e.uuid,
|
||||
e.nome_empresarial,
|
||||
e.nome_fantasia,
|
||||
e.cnpj,
|
||||
e.tipo_estabelecimento,
|
||||
e.status,
|
||||
ue.perfil,
|
||||
ue.acesso_todas_filiais
|
||||
FROM usuario_empresa ue
|
||||
INNER JOIN empresa e
|
||||
ON e.id = ue.empresa_id
|
||||
INNER JOIN usuario u
|
||||
ON u.id = ue.usuario_id
|
||||
WHERE u.uuid = :usuario_uuid
|
||||
AND e.deleted_at IS NULL
|
||||
AND u.deleted_at IS NULL
|
||||
ORDER BY e.tipo_estabelecimento ASC, e.nome_empresarial ASC
|
||||
";
|
||||
|
||||
return $this->db->fetchAll($sql, ['usuario_uuid' => $usuarioUuid]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lista empresas vinculadas ao usuário por ID interno.
|
||||
* Retorna exclusivamente e.uuid como identificador da empresa.
|
||||
*
|
||||
* @param int $usuarioId
|
||||
* @return array
|
||||
*/
|
||||
public function findEmpresasByUsuarioId(int $usuarioId): array {
|
||||
$sql = "
|
||||
SELECT
|
||||
e.uuid,
|
||||
e.nome_empresarial,
|
||||
e.nome_fantasia,
|
||||
e.cnpj,
|
||||
e.tipo_estabelecimento,
|
||||
e.status,
|
||||
ue.perfil,
|
||||
ue.acesso_todas_filiais
|
||||
FROM usuario_empresa ue
|
||||
INNER JOIN empresa e
|
||||
ON e.id = ue.empresa_id
|
||||
WHERE ue.usuario_id = :usuario_id
|
||||
AND e.deleted_at IS NULL
|
||||
ORDER BY e.tipo_estabelecimento ASC, e.nome_empresarial ASC
|
||||
";
|
||||
|
||||
return $this->db->fetchAll($sql, ['usuario_id' => $usuarioId]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user