Added groups view model, implemented basic groups table view

This commit is contained in:
2025-10-11 13:29:59 +02:00
parent d9127736d9
commit fd5dcb5618
5 changed files with 65 additions and 20 deletions

View File

@@ -94,7 +94,8 @@ public class HomeController : Controller
[HttpGet("Groups")]
public async Task<ActionResult> GroupsAsync()
{
return View(); // TODO: Add viewmodel
IEnumerable<GroupModel> groups = await _ldap.ListGroupsAsync();
return View(new GroupsIndexViewModel(groups));
}
[HttpPost("Login")]

View File

@@ -7,12 +7,14 @@ namespace Berufsschule_HAM.Models;
public class GroupModel
{
public required string Cn { get; set; }
public string DisplayName { get; set; }
public string? GidNumber { get; set; }
public List<GroupPermission> Permissions { get; set; }
public GroupModel(Dictionary<string, string> ldapData)
{
Cn = ldapData.GetValueOrDefault("cn") ?? throw new GroupModelConfigurationException();
GidNumber = ldapData.GetValueOrDefault("gidNumber");
DisplayName = ldapData.GetValueOrDefault("displayName") ?? Cn;
string? descriptionValue = ldapData.GetValueOrDefault("description");
if (descriptionValue is null)
{

View File

@@ -0,0 +1,38 @@
namespace Berufsschule_HAM.Models;
public class GroupsIndexViewModel
{
public List<GroupsTableViewModel> GroupsTableViewModels { get; set; } = [];
public GroupsIndexViewModel(IEnumerable<GroupModel> groupModels)
{
foreach (GroupModel model in groupModels)
{
GroupsTableViewModels.Add(new()
{
Cn = model.Cn,
Group = model.DisplayName,
CanInventorize = model.Permissions.Any(x => x == GroupPermission.CanInventorize),
CanManageAssets = model.Permissions.Any(x => x == GroupPermission.CanManageAssets),
CanManageGroups = model.Permissions.Any(x => x == GroupPermission.CanManageGroups),
CanManageLocations = model.Permissions.Any(x => x == GroupPermission.CanManageLocations),
CanManageUsers = model.Permissions.Any(x => x == GroupPermission.CanManageUsers)
});
}
}
}
public class GroupsTableViewModel
{
public required string Cn { get; set; }
public required string Group { get; set; }
public required bool CanInventorize { get; set; }
public required bool CanManageUsers { get; set; }
public required bool CanManageLocations { get; set; }
public required bool CanManageAssets { get; set; }
public required bool CanManageGroups { get; set; }
}

View File

@@ -134,6 +134,11 @@ public partial class LdapService : IDisposable
return returnValue;
}
public async Task<IEnumerable<GroupModel>> ListGroupsAsync()
{
return await ListGroupsAsync(GroupsAttributes);
}
public async Task<IEnumerable<GroupModel>> ListGroupsAsync(string[] attributes)
{
List<GroupModel> returnValue = [];

View File

@@ -1,7 +1,7 @@
@using Microsoft.AspNetCore.Mvc.Localization
@using Berufsschule_HAM.Models
@using System.Buffers.Text
@* @model UsersIndexViewModel *@
@model GroupsIndexViewModel
@inject IViewLocalizer T
@{
ViewData["Title"] = T["Groups"];
@@ -23,39 +23,38 @@
<tr>
<th style="text-align: center">Group</th>
<th style="text-align: center">@T["Can"]:<br/>@T["inventorize"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["Manage users"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["Manage locations"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["Manage assets"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["manage users"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["manage locations"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["manage assets"]</th>
<th style="text-align: center">@T["Can"]:<br/>@T["manage groups"]</th>
<th style="text-align: center">@T["Action"]</th>
</tr>
</thead>
<tbody>
@* @{
foreach (UserTableViewModel userTableViewModel in Model.UserTableViewModels)
@{
foreach (GroupsTableViewModel userTableViewModel in Model.GroupsTableViewModels)
{
<tr>
<td>
<img class="rounded-circle user-icon" src="data:image/jpeg;base64,@userTableViewModel.JpegPhoto" alt="Photo" style="max-width:300px;" />
</td>
<td>@userTableViewModel.Uid</td>
<td>@userTableViewModel.Title</td>
<td>@userTableViewModel.Name</td>
<td>@userTableViewModel.Surname</td>
<td>@userTableViewModel.Workplace</td>
<td>
<div class="d-flex gap-2">
<td style="text-align: center">@userTableViewModel.Group</td>
<td style="text-align: center">@userTableViewModel.CanInventorize</td>
<td style="text-align: center">@userTableViewModel.CanManageUsers</td>
<td style="text-align: center">@userTableViewModel.CanManageLocations</td>
<td style="text-align: center">@userTableViewModel.CanManageAssets</td>
<td style="text-align: center">@userTableViewModel.CanManageGroups</td>
<td style="text-align: center">
<div class="d-flex gap-2 justify-content-center">
<button class="btn btn-sm btn-primary">Update</button>
<button class="btn btn-sm btn-danger btn-delete"
data-user-id="@userTableViewModel.Uid"
data-user-id="@userTableViewModel.Cn"
data-bs-toggle="modal"
data-bs-target="#deleteModal">
🗑️ Delete
Delete
</button>
</div>
</td>
</tr>
}
} *@
}
</tbody>
</table>
</div>