first commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\v0\Usuario\Models;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class UsuarioEmpresaModel {
|
||||
public function __construct(
|
||||
private ?int $usuario_id = null,
|
||||
private ?int $empresa_id = null,
|
||||
) {}
|
||||
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'usuario_id' => $this->getUsuarioId(),
|
||||
'empresa_id' => $this->getEmpresaId(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getUsuarioId(): ?int {
|
||||
return $this->usuario_id;
|
||||
}
|
||||
public function setUsuarioId(?int $usuario_id): void {
|
||||
$this->usuario_id = $usuario_id;
|
||||
}
|
||||
|
||||
public function getEmpresaId(): ?int {
|
||||
return $this->empresa_id;
|
||||
}
|
||||
public function setEmpresaId(?int $empresa_id): void {
|
||||
$this->empresa_id = $empresa_id;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\v0\Usuario\Models;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class UsuarioModel {
|
||||
public function __construct(
|
||||
private ?int $id = null,
|
||||
private ?string $uuid = null,
|
||||
private ?bool $is_active = null,
|
||||
private ?bool $is_root = null,
|
||||
private ?string $nome_completo = null,
|
||||
private ?string $nome_usuario = null,
|
||||
private ?string $email = null,
|
||||
private ?string $senha_hash = null,
|
||||
private ?DateTimeImmutable $created_at = null,
|
||||
private ?DateTimeImmutable $updated_at = null,
|
||||
private ?DateTimeImmutable $deleted_at = null,
|
||||
) {}
|
||||
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'uuid' => $this->getUuid(),
|
||||
'is_active' => $this->getIsActive(),
|
||||
'is_root' => $this->getIsRoot(),
|
||||
'nome_completo' => $this->getNomeCompleto(),
|
||||
'nome_usuario' => $this->getNomeUsuario(),
|
||||
'email' => $this->getEmail(),
|
||||
'senha_hash' => $this->getSenhaHash(),
|
||||
'created_at' => $this->getCreatedAt() ? $this->getCreatedAt()->format('Y-m-d H:i:s') : null,
|
||||
'updated_at' => $this->getUpdatedAt() ? $this->getUpdatedAt()->format('Y-m-d H:i:s') : null,
|
||||
'deleted_at' => $this->getDeletedAt() ? $this->getDeletedAt()->format('Y-m-d H:i:s') : null,
|
||||
];
|
||||
}
|
||||
|
||||
public function setId(?int $id): void {
|
||||
$this->id = $id;
|
||||
}
|
||||
public function getId(): ?int {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setUuid(?string $uuid): void {
|
||||
$this->uuid = $uuid;
|
||||
}
|
||||
public function getUuid(): ?string {
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function setIsActive(?bool $is_active): void {
|
||||
$this->is_active = $is_active;
|
||||
}
|
||||
public function getIsActive(): ?bool {
|
||||
return $this->is_active;
|
||||
}
|
||||
|
||||
public function setIsRoot(?bool $is_root): void {
|
||||
$this->is_root = $is_root;
|
||||
}
|
||||
public function getIsRoot(): ?bool {
|
||||
return $this->is_root;
|
||||
}
|
||||
|
||||
public function setNomeCompleto(?string $nome_completo): void {
|
||||
$this->nome_completo = $nome_completo;
|
||||
}
|
||||
public function getNomeCompleto(): ?string {
|
||||
return $this->nome_completo;
|
||||
}
|
||||
|
||||
public function setNomeUsuario(?string $nome_usuario): void {
|
||||
$this->nome_usuario = $nome_usuario;
|
||||
}
|
||||
public function getNomeUsuario(): ?string {
|
||||
return $this->nome_usuario;
|
||||
}
|
||||
|
||||
public function setEmail(?string $email): void {
|
||||
$this->email = $email;
|
||||
}
|
||||
public function getEmail(): ?string {
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setSenhaHash(?string $senha_hash): void {
|
||||
$this->senha_hash = $senha_hash;
|
||||
}
|
||||
public function getSenhaHash(): ?string {
|
||||
return $this->senha_hash;
|
||||
}
|
||||
|
||||
public function setCreatedAt(?DateTimeImmutable $created_at): void {
|
||||
$this->created_at = $created_at;
|
||||
}
|
||||
public function getCreatedAt(): ?DateTimeImmutable {
|
||||
return $this->created_at;
|
||||
}
|
||||
|
||||
public function setUpdatedAt(?DateTimeImmutable $updated_at): void {
|
||||
$this->updated_at = $updated_at;
|
||||
}
|
||||
public function getUpdatedAt(): ?DateTimeImmutable {
|
||||
return $this->updated_at;
|
||||
}
|
||||
|
||||
public function setDeletedAt(?DateTimeImmutable $deleted_at): void {
|
||||
$this->deleted_at = $deleted_at;
|
||||
}
|
||||
public function getDeletedAt(): ?DateTimeImmutable {
|
||||
return $this->deleted_at;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\v0\Usuario\Models;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class UsuarioSessionModel {
|
||||
public function __construct(
|
||||
private ?int $id = null,
|
||||
private ?string $uuid = null,
|
||||
private ?int $usuario_id = null,
|
||||
private ?string $user_agent = null,
|
||||
private ?string $ip_address = null,
|
||||
private ?string $token_hash = null,
|
||||
private ?DateTimeImmutable $created_at = null,
|
||||
private ?DateTimeImmutable $revoked_at = null,
|
||||
) {}
|
||||
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'id' => $this->getId(),
|
||||
'uuid' => $this->getUuid(),
|
||||
'usuario_id' => $this->getUsuarioId(),
|
||||
'user_agent' => $this->getUserAgent(),
|
||||
'ip_address' => $this->getIpAddress(),
|
||||
'token_hash' => $this->getTokenHash(),
|
||||
'created_at' => $this->getCreatedAt() ? $this->getCreatedAt()->format('Y-m-d H:i:s') : null,
|
||||
'revoked_at' => $this->getRevokedAt() ? $this->getRevokedAt()->format('Y-m-d H:i:s') : null,
|
||||
];
|
||||
}
|
||||
|
||||
public function setId(?int $id): void {
|
||||
$this->id = $id;
|
||||
}
|
||||
public function getId(): ?int {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setUuid(?string $uuid): void {
|
||||
$this->uuid = $uuid;
|
||||
}
|
||||
public function getUuid(): ?string {
|
||||
return $this->uuid;
|
||||
}
|
||||
|
||||
public function setUsuarioId(?int $usuario_id): void {
|
||||
$this->usuario_id = $usuario_id;
|
||||
}
|
||||
public function getUsuarioId(): ?int {
|
||||
return $this->usuario_id;
|
||||
}
|
||||
|
||||
public function setUserAgent(?string $user_agent): void {
|
||||
$this->user_agent = $user_agent;
|
||||
}
|
||||
public function getUserAgent(): ?string {
|
||||
return $this->user_agent;
|
||||
}
|
||||
|
||||
public function setIpAddress(?string $ip_address): void {
|
||||
$this->ip_address = $ip_address;
|
||||
}
|
||||
public function getIpAddress(): ?string {
|
||||
return $this->ip_address;
|
||||
}
|
||||
|
||||
public function setTokenHash(?string $token_hash): void {
|
||||
$this->token_hash = $token_hash;
|
||||
}
|
||||
public function getTokenHash(): ?string {
|
||||
return $this->token_hash;
|
||||
}
|
||||
|
||||
public function setCreatedAt(?DateTimeImmutable $created_at): void {
|
||||
$this->created_at = $created_at;
|
||||
}
|
||||
public function getCreatedAt(): ?DateTimeImmutable {
|
||||
return $this->created_at;
|
||||
}
|
||||
|
||||
public function setRevokedAt(?DateTimeImmutable $revoked_at): void {
|
||||
$this->revoked_at = $revoked_at;
|
||||
}
|
||||
public function getRevokedAt(): ?DateTimeImmutable {
|
||||
return $this->revoked_at;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\v0\Usuario\Repos;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use WorkbloomERP\Services\DBService;
|
||||
use WorkbloomERP\Module\v0\Usuario\Models\UsuarioEmpresaModel;
|
||||
|
||||
class UsuarioEmpresaRepo {
|
||||
protected string $usuarioEmpresaTable = 'shared.usuario_empresa';
|
||||
|
||||
public function __construct(
|
||||
private DBService $db
|
||||
) {}
|
||||
|
||||
public function insert(UsuarioEmpresaModel $usuarioEmpresaModel): bool {
|
||||
$query =
|
||||
"INSERT INTO {$this->usuarioEmpresaTable} (
|
||||
usuario_id,
|
||||
empresa_id
|
||||
) VALUES (
|
||||
:usuario_id,
|
||||
:empresa_id
|
||||
)";
|
||||
|
||||
return $this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
'usuario_id' => $usuarioEmpresaModel->getUsuarioId(),
|
||||
'empresa_id' => $usuarioEmpresaModel->getEmpresaId()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function delete(UsuarioEmpresaModel $usuarioEmpresaModel): bool {
|
||||
$query =
|
||||
"DELETE FROM {$this->usuarioEmpresaTable} WHERE usuario_id = :usuario_id AND empresa_id = :empresa_id";
|
||||
|
||||
return $this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
'usuario_id' => $usuarioEmpresaModel->getUsuarioId(),
|
||||
'empresa_id' => $usuarioEmpresaModel->getEmpresaId()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function findAllByUsuarioId(int $usuario_id): array {
|
||||
$query =
|
||||
"SELECT
|
||||
usuario_id,
|
||||
empresa_id
|
||||
FROM {$this->usuarioEmpresaTable}
|
||||
WHERE usuario_id = :usuario_id";
|
||||
|
||||
return $this->db->fetchAll(
|
||||
sql: $query,
|
||||
params: [
|
||||
'usuario_id' => $usuario_id
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function checkAssociationByUsuarioIdAndEmpresaId(int $usuario_id, int $empresa_id): bool {
|
||||
$query =
|
||||
"SELECT 1 FROM {$this->usuarioEmpresaTable} WHERE usuario_id = :usuario_id AND empresa_id = :empresa_id";
|
||||
|
||||
$result = $this->db->fetchOne(
|
||||
sql: $query,
|
||||
params: [
|
||||
'usuario_id' => $usuario_id,
|
||||
'empresa_id' => $empresa_id
|
||||
]
|
||||
);
|
||||
|
||||
return !empty($result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\v0\Usuario\Repos;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use WorkbloomERP\Services\DBService;
|
||||
use WorkbloomERP\Module\v0\Usuario\Models\UsuarioModel;
|
||||
|
||||
class UsuarioRepo {
|
||||
protected string $usuarioTable = 'usuario';
|
||||
|
||||
public function __construct(
|
||||
private DBService $db
|
||||
) {}
|
||||
|
||||
public function insert(UsuarioModel $usuarioModel): ?UsuarioModel {
|
||||
$query =
|
||||
"INSERT INTO {$this->usuarioTable} (
|
||||
uuid,
|
||||
is_active,
|
||||
is_root,
|
||||
nome_completo,
|
||||
nome_usuario,
|
||||
email,
|
||||
senha_hash,
|
||||
created_at
|
||||
) VALUES (
|
||||
:uuid,
|
||||
:is_active,
|
||||
:is_root,
|
||||
:nome_completo,
|
||||
:nome_usuario,
|
||||
:email,
|
||||
:senha_hash,
|
||||
:created_at
|
||||
)";
|
||||
|
||||
$usuarioModel->setCreatedAt(created_at: new DateTimeImmutable());
|
||||
|
||||
$this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
'uuid' => $usuarioModel->getUuid(),
|
||||
'is_active' => $usuarioModel->getIsActive(),
|
||||
'is_root' => $usuarioModel->getIsRoot(),
|
||||
'nome_completo' => $usuarioModel->getNomeCompleto(),
|
||||
'nome_usuario' => $usuarioModel->getNomeUsuario(),
|
||||
'email' => $usuarioModel->getEmail(),
|
||||
'senha_hash' => $usuarioModel->getSenhaHash(),
|
||||
'created_at' => $usuarioModel->getCreatedAt()->format(format: 'Y-m-d H:i:s')
|
||||
]
|
||||
);
|
||||
|
||||
$usuarioModel->setId(id: $this->db->lastInsertId());
|
||||
return $usuarioModel;
|
||||
}
|
||||
|
||||
public function update(UsuarioModel $usuarioModel): ?UsuarioModel {
|
||||
$query =
|
||||
"UPDATE {$this->usuarioTable} SET
|
||||
is_active = :is_active,
|
||||
is_root = :is_root,
|
||||
nome_completo = :nome_completo,
|
||||
nome_usuario = :nome_usuario,
|
||||
email = :email,
|
||||
senha_hash = :senha_hash,
|
||||
updated_at = :updated_at
|
||||
WHERE id = :id OR uuid = :uuid";
|
||||
|
||||
$usuarioModel->setUpdatedAt(updated_at: new DateTimeImmutable());
|
||||
$this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
':id' => $usuarioModel->getId(),
|
||||
':uuid' => $usuarioModel->getUuid(),
|
||||
':is_active' => $usuarioModel->getIsActive(),
|
||||
':is_root' => $usuarioModel->getIsRoot(),
|
||||
':nome_completo' => $usuarioModel->getNomeCompleto(),
|
||||
':nome_usuario' => $usuarioModel->getNomeUsuario(),
|
||||
':email' => $usuarioModel->getEmail(),
|
||||
':senha_hash' => $usuarioModel->getSenhaHash(),
|
||||
':updated_at' => $usuarioModel->getUpdatedAt()->format(format: 'Y-m-d H:i:s')
|
||||
]
|
||||
);
|
||||
|
||||
return $usuarioModel;
|
||||
}
|
||||
|
||||
public function delete(UsuarioModel $usuarioModel): ?UsuarioModel {
|
||||
$query =
|
||||
"UPDATE {$this->usuarioTable} SET
|
||||
is_active = 0,
|
||||
deleted_at = :deleted_at
|
||||
WHERE id = :id OR uuid = :uuid";
|
||||
|
||||
$usuarioModel->setDeletedAt(deleted_at: new DateTimeImmutable());
|
||||
|
||||
$this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
':id' => $usuarioModel->getId(),
|
||||
':uuid' => $usuarioModel->getUuid(),
|
||||
':deleted_at' => $usuarioModel->getDeletedAt()->format(format: 'Y-m-d H:i:s')
|
||||
]
|
||||
);
|
||||
|
||||
return $usuarioModel;
|
||||
}
|
||||
|
||||
public function findByIdentifier(string $identifier, mixed $value): ?UsuarioModel {
|
||||
$query =
|
||||
"SELECT
|
||||
id,
|
||||
uuid,
|
||||
is_active,
|
||||
is_root,
|
||||
nome_completo,
|
||||
nome_usuario,
|
||||
email,
|
||||
senha_hash,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
FROM {$this->usuarioTable}
|
||||
WHERE {$identifier} = :value
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1";
|
||||
|
||||
$result = $this->db->fetchOne(
|
||||
sql: $query,
|
||||
params: [
|
||||
':value' => $value
|
||||
]
|
||||
);
|
||||
|
||||
return $result ? $this->mapToModel($result) : null;
|
||||
}
|
||||
|
||||
public function findByLogin(string $login): ?UsuarioModel {
|
||||
$query =
|
||||
"SELECT
|
||||
id,
|
||||
uuid,
|
||||
is_active,
|
||||
is_root,
|
||||
nome_completo,
|
||||
nome_usuario,
|
||||
email,
|
||||
senha_hash,
|
||||
created_at,
|
||||
updated_at,
|
||||
deleted_at
|
||||
FROM {$this->usuarioTable}
|
||||
WHERE (nome_usuario = :login OR email = :login)
|
||||
AND deleted_at IS NULL
|
||||
LIMIT 1";
|
||||
|
||||
$result = $this->db->fetchOne(
|
||||
sql: $query,
|
||||
params: [
|
||||
':login' => $login
|
||||
]
|
||||
);
|
||||
|
||||
return $result ? $this->mapToModel($result) : null;
|
||||
}
|
||||
|
||||
private function mapToModel(array $data): UsuarioModel {
|
||||
return new UsuarioModel(
|
||||
id: $data['id'],
|
||||
uuid: $data['uuid'],
|
||||
is_active: $data['is_active'],
|
||||
is_root: $data['is_root'],
|
||||
nome_completo: $data['nome_completo'],
|
||||
nome_usuario: $data['nome_usuario'],
|
||||
email: $data['email'],
|
||||
senha_hash: $data['senha_hash'],
|
||||
created_at: $data['created_at'] ? new DateTimeImmutable($data['created_at']) : null,
|
||||
updated_at: $data['updated_at'] ? new DateTimeImmutable($data['updated_at']) : null,
|
||||
deleted_at: $data['deleted_at'] ? new DateTimeImmutable($data['deleted_at']) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
namespace WorkbloomERP\Module\v0\Usuario\Repos;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use WorkbloomERP\Services\DBService;
|
||||
use WorkbloomERP\Module\v0\Usuario\Models\UsuarioSessionModel;
|
||||
|
||||
class UsuarioSessionRepo {
|
||||
protected string $usuarioSessionTable = 'usuario_session';
|
||||
|
||||
public function __construct(
|
||||
private DBService $db
|
||||
) {}
|
||||
|
||||
public function insert(UsuarioSessionModel $usuarioSessionModel): ?UsuarioSessionModel {
|
||||
$query =
|
||||
"INSERT INTO {$this->usuarioSessionTable} (
|
||||
uuid,
|
||||
usuario_id,
|
||||
user_agent,
|
||||
ip_address,
|
||||
token_hash,
|
||||
created_at
|
||||
) VALUES (
|
||||
:uuid,
|
||||
:usuario_id,
|
||||
:user_agent,
|
||||
:ip_address,
|
||||
:token_hash,
|
||||
:created_at
|
||||
)";
|
||||
|
||||
$usuarioSessionModel->setCreatedAt(new DateTimeImmutable());
|
||||
|
||||
$this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
':uuid' => $usuarioSessionModel->getUuid(),
|
||||
':usuario_id' => $usuarioSessionModel->getUsuarioId(),
|
||||
':user_agent' => $usuarioSessionModel->getUserAgent(),
|
||||
':ip_address' => $usuarioSessionModel->getIpAddress(),
|
||||
':token_hash' => $usuarioSessionModel->getTokenHash(),
|
||||
':created_at' => $usuarioSessionModel->getCreatedAt()->format('Y-m-d H:i:s')
|
||||
]
|
||||
);
|
||||
|
||||
$usuarioSessionModel->setId($this->db->lastInsertId());
|
||||
return $usuarioSessionModel;
|
||||
}
|
||||
|
||||
public function update(UsuarioSessionModel $usuarioSessionModel): bool {
|
||||
$query =
|
||||
"UPDATE {$this->usuarioSessionTable} SET
|
||||
usuario_id = :usuario_id,
|
||||
user_agent = :user_agent,
|
||||
ip_address = :ip_address,
|
||||
token_hash = :token_hash,
|
||||
revoked_at = :revoked_at
|
||||
WHERE id = :id OR uuid = :uuid";
|
||||
|
||||
return $this->db->execute(
|
||||
sql: $query,
|
||||
params: [
|
||||
':id' => $usuarioSessionModel->getId(),
|
||||
':uuid' => $usuarioSessionModel->getUuid(),
|
||||
':usuario_id' => $usuarioSessionModel->getUsuarioId(),
|
||||
':user_agent' => $usuarioSessionModel->getUserAgent(),
|
||||
':ip_address' => $usuarioSessionModel->getIpAddress(),
|
||||
':token_hash' => $usuarioSessionModel->getTokenHash(),
|
||||
':revoked_at' => $usuarioSessionModel->getRevokedAt() ? $usuarioSessionModel->getRevokedAt()->format('Y-m-d H:i:s') : null
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function findByIdentifier(string $identifier, mixed $value): ?UsuarioSessionModel {
|
||||
$query =
|
||||
"SELECT
|
||||
id,
|
||||
uuid,
|
||||
usuario_id,
|
||||
user_agent,
|
||||
ip_address,
|
||||
token_hash,
|
||||
created_at,
|
||||
revoked_at
|
||||
FROM {$this->usuarioSessionTable}
|
||||
WHERE $identifier = :value
|
||||
LIMIT 1";
|
||||
|
||||
$result = $this->db->fetchOne(
|
||||
sql: $query,
|
||||
params: [':value' => $value]
|
||||
);
|
||||
|
||||
return $result ? $this->mapToModel($result) : null;
|
||||
}
|
||||
|
||||
private function mapToModel(array $data) {
|
||||
return new UsuarioSessionModel(
|
||||
id: $data['id'],
|
||||
uuid: $data['uuid'],
|
||||
usuario_id: $data['usuario_id'],
|
||||
user_agent: $data['user_agent'],
|
||||
ip_address: $data['ip_address'],
|
||||
token_hash: $data['token_hash'],
|
||||
created_at: $data['created_at'] ? new DateTimeImmutable($data['created_at']) : null,
|
||||
revoked_at: $data['revoked_at'] ? new DateTimeImmutable($data['revoked_at']) : null,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user