fix(models): Don't crash when no kanidm profile exists

This commit is contained in:
Tom Hubrecht 2024-09-18 22:20:05 +02:00
parent 1d2f4a5866
commit 97535a6bc0
Signed by: thubrecht
SSH key fingerprint: SHA256:r+nK/SIcWlJ0zFZJGHtlAoRwq1Rm+WcKAm5ADYMoQPc

View file

@ -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
)