Changeset 426 for binary-improvements2/WebServer/src/WebAPI
- Timestamp:
- Apr 24, 2023, 2:40:34 PM (19 months ago)
- Location:
- binary-improvements2/WebServer/src/WebAPI
- Files:
-
- 8 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebAPI/APIs/Command.cs
r408 r426 3 3 using JetBrains.Annotations; 4 4 using Utf8Json; 5 using Webserver.Permissions; 5 6 6 7 namespace Webserver.WebAPI.APIs { … … 130 131 } 131 132 132 public override int DefaultPermissionLevel () => 2000;133 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; 133 134 } 134 135 } -
binary-improvements2/WebServer/src/WebAPI/APIs/Mods.cs
r425 r426 1 1 using JetBrains.Annotations; 2 2 using Utf8Json; 3 using Webserver.Permissions; 3 4 4 5 namespace Webserver.WebAPI.APIs { 5 6 [UsedImplicitly] 6 public class WebMods : AbsRestApi {7 public class Mods : AbsRestApi { 7 8 private readonly byte[] loadedWebMods; 8 9 9 public WebMods (Web _parent) {10 public Mods (Web _parent) { 10 11 JsonWriter writer = new JsonWriter (); 11 12 writer.WriteBeginArray (); … … 13 14 for (int i = 0; i < _parent.webMods.Count; i++) { 14 15 WebMod webMod = _parent.webMods [i]; 15 16 16 17 if (i > 0) { 17 18 writer.WriteValueSeparator (); 18 19 } 19 20 20 21 writer.WriteBeginObject (); 21 22 22 writer.WriteString ("name"); 23 writer.WriteNameSeparator (); 24 writer.WriteString (webMod.ParentMod.Name); 23 writeModJson (ref writer, webMod); 25 24 26 string webModReactBundle = webMod.ReactBundle; 27 if (webModReactBundle != null) { 25 if (webMod.ReactBundle != null || webMod.CssPath != null) { 28 26 writer.WriteValueSeparator (); 29 writer.WriteString ("bundle");30 writer.WriteNameSeparator ();31 writer.WriteString (webModReactBundle);32 }33 27 34 string webModCssFile = webMod.CssPath; 35 if (webModCssFile != null) { 36 writer.WriteValueSeparator (); 37 writer.WriteString ("css"); 38 writer.WriteNameSeparator (); 39 writer.WriteString (webModCssFile); 28 writer.WritePropertyName ("web"); 29 writer.WriteBeginObject (); 30 31 string webModReactBundle = webMod.ReactBundle; 32 if (webModReactBundle != null) { 33 writer.WritePropertyName ("bundle"); 34 writer.WriteString (webModReactBundle); 35 } 36 37 string webModCssFile = webMod.CssPath; 38 if (webModCssFile != null) { 39 if (webModReactBundle != null) { 40 writer.WriteValueSeparator (); 41 } 42 43 writer.WritePropertyName ("css"); 44 writer.WriteString (webModCssFile); 45 } 46 47 writer.WriteEndObject (); 40 48 } 41 49 … … 54 62 } 55 63 56 public override int DefaultPermissionLevel () => 2000; 64 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; 65 66 private void writeModJson (ref JsonWriter _writer, WebMod _webMod) { 67 _writer.WritePropertyName ("name"); 68 _writer.WriteString (_webMod.ParentMod.Name); 69 70 _writer.WriteValueSeparator (); 71 _writer.WritePropertyName ("displayName"); 72 JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.DisplayName); 73 74 _writer.WriteValueSeparator (); 75 _writer.WritePropertyName ("description"); 76 JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.Description); 77 78 _writer.WriteValueSeparator (); 79 _writer.WritePropertyName ("author"); 80 JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.Author); 81 82 _writer.WriteValueSeparator (); 83 _writer.WritePropertyName ("version"); 84 JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.VersionString); 85 86 _writer.WriteValueSeparator (); 87 _writer.WritePropertyName ("website"); 88 JsonCommons.WriteStringOrNull (ref _writer, _webMod.ParentMod.Website); 89 } 57 90 } 58 91 } -
binary-improvements2/WebServer/src/WebAPI/APIs/Player.cs
r425 r426 256 256 } 257 257 258 public override int DefaultPermissionLevel () => 2000;258 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; 259 259 } 260 260 } -
binary-improvements2/WebServer/src/WebAPI/APIs/RegisterUser.cs
r416 r426 80 80 } 81 81 82 // TODO: Check if username is already used! 82 // TODO: Check if username is already used by someone else! 83 // TODO: Remove existing username if player already had one! 83 84 84 85 AdminWebUsers.Instance.AddUser (username, password, regData.PlatformUserId, regData.CrossPlatformUserId); … … 95 96 } 96 97 97 public override int DefaultPermissionLevel () => 2000;98 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; 98 99 } 99 100 } -
binary-improvements2/WebServer/src/WebAPI/APIs/ServerStats.cs
r408 r426 2 2 using Utf8Json; 3 3 using Webserver.LiveData; 4 using Webserver.Permissions; 4 5 5 6 namespace Webserver.WebAPI.APIs { … … 47 48 } 48 49 49 public override int DefaultPermissionLevel () => 2000;50 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; 50 51 } 51 52 } -
binary-improvements2/WebServer/src/WebAPI/APIs/WebUiUpdates.cs
r408 r426 2 2 using Utf8Json; 3 3 using Webserver.LiveData; 4 using Webserver.Permissions; 4 5 5 6 namespace Webserver.WebAPI.APIs { … … 57 58 } 58 59 59 public override int DefaultPermissionLevel () => 2000;60 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest; 60 61 } 61 62 } -
binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
r418 r426 10 10 private static readonly UnityEngine.Profiling.CustomSampler jsonDeserializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Deserialize"); 11 11 12 protected readonly string[] CachedPerMethodModuleNames = new string[(int)ERequestMethod.Count];13 14 12 protected AbsRestApi (string _name = null) : this(null, _name) { 15 13 } … … 19 17 20 18 protected override void RegisterPermissions () { 21 base.RegisterPermissions (); 22 23 for (int i = 0; i < (int)ERequestMethod.Count; i++) { 24 ERequestMethod method = (ERequestMethod)i; 25 26 if (method is not (ERequestMethod.GET or ERequestMethod.PUT or ERequestMethod.POST or ERequestMethod.DELETE)) { 27 continue; 28 } 29 30 CachedPerMethodModuleNames [i] = $"webapi.{Name}:{method.ToStringCached ()}"; 31 AdminWebModules.Instance.AddKnownModule (CachedPerMethodModuleNames [i], DefaultMethodPermissionLevel (method)); 32 } 19 AdminWebModules.Instance.AddKnownModule (new AdminWebModules.WebModule (CachedApiModuleName, DefaultPermissionLevel (), 20 DefaultMethodPermissionLevels (), true)); 33 21 } 34 22 … … 136 124 137 125 public override bool Authorized (RequestContext _context) { 138 return ActiveMethodPermissionLevel (_context.Method) >= _context.PermissionLevel; 126 AdminWebModules.WebModule module = AdminWebModules.Instance.GetModule (CachedApiModuleName); 127 128 if (module.LevelPerMethod != null) { 129 int perMethodLevel = module.LevelPerMethod [(int)_context.Method]; 130 if (perMethodLevel == AdminWebModules.MethodLevelNotSupported) { 131 return false; 132 } 133 134 if (perMethodLevel != AdminWebModules.MethodLevelInheritGlobal) { 135 return perMethodLevel >= _context.PermissionLevel; 136 } 137 } 138 139 return module.LevelGlobal >= _context.PermissionLevel; 139 140 } 140 141 141 142 /// <summary> 142 /// Define default permission levels per HTTP method 143 /// Define default permission levels per HTTP method as an array of levels. See <see cref="ERequestMethod"/> for the order of entries. 143 144 /// </summary> 144 /// <param name="_method">HTTP method to return the default value for</param> 145 /// <returns>Default permission level for the given HTTP method. A value of int.MinValue means no per-method default, use per-API default</returns> 146 public virtual int DefaultMethodPermissionLevel (ERequestMethod _method) => int.MinValue; 147 148 public virtual int ActiveMethodPermissionLevel (ERequestMethod _method) { 149 string methodApiModuleName = CachedPerMethodModuleNames [(int)_method]; 150 151 if (methodApiModuleName == null) { 152 return 0; 153 } 154 155 AdminWebModules.WebModule? overrideModule = AdminWebModules.Instance.GetModule (methodApiModuleName, false); 156 if (overrideModule.HasValue) { 157 return overrideModule.Value.PermissionLevel; 158 } 159 160 overrideModule = AdminWebModules.Instance.GetModule (CachedApiModuleName, false); 161 if (overrideModule.HasValue) { 162 return overrideModule.Value.PermissionLevel; 163 } 164 165 int defaultMethodPermissionLevel = DefaultMethodPermissionLevel (_method); 166 // ReSharper disable once ConvertIfStatementToReturnStatement 167 if (defaultMethodPermissionLevel != int.MinValue) { 168 return defaultMethodPermissionLevel; 169 } 170 171 return DefaultPermissionLevel (); 172 } 145 /// <returns>Default permission levels for supported methods. See <see cref="AdminWebModules.MethodLevelNotSupported"/> and <see cref="AdminWebModules.MethodLevelInheritGlobal"/>.</returns> 146 public virtual int[] DefaultMethodPermissionLevels () => new[] { 147 AdminWebModules.MethodLevelNotSupported, 148 AdminWebModules.MethodLevelInheritGlobal, 149 AdminWebModules.MethodLevelInheritGlobal, 150 AdminWebModules.MethodLevelInheritGlobal, 151 AdminWebModules.MethodLevelInheritGlobal 152 }; 173 153 174 154 #region Helpers -
binary-improvements2/WebServer/src/WebAPI/AbsWebAPI.cs
r418 r426 19 19 20 20 protected virtual void RegisterPermissions () { 21 AdminWebModules.Instance.AddKnownModule ( $"webapi.{Name}", DefaultPermissionLevel ());21 AdminWebModules.Instance.AddKnownModule (new AdminWebModules.WebModule(CachedApiModuleName, DefaultPermissionLevel (), true)); 22 22 } 23 23 … … 25 25 26 26 public virtual bool Authorized (RequestContext _context) { 27 return AdminWebModules.Instance. ModuleAllowedWithLevel (CachedApiModuleName, _context.PermissionLevel);27 return AdminWebModules.Instance.GetModule (CachedApiModuleName).LevelGlobal >= _context.PermissionLevel; 28 28 } 29 29 -
binary-improvements2/WebServer/src/WebAPI/JsonCommons.cs
r425 r426 60 60 _writer.WriteString (_dateTime.ToString ("o")); 61 61 } 62 63 public static void WriteStringOrNull (ref JsonWriter _writer, string _string) { 64 if (_string == null) { 65 _writer.WriteNull (); 66 } else { 67 _writer.WriteString (_string); 68 } 69 } 62 70 } 63 71 }
Note:
See TracChangeset
for help on using the changeset viewer.