Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to automatically indicate when an endpoint requires Admin permissions. #208

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions middleware/access_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
no_auth: bool = False
restrict_to_permissions: Optional[list[PermissionsEnum]] = None

def requires_admin_permissions(self) -> bool:

Check warning on line 35 in middleware/access_logic.py

View workflow job for this annotation

GitHub Actions / flake8

[flake8] middleware/access_logic.py#L35 <102>

Missing docstring in public method
Raw output
./middleware/access_logic.py:35:1: D102 Missing docstring in public method
if self.restrict_to_permissions is None:
return False
return len(self.restrict_to_permissions) > 0


WRITE_ONLY_AUTH_INFO = AuthenticationInfo(
allowed_access_methods=[AccessTypeEnum.JWT],
Expand Down
37 changes: 9 additions & 28 deletions middleware/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,21 @@ def endpoint_info(
auth_info: AuthenticationInfo,
schema_config: SchemaConfigs,
response_info: ResponseInfo,
**doc_kwargs,
description: str = "",
**additional_doc_kwargs,
):
"""
A more sophisticated form of `endpoint_info`, with more robust
schema and response definition.
Designed to eventually replace all instances of endpoint_info
"""

doc_kwargs = {"description": description, **additional_doc_kwargs}
if auth_info.requires_admin_permissions():
doc_kwargs["description"] = (
"**Requires admin permissions.**\n" + doc_kwargs["description"]
)

if schema_config.value.input_schema is not None:
input_doc_info = get_restx_param_documentation(
namespace=namespace,
Expand Down Expand Up @@ -224,30 +232,3 @@ def _add_auth_info_to_parser(auth_info: AuthenticationInfo, parser: RequestParse
header_arg_function(parser)
return
raise Exception("Must have at least one access method")


def _get_input_doc_info(
namespace, input_schema, input_model=None, input_model_name: Optional[str] = None
) -> FlaskRestxDocInfo:
check_for_mutually_exclusive_arguments(input_schema, input_model)
if input_model is not None:
return FlaskRestxDocInfo(
model=input_model,
parser=namespace.parser(),
)
if input_schema is None:
return FlaskRestxDocInfo(
model=None,
parser=namespace.parser(),
)

# Assume input schema is defined
return get_restx_param_documentation(
namespace=namespace,
schema=input_schema,
model_name=(
input_schema.__class__.__name__
if input_model_name is None
else input_model_name
),
)
Loading