role/securable editor more or less functional

This commit is contained in:
Daniel Schick 2023-04-22 19:32:54 +02:00
parent fd0497fee4
commit 7ea16692cc
5 changed files with 116 additions and 43 deletions

View File

@ -216,7 +216,7 @@ ENGINE=InnoDB
; ;
CREATE TABLE `role_securable_map` ( CREATE TABLE `role_securable_map` (
`id` INT(10) UNSIGNED NOT NULL, `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`role_id` INT(10) UNSIGNED NOT NULL, `role_id` INT(10) UNSIGNED NOT NULL,
`securable_id` INT(10) UNSIGNED NOT NULL, `securable_id` INT(10) UNSIGNED NOT NULL,
`created` DATETIME NULL DEFAULT current_timestamp(), `created` DATETIME NULL DEFAULT current_timestamp(),

View File

@ -72,7 +72,7 @@ namespace RoleEditor
#region button callbacks #region button callbacks
private void buttonParticipantSave_Click(object sender, RoutedEventArgs e) private async void buttonParticipantSave_Click(object sender, RoutedEventArgs e)
{ {
Participant? p = this.listBoxParticipant.SelectedItem as Participant; Participant? p = this.listBoxParticipant.SelectedItem as Participant;
if (p != null) if (p != null)
@ -81,14 +81,14 @@ namespace RoleEditor
p.Street = this.textBoxParticipantStreet.Text.Trim(); p.Street = this.textBoxParticipantStreet.Text.Trim();
p.PostalCode = this.textBoxParticipantPostalCode.Text.Trim(); p.PostalCode = this.textBoxParticipantPostalCode.Text.Trim();
p.City = this.textBoxParticipantCity.Text.Trim(); p.City = this.textBoxParticipantCity.Text.Trim();
p.Save(_dbManager); await p.Save(_dbManager);
this.listBoxParticipant.ItemsSource = null; this.listBoxParticipant.ItemsSource = null;
this.listBoxParticipant.ItemsSource = _users; this.listBoxParticipant.ItemsSource = _users;
this.listBoxParticipant.SelectedItem = p; this.listBoxParticipant.SelectedItem = p;
} }
} }
private void buttonUserSave_Click(object sender, RoutedEventArgs e) private async void buttonUserSave_Click(object sender, RoutedEventArgs e)
{ {
User? u = this.listBoxUser.SelectedItem as User; User? u = this.listBoxUser.SelectedItem as User;
if(u != null) if(u != null)
@ -107,14 +107,14 @@ namespace RoleEditor
u.PasswordHash = hashedInputStringBuilder.ToString(); u.PasswordHash = hashedInputStringBuilder.ToString();
} }
u.APIKey = this.textBoxUserAPIKey.Text.Trim(); u.APIKey = this.textBoxUserAPIKey.Text.Trim();
u.Save(_dbManager); await u.Save(_dbManager);
this.listBoxUser.ItemsSource = null; this.listBoxUser.ItemsSource = null;
this.listBoxUser.ItemsSource = _users; this.listBoxUser.ItemsSource = _users;
this.listBoxUser.SelectedItem = u; this.listBoxUser.SelectedItem = u;
} }
} }
private void buttonAddRole_Click(object sender, RoutedEventArgs e) private async void buttonAddRole_Click(object sender, RoutedEventArgs e)
{ {
Role? r = this.listBoxRoles.SelectedItem as Role; Role? r = this.listBoxRoles.SelectedItem as Role;
User? u = this.listBoxUser.SelectedItem as User; User? u = this.listBoxUser.SelectedItem as User;
@ -138,61 +138,84 @@ namespace RoleEditor
ra.RoleId = (int) r.Id; ra.RoleId = (int) r.Id;
ra.AssignedRole = r; ra.AssignedRole = r;
ra.AssignedUser = u; ra.AssignedUser = u;
ra.Save(_dbManager); await ra.Save(_dbManager);
_assignedRoles.Add(ra); _assignedRoles.Add(ra);
} }
} }
} }
private void buttonRemoveRole_Click(object sender, RoutedEventArgs e) private async void buttonRemoveRole_Click(object sender, RoutedEventArgs e)
{ {
// remove role from user // remove role from user
RoleAssignment? ra = this.listBoxUserRoles.SelectedItem as RoleAssignment; RoleAssignment? ra = this.listBoxUserRoles.SelectedItem as RoleAssignment;
if(ra != null) if(ra != null)
{ {
ra.Delete(_dbManager); await ra.Delete(_dbManager);
if(_assignedRoles.Contains(ra)) if(_assignedRoles.Contains(ra))
_assignedRoles.Remove(ra); _assignedRoles.Remove(ra);
} }
} }
private void buttonAddSecurable_Click(object sender, RoutedEventArgs e) private async void buttonAddSecurable_Click(object sender, RoutedEventArgs e)
{ {
if ((this.listBoxRoles.SelectedItem is Role r) && (this.listBoxSecurables.SelectedItem is Securable s))
{
// test if assignment is already present
bool foundMatchingAssignment = false;
foreach (SecurableAssignment sa in _assignedSecurables)
{
if ((sa.SecurableId == s.Id) && (sa.RoleId == r.Id))
{
foundMatchingAssignment = true;
break;
}
}
if (!foundMatchingAssignment)
{
SecurableAssignment sa = new SecurableAssignment();
sa.SecurableId = (int)s.Id;
sa.RoleId = (int)r.Id;
sa.AssignedRole = r;
sa.AssignedSecurable = s;
await sa.Save(_dbManager);
_assignedSecurables.Add(sa);
}
}
} }
private void buttonRemoveSecurable_Click(object sender, RoutedEventArgs e) private async void buttonRemoveSecurable_Click(object sender, RoutedEventArgs e)
{ {
SecurableAssignment? sa = this.listBoxRoleSecurables.SelectedItem as SecurableAssignment; SecurableAssignment? sa = this.listBoxRoleSecurables.SelectedItem as SecurableAssignment;
if(sa != null) if(sa != null)
{ {
sa.Delete(_dbManager); await sa.Delete(_dbManager);
if (_assignedSecurables.Contains(sa)) if (_assignedSecurables.Contains(sa))
_assignedSecurables.Remove(sa); _assignedSecurables.Remove(sa);
} }
} }
private void buttonSaveSecurable_Click(object sender, RoutedEventArgs e) private async void buttonSaveSecurable_Click(object sender, RoutedEventArgs e)
{ {
Securable? s = this.listBoxSecurables.SelectedItem as Securable; Securable? s = this.listBoxSecurables.SelectedItem as Securable;
if(s != null) if(s != null)
{ {
s.Name = this.textBoxSecurableName.Text.Trim(); s.Name = this.textBoxSecurableName.Text.Trim();
s.Save(_dbManager); await s.Save(_dbManager);
this.listBoxSecurables.ItemsSource = null; this.listBoxSecurables.ItemsSource = null;
this.listBoxSecurables.ItemsSource = _securables; this.listBoxSecurables.ItemsSource = _securables;
this.listBoxSecurables.SelectedItem = s; this.listBoxSecurables.SelectedItem = s;
} }
} }
private void buttonSaveRole_Click(object sender, RoutedEventArgs e) private async void buttonSaveRole_Click(object sender, RoutedEventArgs e)
{ {
Role? r = this.listBoxRoles.SelectedItem as Role; Role? r = this.listBoxRoles.SelectedItem as Role;
if(r != null) if(r != null)
{ {
r.Name = this.textBoxRoleName.Text.Trim(); r.Name = this.textBoxRoleName.Text.Trim();
r.Description = this.textBoxRoleDescription.Text.Trim(); r.Description = this.textBoxRoleDescription.Text.Trim();
r.Save(_dbManager); await r.Save(_dbManager);
this.listBoxRoles.ItemsSource = null; this.listBoxRoles.ItemsSource = null;
this.listBoxRoles.ItemsSource = _roles; this.listBoxRoles.ItemsSource = _roles;
this.listBoxRoles.SelectedItem = r; this.listBoxRoles.SelectedItem = r;
@ -236,9 +259,12 @@ namespace RoleEditor
foreach (Securable s in this._securables) foreach (Securable s in this._securables)
{ {
if (sa.SecurableId == s.Id) if (sa.SecurableId == s.Id)
{
sa.AssignedSecurable = s; sa.AssignedSecurable = s;
_assignedSecurables.Add(sa); break;
}
} }
_assignedSecurables.Add(sa);
} }
} }
} }
@ -264,9 +290,12 @@ namespace RoleEditor
foreach (Role r in this._roles) foreach (Role r in this._roles)
{ {
if (ra.RoleId == r.Id) if (ra.RoleId == r.Id)
{
ra.AssignedRole = r; ra.AssignedRole = r;
_assignedRoles.Add(ra); break;
}
} }
_assignedRoles.Add(ra);
} }
} }
} }
@ -281,9 +310,20 @@ namespace RoleEditor
#region menuitem callbacks #region menuitem callbacks
private void menuItemDeleteParticipant_Click(object sender, RoutedEventArgs e) private async void menuItemDeleteParticipant_Click(object sender, RoutedEventArgs e)
{ {
try
{
if(this.listBoxParticipant.SelectedItem is Participant p)
{
await p.Delete(_dbManager);
this._participants.Remove(p);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
} }
private void menuItemNewParticipant_Click(object sender, RoutedEventArgs e) private void menuItemNewParticipant_Click(object sender, RoutedEventArgs e)
@ -305,9 +345,20 @@ namespace RoleEditor
} }
} }
private void menuItemDeleteUser_Click(object sender, RoutedEventArgs e) private async void menuItemDeleteUser_Click(object sender, RoutedEventArgs e)
{ {
try
{
if (this.listBoxUser.SelectedItem is User u)
{
await u.Delete(_dbManager);
this._users.Remove(u);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
} }
private void menuItemNewRole_Click(object sender, RoutedEventArgs e) private void menuItemNewRole_Click(object sender, RoutedEventArgs e)
@ -317,9 +368,20 @@ namespace RoleEditor
this.listBoxRoles.SelectedItem = r; this.listBoxRoles.SelectedItem = r;
} }
private void menuItemDeleteRole_Click(object sender, RoutedEventArgs e) private async void menuItemDeleteRole_Click(object sender, RoutedEventArgs e)
{ {
try
{
if (this.listBoxRoles.SelectedItem is Role r)
{
await r.Delete(_dbManager);
this._roles.Remove(r);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
} }
private void menuItemNewSecurable_Click(object sender, RoutedEventArgs e) private void menuItemNewSecurable_Click(object sender, RoutedEventArgs e)
@ -329,9 +391,20 @@ namespace RoleEditor
this.listBoxSecurables.SelectedItem = s; this.listBoxSecurables.SelectedItem = s;
} }
private void menuItemDeleteSecurable_Click(object sender, RoutedEventArgs e) private async void menuItemDeleteSecurable_Click(object sender, RoutedEventArgs e)
{ {
try
{
if (this.listBoxSecurables.SelectedItem is Securable s)
{
await s.Delete(_dbManager);
this._securables.Remove(s);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
} }
#endregion #endregion

View File

@ -45,7 +45,7 @@ namespace brecal.model
/// <summary> /// <summary>
/// Each database entity must be able to save itself to the database /// Each database entity must be able to save itself to the database
/// </summary> /// </summary>
public async void Save(IDBManager manager) public async Task Save(IDBManager manager)
{ {
if (this.Created.HasValue) if (this.Created.HasValue)
{ {
@ -60,7 +60,7 @@ namespace brecal.model
/// <summary> /// <summary>
/// Each entity must be able to delete itself /// Each entity must be able to delete itself
/// </summary> /// </summary>
public async void Delete(IDBManager manager) public async Task Delete(IDBManager manager)
{ {
await manager.ExecuteNonQuery(this.SetDelete); await manager.ExecuteNonQuery(this.SetDelete);
} }

View File

@ -1,9 +1,4 @@
using System; using System.Data;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model namespace brecal.model
{ {
@ -105,7 +100,7 @@ namespace brecal.model
} }
else else
{ {
return $"{AssignedRole.Name}:{Id}"; return AssignedRole.Name ?? AssignedRole.Id.ToString();
} }
} }

View File

@ -1,9 +1,4 @@
using System; using System.Data;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model namespace brecal.model
{ {
@ -73,7 +68,17 @@ namespace brecal.model
public override void SetCreate(IDbCommand cmd) public override void SetCreate(IDbCommand cmd)
{ {
throw new NotImplementedException(); cmd.CommandText = "INSERT INTO role_securable_map (securable_id, role_id) VALUES (@SECURABLEID, @ROLEID)";
IDbDataParameter userid = cmd.CreateParameter();
userid.ParameterName = "SECURABLEID";
userid.Value = this.SecurableId;
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) public override void SetDelete(IDbCommand cmd)
@ -94,7 +99,7 @@ namespace brecal.model
} }
else else
{ {
return $"{AssignedSecurable.Name}:{Id}"; return AssignedSecurable.Name ?? AssignedSecurable.Id.ToString();
} }
} }