110 lines
3.3 KiB
C#
110 lines
3.3 KiB
C#
using System.Data;
|
|
|
|
namespace brecal.model
|
|
{
|
|
public class SecurableAssignment : DbEntity
|
|
{
|
|
#region Properties
|
|
|
|
public Role? AssignedRole { get; set; }
|
|
|
|
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();
|
|
}
|
|
|
|
public override void SetCreate(IDbCommand cmd)
|
|
{
|
|
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)
|
|
{
|
|
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 ?? AssignedSecurable.Id.ToString();
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|