Skip to content

Commit a6e7865

Browse files
author
Matthew Bate
committed
Admin Tab Updates Completed. Permission Enforcement Next
1 parent f112ff8 commit a6e7865

File tree

17 files changed

+608
-54
lines changed

17 files changed

+608
-54
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using BHD_ServerManager.Classes.InstanceManagers;
2+
using HawkSyncShared;
3+
using HawkSyncShared.DTOs;
4+
using Microsoft.AspNetCore.Authorization;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace BHD_ServerManager.API.Controllers;
8+
9+
[ApiController]
10+
[Route("api/[controller]")]
11+
[Authorize]
12+
public class AdminController : ControllerBase
13+
{
14+
[HttpPost("create")]
15+
public ActionResult<AdminCommandResult> CreateUser([FromBody] CreateUserRequest request)
16+
{
17+
var result = adminInstanceManager.CreateUser(request);
18+
CommonCore.instanceAdmin!.ForceUIUpdate = true;
19+
return Ok(new AdminCommandResult
20+
{
21+
Success = result.Success,
22+
Message = result.Message
23+
});
24+
}
25+
26+
[HttpPost("update")]
27+
public ActionResult<AdminCommandResult> UpdateUser([FromBody] UpdateUserRequest request)
28+
{
29+
var result = adminInstanceManager.UpdateUser(request);
30+
CommonCore.instanceAdmin!.ForceUIUpdate = true;
31+
return Ok(new AdminCommandResult
32+
{
33+
Success = result.Success,
34+
Message = result.Message
35+
});
36+
}
37+
38+
[HttpPost("delete")]
39+
public ActionResult<AdminCommandResult> DeleteUser([FromBody] DeleteUserRequest request)
40+
{
41+
var result = adminInstanceManager.DeleteUser(request.UserID);
42+
CommonCore.instanceAdmin!.ForceUIUpdate = true;
43+
return Ok(new AdminCommandResult
44+
{
45+
Success = result.Success,
46+
Message = result.Message
47+
});
48+
}
49+
}

BHD-ServerManager/API/Controllers/PlayerController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public async Task<ActionResult<CommandResult>> BanPlayer([FromBody] BanPlayerCom
8787
// if BanIP is false
8888
if (!banIP && (playerName == string.Empty || playerName == null))
8989
{
90-
result = playerInstanceManager.BanPlayerByName(playerName, playerSlot);
90+
result = playerInstanceManager.BanPlayerByName(playerName!, playerSlot);
9191
return Ok(new CommandResult
9292
{
9393
Success = result.Success,
@@ -98,9 +98,9 @@ public async Task<ActionResult<CommandResult>> BanPlayer([FromBody] BanPlayerCom
9898
// if playerName is empty or null and BanIP is true
9999
if (banIP && (playerName == string.Empty || playerName == null))
100100
{
101-
if (IPAddress.TryParse(playerIP, out IPAddress ipAddress))
101+
if (IPAddress.TryParse(playerIP, out IPAddress? ipAddress))
102102
{
103-
result = await playerInstanceManager.BanPlayerByIPAsync(ipAddress, playerName, playerSlot);
103+
result = await playerInstanceManager.BanPlayerByIPAsync(ipAddress, playerName!, playerSlot);
104104
}
105105
else
106106
{
@@ -120,7 +120,7 @@ public async Task<ActionResult<CommandResult>> BanPlayer([FromBody] BanPlayerCom
120120
// BanIP is true and playerName is not empty or null
121121
if(banIP && !(playerName == string.Empty || playerName == null))
122122
{
123-
if (IPAddress.TryParse(playerIP, out IPAddress ipAddress))
123+
if (IPAddress.TryParse(playerIP, out IPAddress? ipAddress))
124124
{
125125
result = await playerInstanceManager.BanPlayerByBothAsync(playerName, ipAddress, playerSlot);
126126
}

BHD-ServerManager/API/Controllers/SnapshotController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public ActionResult<ServerSnapshot> GetSnapshot()
2424
CommonCore.instancePlayers ?? new(),
2525
CommonCore.instanceChat ?? new(),
2626
CommonCore.instanceBans ?? new(),
27-
CommonCore.instanceMaps ?? new()
27+
CommonCore.instanceMaps ?? new(),
28+
CommonCore.instanceAdmin ?? new()
2829
);
2930

3031
return Ok(snapshot);

BHD-ServerManager/API/Services/InstanceBroadcastService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
3636
CommonCore.instancePlayers ?? new(),
3737
CommonCore.instanceChat ?? new(),
3838
CommonCore.instanceBans ?? new(),
39-
CommonCore.instanceMaps ?? new()
39+
CommonCore.instanceMaps ?? new(),
40+
CommonCore.instanceAdmin ?? new()
4041
);
4142

4243
await _hubContext.Clients.Group("ServerUpdates")

BHD-ServerManager/API/Services/InstanceMapper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ public static ServerSnapshot CreateSnapshot(
1515
playerInstance playerList,
1616
chatInstance chat,
1717
banInstance bans,
18-
mapInstance maps)
18+
mapInstance maps,
19+
adminInstance admin)
1920
{
2021
return new ServerSnapshot
2122
{
@@ -24,6 +25,7 @@ public static ServerSnapshot CreateSnapshot(
2425
Chat = chat,
2526
Bans = bans,
2627
Maps = maps,
28+
Admins = admin,
2729
SnapshotTime = DateTime.UtcNow
2830
};
2931
}

BHD-ServerManager/Classes/InstanceManagers/adminInstanceManager.cs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ public static class adminInstanceManager
2727
// DATA TRANSFER OBJECTS (DTOs)
2828
// ================================================================================
2929

30-
/// <summary>
31-
/// Request for creating a new user
32-
/// </summary>
33-
public record CreateUserRequest(
34-
string Username,
35-
string Password,
36-
List<string> Permissions,
37-
bool IsActive,
38-
string Notes
39-
);
40-
41-
/// <summary>
42-
/// Request for updating an existing user
43-
/// </summary>
44-
public record UpdateUserRequest(
45-
int UserID,
46-
string Username,
47-
string? NewPassword, // null = don't change password
48-
List<string> Permissions,
49-
bool IsActive,
50-
string Notes
51-
);
52-
5330
// ================================================================================
5431
// CACHE MANAGEMENT
5532
// ================================================================================
@@ -653,13 +630,14 @@ public static OperationResult InitializeDefaultAdmin()
653630
}
654631

655632
// Create default admin
656-
var request = new CreateUserRequest(
657-
Username: "admin",
658-
Password: "admin", // User should change this immediately
659-
Permissions: GetDefaultPermissionsForRole("admin"),
660-
IsActive: true,
661-
Notes: "Default administrator account - CHANGE PASSWORD IMMEDIATELY"
662-
);
633+
var request = new CreateUserRequest
634+
{
635+
Username = "admin",
636+
Password = "admin",
637+
Permissions = GetDefaultPermissionsForRole("admin"),
638+
IsActive = true,
639+
Notes = "Default administrator account - CHANGE PASSWORD IMMEDIATELY"
640+
};
663641

664642
var result = CreateUser(request);
665643

BHD-ServerManager/Classes/Tickers/tickerServerManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void runTicker()
8383
thisServer.PlayersTab.tickerPlayerHook(); // Update Players Tab
8484
thisServer.ChatTab.ChatTickerHook(); // Update Chat Tab
8585
thisServer.StatsTab.StatsTickerHook(); // Update Stats Tab
86-
86+
thisServer.AdminTab.tickerAdmin_Tick(); // Update Admin Tab
8787
thisServer.MapsTab.UpdateCurrentMapHighlighting(); // Current Map Highlighting Update
8888
});
8989

BHD-ServerManager/Forms/Panels/tabAdmin.Designer.cs

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BHD-ServerManager/Forms/Panels/tabAdmin.cs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using BHD_ServerManager.Classes.SupportClasses;
55
using HawkSyncShared.DTOs;
66
using static BHD_ServerManager.Classes.InstanceManagers.adminInstanceManager;
7+
using HawkSyncShared.Instances;
78

89
namespace BHD_ServerManager.Forms.Panels;
910

@@ -13,6 +14,8 @@ public partial class tabAdmin : UserControl
1314
// FIELDS
1415
// ================================================================================
1516

17+
private adminInstance adminInstance => CommonCore.instanceAdmin!;
18+
1619
private int _selectedUserID = -1;
1720
private bool _isEditMode = false;
1821

@@ -31,10 +34,31 @@ public tabAdmin()
3134
{
3235
InitializeComponent();
3336
InitializeEvents();
37+
38+
adminInstanceManager.LoadUsersCache();
3439
LoadUserList();
40+
3541
SetFormMode(FormMode.View);
3642
}
3743

44+
public void tickerAdmin_Tick()
45+
{
46+
if(InvokeRequired)
47+
{
48+
Invoke(new Action(tickerAdmin_Tick));
49+
return;
50+
}
51+
52+
//Periodic tasks can be added here if needed
53+
if (adminInstance.ForceUIUpdate)
54+
{
55+
adminInstanceManager.LoadUsersCache();
56+
LoadUserList();
57+
adminInstance.ForceUIUpdate = false;
58+
}
59+
60+
}
61+
3862
private void InitializeEvents()
3963
{
4064
// Grid events
@@ -312,13 +336,14 @@ private void CreateUser()
312336
}
313337

314338
// Build request
315-
var request = new CreateUserRequest(
316-
Username: textBox_username.Text.Trim(),
317-
Password: textBox_password.Text,
318-
Permissions: GetSelectedPermissions(),
319-
IsActive: checkBox_userActive.Checked,
320-
Notes: textBox_userNotes.Text.Trim()
321-
);
339+
var request = new CreateUserRequest
340+
{
341+
Username = textBox_username.Text.Trim(),
342+
Password = textBox_password.Text,
343+
Permissions = GetSelectedPermissions(),
344+
IsActive = checkBox_userActive.Checked,
345+
Notes = textBox_userNotes.Text.Trim()
346+
};
322347

323348
// Call manager (cache will be auto-updated)
324349
var result = adminInstanceManager.CreateUser(request);
@@ -343,16 +368,17 @@ private void CreateUser()
343368
private void UpdateUser()
344369
{
345370
// Build request
346-
var request = new UpdateUserRequest(
347-
UserID: _selectedUserID,
348-
Username: textBox_username.Text.Trim(),
349-
NewPassword: string.IsNullOrWhiteSpace(textBox_password.Text)
371+
var request = new UpdateUserRequest
372+
{
373+
UserID = _selectedUserID,
374+
Username = textBox_username.Text.Trim(),
375+
NewPassword = string.IsNullOrWhiteSpace(textBox_password.Text)
350376
? null
351377
: textBox_password.Text,
352-
Permissions: GetSelectedPermissions(),
353-
IsActive: checkBox_userActive.Checked,
354-
Notes: textBox_userNotes.Text.Trim()
355-
);
378+
Permissions = GetSelectedPermissions(),
379+
IsActive = checkBox_userActive.Checked,
380+
Notes = textBox_userNotes.Text.Trim()
381+
};
356382

357383
// Call manager (cache will be auto-updated)
358384
var result = adminInstanceManager.UpdateUser(request);

HawkSyncShared/DTOs/AdminDTOs.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,34 @@ public record UserDTO
1616
public DateTime? LastLogin { get; init; }
1717
public string Notes { get; init; } = string.Empty;
1818
}
19+
20+
public class CreateUserRequest
21+
{
22+
public string Username { get; set; } = "";
23+
public string Password { get; set; } = "";
24+
public List<string> Permissions { get; set; } = new();
25+
public bool IsActive { get; set; }
26+
public string Notes { get; set; } = "";
27+
}
28+
29+
public class UpdateUserRequest
30+
{
31+
public int UserID { get; set; }
32+
public string Username { get; set; } = "";
33+
public string? NewPassword { get; set; }
34+
public List<string> Permissions { get; set; } = new();
35+
public bool IsActive { get; set; }
36+
public string Notes { get; set; } = "";
37+
}
38+
39+
public class DeleteUserRequest
40+
{
41+
public int UserID { get; set; }
42+
}
43+
44+
public class AdminCommandResult
45+
{
46+
public bool Success { get; set; }
47+
public string Message { get; set; } = "";
48+
}
1949
}

0 commit comments

Comments
 (0)