rbac: handle lookup error (#7341)
Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
parent
f22daca091
commit
83b84e8d26
|
@ -32,13 +32,19 @@ class PermissionSerializer(ModelSerializer):
|
|||
|
||||
def get_app_label_verbose(self, instance: Permission) -> str:
|
||||
"""Human-readable app label"""
|
||||
try:
|
||||
return apps.get_app_config(instance.content_type.app_label).verbose_name
|
||||
except LookupError:
|
||||
return f"{instance.content_type.app_label}.{instance.content_type.model}"
|
||||
|
||||
def get_model_verbose(self, instance: Permission) -> str:
|
||||
"""Human-readable model name"""
|
||||
try:
|
||||
return apps.get_model(
|
||||
instance.content_type.app_label, instance.content_type.model
|
||||
)._meta.verbose_name
|
||||
except LookupError:
|
||||
return f"{instance.content_type.app_label}.{instance.content_type.model}"
|
||||
|
||||
class Meta:
|
||||
model = Permission
|
||||
|
|
|
@ -28,9 +28,12 @@ class ExtraRoleObjectPermissionSerializer(RoleObjectPermissionSerializer):
|
|||
|
||||
def get_model_verbose(self, instance: GroupObjectPermission) -> str:
|
||||
"""Get model label from permission's model"""
|
||||
try:
|
||||
return apps.get_model(
|
||||
instance.content_type.app_label, instance.content_type.model
|
||||
)._meta.verbose_name
|
||||
except LookupError:
|
||||
return f"{instance.content_type.app_label}.{instance.content_type.model}"
|
||||
|
||||
def get_object_description(self, instance: GroupObjectPermission) -> Optional[str]:
|
||||
"""Get model description from attached model. This operation takes at least
|
||||
|
@ -38,7 +41,10 @@ class ExtraRoleObjectPermissionSerializer(RoleObjectPermissionSerializer):
|
|||
view_ permission on the object"""
|
||||
app_label = instance.content_type.app_label
|
||||
model = instance.content_type.model
|
||||
try:
|
||||
model_class = apps.get_model(app_label, model)
|
||||
except LookupError:
|
||||
return None
|
||||
objects = get_objects_for_group(instance.group, f"{app_label}.view_{model}", model_class)
|
||||
obj = objects.first()
|
||||
if not obj:
|
||||
|
|
|
@ -28,9 +28,12 @@ class ExtraUserObjectPermissionSerializer(UserObjectPermissionSerializer):
|
|||
|
||||
def get_model_verbose(self, instance: UserObjectPermission) -> str:
|
||||
"""Get model label from permission's model"""
|
||||
try:
|
||||
return apps.get_model(
|
||||
instance.content_type.app_label, instance.content_type.model
|
||||
)._meta.verbose_name
|
||||
except LookupError:
|
||||
return f"{instance.content_type.app_label}.{instance.content_type.model}"
|
||||
|
||||
def get_object_description(self, instance: UserObjectPermission) -> Optional[str]:
|
||||
"""Get model description from attached model. This operation takes at least
|
||||
|
@ -38,7 +41,10 @@ class ExtraUserObjectPermissionSerializer(UserObjectPermissionSerializer):
|
|||
view_ permission on the object"""
|
||||
app_label = instance.content_type.app_label
|
||||
model = instance.content_type.model
|
||||
try:
|
||||
model_class = apps.get_model(app_label, model)
|
||||
except LookupError:
|
||||
return None
|
||||
objects = get_objects_for_user(instance.user, f"{app_label}.view_{model}", model_class)
|
||||
obj = objects.first()
|
||||
if not obj:
|
||||
|
|
Reference in a new issue