added "UserPermissionTesting_CS_WinForms" project
This commit is contained in:
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace UserPermissionTest_CS_WinForms
|
||||
{
|
||||
public partial class MainForm : Form
|
||||
{
|
||||
public MainForm()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void MainForm_Load(object sender, EventArgs e)
|
||||
{
|
||||
// Subscribe to session state change notifications
|
||||
SessionManager.SessionStateChanged += UpdateSessionUi;
|
||||
|
||||
// Trigger initial UI update
|
||||
UpdateSessionUi();
|
||||
}
|
||||
|
||||
private void UpdateSessionUi()
|
||||
{
|
||||
if (this.InvokeRequired)
|
||||
{
|
||||
this.Invoke(new Action(UpdateSessionUi));
|
||||
return;
|
||||
}
|
||||
|
||||
var user = SessionManager.CurrentUser;
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
// Signed Out State
|
||||
lblStatusBadge.Text = "SIGNED OUT";
|
||||
lblStatusBadge.BackColor = Color.FromArgb(239, 68, 68); // Red
|
||||
lblUsernameValue.Text = "Not Logged In";
|
||||
lblUsernameValue.ForeColor = Color.FromArgb(100, 116, 139); // Slate-400
|
||||
lblFullNameValue.Text = "-";
|
||||
|
||||
lstUserPermissions.Items.Clear();
|
||||
lstUserPermissions.Items.Add("(Sign in to view permissions)");
|
||||
|
||||
btnLogin.Enabled = true;
|
||||
btnLogout.Enabled = false;
|
||||
|
||||
// Authorization: Lock directory when not logged in
|
||||
btnUsers.Enabled = false;
|
||||
btnUsers.Text = "🔒 Manage Users";
|
||||
btnUsers.BackColor = Color.FromArgb(241, 245, 249);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Signed In State
|
||||
lblStatusBadge.Text = "SIGNED IN";
|
||||
lblStatusBadge.BackColor = Color.FromArgb(16, 185, 129); // Green
|
||||
lblUsernameValue.Text = user.Username;
|
||||
lblUsernameValue.ForeColor = Color.FromArgb(15, 23, 42); // Navy-900
|
||||
lblFullNameValue.Text = user.FullName;
|
||||
|
||||
lstUserPermissions.Items.Clear();
|
||||
if (user.Permissions.Count == 0)
|
||||
{
|
||||
lstUserPermissions.Items.Add("(No permissions assigned)");
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var permission in user.Permissions)
|
||||
{
|
||||
lstUserPermissions.Items.Add("✓ " + permission);
|
||||
}
|
||||
}
|
||||
|
||||
btnLogin.Enabled = false;
|
||||
btnLogout.Enabled = true;
|
||||
|
||||
// Authorization: Check if user has 'Manage Users' or 'Full Control'
|
||||
bool hasAccess = user.Permissions.Contains("Manage Users") || user.Permissions.Contains("Full Control");
|
||||
if (hasAccess)
|
||||
{
|
||||
btnUsers.Enabled = true;
|
||||
btnUsers.Text = "👥 Manage Users";
|
||||
btnUsers.BackColor = Color.FromArgb(241, 245, 249);
|
||||
}
|
||||
else
|
||||
{
|
||||
btnUsers.Enabled = false;
|
||||
btnUsers.Text = "🔒 Manage Users (Locked)";
|
||||
btnUsers.BackColor = Color.FromArgb(241, 245, 249);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void btnLogin_Click(object sender, EventArgs e)
|
||||
{
|
||||
using (var loginDialog = new LoginDialog())
|
||||
{
|
||||
if (loginDialog.ShowDialog(this) == DialogResult.OK)
|
||||
{
|
||||
MessageBox.Show(
|
||||
$"Welcome back, {SessionManager.CurrentUser?.FullName}!",
|
||||
"Sign In Successful",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Information);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void btnLogout_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (SessionManager.CurrentUser != null)
|
||||
{
|
||||
string username = SessionManager.CurrentUser.Username;
|
||||
SessionManager.Logout();
|
||||
MessageBox.Show(
|
||||
$"User '{username}' has been successfully logged out.",
|
||||
"Signed Out",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Information);
|
||||
}
|
||||
}
|
||||
|
||||
private void btnUsers_Click(object sender, EventArgs e)
|
||||
{
|
||||
var user = SessionManager.CurrentUser;
|
||||
if (user == null || (!user.Permissions.Contains("Manage Users") && !user.Permissions.Contains("Full Control")))
|
||||
{
|
||||
MessageBox.Show(
|
||||
"Security Exception: You do not possess the required credentials ('Manage Users' or 'Full Control') to access directory configuration.",
|
||||
"Access Denied",
|
||||
MessageBoxButtons.OK,
|
||||
MessageBoxIcon.Stop);
|
||||
return;
|
||||
}
|
||||
|
||||
using (var userSettings = new UserSettings())
|
||||
{
|
||||
userSettings.ShowDialog(this);
|
||||
}
|
||||
|
||||
// After closing settings, refresh the session UI (in case the current user's profile was changed)
|
||||
UpdateSessionUi();
|
||||
}
|
||||
|
||||
protected override void OnFormClosing(FormClosingEventArgs e)
|
||||
{
|
||||
// Unsubscribe to prevent reference leaks
|
||||
SessionManager.SessionStateChanged -= UpdateSessionUi;
|
||||
base.OnFormClosing(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user