Skip to content

Commit

Permalink
Merge pull request #208 from Police-Data-Accessibility-Project/mc_546…
Browse files Browse the repository at this point in the history
…_admin_permissions_documentation

Add logic to automatically indicate when an endpoint requires Admin permissions.
  • Loading branch information
maxachis authored Dec 2, 2024
2 parents 351b350 + a5571d7 commit 6ffaede
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 28 deletions.
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 @@ class AuthenticationInfo(BaseModel):
no_auth: bool = False
restrict_to_permissions: Optional[list[PermissionsEnum]] = None

def requires_admin_permissions(self) -> bool:
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
),
)

0 comments on commit 6ffaede

Please sign in to comment.