role / securable mappings

This commit is contained in:
Daniel Schick 2023-04-21 11:57:04 +02:00
parent a4737b8b1f
commit fd0497fee4
10 changed files with 167 additions and 41 deletions

View File

@ -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);
}
}
}
}

View File

@ -12,9 +12,9 @@ namespace brecal.model
delegate List<DbEntity> LoadFunc<T>(T entity);
delegate void QueryFunc(IDbCommand cmd, params object[] args);
delegate void QueryFunc(IDbCommand cmd, params object?[] args);
Task<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object[] args);
Task<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object?[] args);
Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction);

View File

@ -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";
}

View File

@ -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";
}

View File

@ -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)

View File

@ -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";
}

View File

@ -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<List<SecurableAssignment>> LoadForRole(Role? r, IDBManager manager)
{
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems, args: r);
List<SecurableAssignment> result = new();
foreach (SecurableAssignment sa in loadResultList.Cast<SecurableAssignment>())
{
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<DbEntity> LoadElems(IDataReader reader)
{
List<DbEntity> result = new List<DbEntity>();
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}: <defunct securable>";
}
else
{
return $"{AssignedSecurable.Name}:{Id}";
}
}
#endregion
}
}

View File

@ -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);
}

View File

@ -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<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object[] args)
public async Task<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object?[] args)
{
await using MySqlConnection connection = new MySqlConnection(_connectionString);
await connection.OpenAsync();
using MySqlCommand cmd = new();
cmd.Connection = connection;
prepareAction(cmd, args);

View File

@ -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",