Implemented automated asset counter into asset creation and asset update, Fixed uid visible in owner attribute in Assets frontend

This commit is contained in:
2025-10-12 18:49:09 +02:00
parent 434b0952dc
commit a3c5a5341d
5 changed files with 82 additions and 33 deletions

View File

@@ -28,6 +28,10 @@ public class AssetsController : Controller
{ {
var assetList = await _ldap.ListDeviceAsync(Cn); var assetList = await _ldap.ListDeviceAsync(Cn);
result = new AssetsGetResponseModel(successful: true, assetModel: assetList); result = new AssetsGetResponseModel(successful: true, assetModel: assetList);
if (result.AssetsModel is not null)
{
result.AssetsModel.Owner = result.AssetsModel?.Owner?.Replace("uid=", "");
}
} }
catch (Exception e) catch (Exception e)
{ {
@@ -45,6 +49,11 @@ public class AssetsController : Controller
{ {
var assetList = await _ldap.ListDeviceAsync(); var assetList = await _ldap.ListDeviceAsync();
result = new AssetsGetAllResponseModel(successful: true, assetsModel: assetList); result = new AssetsGetAllResponseModel(successful: true, assetsModel: assetList);
result.AssetsModel = result.AssetsModel?.Select(asset =>
{
asset.Owner = asset.Owner?.Replace("uid=", "");
return asset;
});
} }
catch (Exception e) catch (Exception e)
{ {
@@ -71,13 +80,10 @@ public class AssetsController : Controller
{ {
LdapAttributeSet attributeSet = LdapAttributeSet attributeSet =
[ [
new LdapAttribute("objectClass", new[] {"top", "device", "extensibleObject"}), new LdapAttribute("objectClass", ["top", "device", "extensibleObject"]),
]; ];
if (assetModel.Cn != null) attributeSet.Add(new LdapAttribute("cn", await _ldap.GetAssetCounterAndIncrementAsync()));
{
attributeSet.Add(new LdapAttribute("cn", assetModel.Cn));
}
if (assetModel.SerialNumber != null) if (assetModel.SerialNumber != null)
{ {
attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber)); attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber));
@@ -168,7 +174,7 @@ public class AssetsController : Controller
} }
if (requestModel.Owner is not null) if (requestModel.Owner is not null)
{ {
await _ldap.UpdateAsset(cn, "owner", requestModel.Owner); await _ldap.UpdateAsset(cn, "owner", $"uid={requestModel.Owner}");
} }
if (requestModel.SerialNumber is not null) if (requestModel.SerialNumber is not null)
{ {

View File

@@ -1,11 +0,0 @@
namespace Berufsschule_HAM.Models;
public class AssetsCreateRequestModel
{
public required string Cn { get; set; }
public AssetDescription? Description { get; set; } = null;
public string? Location { get; set; } = null;
public string? Name { get; set; } = null;
public string? Owner { get; set; } = null;
public string? SerialNumber { get; set; } = null;
}

View File

@@ -1,15 +1,31 @@
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
namespace Berufsschule_HAM.Models; namespace Berufsschule_HAM.Models;
public class AssetsModifyRequestModel public class AssetsCreateRequestModel
{ {
public required string Cn { get; set; }
public string? NewCn { get; set; } = null;
[FromBody]
public AssetDescription? Description { get; set; } = null; public AssetDescription? Description { get; set; } = null;
public string? Location { get; set; } = null; public string? Location { get; set; } = null;
public string? Name { get; set; } = null; public string? Name { get; set; } = null;
public string? Owner { get; set; } = null; public string? Owner { get; set; } = null;
public string? SerialNumber { get; set; } = null; public string? SerialNumber { get; set; } = null;
}
public class AssetsModifyRequestModel
{
[JsonPropertyName("Cn")]
public required string Cn { get; set; }
[JsonPropertyName("NewCn")]
public string? NewCn { get; set; } = null;
[JsonPropertyName("Description")]
public AssetDescription? Description { get; set; } = null;
[JsonPropertyName("Location")]
public string? Location { get; set; } = null;
[JsonPropertyName("Name")]
public string? Name { get; set; } = null;
[JsonPropertyName("Owner")]
public string? Owner { get; set; } = null;
[JsonPropertyName("SerialNumber")]
public string? SerialNumber { get; set; } = null;
} }

View File

@@ -118,6 +118,51 @@ public partial class LdapService : IDisposable
return true; return true;
} }
public async Task<string> GetAssetCounterAndIncrementAsync()
{
AssetsMetadataModel assetModel = await GetAssetCounterAsync();
string returnValue = assetModel.CnCounter.ToString();
assetModel.CnCounter++;
await UpdateAssetCounterAsync(assetModel);
return returnValue;
}
public async Task<AssetsMetadataModel> GetAssetCounterAsync()
{
Dictionary<string, string> entry = (await ListObjectBy(_opts.BaseDn, _opts.AssetsOu, ["description"])).First();
try
{
string description = entry["description"];
return JsonSerializer.Deserialize<AssetsMetadataModel>(description) ?? throw new Exception($"Unable to deserialize description: {description}");
}
catch (Exception ex)
{
_logger.LogError(ex, "Unable to retrieve asset counter due to exception: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
throw;
}
}
public async Task<bool> UpdateAssetCounterAsync(AssetsMetadataModel assetMetadataModel)
{
await ConnectAndBind();
try
{
string dn = AssetsBaseDn; //PrependRDN($"", MigrationsBaseDn);
string targetText = JsonSerializer.Serialize(assetMetadataModel);
_logger.LogInformation("Setting the LDAP asset counter to {targetText} for {dn}", [targetText, dn]);
var modification = new LdapModification(
LdapModification.Replace,
new LdapAttribute("description", targetText)
);
await _conn.ModifyAsync(dn, modification);
}
catch (Exception)
{
return false;
}
return true;
}
public async Task<IEnumerable<UserModel>> ListUsersAsync(string[] attributes) public async Task<IEnumerable<UserModel>> ListUsersAsync(string[] attributes)
{ {
List<UserModel> returnValue = []; List<UserModel> returnValue = [];

View File

@@ -182,10 +182,6 @@
<div class="modal-body"> <div class="modal-body">
<div class="row g-3"> <div class="row g-3">
<!-- Basic Info --> <!-- Basic Info -->
<div class="col-md-6">
<label class="form-label">@T["Asset ID (Cn)"] *</label>
<input type="text" class="form-control" name="Cn" required />
</div>
<div class="col-md-6"> <div class="col-md-6">
<label class="form-label">@T["Name"]</label> <label class="form-label">@T["Name"]</label>
<input type="text" class="form-control" name="Name" /> <input type="text" class="form-control" name="Name" />
@@ -232,7 +228,7 @@
<!-- Dynamic attribute rows will appear here --> <!-- Dynamic attribute rows will appear here -->
</div> </div>
<button type="button" class="btn btn-sm btn-success mt-3" id="addAttributeBtn"> <button type="button" class="btn btn-sm btn-success mt-3" id="addAttributeBtn">
@T["Add Attribute"] @T["Add Attribute"]
</button> </button>
</div> </div>
@@ -363,10 +359,6 @@
<div class="modal-body"> <div class="modal-body">
<div class="row g-3"> <div class="row g-3">
<!-- Same fields as in Create --> <!-- Same fields as in Create -->
<div class="col-md-6">
<label class="form-label">@T["Asset ID (Cn)"] *</label>
<input type="text" class="form-control" name="Cn" readonly />
</div>
<div class="col-md-6"> <div class="col-md-6">
<label class="form-label">@T["Name"]</label> <label class="form-label">@T["Name"]</label>
<input type="text" class="form-control" name="Name" /> <input type="text" class="form-control" name="Name" />
@@ -408,7 +400,7 @@
</div> </div>
<div id="updateAttributesContainer" class="d-flex flex-column gap-2"></div> <div id="updateAttributesContainer" class="d-flex flex-column gap-2"></div>
<button type="button" class="btn btn-sm btn-success mt-3" id="updateAddAttributeBtn"> <button type="button" class="btn btn-sm btn-success mt-3" id="updateAddAttributeBtn">
@T["Add Attribute"] @T["Add Attribute"]
</button> </button>
</div> </div>
@@ -450,6 +442,7 @@ document.addEventListener('DOMContentLoaded', () => {
const updateForm = document.getElementById('updateAssetForm'); const updateForm = document.getElementById('updateAssetForm');
const updateAttributesContainer = document.getElementById('updateAttributesContainer'); const updateAttributesContainer = document.getElementById('updateAttributesContainer');
const addAttrBtn = document.getElementById('updateAddAttributeBtn'); const addAttrBtn = document.getElementById('updateAddAttributeBtn');
let assetId = null;
addAttrBtn.addEventListener('click', () => { addAttrBtn.addEventListener('click', () => {
const row = document.createElement('div'); const row = document.createElement('div');
@@ -470,7 +463,7 @@ document.addEventListener('DOMContentLoaded', () => {
updateModal.addEventListener('show.bs.modal', async event => { updateModal.addEventListener('show.bs.modal', async event => {
const button = event.relatedTarget; const button = event.relatedTarget;
const assetId = button.getAttribute('data-asset-id'); assetId = button.getAttribute('data-asset-id');
updateAttributesContainer.innerHTML = ''; updateAttributesContainer.innerHTML = '';
updateForm.reset(); updateForm.reset();
@@ -524,7 +517,7 @@ document.addEventListener('DOMContentLoaded', () => {
e.preventDefault(); e.preventDefault();
const formData = new FormData(updateForm); const formData = new FormData(updateForm);
const jsonData = {}; const jsonData = {"Cn": assetId};
for (const [key, value] of formData.entries()) { for (const [key, value] of formData.entries()) {
if (!value) continue; if (!value) continue;
const keys = key.split('.'); const keys = key.split('.');