Implemented Groups edit buttons

This commit is contained in:
2025-10-12 16:13:41 +02:00
parent 4b6fb74a76
commit 164831c903
5 changed files with 237 additions and 27 deletions

View File

@@ -18,30 +18,52 @@ public class GroupsController : Controller
_logger = logger;
}
[HttpGet("Index")]
public async Task<IEnumerable<GroupModel>> Index(GroupsIndexRequestModel requestModel)
[HttpGet("Get")]
public async Task<GroupsGetResponseModel> GetAsync(GroupsIndexRequestModel model)
{
string? cn = requestModel.Cn;
List<string> attributes = [.. _ldap.GroupsAttributes];
if (!requestModel.GidNumber) attributes.Remove("gidNumber");
if (!requestModel.Permissions) attributes.Remove("description");
IEnumerable<GroupModel> groups;
if (cn is null)
if (model is null)
{
groups = await _ldap.ListGroupsAsync([.. attributes]);
return new GroupsGetResponseModel(
successful: false,
groupModels: null,
exception: "Unable to create a group because the GroupsCreateRequestModel is null.");
}
else
try
{
try
string? cn = model.Cn;
List<string> attributes = [.. _ldap.GroupsAttributes];
if (!model.GidNumber) attributes.Remove("gidNumber");
if (!model.Permissions) attributes.Remove("description");
IEnumerable<GroupModel> groups;
if (cn is null)
{
groups = [await _ldap.GetGroupByCnAsync(cn, [.. attributes])];
groups = await _ldap.ListGroupsAsync([.. attributes]);
}
catch (InvalidOperationException)
else
{
groups = [];
try
{
groups = [await _ldap.GetGroupByCnAsync(cn, [.. attributes])];
}
catch (InvalidOperationException)
{
groups = [];
}
}
return new(true, groups);
} catch (Exception ex)
{
if (model.Cn is not null)
{
_logger.LogError("Unable to get group {model.Cn}: {ex.Message} - {ex.StackTrace}", [model.Cn, ex.Message, ex.StackTrace]);
}
else
{
_logger.LogError("Unable to get groups: {ex.Message} - {ex.StackTrace}", [ex.Message, ex.StackTrace]);
}
return new(false, null, ex.Message);
}
return groups;
}
[HttpDelete("Delete")]
@@ -75,6 +97,7 @@ public class GroupsController : Controller
List<GroupPermission> permissions = model.Permissions;
string gidNumber = "0"; // TODO implement counter
string cn = model.Cn;
string displayName = model.DisplayName;
LdapAttributeSet attributeSet =
[
@@ -84,7 +107,7 @@ public class GroupsController : Controller
new LdapAttribute("gidNumber", gidNumber),
new LdapAttribute(
"description",
JsonSerializer.Serialize(new GroupPermissions(){Permissions = permissions}))
JsonSerializer.Serialize(new GroupDescription(){DisplayName = displayName, Permissions = permissions}))
];
await _ldap.CreateGroup(cn, attributeSet);
@@ -97,13 +120,13 @@ public class GroupsController : Controller
}
}
[HttpPost("Update")]
public async Task<bool> Update([FromBody]GroupsModifyRequestModel requestModel)
[HttpPatch("Update")]
public async Task<GroupsUpdateResponseModel> Update([FromBody]GroupsModifyRequestModel requestModel)
{
if (requestModel is null)
{
_logger.LogError("Unable to update a group because the GroupsModifyRequestModel is null");
return false;
return new(false, "Unable to update a group because the GroupsModifyRequestModel is null");
}
string cn = requestModel.Cn;
@@ -116,10 +139,10 @@ public class GroupsController : Controller
{
await _ldap.UpdateGroup(cn, "gidNumber", requestModel.GidNumber);
}
if (requestModel.Permissions is not null)
if (requestModel.Description is not null)
{
await _ldap.UpdateGroup(cn, "description", JsonSerializer.Serialize(requestModel.Permissions));
await _ldap.UpdateGroup(cn, "description", JsonSerializer.Serialize(requestModel.Description));
}
return true;
return new(true);
}
}