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));
attributeSet.Add(new LdapAttribute("name", assetModel.Name));
attributeSet.Add(new LdapAttribute(
"description", "description",
JsonSerializer.Serialize(new AssetDescription() JsonSerializer.Serialize(new AssetDescription()
{ {
Type = assetModel.Description.Type, Type = assetModel.Description!.Type,
Purchase = new AssetPurchase Purchase = new AssetPurchase
{ {
PurchasedAt = assetModel.Description.Purchase.PurchasedAt, PurchasedAt = assetModel.Description.Purchase!.PurchasedAt,
PurchaseDate = assetModel.Description.Purchase.PurchaseDate, PurchaseDate = assetModel.Description.Purchase.PurchaseDate,
PurchaseValue = assetModel.Description.Purchase.PurchaseValue PurchaseValue = assetModel.Description.Purchase.PurchaseValue
} }
}) })
)); )
];
await _ldap.CreateAsset(attributeSet); await _ldap.CreateAsset(attributeSet);

View File

@@ -66,17 +66,19 @@ public class GroupsController : Controller
{ {
description ??= JsonSerializer.Serialize(new GroupPermissions() {Permissions = []}); description ??= JsonSerializer.Serialize(new GroupPermissions() {Permissions = []});
var attributeSet = new LdapAttributeSet(); LdapAttributeSet attributeSet =
attributeSet.Add(new LdapAttribute("objectClass", "posixGroup")); [
attributeSet.Add(new LdapAttribute("objectClass", "top")); new LdapAttribute("objectClass", "posixGroup"),
attributeSet.Add(new LdapAttribute("cn", cn)); new LdapAttribute("objectClass", "top"),
attributeSet.Add(new LdapAttribute("gidNumber", gidNumber)); new LdapAttribute("cn", cn),
attributeSet.Add(new LdapAttribute( new LdapAttribute("gidNumber", gidNumber),
new LdapAttribute(
"description", "description",
JsonSerializer.Serialize(new GroupPermissions() JsonSerializer.Serialize(new GroupPermissions()
{ {
Permissions = [.. permissions] 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;
} }