167 lines
6.5 KiB
C#
167 lines
6.5 KiB
C#
using CPM;
|
|
using MLL;
|
|
using System;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace UI.Dashboards.Cadastro
|
|
{
|
|
public class UsuariosPanel : FormularioModelo
|
|
{
|
|
// Controles marcados como anuláveis para sanar avisos de compilação modernos (.NET 8 NRT)
|
|
private LV_TEXTBOX1? txtId;
|
|
private LV_TEXTBOX1? txtEmpresaId;
|
|
private LV_TEXTBOX1? txtNome;
|
|
private LV_TEXTBOX1? txtEmail;
|
|
private LV_TEXTBOX1? txtUsuario;
|
|
private LV_TEXTBOX1? txtSenhaHash;
|
|
private LV_TEXTBOX1? txtUltimoLogin;
|
|
private LV_TEXTBOX1? txtCriadoEm;
|
|
private LV_TEXTBOX1? txtAtualizadoEm;
|
|
private CheckBox? chkAtivo;
|
|
|
|
public UsuariosPanel()
|
|
{
|
|
this.Titulo = "GESTÃO E CONTROLE DE USUÁRIOS";
|
|
MontarInterfaceCompleta();
|
|
}
|
|
|
|
private void MontarInterfaceCompleta()
|
|
{
|
|
// --- SEÇÃO 1: IDENTIFICAÇÃO DO COLABORADOR ---
|
|
content.Controls.Add(CreateSectionHeader("Identificação do Operador", 20));
|
|
|
|
txtId = AddInput(content, "ID USUÁRIO", 20, 55, 100, 28, true);
|
|
txtEmpresaId = AddInput(content, "ID EMPRESA VÍNCULO", 135, 55, 130, 28);
|
|
txtNome = AddInput(content, "NOME COMPLETO", 280, 55, 370, 28);
|
|
|
|
chkAtivo = new CheckBox
|
|
{
|
|
Text = "Usuário Ativo",
|
|
Location = new Point(665, 58),
|
|
Size = new Size(120, 24),
|
|
Font = new Font("Segoe UI", 10f, FontStyle.Bold),
|
|
ForeColor = Color.FromArgb(45, 45, 45),
|
|
Checked = true
|
|
};
|
|
content.Controls.Add(chkAtivo);
|
|
|
|
txtEmail = AddInput(content, "E-MAIL DE CONTATO", 20, 115, 630, 28);
|
|
|
|
// --- SEÇÃO 2: CREDENCIAIS E AUTENTICAÇÃO ---
|
|
content.Controls.Add(CreateSectionHeader("Credenciais de Autenticação e Segurança", 185));
|
|
|
|
txtUsuario = AddInput(content, "NOME DE USUÁRIO / LOGIN", 20, 220, 250, 28);
|
|
|
|
// Campo de senha configurado nativamente para ocultar a digitação
|
|
txtSenhaHash = AddInput(content, "SENHA DE ACESSO", 285, 220, 250, 28);
|
|
if (txtSenhaHash != null)
|
|
{
|
|
txtSenhaHash.UseSystemPasswordChar = true;
|
|
}
|
|
|
|
// --- SEÇÃO 3: AUDITORIA E LOGS ---
|
|
content.Controls.Add(CreateSectionHeader("Histórico e Auditoria de Acessos", 290));
|
|
|
|
txtUltimoLogin = AddInput(content, "ÚLTIMO LOGIN", 20, 325, 200, 28, true);
|
|
txtCriadoEm = AddInput(content, "DATA DE CRIAÇÃO", 235, 325, 180, 28, true);
|
|
txtAtualizadoEm = AddInput(content, "ÚLTIMA ATUALIZAÇÃO", 430, 325, 180, 28, true);
|
|
}
|
|
|
|
// --- MÉTODOS OBRIGATÓRIOS DO FORMULÁRIO MODELO ---
|
|
|
|
protected override void OnNovo()
|
|
{
|
|
// Limpa de forma iterativa todas as caixas de texto editáveis do painel
|
|
foreach (Control c in content.Controls)
|
|
{
|
|
if (c is LV_TEXTBOX1 txt && !txt.ReadOnly)
|
|
{
|
|
txt.Text = string.Empty;
|
|
}
|
|
}
|
|
|
|
if (txtId != null) txtId.Text = "0";
|
|
if (txtEmpresaId != null) txtEmpresaId.Text = "1";
|
|
if (txtUltimoLogin != null) txtUltimoLogin.Text = "Nunca Conectado";
|
|
|
|
// Tratamento como string para exibição visual imediata das datas padrões
|
|
if (txtCriadoEm != null) txtCriadoEm.Text = DateTime.Now.ToString("g");
|
|
if (txtAtualizadoEm != null) txtAtualizadoEm.Text = DateTime.Now.ToString("g");
|
|
|
|
if (chkAtivo != null) chkAtivo.Checked = true;
|
|
|
|
txtNome?.Focus();
|
|
}
|
|
|
|
protected override void OnSalvar()
|
|
{
|
|
if (string.IsNullOrWhiteSpace(txtNome?.Text))
|
|
{
|
|
MessageBox.Show("O nome completo do operador é obrigatório.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtNome?.Focus();
|
|
return;
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(txtUsuario?.Text))
|
|
{
|
|
MessageBox.Show("O login do usuário é obrigatório para autenticação.", "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Warning);
|
|
txtUsuario?.Focus();
|
|
return;
|
|
}
|
|
|
|
int idUsuario = string.IsNullOrEmpty(txtId?.Text) ? 0 : Convert.ToInt32(txtId.Text);
|
|
int idEmpresa = string.IsNullOrEmpty(txtEmpresaId?.Text) ? 0 : Convert.ToInt32(txtEmpresaId.Text);
|
|
|
|
// Parsing resiliente de datas anuláveis (DateTime?) baseadas na interface
|
|
DateTime? ultimoLogin = null;
|
|
if (!string.IsNullOrEmpty(txtUltimoLogin?.Text) && DateTime.TryParse(txtUltimoLogin.Text, out DateTime ulParsed))
|
|
ultimoLogin = ulParsed;
|
|
|
|
DateTime? criadoEm = null;
|
|
if (!string.IsNullOrEmpty(txtCriadoEm?.Text) && DateTime.TryParse(txtCriadoEm.Text, out DateTime cParsed))
|
|
criadoEm = cParsed;
|
|
|
|
// Mapeamento absoluto das 10 propriedades da classe ModeloUsuarios
|
|
var usuario = new ModeloUsuarios
|
|
{
|
|
Id = idUsuario,
|
|
EmpresaId = idEmpresa,
|
|
Nome = txtNome.Text,
|
|
Email = txtEmail?.Text ?? string.Empty,
|
|
Usuario = txtUsuario.Text,
|
|
SenhaHash = txtSenhaHash?.Text ?? string.Empty, // Lembre-se de hashear (ex: SHA256/BCrypt) na sua BLL antes do banco
|
|
Ativo = chkAtivo?.Checked ?? false,
|
|
UltimoLogin = ultimoLogin,
|
|
CriadoEm = criadoEm,
|
|
AtualizadoEm = DateTime.Now // Carimba o momento exato da persistência
|
|
};
|
|
|
|
// Objeto completo pronto para ser passado à sua camada de regras de negócio (BLL)
|
|
MessageBox.Show($"Usuário '{usuario.Usuario}' salvo com sucesso!", "LevelOS", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
}
|
|
|
|
protected override void OnAlterar()
|
|
{
|
|
txtNome?.Focus();
|
|
}
|
|
|
|
protected override void OnExcluir()
|
|
{
|
|
if (MessageBox.Show("Confirma a exclusão deste operador? O acesso ao sistema será revogado imediatamente.", "LevelOS", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
|
|
{
|
|
OnNovo();
|
|
}
|
|
}
|
|
|
|
protected override void OnLocalizar()
|
|
{
|
|
// Aciona a Grid / Busca Geral de usuários cadastrados
|
|
}
|
|
|
|
protected override void OnCancelar()
|
|
{
|
|
OnNovo();
|
|
}
|
|
}
|
|
} |