refactor authentication / authorization into a mixin

This commit is contained in:
Jakob Ketterl 2021-02-10 20:21:45 +01:00
parent 8422a33081
commit 17c20d12e0
3 changed files with 9 additions and 8 deletions

View File

@ -1,4 +1,3 @@
from .template import WebpageController
from .session import SessionStorage from .session import SessionStorage
from owrx.config import Config from owrx.config import Config
from owrx.users import UserList from owrx.users import UserList
@ -25,7 +24,7 @@ class Authentication(object):
return None return None
class AdminController(WebpageController): class AuthorizationMixin(object):
def __init__(self, handler, request, options): def __init__(self, handler, request, options):
self.authentication = Authentication() self.authentication = Authentication()
self.user = self.authentication.getUser(request) self.user = self.authentication.getUser(request)

View File

@ -1,9 +1,10 @@
from owrx.controllers.admin import AdminController from owrx.controllers.template import WebpageController
from owrx.controllers.admin import AuthorizationMixin
from owrx.users import UserList, DefaultPasswordClass from owrx.users import UserList, DefaultPasswordClass
from urllib.parse import parse_qs from urllib.parse import parse_qs
class ProfileController(AdminController): class ProfileController(AuthorizationMixin, WebpageController):
def isAuthorized(self): def isAuthorized(self):
return self.user is not None and self.user.is_enabled() and self.user.must_change_password return self.user is not None and self.user.is_enabled() and self.user.must_change_password

View File

@ -1,4 +1,5 @@
from .admin import AdminController from owrx.controllers.template import WebpageController
from owrx.controllers.admin import AuthorizationMixin
from owrx.config import Config, CoreConfig from owrx.config import Config, CoreConfig
from urllib.parse import parse_qs from urllib.parse import parse_qs
from owrx.form import ( from owrx.form import (
@ -54,12 +55,12 @@ class Section(object):
return {k: v for i in self.inputs for k, v in i.parse(data).items()} return {k: v for i in self.inputs for k, v in i.parse(data).items()}
class SettingsController(AdminController): class SettingsController(AuthorizationMixin, WebpageController):
def indexAction(self): def indexAction(self):
self.serve_template("settings.html", **self.template_variables()) self.serve_template("settings.html", **self.template_variables())
class SdrSettingsController(AdminController): class SdrSettingsController(AuthorizationMixin, WebpageController):
def template_variables(self): def template_variables(self):
variables = super().template_variables() variables = super().template_variables()
variables["devices"] = self.render_devices() variables["devices"] = self.render_devices()
@ -93,7 +94,7 @@ class SdrSettingsController(AdminController):
self.serve_template("sdrsettings.html", **self.template_variables()) self.serve_template("sdrsettings.html", **self.template_variables())
class GeneralSettingsController(AdminController): class GeneralSettingsController(AuthorizationMixin, WebpageController):
sections = [ sections = [
Section( Section(
"Receiver information", "Receiver information",