class ApplicationError(Exception): def __init__(self, *messages, htmlstatus=400): self.htmlstatus = htmlstatus Exception.__init__(self, *messages) @property def error(self): error = { "status": self.htmlstatus, "messages": list(self.args) } return error class AutorisationError(ApplicationError): def __init__(self, allowed_roles=[], user_roles=[]): self.allowed_roles = allowed_roles self.user_roles = user_roles if user_roles is None: userrolestxt = "NoSysAdmin" else: userrolestxt = ", ".join([role.name for role in self.user_roles]) # Die Meldung über erforderliche Rollen nur aufnehmen, wenn # welche spezifiziert sind. Bei leerer allowed_roles-Liste # (z.B. Endpunkte, die nur AppAdmin/SysAdmin erlauben) entfällt # dieser Teil der Fehlermeldung komplett. messages = [ "Sie haben nicht die passende Rolle für diese Funktion", "Ihre Rollen sind: " + userrolestxt, ] if self.allowed_roles: messages.append("Erforderliche Rollen sind: " + ", ".join(self.allowed_roles)) ApplicationError.__init__(self, *messages, htmlstatus=HtmlStatusCode.Unauthorized) class HtmlStatusCode(): Bad_Request = 400 Unauthorized = 401 Payment_Required = 402 Forbidden = 403 Not_Found = 404 Method_Not_Allowed = 405