Changeset 487 for TFP-WebServer/WebServer/src/Permissions
- Timestamp:
- Jun 17, 2024, 5:25:43 PM (6 months ago)
- Location:
- TFP-WebServer/WebServer/src/Permissions
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
TFP-WebServer/WebServer/src/Permissions/AdminApiTokens.cs
r474 r487 19 19 } 20 20 21 p rotectedoverride void ParseElement (XmlElement _childElement) {21 public override void ParseElement (XmlElement _childElement) { 22 22 if (ApiToken.TryParse (_childElement, out ApiToken apiToken)) { 23 23 tokens [apiToken.Name] = apiToken; -
TFP-WebServer/WebServer/src/Permissions/AdminWebModules.cs
r474 r487 1 using System; 1 2 using System.Collections.Generic; 2 3 using System.Xml; 4 using JetBrains.Annotations; 5 using UnityEngine; 3 6 4 7 namespace Webserver.Permissions { … … 16 19 17 20 public override void Clear () { 21 allModulesList.Clear (); 18 22 modules.Clear (); 19 23 } 20 24 21 protected override void ParseElement (XmlElement _childElement) { 22 if (WebModule.TryParse (_childElement, out WebModule webModule)) { 23 modules [webModule.Name] = webModule; 24 } 25 public override void ParseElement (XmlElement _childElement) { 26 allModulesList.Clear (); 27 28 if (!WebModule.TryParse (_childElement, out WebModule webModule)) { 29 return; 30 } 31 32 if (knownModules.TryGetValue (webModule.Name, out WebModule knownModule)) { 33 webModule = webModule.FixPermissionLevelsFromKnownModule (knownModule); 34 } 35 36 modules [webModule.Name] = webModule; 25 37 } 26 38 … … 43 55 lock (Parent) { 44 56 allModulesList.Clear (); 45 57 46 58 modules [_module.Name] = _module; 47 59 Parent.Save (); … … 80 92 #endregion 81 93 82 public struct WebModule {83 public string Name;84 public int LevelGlobal;85 public int[] LevelPerMethod;86 public bool IsDefault;87 88 public WebModule (string _name, int _level, bool _isDefault = false) {94 public readonly struct WebModule { 95 public readonly string Name; 96 public readonly int LevelGlobal; 97 public readonly int[] LevelPerMethod; 98 public readonly bool IsDefault; 99 100 public WebModule (string _name, int _level, bool _isDefault) { 89 101 LevelPerMethod = null; 90 102 … … 94 106 } 95 107 96 public WebModule (string _name, int _levelGlobal, int[] _levelPerMethod, bool _isDefault = false) {97 if (_levelPerMethod == null ||_levelPerMethod.Length != (int)ERequestMethod.Count) {108 public WebModule (string _name, int _levelGlobal, int[] _levelPerMethod, bool _isDefault) { 109 if (_levelPerMethod != null && _levelPerMethod.Length != (int)ERequestMethod.Count) { 98 110 LevelPerMethod = createDefaultPerMethodArray (); 99 111 … … 205 217 } 206 218 207 _result = perMethodLevels != null ? new WebModule (name, permissionLevel, perMethodLevels) : new WebModule (name, permissionLevel);219 _result = new WebModule (name, permissionLevel, perMethodLevels, false); 208 220 209 221 return true; 210 222 } 211 223 224 [MustUseReturnValue] 225 public WebModule SetLevelGlobal (int _level) { 226 int[] perMethodClone = LevelPerMethod == null ? null : new int[LevelPerMethod.Length]; 227 if (perMethodClone != null) { 228 Array.Copy (LevelPerMethod, perMethodClone, perMethodClone.Length); 229 } 230 231 return new WebModule (Name, _level, perMethodClone, false); 232 } 233 234 [MustUseReturnValue] 235 public WebModule SetLevelForMethod (ERequestMethod _method, int _level) { 236 int[] perMethodClone = createDefaultPerMethodArray (); 237 if (LevelPerMethod != null) { 238 Array.Copy (LevelPerMethod, perMethodClone, perMethodClone.Length); 239 } 240 241 perMethodClone[(int)_method] = _level; 242 243 return new WebModule (Name, LevelGlobal, perMethodClone, false); 244 } 245 212 246 private static int[] createDefaultPerMethodArray () { 213 247 int[] result = new int[(int)ERequestMethod.Count]; … … 219 253 return result; 220 254 } 255 256 [MustUseReturnValue] 257 public WebModule FixPermissionLevelsFromKnownModule (WebModule _knownModule) { 258 if (_knownModule.LevelPerMethod == null) { 259 if (LevelPerMethod != null) { 260 return new WebModule (Name, LevelGlobal, false); 261 } 262 return this; 263 } 264 265 WebModule result = this; 266 for (int i = 0; i < _knownModule.LevelPerMethod.Length; i++) { 267 if (result.LevelPerMethod == null || result.LevelPerMethod[i] == MethodLevelNotSupported) { 268 result = result.SetLevelForMethod ((ERequestMethod)i, _knownModule.LevelPerMethod[i]); 269 } 270 } 271 272 return result; 273 } 221 274 } 222 275 … … 253 306 254 307 public void AddKnownModule (WebModule _module) { 308 if (!_module.IsDefault) { 309 Log.Warning ($"Call to AddKnownModule with IsDefault==false! From:\n{StackTraceUtility.ExtractStackTrace()}"); 310 } 311 255 312 if (string.IsNullOrEmpty (_module.Name)) { 256 313 return; 257 314 } 258 315 259 _module.IsDefault = true;260 261 316 lock (Parent) { 262 317 allModulesList.Clear (); 263 318 knownModules [_module.Name] = _module; 319 320 if (modules.TryGetValue (_module.Name, out WebModule overrideModule)) { 321 overrideModule = overrideModule.FixPermissionLevelsFromKnownModule (_module); 322 modules[_module.Name] = overrideModule; 323 } 264 324 } 265 325 } -
TFP-WebServer/WebServer/src/Permissions/AdminWebUsers.cs
r485 r487 21 21 } 22 22 23 p rotectedoverride void ParseElement (XmlElement _childElement) {23 public override void ParseElement (XmlElement _childElement) { 24 24 if (WebUser.TryParse (_childElement, out WebUser webUser)) { 25 25 users [webUser.Name] = webUser;
Note:
See TracChangeset
for help on using the changeset viewer.