Lupusregina Beta
Lupusregina Beta
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
Ahh. What would I use if not the hashset for the static data then? The tolower trim was to normalize the input, but that's gone now as I switched to using an enum for the permission nodes rather than typing them as strings. Roles are variable, users can add them. Thanks!
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
How do you kill off left padding/margin in DataGridView headers?
Of course, now that I think about it, this was a few years ago and since then I've bought a new rig; Core i7-13700K 3.4ghz, 128GB DDR4-3200, Samsung 990 Pro M.2 SSD so maybe I should try again. I can't imagine how VS could lag that, lol
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
How do you kill off left padding/margin in DataGridView headers?
I installed 20... 22? i think it was at one point and it seemed really laggy. Like intellisense would take a couple seconds to update, it was frustrating. 2017 is lightning fast, so I just went back to it. Some day I'll hit a wall where I need newer than .NET 4.8 and that will force me to upgrade, but haven't hit it yet hehe.
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
public static class Perm {
private static Dictionary<Guid, HashSet<string>> Permissions = new Dictionary<Guid, HashSet<string>>();
private static Dictionary<string, HashSet<string>> Inheritance = new Dictionary<string, HashSet<string>>();

public static void Init() {
foreach (User user in DataManager.GetUsers()) {
Permissions[user.Id] = DataManager.GetUserPermissionSet(user.Id);
}
Inheritance = DataManager.GetInheritanceSet();
}

public static bool Has(string node, Guid id = default(Guid)) {
if (string.IsNullOrEmpty(node)) return false;
node = node.ToLower().Trim();

if (id == Guid.Empty) {
if (cfg.LoggedInUser == Guid.Empty) return false;
id = cfg.LoggedInUser;
}

if (!Permissions.ContainsKey(id)) return false;
if (Permissions[id].Contains("administrator") || Permissions[id].Contains(node)) return true;
if (!Inheritance.ContainsKey(node)) return false;

return Inheritance[node].Any(x => Permissions[id].Contains(x));
}
}
public static class Perm {
private static Dictionary<Guid, HashSet<string>> Permissions = new Dictionary<Guid, HashSet<string>>();
private static Dictionary<string, HashSet<string>> Inheritance = new Dictionary<string, HashSet<string>>();

public static void Init() {
foreach (User user in DataManager.GetUsers()) {
Permissions[user.Id] = DataManager.GetUserPermissionSet(user.Id);
}
Inheritance = DataManager.GetInheritanceSet();
}

public static bool Has(string node, Guid id = default(Guid)) {
if (string.IsNullOrEmpty(node)) return false;
node = node.ToLower().Trim();

if (id == Guid.Empty) {
if (cfg.LoggedInUser == Guid.Empty) return false;
id = cfg.LoggedInUser;
}

if (!Permissions.ContainsKey(id)) return false;
if (Permissions[id].Contains("administrator") || Permissions[id].Contains(node)) return true;
if (!Inheritance.ContainsKey(node)) return false;

return Inheritance[node].Any(x => Permissions[id].Contains(x));
}
}
GetInheritanceSet returns a HashSet with all the nodes that would grant the key node, like
Dictionary<string, HashSet<string>> {
{ "game.view", new HashSet<string>() {
"game.edit",
"game.delete",
"game.launch"
}
},
{ "game.edit", new HashSet<string>() {
"game.delete"
}
}
};
Dictionary<string, HashSet<string>> {
{ "game.view", new HashSet<string>() {
"game.edit",
"game.delete",
"game.launch"
}
},
{ "game.edit", new HashSet<string>() {
"game.delete"
}
}
};
So if I do Has("Game.View") and the user doesn't explicitly have that but they do have Game.Launch, it would return true. Does that look workable?
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
I have a followup question. I'm working on implementing this. I have a static Perm class now, with a private Dictionary<Guid, HashSet<string>> Permissions. I have an Init method that populates the dict with the user's ID's then all the nodes to which they have permission. Then I have:
public static bool Has(string node, Guid id = default(Guid)) {
if (string.IsNullOrEmpty(node)) return false;
if (id == Guid.Empty) {
if (cfg.LoggedInUser == Guid.Empty) return false;
id = cfg.LoggedInUser;
}
if (!Permissions.ContainsKey(id)) return false;

return Permissions[id].Contains("administrator") || Permissions[id].Contains(node).ToLower().Trim();
}
public static bool Has(string node, Guid id = default(Guid)) {
if (string.IsNullOrEmpty(node)) return false;
if (id == Guid.Empty) {
if (cfg.LoggedInUser == Guid.Empty) return false;
id = cfg.LoggedInUser;
}
if (!Permissions.ContainsKey(id)) return false;

return Permissions[id].Contains("administrator") || Permissions[id].Contains(node).ToLower().Trim();
}
But I'd like to support inheritance. Like If I have nodes like:
Game.View
Game.Edit
Game.Delete
Game.Launch
Game.View
Game.Edit
Game.Delete
Game.Launch
If I'm checking for Game.View, then if they have edit, delete or launch it stands to reason that view is a given. What would be a good way to handle that?
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
Thanks, appreciate the advice!
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
Hmm I could keep a cache in the perm manager and when an admin changes ppls perms, flush cache and/or flush it every X minutes that may reduce load.
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
Roundtripping to MySQL that much won't be an issue? Well, it's not like I'm making some high use app anyway lol so I doubt it'd be an issue. Just curious.
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
Solution for lots of flags
So like when I do a permission check, just have a method that does like SELECT index FROM permissions WHERE uid = ?uid AND permcode = ?perm Then pass the user Guid and maybe a string like "game.delete" then if a row is returned, I know they're authorized else deny?
20 replies
CC#
Created by Lupusregina Beta on 2/26/2025 in #help
How do you kill off left padding/margin in DataGridView headers?
Yea, I'm afraid I'm old school hehe. Using VS2017 and WinForms. I know, I know.. I need to get with it already and learn WPF I just haven't gotten around to it. Thanks though!
20 replies
CC#
Created by Lupusregina Beta on 11/6/2024 in #help
Control getting resized when adding to FlowLayoutPanel
The behavior I was expecting was just for it to add my UserControl with the data populated, and not adjust the UC's width. I wasn't sure why the width would get fiddled with. As to why a FLP, I need to add a variable amount of controls to the form and have it scrollable. I wasn't sure there was a better way than a FLP?
4 replies