mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Implemented automated asset counter into asset creation and asset update, Fixed uid visible in owner attribute in Assets frontend
This commit is contained in:
@@ -28,6 +28,10 @@ public class AssetsController : Controller
|
||||
{
|
||||
var assetList = await _ldap.ListDeviceAsync(Cn);
|
||||
result = new AssetsGetResponseModel(successful: true, assetModel: assetList);
|
||||
if (result.AssetsModel is not null)
|
||||
{
|
||||
result.AssetsModel.Owner = result.AssetsModel?.Owner?.Replace("uid=", "");
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -45,6 +49,11 @@ public class AssetsController : Controller
|
||||
{
|
||||
var assetList = await _ldap.ListDeviceAsync();
|
||||
result = new AssetsGetAllResponseModel(successful: true, assetsModel: assetList);
|
||||
result.AssetsModel = result.AssetsModel?.Select(asset =>
|
||||
{
|
||||
asset.Owner = asset.Owner?.Replace("uid=", "");
|
||||
return asset;
|
||||
});
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@@ -71,13 +80,10 @@ public class AssetsController : Controller
|
||||
{
|
||||
LdapAttributeSet attributeSet =
|
||||
[
|
||||
new LdapAttribute("objectClass", new[] {"top", "device", "extensibleObject"}),
|
||||
new LdapAttribute("objectClass", ["top", "device", "extensibleObject"]),
|
||||
];
|
||||
|
||||
if (assetModel.Cn != null)
|
||||
{
|
||||
attributeSet.Add(new LdapAttribute("cn", assetModel.Cn));
|
||||
}
|
||||
attributeSet.Add(new LdapAttribute("cn", await _ldap.GetAssetCounterAndIncrementAsync()));
|
||||
if (assetModel.SerialNumber != null)
|
||||
{
|
||||
attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber));
|
||||
@@ -168,7 +174,7 @@ public class AssetsController : Controller
|
||||
}
|
||||
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)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -1,15 +1,31 @@
|
||||
using System.Text.Json.Serialization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
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 string? Location { get; set; } = null;
|
||||
public string? Name { get; set; } = null;
|
||||
public string? Owner { 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;
|
||||
}
|
||||
@@ -118,6 +118,51 @@ public partial class LdapService : IDisposable
|
||||
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)
|
||||
{
|
||||
List<UserModel> returnValue = [];
|
||||
|
||||
@@ -182,10 +182,6 @@
|
||||
<div class="modal-body">
|
||||
<div class="row g-3">
|
||||
<!-- 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">
|
||||
<label class="form-label">@T["Name"]</label>
|
||||
<input type="text" class="form-control" name="Name" />
|
||||
@@ -232,7 +228,7 @@
|
||||
<!-- Dynamic attribute rows will appear here -->
|
||||
</div>
|
||||
<button type="button" class="btn btn-sm btn-success mt-3" id="addAttributeBtn">
|
||||
➕ @T["Add Attribute"]
|
||||
@T["Add Attribute"]
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -363,10 +359,6 @@
|
||||
<div class="modal-body">
|
||||
<div class="row g-3">
|
||||
<!-- 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">
|
||||
<label class="form-label">@T["Name"]</label>
|
||||
<input type="text" class="form-control" name="Name" />
|
||||
@@ -408,7 +400,7 @@
|
||||
</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">
|
||||
➕ @T["Add Attribute"]
|
||||
@T["Add Attribute"]
|
||||
</button>
|
||||
</div>
|
||||
|
||||
@@ -450,6 +442,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
const updateForm = document.getElementById('updateAssetForm');
|
||||
const updateAttributesContainer = document.getElementById('updateAttributesContainer');
|
||||
const addAttrBtn = document.getElementById('updateAddAttributeBtn');
|
||||
let assetId = null;
|
||||
|
||||
addAttrBtn.addEventListener('click', () => {
|
||||
const row = document.createElement('div');
|
||||
@@ -470,7 +463,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
|
||||
updateModal.addEventListener('show.bs.modal', async event => {
|
||||
const button = event.relatedTarget;
|
||||
const assetId = button.getAttribute('data-asset-id');
|
||||
assetId = button.getAttribute('data-asset-id');
|
||||
|
||||
updateAttributesContainer.innerHTML = '';
|
||||
updateForm.reset();
|
||||
@@ -524,7 +517,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
e.preventDefault();
|
||||
|
||||
const formData = new FormData(updateForm);
|
||||
const jsonData = {};
|
||||
const jsonData = {"Cn": assetId};
|
||||
for (const [key, value] of formData.entries()) {
|
||||
if (!value) continue;
|
||||
const keys = key.split('.');
|
||||
|
||||
Reference in New Issue
Block a user