diff --git a/src/RoleEditor/MainWindow.xaml.cs b/src/RoleEditor/MainWindow.xaml.cs index 2d6fe28..9e720c0 100644 --- a/src/RoleEditor/MainWindow.xaml.cs +++ b/src/RoleEditor/MainWindow.xaml.cs @@ -1,25 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Configuration; -using System.Collections.Specialized; -using System.Linq; + +using System; +using System.Collections.ObjectModel; +using System.Security.Cryptography; using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using System.Collections.ObjectModel; + using brecal.model; using brecal.mysql; -using System.Security.Cryptography; -using System.Security.Policy; -using System.Windows.Markup; namespace RoleEditor { @@ -128,12 +116,44 @@ namespace RoleEditor private void buttonAddRole_Click(object sender, RoutedEventArgs e) { + Role? r = this.listBoxRoles.SelectedItem as Role; + User? u = this.listBoxUser.SelectedItem as User; + if((r != null) && (u != null)) + { + // test if assignment is already present + bool foundMatchingAssignment = false; + foreach(RoleAssignment ra in _assignedRoles) + { + if((ra.UserId == u.Id) && (ra.RoleId == r.Id)) + { + foundMatchingAssignment = true; + break; + } + } + if(!foundMatchingAssignment) + { + RoleAssignment ra = new RoleAssignment(); + ra.UserId = (int) u.Id; + ra.RoleId = (int) r.Id; + ra.AssignedRole = r; + ra.AssignedUser = u; + ra.Save(_dbManager); + _assignedRoles.Add(ra); + } + } } private void buttonRemoveRole_Click(object sender, RoutedEventArgs e) { - + // remove role from user + RoleAssignment? ra = this.listBoxUserRoles.SelectedItem as RoleAssignment; + if(ra != null) + { + ra.Delete(_dbManager); + if(_assignedRoles.Contains(ra)) + _assignedRoles.Remove(ra); + } } private void buttonAddSecurable_Click(object sender, RoutedEventArgs e) @@ -143,7 +163,13 @@ namespace RoleEditor private void buttonRemoveSecurable_Click(object sender, RoutedEventArgs e) { - + SecurableAssignment? sa = this.listBoxRoleSecurables.SelectedItem as SecurableAssignment; + if(sa != null) + { + sa.Delete(_dbManager); + if (_assignedSecurables.Contains(sa)) + _assignedSecurables.Remove(sa); + } } private void buttonSaveSecurable_Click(object sender, RoutedEventArgs e) @@ -195,11 +221,26 @@ namespace RoleEditor _users.Add(u); } - private void listBoxRoles_SelectionChanged(object sender, SelectionChangedEventArgs e) + private async void listBoxRoles_SelectionChanged(object sender, SelectionChangedEventArgs e) { Role? r = this.listBoxRoles.SelectedItem as Role; this.textBoxRoleName.Text = (r != null) ? r.Name : string.Empty; this.textBoxRoleDescription.Text = (r != null) ? r.Description : string.Empty; + + _assignedSecurables.Clear(); + if (r != null) + { + // load assigned securables + foreach (SecurableAssignment sa in await SecurableAssignment.LoadForRole(r, _dbManager)) + { + foreach (Securable s in this._securables) + { + if (sa.SecurableId == s.Id) + sa.AssignedSecurable = s; + _assignedSecurables.Add(sa); + } + } + } } private async void listBoxUser_SelectionChanged(object sender, SelectionChangedEventArgs e) @@ -213,15 +254,19 @@ namespace RoleEditor this.textBoxUserModified.Text = (u != null) ? u.Modified.ToString() : string.Empty; this.textBoxUserPassword.Text = string.Empty; - // load roles assigned to user _assignedRoles.Clear(); - foreach (RoleAssignment ra in await RoleAssignment.LoadForUser(u, _dbManager)) + + if (u != null) { - foreach(Role r in this._roles) + // load roles assigned to user + foreach (RoleAssignment ra in await RoleAssignment.LoadForUser(u, _dbManager)) { - if (ra.RoleId == r.Id) - ra.AssignedRole = r; - _assignedRoles.Add(ra); + foreach (Role r in this._roles) + { + if (ra.RoleId == r.Id) + ra.AssignedRole = r; + _assignedRoles.Add(ra); + } } } } diff --git a/src/brecal.model/IDBManager.cs b/src/brecal.model/IDBManager.cs index bcc2814..1fad343 100644 --- a/src/brecal.model/IDBManager.cs +++ b/src/brecal.model/IDBManager.cs @@ -12,9 +12,9 @@ namespace brecal.model delegate List LoadFunc(T entity); - delegate void QueryFunc(IDbCommand cmd, params object[] args); + delegate void QueryFunc(IDbCommand cmd, params object?[] args); - Task> Load(QueryFunc prepareAction, LoadFunc loadAction, params object[] args); + Task> Load(QueryFunc prepareAction, LoadFunc loadAction, params object?[] args); Task ExecuteScalar(Action prepareAction); diff --git a/src/brecal.model/Participant.cs b/src/brecal.model/Participant.cs index fa5738a..e2a1d88 100644 --- a/src/brecal.model/Participant.cs +++ b/src/brecal.model/Participant.cs @@ -53,7 +53,7 @@ namespace brecal.model return result; } - public static void SetLoadQuery(IDbCommand cmd, params object[] list) + public static void SetLoadQuery(IDbCommand cmd, params object?[] list) { cmd.CommandText = "SELECT id, name, street, postal_code, city, flags, created, modified FROM participant"; } diff --git a/src/brecal.model/Role.cs b/src/brecal.model/Role.cs index b9c67d2..c85802b 100644 --- a/src/brecal.model/Role.cs +++ b/src/brecal.model/Role.cs @@ -29,7 +29,7 @@ namespace brecal.model return result; } - public static void SetLoadQuery(IDbCommand cmd, params object[] list) + public static void SetLoadQuery(IDbCommand cmd, params object?[] list) { cmd.CommandText = "SELECT id, name, description, created, modified FROM role"; } diff --git a/src/brecal.model/RoleAssignment.cs b/src/brecal.model/RoleAssignment.cs index 10b2f6c..95498e1 100644 --- a/src/brecal.model/RoleAssignment.cs +++ b/src/brecal.model/RoleAssignment.cs @@ -37,14 +37,15 @@ namespace brecal.model return result; } - public static void SetLoadQuery(IDbCommand cmd, params object[] args) + public static void SetLoadQuery(IDbCommand cmd, params object?[] args) { cmd.CommandText = "SELECT id, user_id, role_id FROM user_role_map WHERE user_id = @UID"; if (args.Length != 1 || !(args[0] is User)) throw new ArgumentException("loader needs single user as argument"); IDataParameter uid = cmd.CreateParameter(); uid.ParameterName = "UID"; - uid.Value = ((User)args[0]).Id; + if (args[0] is User u) + uid.Value = u.Id; cmd.Parameters.Add(uid); } @@ -73,7 +74,17 @@ namespace brecal.model public override void SetCreate(IDbCommand cmd) { - throw new NotImplementedException(); + cmd.CommandText = "INSERT INTO user_role_map (user_id, role_id) VALUES (@USERID, @ROLEID)"; + + IDbDataParameter userid = cmd.CreateParameter(); + userid.ParameterName = "USERID"; + userid.Value = this.UserId; + cmd.Parameters.Add(userid); + + IDbDataParameter roleid = cmd.CreateParameter(); + roleid.ParameterName = "ROLEID"; + roleid.Value = this.RoleId; + cmd.Parameters.Add(roleid); } public override void SetDelete(IDbCommand cmd) diff --git a/src/brecal.model/Securable.cs b/src/brecal.model/Securable.cs index 77076a8..0235ea0 100644 --- a/src/brecal.model/Securable.cs +++ b/src/brecal.model/Securable.cs @@ -27,7 +27,7 @@ namespace brecal.model return result; } - public static void SetLoadQuery(IDbCommand cmd, params object[] list) + public static void SetLoadQuery(IDbCommand cmd, params object?[] list) { cmd.CommandText = "SELECT id, name, created, modified FROM securable"; } diff --git a/src/brecal.model/SecurableAssignment.cs b/src/brecal.model/SecurableAssignment.cs index 5ae9eae..fb7db00 100644 --- a/src/brecal.model/SecurableAssignment.cs +++ b/src/brecal.model/SecurableAssignment.cs @@ -15,8 +15,57 @@ namespace brecal.model public Securable? AssignedSecurable { get; set; } + public int? RoleId { get; set; } + + public int? SecurableId { get; set; } + #endregion + #region public static methods + + public static async Task> LoadForRole(Role? r, IDBManager manager) + { + List loadResultList = await manager.Load(SetLoadQuery, LoadElems, args: r); + List result = new(); + foreach (SecurableAssignment sa in loadResultList.Cast()) + { + sa.AssignedRole = r; + result.Add(sa); + } + + return result; + } + + public static void SetLoadQuery(IDbCommand cmd, params object?[] args) + { + cmd.CommandText = "SELECT id, role_id, securable_id FROM role_securable_map WHERE role_id = @RID"; + if (args.Length != 1 || !(args[0] is Role)) + throw new ArgumentException("loader needs single role as argument"); + IDataParameter rid = cmd.CreateParameter(); + rid.ParameterName = "RID"; + if (args[0] is Role r) + rid.Value = r.Id; + cmd.Parameters.Add(rid); + } + + public static List LoadElems(IDataReader reader) + { + List result = new List(); + while (reader.Read()) + { + SecurableAssignment sa = new(); + sa.Id = (uint)reader.GetInt32(0); + if (!reader.IsDBNull(1)) sa.RoleId = reader.GetInt32(1); + if (!reader.IsDBNull(2)) sa.SecurableId = reader.GetInt32(2); + result.Add(sa); + } + return result; + } + + #endregion + + #region public overrides + public override void SetUpdate(IDbCommand cmd) { throw new NotImplementedException(); @@ -29,8 +78,27 @@ namespace brecal.model public override void SetDelete(IDbCommand cmd) { - throw new NotImplementedException(); + cmd.CommandText = "DELETE FROM role_securable_map WHERE id = @ID"; + + IDataParameter idParam = cmd.CreateParameter(); + idParam.ParameterName = "ID"; + idParam.Value = this.Id; + cmd.Parameters.Add(idParam); } + public override string ToString() + { + if (this.AssignedSecurable == null) + { + return $"{Id}: "; + } + else + { + return $"{AssignedSecurable.Name}:{Id}"; + } + } + + #endregion + } } diff --git a/src/brecal.model/User.cs b/src/brecal.model/User.cs index 7386457..2c4cf65 100644 --- a/src/brecal.model/User.cs +++ b/src/brecal.model/User.cs @@ -31,14 +31,15 @@ namespace brecal.model return result; } - public static void SetLoadQuery(IDbCommand cmd, params object[] args) + public static void SetLoadQuery(IDbCommand cmd, params object?[] args) { cmd.CommandText = "SELECT id, first_name, last_name, user_name, api_key, created, modified FROM user WHERE participant_id = @PID"; if (args.Length != 1 || !(args[0] is Participant)) throw new ArgumentException("loader needs single partipant as argument"); IDataParameter pid = cmd.CreateParameter(); pid.ParameterName = "PID"; - pid.Value = ((Participant)args[0]).Id; + if (args[0] is Participant p) + pid.Value = p.Id; cmd.Parameters.Add(pid); } diff --git a/src/brecal.mysql/DBManager.cs b/src/brecal.mysql/DBManager.cs index 1d7ee6f..054caa9 100644 --- a/src/brecal.mysql/DBManager.cs +++ b/src/brecal.mysql/DBManager.cs @@ -8,13 +8,14 @@ namespace brecal.mysql { public class DBManager : IDBManager { - // TODO: remove this and use certificates instead - private static readonly string _connectionString = "Server=lager;User ID=ds;Password=HalloWach23;Database=bremen_calling"; + // TODO: remove this and use external configuration + private static readonly string _connectionString = "Server=localhost;User ID=ds;Password=HalloWach23;Database=bremen_calling"; - public async Task> Load(QueryFunc prepareAction, LoadFunc loadAction, params object[] args) + public async Task> Load(QueryFunc prepareAction, LoadFunc loadAction, params object?[] args) { await using MySqlConnection connection = new MySqlConnection(_connectionString); await connection.OpenAsync(); + using MySqlCommand cmd = new(); cmd.Connection = connection; prepareAction(cmd, args); diff --git a/src/server/BreCal/local_db.py b/src/server/BreCal/local_db.py index c2c2cf9..ed115a8 100644 --- a/src/server/BreCal/local_db.py +++ b/src/server/BreCal/local_db.py @@ -8,7 +8,7 @@ def initPool(): try: global connection_pool connection_pool = mysql.connector.connect( - host="lager", + host="lager", # TODO: move these settings outside the code! port=3306, user="ds", password="HalloWach23",