started on user role assignments
This commit is contained in:
parent
c9aa439712
commit
a4737b8b1f
@ -34,8 +34,8 @@ namespace RoleEditor
|
||||
private readonly ObservableCollection<Role> _roles = new ObservableCollection<Role>();
|
||||
private readonly ObservableCollection<Securable> _securables = new ObservableCollection<Securable>();
|
||||
private readonly ObservableCollection<User> _users = new ObservableCollection<User>();
|
||||
private readonly ObservableCollection<Role> _assignedRoles = new ObservableCollection<Role>();
|
||||
private readonly ObservableCollection<Securable> _assignedSecurables = new ObservableCollection<Securable>();
|
||||
private readonly ObservableCollection<RoleAssignment> _assignedRoles = new ObservableCollection<RoleAssignment>();
|
||||
private readonly ObservableCollection<SecurableAssignment> _assignedSecurables = new ObservableCollection<SecurableAssignment>();
|
||||
private DBManager _dbManager;
|
||||
|
||||
#endregion
|
||||
@ -202,7 +202,7 @@ namespace RoleEditor
|
||||
this.textBoxRoleDescription.Text = (r != null) ? r.Description : string.Empty;
|
||||
}
|
||||
|
||||
private void listBoxUser_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
private async void listBoxUser_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
User? u = this.listBoxUser.SelectedItem as User;
|
||||
this.textBoxUserFirstName.Text = (u != null) ? u.Firstname : string.Empty;
|
||||
@ -212,6 +212,18 @@ namespace RoleEditor
|
||||
this.textBoxUserCreated.Text = (u != null) ? u.Created.ToString() : string.Empty;
|
||||
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))
|
||||
{
|
||||
foreach(Role r in this._roles)
|
||||
{
|
||||
if (ra.RoleId == r.Id)
|
||||
ra.AssignedRole = r;
|
||||
_assignedRoles.Add(ra);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void listBoxSecurables_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
|
||||
@ -12,12 +12,60 @@ namespace brecal.model
|
||||
|
||||
#region Properties
|
||||
|
||||
public int? UserId { get; set; }
|
||||
|
||||
public int? RoleId { get; set; }
|
||||
|
||||
public Role? AssignedRole { get; set; }
|
||||
|
||||
public User? AssignedUser { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region public static methods
|
||||
|
||||
public static async Task<List<RoleAssignment>> LoadForUser(User? u, IDBManager manager)
|
||||
{
|
||||
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems, args: u);
|
||||
List<RoleAssignment> result = new();
|
||||
foreach (RoleAssignment ra in loadResultList.Cast<RoleAssignment>())
|
||||
{
|
||||
ra.AssignedUser = u;
|
||||
result.Add(ra);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
cmd.Parameters.Add(uid);
|
||||
}
|
||||
|
||||
public static List<DbEntity> LoadElems(IDataReader reader)
|
||||
{
|
||||
List<DbEntity> result = new List<DbEntity>();
|
||||
while (reader.Read())
|
||||
{
|
||||
RoleAssignment ra = new();
|
||||
ra.Id = (uint)reader.GetInt32(0);
|
||||
if (!reader.IsDBNull(1)) ra.UserId = reader.GetInt32(1);
|
||||
if (!reader.IsDBNull(2)) ra.RoleId = reader.GetInt32(2);
|
||||
result.Add(ra);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region overrides
|
||||
|
||||
public override void SetUpdate(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
@ -30,8 +78,27 @@ namespace brecal.model
|
||||
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
cmd.CommandText = "DELETE FROM user_role_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.AssignedRole == null)
|
||||
{
|
||||
return $"{Id}: <defunct role>";
|
||||
}
|
||||
else
|
||||
{
|
||||
return $"{AssignedRole.Name}:{Id}";
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user