mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Merge pull request #63 from LD-Reborn/feature/Create_Assets
CRUD - Assets create
This commit is contained in:
@@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
using Berufsschule_HAM.Models;
|
using Berufsschule_HAM.Models;
|
||||||
using Berufsschule_HAM.Services;
|
using Berufsschule_HAM.Services;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using Novell.Directory.Ldap;
|
||||||
|
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class AssetsController : Controller
|
public class AssetsController : Controller
|
||||||
@@ -22,6 +23,88 @@ public class AssetsController : Controller
|
|||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("Create")]
|
||||||
|
public async Task<bool> Create(AssetsCreateRequestModel assetModel)
|
||||||
|
{
|
||||||
|
if (assetModel is null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Unable to create an asset because the AssetModel is null.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
LdapAttributeSet attributeSet =
|
||||||
|
[
|
||||||
|
new LdapAttribute("objectClass", new[] {"top", "device", "extensibleObject"}),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (assetModel.Cn != null)
|
||||||
|
{
|
||||||
|
attributeSet.Add(new LdapAttribute("cn", assetModel.Cn));
|
||||||
|
}
|
||||||
|
if (assetModel.SerialNumber != null)
|
||||||
|
{
|
||||||
|
attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber));
|
||||||
|
}
|
||||||
|
if (assetModel.Location != null)
|
||||||
|
{
|
||||||
|
attributeSet.Add(new LdapAttribute("l", assetModel.Location));
|
||||||
|
}
|
||||||
|
if (assetModel.Owner != null)
|
||||||
|
{
|
||||||
|
var ownerDn = $"uid={assetModel.Owner}";
|
||||||
|
attributeSet.Add(new LdapAttribute("owner", ownerDn));
|
||||||
|
}
|
||||||
|
if (assetModel.Name != null)
|
||||||
|
{
|
||||||
|
attributeSet.Add(new LdapAttribute("name", assetModel.Name));
|
||||||
|
}
|
||||||
|
if (assetModel.Description != null)
|
||||||
|
{
|
||||||
|
var assetDescription = new AssetDescription();
|
||||||
|
|
||||||
|
if (assetModel.Description.Type != null)
|
||||||
|
{
|
||||||
|
assetDescription.Type = assetModel.Description.Type;
|
||||||
|
}
|
||||||
|
if (assetModel.Description.Purchase != null)
|
||||||
|
{
|
||||||
|
var purchase = new AssetPurchase();
|
||||||
|
if (assetModel.Description.Purchase.PurchasedAt != null)
|
||||||
|
{
|
||||||
|
purchase.PurchasedAt = assetModel.Description.Purchase.PurchasedAt;
|
||||||
|
}
|
||||||
|
if (assetModel.Description.Purchase.PurchaseDate != null)
|
||||||
|
{
|
||||||
|
purchase.PurchaseDate = assetModel.Description.Purchase.PurchaseDate;
|
||||||
|
}
|
||||||
|
if (assetModel.Description.Purchase.PurchasedBy != null)
|
||||||
|
{
|
||||||
|
purchase.PurchasedBy = assetModel.Description.Purchase.PurchasedBy;
|
||||||
|
}
|
||||||
|
if (assetModel.Description.Purchase.PurchaseValue != null)
|
||||||
|
{
|
||||||
|
purchase.PurchaseValue = assetModel.Description.Purchase.PurchaseValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
assetDescription.Purchase = purchase;
|
||||||
|
}
|
||||||
|
|
||||||
|
attributeSet.Add(new LdapAttribute("description", JsonSerializer.Serialize(assetDescription)));
|
||||||
|
}
|
||||||
|
|
||||||
|
await _ldap.CreateAsset(attributeSet);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
_logger.LogError($"Unable to create an asset because of the exception: {e.Message}", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[HttpGet("Delete")]
|
[HttpGet("Delete")]
|
||||||
public async Task<bool> Delete(string cn)
|
public async Task<bool> Delete(string cn)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -65,20 +65,21 @@ public class GroupsController : Controller
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
description ??= JsonSerializer.Serialize(new GroupPermissions() {Permissions = []});
|
description ??= JsonSerializer.Serialize(new GroupPermissions() {Permissions = []});
|
||||||
|
|
||||||
LdapAttributeSet attributeSet =
|
LdapAttributeSet attributeSet =
|
||||||
[
|
[
|
||||||
new LdapAttribute("objectClass", "posixGroup"),
|
new LdapAttribute("objectClass", "posixGroup"),
|
||||||
new LdapAttribute("objectClass", "top"),
|
new LdapAttribute("objectClass", "top"),
|
||||||
new LdapAttribute("cn", cn),
|
new LdapAttribute("cn", cn),
|
||||||
new LdapAttribute("gidNumber", gidNumber),
|
new LdapAttribute("gidNumber", gidNumber),
|
||||||
new LdapAttribute("description",
|
new LdapAttribute(
|
||||||
JsonSerializer.Serialize(
|
"description",
|
||||||
new GroupPermissions()
|
JsonSerializer.Serialize(new GroupPermissions()
|
||||||
{
|
{
|
||||||
Permissions = [.. permissions]
|
Permissions = [.. permissions]
|
||||||
})),
|
}))
|
||||||
];
|
];
|
||||||
|
|
||||||
await _ldap.CreateGroup(cn, attributeSet);
|
await _ldap.CreateGroup(cn, attributeSet);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,6 +74,7 @@ public class UsersController : Controller
|
|||||||
byte[] hashedPassword = SHA256.HashData(passwordBytes);
|
byte[] hashedPassword = SHA256.HashData(passwordBytes);
|
||||||
userPassword = "{SHA256}" + Convert.ToBase64String(hashedPassword);
|
userPassword = "{SHA256}" + Convert.ToBase64String(hashedPassword);
|
||||||
}
|
}
|
||||||
|
|
||||||
LdapAttributeSet attributeSet =
|
LdapAttributeSet attributeSet =
|
||||||
[
|
[
|
||||||
new LdapAttribute("objectClass", "inetOrgPerson"),
|
new LdapAttribute("objectClass", "inetOrgPerson"),
|
||||||
@@ -83,7 +84,7 @@ public class UsersController : Controller
|
|||||||
new LdapAttribute("uid", uid),
|
new LdapAttribute("uid", uid),
|
||||||
new LdapAttribute("jpegPhoto", jpegPhoto),
|
new LdapAttribute("jpegPhoto", jpegPhoto),
|
||||||
new LdapAttribute("description", description),
|
new LdapAttribute("description", description),
|
||||||
new LdapAttribute("userPassword", userPassword)
|
new LdapAttribute("userPassword", userPassword),
|
||||||
];
|
];
|
||||||
await _ldap.CreateUser(uid, attributeSet);
|
await _ldap.CreateUser(uid, attributeSet);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
11
src/Models/AssetsCreateRequestModel.cs
Normal file
11
src/Models/AssetsCreateRequestModel.cs
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
@@ -175,10 +175,16 @@ public partial class LdapService : IDisposable
|
|||||||
await CreateObject(dn, attributeSet);
|
await CreateObject(dn, attributeSet);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task CreateAsset(LdapAttributeSet attributeSet)
|
public async Task CreateAsset(LdapAttributeSet attributeSet)
|
||||||
{
|
{
|
||||||
await CreateObject(AssetsBaseDn, attributeSet);
|
string? cn = attributeSet.GetAttribute("cn")?.StringValue;
|
||||||
}
|
|
||||||
|
if (string.IsNullOrEmpty(cn))
|
||||||
|
throw new ArgumentException("AttributeSet must contain a cn attribute.");
|
||||||
|
|
||||||
|
string dn = PrependRDN($"cn={cn}", AssetsBaseDn);
|
||||||
|
await CreateObject(dn, attributeSet);
|
||||||
|
}
|
||||||
|
|
||||||
public async Task CreateLocation(LdapAttributeSet attributeSet)
|
public async Task CreateLocation(LdapAttributeSet attributeSet)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user