- Timestamp:
- Apr 24, 2023, 2:40:34 PM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/Permissions/AdminWebModules.cs
r418 r426 1 1 using System.Collections.Generic; 2 2 using System.Xml; 3 using UnityEngine; 3 4 4 5 namespace Webserver.Permissions { … … 40 41 41 42 42 public void AddModule (string _module, int _permissionLevel) { 43 WebModule p = new WebModule (_module, _permissionLevel, false); 43 public void AddModule (WebModule _module) { 44 44 lock (this) { 45 45 allModulesList.Clear (); 46 46 47 modules [_module ] = p;47 modules [_module.Name] = _module; 48 48 Parent.Save (); 49 49 } … … 80 80 81 81 #endregion 82 83 public readonly struct WebModule { 84 public readonly string Name; 85 public readonly int PermissionLevel; 86 public readonly bool IsDefault; 87 88 public WebModule (string _name, int _permissionLevel, bool _isDefault) { 82 83 public struct WebModule { 84 public string Name; 85 public int LevelGlobal; 86 public int[] LevelPerMethod; 87 public bool IsDefault; 88 89 public WebModule (string _name, int _level, bool _isDefault = false) { 90 LevelPerMethod = null; 91 89 92 Name = _name; 90 PermissionLevel = _permissionLevel; 93 LevelGlobal = _level; 94 IsDefault = _isDefault; 95 } 96 97 public WebModule (string _name, int _levelGlobal, int[] _levelPerMethod, bool _isDefault = false) { 98 if (_levelPerMethod == null || _levelPerMethod.Length != (int)ERequestMethod.Count) { 99 LevelPerMethod = createDefaultPerMethodArray (); 100 101 for (int i = 0; i < (int)ERequestMethod.Count; i++) { 102 if (_levelPerMethod != null && i < _levelPerMethod.Length) { 103 LevelPerMethod [i] = _levelPerMethod [i]; 104 } 105 } 106 } else { 107 LevelPerMethod = _levelPerMethod; 108 } 109 110 Name = _name; 111 LevelGlobal = _levelGlobal; 91 112 IsDefault = _isDefault; 92 113 } 93 114 94 115 public void ToXml (XmlElement _parent) { 95 _parent.AddXmlElement ("module") 96 .SetAttrib ("name", Name) 97 .SetAttrib ("permission_level", PermissionLevel.ToString ()); 116 bool hasPerMethodLevels = LevelPerMethod != null; 117 118 XmlElement permissionElement = _parent.AddXmlElement ("module") 119 .SetAttrib ("name", Name) 120 .SetAttrib ("permission_level", LevelGlobal.ToString ()); 121 122 if (!hasPerMethodLevels) { 123 return; 124 } 125 126 for (int i = 0; i < LevelPerMethod.Length; i++) { 127 ERequestMethod method = (ERequestMethod)i; 128 int level = LevelPerMethod [i]; 129 130 if (level == MethodLevelNotSupported) { 131 continue; 132 } 133 134 permissionElement.AddXmlElement ("method") 135 .SetAttrib ("name", method.ToStringCached ()) 136 .SetAttrib ("permission_level", level.ToString ()); 137 } 98 138 } 99 139 … … 117 157 return false; 118 158 } 119 120 _result = new WebModule (name, permissionLevel, false); 159 160 int[] perMethodLevels = null; 161 162 foreach (XmlNode child in _element.ChildNodes) { 163 if (child.NodeType != XmlNodeType.Element) { 164 continue; 165 } 166 167 XmlElement childElem = (XmlElement)child; 168 if (childElem.Name != "method") { 169 Log.Warning ($"[Web] [Perms] Ignoring module child element, invalid element name: {childElem.OuterXml}"); 170 continue; 171 } 172 173 if (!childElem.TryGetAttribute ("name", out string methodName)) { 174 Log.Warning ($"[Web] [Perms] Ignoring module child element, missing 'name' attribute: {childElem.OuterXml}"); 175 continue; 176 } 177 178 if (!EnumUtils.TryParse (methodName, out ERequestMethod method, true)) { 179 Log.Warning ( 180 $"[Web] [Perms] Ignoring module child element, unknown method name in 'name' attribute: {childElem.OuterXml}"); 181 continue; 182 } 183 184 if (method >= ERequestMethod.Count) { 185 Log.Warning ( 186 $"[Web] [Perms] Ignoring module child element, invalid method name in 'name' attribute: {childElem.OuterXml}"); 187 continue; 188 } 189 190 if (!childElem.TryGetAttribute ("permission_level", out permissionLevelString)) { 191 Log.Warning ($"[Web] [Perms] Ignoring module child element, missing 'permission_level' attribute: {childElem.OuterXml}"); 192 continue; 193 } 194 195 if (!int.TryParse (permissionLevelString, out int methodPermissionLevel)) { 196 Log.Warning ( 197 $"[Web] [Perms] Ignoring module child element, invalid (non-numeric) value for 'permission_level' attribute: {childElem.OuterXml}"); 198 continue; 199 } 200 201 perMethodLevels ??= createDefaultPerMethodArray (); 202 perMethodLevels [(int)method] = methodPermissionLevel; 203 } 204 205 _result = perMethodLevels != null ? new WebModule (name, permissionLevel, perMethodLevels) : new WebModule (name, permissionLevel); 206 121 207 return true; 122 208 } 209 210 private static int[] createDefaultPerMethodArray () { 211 int[] result = new int[(int)ERequestMethod.Count]; 212 213 for (int i = 0; i < (int)ERequestMethod.Count; i++) { 214 result [i] = MethodLevelNotSupported; 215 } 216 217 return result; 218 } 123 219 } 124 220 … … 127 223 128 224 /// <summary> 225 /// Use global (API based) permission level for the method 226 /// </summary> 227 public const int MethodLevelInheritGlobal = int.MinValue; 228 229 /// <summary> 230 /// Method not supported 231 /// </summary> 232 public const int MethodLevelNotSupported = int.MinValue + 1; 233 234 public const int PermissionLevelUser = 1000; 235 public const int PermissionLevelGuest = 2000; 236 237 /// <summary> 129 238 /// Contains all registered modules and their default permission 130 239 /// </summary> … … 136 245 private readonly List<WebModule> allModulesList = new List<WebModule> (); 137 246 138 public void AddKnownModule ( string _module, int _defaultPermission) {139 if (string.IsNullOrEmpty (_module )) {247 public void AddKnownModule (WebModule _module) { 248 if (string.IsNullOrEmpty (_module.Name)) { 140 249 return; 141 250 } 142 251 143 WebModule p = new WebModule (_module, _defaultPermission, true);144 252 _module.IsDefault = true; 253 145 254 lock (this) { 146 255 allModulesList.Clear (); 147 knownModules [_module ] = p;256 knownModules [_module.Name] = _module; 148 257 } 149 258 } … … 160 269 161 270 public bool ModuleAllowedWithLevel (string _module, int _level) { 162 WebModule permInfo = GetModule (_module)!.Value; 163 return permInfo.PermissionLevel >= _level; 164 } 165 166 public WebModule? GetModule (string _module, bool _returnDefaults = true) { 271 return GetModule (_module).LevelGlobal >= _level; 272 } 273 274 public WebModule GetModule (string _module) { 167 275 if (modules.TryGetValue (_module, out WebModule result)) { 168 276 return result; 169 }170 171 if (!_returnDefaults) {172 return null;173 277 } 174 278
Note:
See TracChangeset
for help on using the changeset viewer.