Use collection initializers

This commit is contained in:
anomny
2025-10-04 15:11:08 +02:00
parent ef3be7cb41
commit 285a0968ec
3 changed files with 46 additions and 44 deletions

View File

@@ -36,30 +36,28 @@ public class AssetsController : Controller
{ {
var ownerDn = $"uid={assetModel.Owner},ou=people,dc=example,dc=com"; var ownerDn = $"uid={assetModel.Owner},ou=people,dc=example,dc=com";
var attributeSet = new LdapAttributeSet(); LdapAttributeSet attributeSet =
attributeSet.Add(new LdapAttribute("objectClass", new[] { [
"top", new LdapAttribute("objectClass", new[] {"top", "device", "extensibleObject"}),
"device", new LdapAttribute("cn", assetModel.Cn),
"extensibleObject" new LdapAttribute("serialNumber", assetModel.SerialNumber),
})); new LdapAttribute("l", assetModel.Location),
attributeSet.Add(new LdapAttribute("cn", assetModel.Cn)); new LdapAttribute("owner", ownerDn),
attributeSet.Add(new LdapAttribute("serialNumber", assetModel.SerialNumber)); new LdapAttribute("name", assetModel.Name),
attributeSet.Add(new LdapAttribute("l", assetModel.Location)); new LdapAttribute(
attributeSet.Add(new LdapAttribute("owner", ownerDn)); "description",
attributeSet.Add(new LdapAttribute("name", assetModel.Name)); JsonSerializer.Serialize(new AssetDescription()
attributeSet.Add(new LdapAttribute(
"description",
JsonSerializer.Serialize(new AssetDescription()
{
Type = assetModel.Description.Type,
Purchase = new AssetPurchase
{ {
PurchasedAt = assetModel.Description.Purchase.PurchasedAt, Type = assetModel.Description!.Type,
PurchaseDate = assetModel.Description.Purchase.PurchaseDate, Purchase = new AssetPurchase
PurchaseValue = assetModel.Description.Purchase.PurchaseValue {
} PurchasedAt = assetModel.Description.Purchase!.PurchasedAt,
}) PurchaseDate = assetModel.Description.Purchase.PurchaseDate,
)); PurchaseValue = assetModel.Description.Purchase.PurchaseValue
}
})
)
];
await _ldap.CreateAsset(attributeSet); await _ldap.CreateAsset(attributeSet);

View File

@@ -65,19 +65,21 @@ public class GroupsController : Controller
try try
{ {
description ??= JsonSerializer.Serialize(new GroupPermissions() {Permissions = []}); description ??= JsonSerializer.Serialize(new GroupPermissions() {Permissions = []});
var attributeSet = new LdapAttributeSet();
attributeSet.Add(new LdapAttribute("objectClass", "posixGroup"));
attributeSet.Add(new LdapAttribute("objectClass", "top"));
attributeSet.Add(new LdapAttribute("cn", cn));
attributeSet.Add(new LdapAttribute("gidNumber", gidNumber));
attributeSet.Add(new LdapAttribute(
"description",
JsonSerializer.Serialize(new GroupPermissions()
{
Permissions = [.. permissions]
})));
LdapAttributeSet attributeSet =
[
new LdapAttribute("objectClass", "posixGroup"),
new LdapAttribute("objectClass", "top"),
new LdapAttribute("cn", cn),
new LdapAttribute("gidNumber", gidNumber),
new LdapAttribute(
"description",
JsonSerializer.Serialize(new GroupPermissions()
{
Permissions = [.. permissions]
}))
];
await _ldap.CreateGroup(cn, attributeSet); await _ldap.CreateGroup(cn, attributeSet);
return true; return true;
} }

View File

@@ -75,15 +75,17 @@ public class UsersController : Controller
userPassword = "{SHA256}" + Convert.ToBase64String(hashedPassword); userPassword = "{SHA256}" + Convert.ToBase64String(hashedPassword);
} }
LdapAttributeSet attributeSet = new LdapAttributeSet(); LdapAttributeSet attributeSet =
attributeSet.Add(new LdapAttribute("objectClass", "inetOrgPerson")); [
attributeSet.Add(new LdapAttribute("cn", cn)); new LdapAttribute("objectClass", "inetOrgPerson"),
attributeSet.Add(new LdapAttribute("sn", sn)); new LdapAttribute("cn", cn),
attributeSet.Add(new LdapAttribute("title", title)); new LdapAttribute("sn", sn),
attributeSet.Add(new LdapAttribute("uid", uid)); new LdapAttribute("title", title),
attributeSet.Add(new LdapAttribute("jpegPhoto", jpegPhoto)); new LdapAttribute("uid", uid),
attributeSet.Add(new LdapAttribute("description", description)); new LdapAttribute("jpegPhoto", jpegPhoto),
attributeSet.Add(new LdapAttribute("userPassword", userPassword)); new LdapAttribute("description", description),
new LdapAttribute("userPassword", userPassword),
];
await _ldap.CreateUser(uid, attributeSet); await _ldap.CreateUser(uid, attributeSet);
return true; return true;
} }