[404] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Xml;
|
---|
| 3 |
|
---|
| 4 | namespace Webserver.Permissions {
|
---|
| 5 | public class AdminWebModules : AdminSectionAbs {
|
---|
| 6 | public static AdminWebModules Instance { get; private set; }
|
---|
| 7 |
|
---|
| 8 | public AdminWebModules (AdminTools _parent) : base (_parent, "webmodules") {
|
---|
| 9 | Instance = this;
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | private readonly Dictionary<string, WebModule> modules = new CaseInsensitiveStringDictionary<WebModule> ();
|
---|
| 13 |
|
---|
| 14 |
|
---|
| 15 | #region IO
|
---|
| 16 |
|
---|
| 17 | public override void Clear () {
|
---|
| 18 | modules.Clear ();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | protected override void ParseElement (XmlElement _childElement) {
|
---|
| 22 | if (WebModule.TryParse (_childElement, out WebModule webModule)) {
|
---|
| 23 | modules [webModule.Name] = webModule;
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | public override void Save (XmlElement _root) {
|
---|
| 28 | XmlElement modulesElement = _root.AddXmlElement (SectionTypeName);
|
---|
| 29 | // modulesElement.AddXmlComment (" <module name=\"adminuser1\" secret=\"supersecrettoken\" permission_level=\"0\" /> ");
|
---|
| 30 |
|
---|
| 31 | foreach ((string _, WebModule module) in modules) {
|
---|
| 32 | module.ToXml (modulesElement);
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | #endregion
|
---|
| 37 |
|
---|
| 38 |
|
---|
| 39 | #region Runtime interaction
|
---|
| 40 |
|
---|
| 41 |
|
---|
| 42 | public void AddModule (string _module, int _permissionLevel) {
|
---|
| 43 | WebModule p = new WebModule (_module, _permissionLevel);
|
---|
| 44 | lock (this) {
|
---|
| 45 | allModulesList.Clear ();
|
---|
| 46 |
|
---|
| 47 | modules [_module] = p;
|
---|
| 48 | Parent.Save ();
|
---|
| 49 | }
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public bool RemoveModule (string _module) {
|
---|
| 53 | lock (this) {
|
---|
| 54 | allModulesList.Clear ();
|
---|
| 55 |
|
---|
| 56 | bool removed = modules.Remove (_module);
|
---|
| 57 | if (removed) {
|
---|
| 58 | Parent.Save ();
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return removed;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public List<WebModule> GetModules () {
|
---|
| 66 | lock (this) {
|
---|
| 67 | if (allModulesList.Count != 0) {
|
---|
| 68 | return allModulesList;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | foreach ((string moduleName, WebModule moduleDefaultPerm) in knownModules) {
|
---|
| 72 | allModulesList.Add (modules.TryGetValue (moduleName, out WebModule modulePermission)
|
---|
| 73 | ? modulePermission
|
---|
| 74 | : moduleDefaultPerm);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | return allModulesList;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | #endregion
|
---|
| 82 |
|
---|
| 83 | public readonly struct WebModule {
|
---|
| 84 | public readonly string Name;
|
---|
| 85 | public readonly int PermissionLevel;
|
---|
| 86 |
|
---|
| 87 | public WebModule (string _name, int _permissionLevel) {
|
---|
| 88 | Name = _name;
|
---|
| 89 | PermissionLevel = _permissionLevel;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | public void ToXml (XmlElement _parent) {
|
---|
| 93 | _parent.AddXmlElement ("module")
|
---|
| 94 | .SetAttrib ("name", Name)
|
---|
| 95 | .SetAttrib ("permission_level", PermissionLevel.ToString ());
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | public static bool TryParse (XmlElement _element, out WebModule _result) {
|
---|
| 99 | _result = default;
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | if (!_element.TryGetAttribute ("name", out string name)) {
|
---|
| 103 | Log.Warning ($"[Web] [Perms] Ignoring module-entry because of missing 'name' attribute: {_element.OuterXml}");
|
---|
| 104 | return false;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | if (!_element.TryGetAttribute ("permission_level", out string permissionLevelString)) {
|
---|
| 108 | Log.Warning ($"[Web] [Perms] Ignoring module-entry because of missing 'permission_level' attribute: {_element.OuterXml}");
|
---|
| 109 | return false;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | if (!int.TryParse (permissionLevelString, out int permissionLevel)) {
|
---|
| 113 | Log.Warning (
|
---|
| 114 | $"[Web] [Perms] Ignoring module-entry because of invalid (non-numeric) value for 'permission_level' attribute: {_element.OuterXml}");
|
---|
| 115 | return false;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | _result = new WebModule (name, permissionLevel);
|
---|
| 119 | return true;
|
---|
| 120 | }
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 |
|
---|
| 124 | #region Specials
|
---|
| 125 |
|
---|
| 126 | /// <summary>
|
---|
| 127 | /// Contains all registered modules and their default permission
|
---|
| 128 | /// </summary>
|
---|
| 129 | private readonly Dictionary<string, WebModule> knownModules = new CaseInsensitiveStringDictionary<WebModule> ();
|
---|
| 130 |
|
---|
| 131 | /// <summary>
|
---|
| 132 | /// Public list of all modules, both those with custom permissions as well as those that do not with their default permission
|
---|
| 133 | /// </summary>
|
---|
| 134 | private readonly List<WebModule> allModulesList = new List<WebModule> ();
|
---|
| 135 |
|
---|
| 136 | public void AddKnownModule (string _module, int _defaultPermission) {
|
---|
| 137 | if (string.IsNullOrEmpty (_module)) {
|
---|
| 138 | return;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | WebModule p = new WebModule (_module, _defaultPermission);
|
---|
| 142 |
|
---|
| 143 | lock (this) {
|
---|
| 144 | allModulesList.Clear ();
|
---|
| 145 | knownModules [_module] = p;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | public bool IsKnownModule (string _module) {
|
---|
| 150 | if (string.IsNullOrEmpty (_module)) {
|
---|
| 151 | return false;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | lock (this) {
|
---|
| 155 | return knownModules.ContainsKey (_module);
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public bool ModuleAllowedWithLevel (string _module, int _level) {
|
---|
| 160 | WebModule permInfo = GetModule (_module);
|
---|
| 161 | return permInfo.PermissionLevel >= _level;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | public WebModule GetModule (string _module) {
|
---|
| 165 | if (modules.TryGetValue (_module, out WebModule result)) {
|
---|
| 166 | return result;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | return knownModules.TryGetValue (_module, out result) ? result : defaultModulePermission;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | private readonly WebModule defaultModulePermission = new WebModule ("", 0);
|
---|
| 173 |
|
---|
| 174 | #endregion
|
---|
| 175 | }
|
---|
| 176 | }
|
---|