public AvencaFuncionario(string pUsername, string pPassword) { AvencaFuncionario newUser; try { Username = pUsername; Password = pPassword; if ((newUser = GetFromDB(pUsername, pPassword)) != null) { this.Id = newUser.Id; this.Nome = newUser.Nome; this.CPF = newUser.CPF; this.DataNascimento = newUser.DataNascimento; this.Email = newUser.Email; this.Endereco = newUser.Endereco; this.HorarioEntrada = newUser.HorarioEntrada; this.HorarioSaida = newUser.HorarioSaida; this.Setor = newUser.Setor; this.Sexo = newUser.Sexo; this.Telefone = newUser.Telefone; this.IdPermissionGroup = newUser.IdPermissionGroup; } } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } }
private AvencaFuncionario createFuncionario() { try { AvencaFuncionario newUser = new AvencaFuncionario(txtUsername.Text.ToLower(), txtPassword.Text); newUser.Nome = txtNome.Text.ToUpper(); newUser.CPF = txtCPF.Text.ToUpper(); newUser.DataNascimentoStr = dtpDataNascimento.Text.ToUpper(); newUser.Email = txtEmail.Text.ToUpper(); newUser.Endereco = txtEndereco.Text.ToUpper(); newUser.HorarioEntradaStr = dtpHorarioEntrada.Text.ToUpper(); newUser.HorarioSaidaStr = dtpHorarioSaida.Text.ToUpper(); newUser.Setor = txtSetor.Text.ToUpper(); newUser.Sexo = txtSexo.Text.ToUpper(); newUser.Telefone = txtTelefone.Text.ToUpper(); newUser.IdPermissionGroup = cbGrupo.SelectedIndex; if (newUser.AddToDB()) { return(newUser); } else { return(null); } } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } return(null); }
public static int ValidateUser(AvencaFuncionario funcionario) { using (SqlConnection SqlConn = new SqlConnection(AvencaDB.ConnectionString)) { using (System.Data.SqlClient.SqlCommand sqlcomm = new System.Data.SqlClient.SqlCommand("VALIDATE_USER", SqlConn)) { try { SqlConn.Open(); sqlcomm.CommandType = CommandType.StoredProcedure; sqlcomm.Parameters.AddWithValue("@Username", funcionario.Username); sqlcomm.Parameters.AddWithValue("@Password", funcionario.Password); SqlParameter retval = new SqlParameter("@result", SqlDbType.Int); retval.Direction = ParameterDirection.Output; sqlcomm.Parameters.Add(retval); sqlcomm.ExecuteNonQuery(); SqlConn.Close(); return((int)retval.Value); } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } } } return(0); }
private static bool PrintEtiqueta(string printerName) { bool result = false; bool canPrint = false; try { for (int i = 0; i < PrinterSettings.InstalledPrinters.Count; i++) { if (PrinterSettings.InstalledPrinters[i] == printerName) { canPrint = true; } } if (canPrint) { using (PrintDocument printDoc = new PrintDocument()) { printDoc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(printDoc_PrintEtiqueta); printDoc.PrinterSettings.PrinterName = printerName; printDoc.Print(); } } else { throw new Exception("Printer not found: " + printerName); } } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } return(result); }
public static bool SendEmail(string recipients, string subject, string body, string[] attachments = null) { bool result = false; SmtpClient client = new SmtpClient(); if (!configured) { cPort = 587; cHost = "smtp.gmail.com"; cSsl = true; cUsername = "******"; cPassword = "******"; cFrom = "Restaurante Avenca<*****@*****.**>"; } try { client.Port = cPort; client.Host = cHost; client.EnableSsl = cSsl; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new System.Net.NetworkCredential(cUsername, cPassword); MailMessage mm = new MailMessage(cFrom, recipients, subject, body); mm.BodyEncoding = UTF8Encoding.UTF8; mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure; double totalSize = 0; if (attachments != null) { for (int i = 0; i < attachments.Length; i++) { Attachment att = new Attachment(attachments[i], (string)null); mm.Attachments.Add(att); totalSize += att.ContentStream.Length; } int timeout = (int)(totalSize / 100 * 2); client.Timeout = (timeout < 30000) ? 30000 : timeout; } client.Send(mm); for (int i = 0; i < mm.Attachments.Count; i++) { mm.Attachments[i].Dispose(); } result = true; } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } finally { client.Dispose(); } return(result); }
public static AvencaFuncionario FuncionarioGet(string pUsername, string pPassword) { AvencaFuncionario funcionario = null; using (var connection = new SqlConnection(AvencaDB.ConnectionString)) { using (var command = new SqlCommand()) { try { command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = string.Format("SELECT * FROM FUNCIONARIO WHERE Username = @Username AND Password = @Password"); command.Parameters.AddWithValue("@Username", pUsername); command.Parameters.AddWithValue("@Password", AvencaPermission.HashPassword(pPassword)); connection.Open(); var reader = command.ExecuteReader(); if (reader.Read()) { funcionario = new AvencaFuncionario(); funcionario.Username = pUsername; funcionario.Password = pPassword; funcionario.Id = (int)reader["Id"]; funcionario.Nome = reader["Nome"].ToString(); funcionario.CPF = reader["CPF"].ToString(); funcionario.DataNascimentoStr = reader["DataNascimento"].ToString(); funcionario.Email = reader["Email"].ToString(); funcionario.Endereco = reader["Endereco"].ToString(); funcionario.HorarioEntradaStr = reader["HorarioEntrada"].ToString(); funcionario.HorarioSaidaStr = reader["HorarioSaida"].ToString(); funcionario.Setor = reader["Setor"].ToString(); funcionario.Sexo = reader["Sexo"].ToString(); funcionario.Telefone = reader["Telefone"].ToString(); funcionario.IdPermissionGroup = (int)reader["IdPermissionGroup"]; } reader.Close(); } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } finally { connection.Close(); } } } return(funcionario); }
public static bool FuncionarioAdd(AvencaFuncionario newUser) { var res = false; using (var connection = new SqlConnection(AvencaDB.ConnectionString)) { using (var command = new SqlCommand()) { command.Connection = connection; command.CommandType = CommandType.Text; command.CommandText = @"INSERT INTO [dbo].[Funcionario] (Nome,CPF,Telefone,Email,Endereco,DataNascimento,HorarioEntrada, HorarioSaida,Sexo,Setor,Username,Password,IdPermissionGroup) VALUES (@Nome,@CPF,@Telefone,@Email,@Endereco,@DataNascimento,@HorarioEntrada, @HorarioSaida,@Sexo,@Setor,@Username,@Password,@IdPermissionGroup)"; command.Parameters.AddWithValue("@Nome", newUser.Nome); command.Parameters.AddWithValue("@CPF", newUser.CPF); command.Parameters.AddWithValue("@Telefone", newUser.Telefone); command.Parameters.AddWithValue("@Email", newUser.Email); command.Parameters.AddWithValue("@Endereco", newUser.Endereco); command.Parameters.AddWithValue("@DataNascimento", newUser.DataNascimento); command.Parameters.AddWithValue("@HorarioEntrada", newUser.HorarioEntrada); command.Parameters.AddWithValue("@HorarioSaida", newUser.HorarioSaida); command.Parameters.AddWithValue("@Sexo", newUser.Sexo); command.Parameters.AddWithValue("@Setor", newUser.Setor); command.Parameters.AddWithValue("@Username", newUser.Username); command.Parameters.AddWithValue("@Password", newUser.Password); command.Parameters.AddWithValue("@IdPermissionGroup", newUser.IdPermissionGroup); try { connection.Open(); var recordsAffected = command.ExecuteNonQuery(); res = recordsAffected > 0; } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } finally { connection.Close(); } } } return(res); }
public static string HashPassword(string inputString) { string hash = ""; try { byte[] data = System.Text.Encoding.ASCII.GetBytes(inputString); data = new System.Security.Cryptography.SHA256Managed().ComputeHash(data); hash = System.Text.Encoding.ASCII.GetString(data); } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } return(hash); }
public static int RequestLogin(Form frmCaller, bool isLogoff = false) { int userId = 0; while (userId <= 0) { try { using (frmLogin fLogin = new frmLogin()) { if (fLogin.ShowDialog(frmCaller, isLogoff) == DialogResult.OK) { Usuario = fLogin.User; userId = ValidateUser(Usuario); if (userId > 0) { if (!HasPermission(frmCaller)) { userId = 0; } } } else { break; } } } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } if (userId == 0) { MessageBox.Show(frmCaller, "Permissão Negada", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error); } } return(userId); }
public static bool HasPermission(string pUsername, string objectName, bool showMessage = false) { bool granted = false; using (SqlConnection SqlConn = new SqlConnection(AvencaDB.ConnectionString)) { using (System.Data.SqlClient.SqlCommand sqlcomm = new System.Data.SqlClient.SqlCommand("GET_PERMISSION", SqlConn)) { try { SqlConn.Open(); sqlcomm.CommandType = CommandType.StoredProcedure; sqlcomm.Parameters.AddWithValue("@Username", pUsername); sqlcomm.Parameters.AddWithValue("@ObjectName", string.Format("{0}.{1}", Application.ProductName, objectName)); SqlParameter retval = new SqlParameter("@result", SqlDbType.Bit, 1); retval.Direction = ParameterDirection.Output; sqlcomm.Parameters.Add(retval); sqlcomm.ExecuteNonQuery(); SqlConn.Close(); granted = (bool)retval.Value; if (!granted && showMessage) { MessageBox.Show(null, "Permissão Negada", "ERRO", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } } } return(granted); }
public static bool HasPermission(Control callerObject, bool showMessage = false) { bool permissionGranted = false; try { if (((string)callerObject.Tag == "granted") || ((callerObject.Tag != null) && (callerObject.Tag.ToString().Contains(Usuario.IdPermissionGroup.ToString())))) { permissionGranted = true; } else { string objName = (callerObject.Parent != null) ? string.Format("{0}.{1}", callerObject.Parent.Name, callerObject.Name) : callerObject.Name; permissionGranted = (AvencaPermission.Usuario != null) && (HasPermission(AvencaPermission.Usuario.Username, objName, showMessage)); } if (permissionGranted) { callerObject.Tag = "granted"; for (int i = 0; i < callerObject.Controls.Count; i++) { HasPermission(callerObject.Controls[i]); } } else if ((callerObject.Tag != null) && ((string)callerObject.Tag != "")) { callerObject.Enabled = false; } } catch (Exception ex) { AvencaErrorHandler.eventLogError(ex); } return(permissionGranted); }