Changeset 426 for binary-improvements2/WebServer/src/WebAPI/AbsRestApi.cs
- Timestamp:
- Apr 24, 2023, 2:40:34 PM (19 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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
Note:
See TracChangeset
for help on using the changeset viewer.