Ignore:
Timestamp:
Feb 27, 2023, 9:40:12 PM (21 months ago)
Author:
alloc
Message:

Refactored API authorization to support per-HTTP-method permission levels

File:
1 edited

Legend:

Unmodified
Added
Removed
  • binary-improvements2/WebServer/src/WebAPI/AbsWebAPI.cs

    r410 r418  
     1using Webserver.Permissions;
     2
    13namespace Webserver.WebAPI {
    24        public abstract class AbsWebAPI {
    35                public readonly string Name;
    46                protected readonly Web ParentWeb;
     7
     8                protected readonly string CachedApiModuleName;
    59
    610                protected AbsWebAPI (string _name = null) : this(null, _name) {
     
    1014                        Name = _name ?? GetType ().Name;
    1115                        ParentWeb = _parentWeb;
     16                        CachedApiModuleName = $"webapi.{Name}";
     17                        RegisterPermissions ();
     18                }
     19
     20                protected virtual void RegisterPermissions () {
     21                        AdminWebModules.Instance.AddKnownModule ($"webapi.{Name}", DefaultPermissionLevel ());
    1222                }
    1323
    1424                public abstract void HandleRequest (RequestContext _context);
    1525
     26                public virtual bool Authorized (RequestContext _context) {
     27                        return AdminWebModules.Instance.ModuleAllowedWithLevel (CachedApiModuleName, _context.PermissionLevel);
     28                }
     29
    1630                public virtual int DefaultPermissionLevel () => 0;
    1731        }
Note: See TracChangeset for help on using the changeset viewer.