using BrightIdeasSoftware; using Deckungsbeitrag; using Npgsql; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace DatenDB { public enum BenutzerRolle { Verwaltung = 0, Fahrer = 1, Admin = 2, Waschstrasse = 3, Master = 4, Expedit = 5, Frottee = 6, Flach = 7 } public class Benutzer { // 0 1 2 3 4 5 6 7 8 public const string COLUMNS = "benutzer_id, rolle, passwort, vorname, nachname, schein, gueltig_bis, ist_aktiv, benutzer_name"; private const string TABLE = "kundenverwaltung.benutzer"; public Benutzer() { } public static List GetList() { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Benutzer(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetFahrerList() { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where rolle = 'Fahrer' and ist_aktiv is true order by benutzer_id asc"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Benutzer(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static List GetArbeiterList() { DatenbankConnection.GetConnection().Open(); List resultList = new List(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where ist_aktiv is true and rolle = 'Fahrer' or rolle = 'Verwaltung' or rolle = 'Expedit' order by benutzer_id asc"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) resultList.Add(new Benutzer(reader)); reader.Close(); DatenbankConnection.GetConnection().Close(); return resultList; } public static Benutzer GetBenutzer(string benutzerName, int? benID) { Benutzer benutzer = new Benutzer(); try { DatenbankConnection.GetConnection().Open(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (!string.IsNullOrEmpty(benutzerName)) command.CommandText = $"select {COLUMNS} from {TABLE} where benutzer_name = '{benutzerName}'"; if (benID != null) command.CommandText = $"select {COLUMNS} from {TABLE} where benutzer_id = {benID}"; NpgsqlDataReader reader = command.ExecuteReader(); while (reader.Read()) benutzer = new Benutzer(reader); reader.Close(); DatenbankConnection.GetConnection().Close(); return benutzer; } catch (Exception ex) { Program.AddFehler(null, ex.Message, ex.StackTrace, null); throw; } } #region Properties /// /// Benutzer ID /// [OLVIgnore] public int? BenutzerID { get; set; } /// /// Rolle des Benutzers /// [OLVColumn("Rolle", DisplayIndex = 4)] public BenutzerRolle Rolle { get; set; } = BenutzerRolle.Waschstrasse; /// /// Passwort wird von OLV ignoriert /// [OLVIgnore] public string Passwort { get; set; } /// /// Vorname & Nachname des Benutzers /// [OLVColumn("Vorname", DisplayIndex = 1)] public string Vorname { get; set; } [OLVColumn("Name", DisplayIndex = 2)] public string Nachname { get; set; } /// /// Führerscheinnummer und Ablaufdatum /// [OLVColumn("FührerscheinNr", DisplayIndex = 6, IsVisible = false)] public int? Schein { get; set; } [OLVColumn("Ablaufdatum", DisplayIndex = 7, IsVisible = false)] public DateTime? GueltigBis { get; set; } /// /// Status des Benutzers. /// [OLVColumn("Aktiv", DisplayIndex = 5, CheckBoxes = true)] public bool Aktiv { get; set; } = true; /// /// Benutzername wird automatisch generiert (Vorname + Nachname in Kleinbuchstaben ohne Leerzeichen) /// [OLVColumn("Benutzername", DisplayIndex = 3)] public string BenutzerName { get; set; } #endregion public static Benutzer Get(string benutzerName, string passwort) { try { DatenbankConnection.GetConnection().Open(); } catch (NpgsqlException) { throw new LoginException("Datenbank ist nicht verbunden", -4); } NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); command.CommandText = $"select {COLUMNS} from {TABLE} where lower(benutzer_name) = :ben"; command.Parameters.AddWithValue(":ben", benutzerName.ToLower()); NpgsqlDataReader reader = command.ExecuteReader(); Benutzer person = null; if (reader.Read()) person = new Benutzer(reader); reader.Close(); DatenbankConnection.GetConnection().Close(); if (person == null) throw new LoginException("Benutzer nicht gefunden!", -1); else if (string.IsNullOrEmpty(person.Passwort)) throw new LoginException("Benutzer gefunden, Passwort bestätigen", -3); if (person.Passwort != passwort) throw new LoginException("Passwort ist falsch!", -2); return person; } //FEHLERMELDUNG DYNAMISCH GENERIEREN... public Benutzer(NpgsqlDataReader reader) { this.BenutzerID = reader.GetInt32(0); this.Rolle = reader.IsDBNull(1) ? BenutzerRolle.Verwaltung : (BenutzerRolle)Enum.Parse(typeof(BenutzerRolle), reader.GetString(1)); this.Passwort = reader.IsDBNull(2) ? string.Empty : reader.GetString(2); this.Vorname = reader.IsDBNull(3) ? string.Empty : reader.GetString(3); this.Nachname = reader.IsDBNull(4) ? string.Empty : reader.GetString(4); this.Schein = reader.IsDBNull(5) ? null : (int?)reader.GetInt32(5); this.GueltigBis = reader.IsDBNull(6) ? null : (DateTime?)reader.GetDateTime(6); this.Aktiv = reader.IsDBNull(7) ? true : reader.GetBoolean(7); this.BenutzerName = reader.IsDBNull(8) ? string.Empty : reader.GetString(8); } public int Save() { DatenbankConnection.GetConnection().Open(); NpgsqlCommand command = new NpgsqlCommand(); command.Connection = DatenbankConnection.GetConnection(); if (this.BenutzerID.HasValue & this.BenutzerID != 0) { command.CommandText = $"update {TABLE} set rolle = :p1, passwort = :p2, vorname = :p3, nachname = :p4, schein = :p5, gueltig_bis = :p6, ist_aktiv = :p7, benutzer_name = :p8 where benutzer_id = :p0"; } else { command.CommandText = "select nextval('kundenverwaltung.benutzer_seq')"; this.BenutzerID = (int)(long)command.ExecuteScalar(); command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4, :p5, :p6, :p7, :p8)"; } command.Parameters.AddWithValue("p0", this.BenutzerID); command.Parameters.AddWithValue("p1", this.Rolle.ToString()); command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(this.Passwort) ? (object)DBNull.Value : this.Passwort); command.Parameters.AddWithValue("p3", string.IsNullOrEmpty(this.Vorname) ? (object)DBNull.Value : this.Vorname); command.Parameters.AddWithValue("p4", string.IsNullOrEmpty(this.Nachname) ? (object)DBNull.Value : this.Nachname); command.Parameters.AddWithValue("p5", this.Schein.HasValue ? this.Schein.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p6", this.Rolle == BenutzerRolle.Fahrer ? this.GueltigBis.Value : (object)DBNull.Value); command.Parameters.AddWithValue("p7", this.Aktiv); command.Parameters.AddWithValue("p8", string.IsNullOrEmpty(this.BenutzerName) ? (object)DBNull.Value : this.BenutzerName); int result = command.ExecuteNonQuery(); DatenbankConnection.GetConnection().Close(); return result; } } }