From 97535a6bc0baa15b469236a92f60270ca267945b Mon Sep 17 00:00:00 2001 From: Tom Hubrecht Date: Wed, 18 Sep 2024 22:20:05 +0200 Subject: [PATCH] fix(models): Don't crash when no kanidm profile exists --- src/dgsi/models.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/dgsi/models.py b/src/dgsi/models.py index b60cd1d..70c2b4d 100644 --- a/src/dgsi/models.py +++ b/src/dgsi/models.py @@ -4,6 +4,7 @@ from typing import Optional from asgiref.sync import async_to_sync from django.contrib.auth.models import AbstractUser +from kanidm.exceptions import NoMatchingEntries from kanidm.models.person import Person from shared.kanidm import client @@ -23,14 +24,19 @@ class User(AbstractUser): """ @cached_property - def kanidm(self) -> KanidmProfile: - radius_data = async_to_sync(client.get_radius_token)(self.username).data + def kanidm(self) -> Optional[KanidmProfile]: + try: + radius_data = async_to_sync(client.get_radius_token)(self.username).data - return KanidmProfile( - person=async_to_sync(client.person_account_get)(self.username), - radius_secret=radius_data and radius_data.get("secret"), - ) + return KanidmProfile( + person=async_to_sync(client.person_account_get)(self.username), + radius_secret=radius_data and radius_data.get("secret"), + ) + except NoMatchingEntries: + return None @property def is_admin(self) -> bool: - return ADMIN_GROUP in self.kanidm.person.memberof + return (self.kanidm is not None) and ( + ADMIN_GROUP in self.kanidm.person.memberof + )