Skip to content

Commit

Permalink
fix: lazy loaded permission callback (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasvinclav authored Dec 22, 2023
1 parent 4c49f08 commit 97ca6dd
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/unfold/sites.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,8 @@ def _get_is_active(link: str) -> bool:
allowed_items = []

for item in group["items"]:
# Permission callbacks
# Skip item if permission check fails
if not self._call_permission_callback(item.get("permission"), request):
# Skip item if permission check fails
continue

item["active"] = False
Expand Down Expand Up @@ -280,8 +279,8 @@ def get_tabs_list(self, request: HttpRequest) -> List[Dict[str, Any]]:
allowed_items = []

for item in tab["items"]:
# Skip item if permission check fails
if not self._call_permission_callback(item.get("permission"), request):
# Skip item if permission check fails
continue

allowed_items.append(item)
Expand All @@ -306,14 +305,22 @@ def _get_mode_images(

def _call_permission_callback(
self, callback: Union[str, Callable, None], request: HttpRequest
):
) -> bool:
if callback is None:
return True

if isinstance(callback, str):
callback = import_string(callback)
try:
callback = import_string(callback)
except ImportError:
pass

if isinstance(callback, str) or isinstance(callback, Callable):
# We are not able to use here "is" because type is lazy loaded function
if lazy(callback)(request) == True: # noqa: E712
return True

return callback(request)
return False

def _get_value(
self, instance: Union[str, Callable, None], *args: Any
Expand Down

0 comments on commit 97ca6dd

Please sign in to comment.