From ca8dcf03f9e637782536f0bb09e53cc012b53cca Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Sun, 17 Sep 2023 11:52:13 +0200 Subject: [PATCH] Be more forgiving --- CHANGELOG.rst | 5 +++++ authlib/roles.py | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2d38b8e..d35a039 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -5,6 +5,11 @@ Change log Next version ============ +- Changed the roles implementation to allow using arbitrary names for the role + field. +- Stopped crashing when encountering an unknown role -- doing nothing in + ``has_perm`` is an acceptable fallback. + 0.16 (2023-09-17) ================= diff --git a/authlib/roles.py b/authlib/roles.py index fad4b6c..8a124c6 100644 --- a/authlib/roles.py +++ b/authlib/roles.py @@ -40,8 +40,8 @@ def contribute_to_class(self, cls, name): super().contribute_to_class(cls, name) def _role_has_perm(self, *, perm, obj): - if callback := _roles()[self.role].get("callback"): - return self.is_active and callback(user=self, perm=perm, obj=obj) + if (r := _roles()[getattr(self, name)]) and (cb := r.get("callback")): + return self.is_active and cb(user=self, perm=perm, obj=obj) cls._role_has_perm = _role_has_perm