fix(models): Don't crash when no kanidm profile exists
This commit is contained in:
parent
1d2f4a5866
commit
97535a6bc0
1 changed files with 13 additions and 7 deletions
|
@ -4,6 +4,7 @@ from typing import Optional
|
||||||
|
|
||||||
from asgiref.sync import async_to_sync
|
from asgiref.sync import async_to_sync
|
||||||
from django.contrib.auth.models import AbstractUser
|
from django.contrib.auth.models import AbstractUser
|
||||||
|
from kanidm.exceptions import NoMatchingEntries
|
||||||
from kanidm.models.person import Person
|
from kanidm.models.person import Person
|
||||||
|
|
||||||
from shared.kanidm import client
|
from shared.kanidm import client
|
||||||
|
@ -23,14 +24,19 @@ class User(AbstractUser):
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def kanidm(self) -> KanidmProfile:
|
def kanidm(self) -> Optional[KanidmProfile]:
|
||||||
|
try:
|
||||||
radius_data = async_to_sync(client.get_radius_token)(self.username).data
|
radius_data = async_to_sync(client.get_radius_token)(self.username).data
|
||||||
|
|
||||||
return KanidmProfile(
|
return KanidmProfile(
|
||||||
person=async_to_sync(client.person_account_get)(self.username),
|
person=async_to_sync(client.person_account_get)(self.username),
|
||||||
radius_secret=radius_data and radius_data.get("secret"),
|
radius_secret=radius_data and radius_data.get("secret"),
|
||||||
)
|
)
|
||||||
|
except NoMatchingEntries:
|
||||||
|
return None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_admin(self) -> bool:
|
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
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue