mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Implemented Groups edit buttons
This commit is contained in:
@@ -18,13 +18,23 @@ 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;
|
||||
if (model is null)
|
||||
{
|
||||
return new GroupsGetResponseModel(
|
||||
successful: false,
|
||||
groupModels: null,
|
||||
exception: "Unable to create a group because the GroupsCreateRequestModel is null.");
|
||||
}
|
||||
try
|
||||
{
|
||||
|
||||
string? cn = model.Cn;
|
||||
List<string> attributes = [.. _ldap.GroupsAttributes];
|
||||
if (!requestModel.GidNumber) attributes.Remove("gidNumber");
|
||||
if (!requestModel.Permissions) attributes.Remove("description");
|
||||
if (!model.GidNumber) attributes.Remove("gidNumber");
|
||||
if (!model.Permissions) attributes.Remove("description");
|
||||
IEnumerable<GroupModel> groups;
|
||||
if (cn is null)
|
||||
{
|
||||
@@ -41,7 +51,19 @@ public class GroupsController : Controller
|
||||
groups = [];
|
||||
}
|
||||
}
|
||||
return 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);
|
||||
}
|
||||
}
|
||||
|
||||
[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);
|
||||
}
|
||||
}
|
||||
@@ -6,15 +6,18 @@ namespace Berufsschule_HAM.Models;
|
||||
|
||||
public class GroupModel
|
||||
{
|
||||
[JsonPropertyName("Cn")]
|
||||
public required string Cn { get; set; }
|
||||
[JsonPropertyName("DisplayName")]
|
||||
public string DisplayName { get; set; }
|
||||
[JsonPropertyName("GidNumber")]
|
||||
public string? GidNumber { get; set; }
|
||||
[JsonPropertyName("Permissions")]
|
||||
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)
|
||||
{
|
||||
@@ -22,13 +25,18 @@ public class GroupModel
|
||||
}
|
||||
else
|
||||
{
|
||||
Permissions = JsonSerializer.Deserialize<GroupPermissions>(descriptionValue)?.Permissions ?? [];
|
||||
GroupDescription? description = JsonSerializer.Deserialize<GroupDescription>(descriptionValue);
|
||||
DisplayName = description?.DisplayName ?? Cn;
|
||||
Permissions = description?.Permissions ?? [];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class GroupPermissions
|
||||
public class GroupDescription
|
||||
{
|
||||
[JsonPropertyName("DisplayName")]
|
||||
public required string DisplayName { get; set; }
|
||||
[JsonPropertyName("Permissions")]
|
||||
public required List<GroupPermission> Permissions { get; set; }
|
||||
}
|
||||
|
||||
|
||||
@@ -21,8 +21,12 @@ public class GroupsCreateRequestModel
|
||||
|
||||
public class GroupsModifyRequestModel
|
||||
{
|
||||
[JsonPropertyName("Cn")]
|
||||
public required string Cn { get; set; }
|
||||
[JsonPropertyName("NewCn")]
|
||||
public string? NewCn { get; set; } = null;
|
||||
[JsonPropertyName("GidNumber")]
|
||||
public string? GidNumber { get; set; } = null;
|
||||
public GroupPermissions? Permissions { get; set; } = null;
|
||||
[JsonPropertyName("Description")]
|
||||
public GroupDescription? Description { get; set; } = null;
|
||||
}
|
||||
@@ -1,11 +1,27 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Berufsschule_HAM.Models;
|
||||
|
||||
public class GroupsGetResponseModel(bool successful, IEnumerable<GroupModel>? groupModels, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
[JsonPropertyName("GroupModels")]
|
||||
public IEnumerable<GroupModel> GroupModels { get; set; } = groupModels ?? [];
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public class GroupsCreateResponseModel(bool successful, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public class GroupsUpdateResponseModel(bool successful, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
public string? Exception { get; set; } = exception;
|
||||
}
|
||||
|
||||
public class GroupsDeleteResponseModel(bool successful, string exception = "None")
|
||||
{
|
||||
public bool Success { get; set; } = successful;
|
||||
|
||||
@@ -45,7 +45,12 @@
|
||||
<td style="text-align: center">@(groupTableViewModel.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-warning btn-update"
|
||||
data-group-id="@groupTableViewModel.Cn"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#updateGroupModal">
|
||||
@T["Update"]
|
||||
</button>
|
||||
<button class="btn btn-sm btn-danger btn-delete"
|
||||
data-group-id="@groupTableViewModel.Cn"
|
||||
data-bs-toggle="modal"
|
||||
@@ -297,3 +302,157 @@
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
<!-- Groups Update Modal -->
|
||||
<div class="modal fade" id="updateGroupModal" tabindex="-1" aria-labelledby="updateGroupModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg modal-dialog-centered">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header bg-warning text-dark">
|
||||
<h5 class="modal-title" id="updateGroupModalLabel">@T["Update Group"]</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
|
||||
<form id="updateGroupForm">
|
||||
<div class="modal-body">
|
||||
<div class="row g-3 justify-content-center">
|
||||
<!-- Basic Info -->
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">@T["Group ID"] *</label>
|
||||
<input type="text" class="form-control" name="Cn" required />
|
||||
</div>
|
||||
<div class="col-md-6">
|
||||
<label class="form-label">@T["Display name"] *</label>
|
||||
<input type="text" class="form-control" name="DisplayName" />
|
||||
</div>
|
||||
|
||||
<div class="col-md-3">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" name="Description.Permissions.CanInventorize" id="canInventorize" />
|
||||
<label class="form-check-label" for="canInventorize">@T["Can inventorize"]</label>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" name="Description.Permissions.CanManageAssets" id="canManageAssets" />
|
||||
<label class="form-check-label" for="canManageAssets">@T["Can manage assets"]</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-3">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" name="Description.Permissions.CanManageUsers" id="canManageUsers" />
|
||||
<label class="form-check-label" for="canManageUsers">@T["Can manage users"]</label>
|
||||
</div>
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" name="Description.Permissions.CanManageGroups" id="canManageGroups" />
|
||||
<label class="form-check-label" for="canManageGroups">@T["Can manage groups"]</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-md-4">
|
||||
<div class="form-check">
|
||||
<input type="checkbox" class="form-check-input" name="Description.Permissions.CanManageLocations" id="canManageLocations" />
|
||||
<label class="form-check-label" for="canManageLocations">@T["Can manage locations"]</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">@T["Cancel"]</button>
|
||||
<button type="submit" class="btn btn-warning">@T["Save Changes"]</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
const updateButtons = document.querySelectorAll('.btn-update');
|
||||
const updateModal = document.getElementById('updateGroupModal');
|
||||
const updateForm = document.getElementById('updateGroupForm');
|
||||
|
||||
updateModal.addEventListener('show.bs.modal', async event => {
|
||||
const button = event.relatedTarget;
|
||||
const groupId = button.getAttribute('data-group-id');
|
||||
|
||||
updateForm.reset();
|
||||
|
||||
try {
|
||||
const response = await fetch(`/Groups/Get?cn=${groupId}`);
|
||||
const responseJson = await response.json();
|
||||
const groups = responseJson.GroupModels[0];
|
||||
|
||||
for (const [key, value] of Object.entries(groups)) {
|
||||
const input = updateForm.querySelector(`[name="${key}"]`);
|
||||
if (input) input.value = value;
|
||||
}
|
||||
if (groups.Permissions) {
|
||||
for (const permission of groups.Permissions)
|
||||
{
|
||||
updateForm.querySelector(`[name="Description.Permissions.${permission}"]`).checked = true;
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
showToast('Error loading group data', 'danger');
|
||||
}
|
||||
});
|
||||
|
||||
updateForm.addEventListener('submit', async e => {
|
||||
e.preventDefault();
|
||||
|
||||
const formData = new FormData(updateForm);
|
||||
const jsonData = {};
|
||||
for (const [key, value] of formData.entries()) {
|
||||
if (!value) continue;
|
||||
const keys = key.split('.');
|
||||
let target = jsonData;
|
||||
for (let i = 0; i < keys.length - 1; i++) {
|
||||
target[keys[i]] = target[keys[i]] || {};
|
||||
target = target[keys[i]];
|
||||
}
|
||||
target[keys[keys.length - 1]] = value;
|
||||
}
|
||||
let permissions = [];
|
||||
const elements = document.querySelectorAll('[name^="Description.Permissions"]');
|
||||
for (const element of elements)
|
||||
{
|
||||
if (element.checked)
|
||||
{
|
||||
permissions.push(element.name.substr(element.name.lastIndexOf(".") + 1));
|
||||
}
|
||||
}
|
||||
jsonData.Description.Permissions = permissions;
|
||||
jsonData.Description.DisplayName = jsonData.DisplayName;
|
||||
jsonData.DisplayName = null;
|
||||
try {
|
||||
const response = await fetch('/Groups/Update', {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
|
||||
body: JSON.stringify(jsonData)
|
||||
});
|
||||
const result = await response.json();
|
||||
|
||||
if (result.success) {
|
||||
bootstrap.Modal.getInstance(updateModal).hide();
|
||||
showToast('Group updated successfully', 'success');
|
||||
|
||||
// Optionally refresh the row
|
||||
const row = [...document.querySelectorAll('tr')]
|
||||
.find(r => r.querySelector(`[data-group-id="${jsonData.Cn}"]`));
|
||||
if (row) {
|
||||
row.children[0].textContent = jsonData.Description.DisplayName || '';
|
||||
row.children[1].textContent = jsonData.Description.Permissions.includes("CanInventorize") ? "☑️" : "❌" || '';
|
||||
row.children[2].textContent = jsonData.Description.Permissions.includes("CanManageUsers") ? "☑️" : "❌" || '';
|
||||
row.children[3].textContent = jsonData.Description.Permissions.includes("CanManageLocations") ? "☑️" : "❌" || '';
|
||||
row.children[4].textContent = jsonData.Description.Permissions.includes("CanManageAssets") ? "☑️" : "❌" || '';
|
||||
row.children[5].textContent = jsonData.Description.Permissions.includes("CanManageGroups") ? "☑️" : "❌" || '';
|
||||
}
|
||||
} else {
|
||||
showToast(result.reason || 'Error updating group', 'danger');
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
showToast('Error contacting server', 'danger');
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
Reference in New Issue
Block a user