Skip to content

Commit b011a89

Browse files
NRL-2099 Make permission printing pretty and consistently formatted
1 parent 78b9c5b commit b011a89

File tree

1 file changed

+59
-35
lines changed

1 file changed

+59
-35
lines changed

scripts/manage_permissions.py

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import fire
1010
from aws_session_assume import get_boto_session
1111

12-
from nrlf.core.constants import TYPE_ATTRIBUTES, AccessControls
12+
from nrlf.core.constants import CATEGORY_ATTRIBUTES, TYPE_ATTRIBUTES, AccessControls
1313

1414
nrl_env = os.getenv("ENV", "dev")
1515
nrl_auth_bucket_name = os.getenv(
@@ -167,21 +167,44 @@ def list_available_access_controls() -> None:
167167

168168

169169
def _print_perm(
170-
perms_pretty: dict, lookup_path: str, perm_pretty_name: str, perm_key: str
170+
perm_pretty_name: str,
171+
perm_to_print: list,
171172
):
173+
# if not perm_to_print:
174+
# return
172175
print()
173-
access_controls = perms_pretty.get(perm_key, [])
174-
if access_controls:
175-
print(f"{lookup_path} has these {perm_pretty_name}s:")
176-
for control in access_controls:
177-
print(f"- {control}")
178-
else:
179-
print(f"{lookup_path} has no {perm_pretty_name}s")
176+
plural = (
177+
perm_pretty_name if perm_pretty_name.endswith("s") else f"{perm_pretty_name}s"
178+
)
179+
print(f"{plural.upper()} ({len(perm_to_print)})")
180+
for perm in perm_to_print:
181+
print(f"- {perm}")
182+
183+
184+
def _print_perm_with_lookup(
185+
perm_pretty_name: str,
186+
perm_to_print: list,
187+
attribute_lookup: dict[str, dict[str, str]],
188+
):
189+
"""
190+
Lookup human-readable names for a permission and print
191+
"""
192+
printable = []
193+
for perm_item in perm_to_print:
194+
display_name = attribute_lookup.get(
195+
perm_item, {"display": f"Unknown {perm_pretty_name.lower()}"}
196+
)["display"]
197+
printable_perm_and_display_name = "%-45s (%s)" % (
198+
display_name[:44],
199+
perm_item,
200+
)
201+
printable.append(printable_perm_and_display_name)
202+
_print_perm(perm_pretty_name, printable)
180203

181204

182205
def show_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
183206
"""
184-
Show the permissions for a given application or organization.
207+
Show permissions for a given application or organization.
185208
"""
186209
if supplier_type.lower() not in SupplierType.list() or not app_id:
187210
print("Usage: show permissions for a given organisation or app")
@@ -204,37 +227,38 @@ def show_perms(supplier_type: SupplierType, app_id: str, org_ods=None) -> None:
204227

205228
perms_pretty = json.loads(perms_ugly)
206229
if not perms_pretty:
207-
print(f"No pointer-types found in permission file for {lookup_path}.")
230+
print(f"No permissions found in file for {lookup_path}.")
208231
return
209232

210-
pretty_type_data = {
211-
pointertype_perm: TYPE_ATTRIBUTES.get(
212-
pointertype_perm, {"display": "Unknown type"}
213-
)
214-
for pointertype_perm in perms_pretty.get("types")
215-
}
216-
types = [
217-
"%-45s (%s)"
218-
% (pretty_type_data[pointertype_perm]["display"][:44], pointertype_perm)
219-
for pointertype_perm in perms_pretty.get("types")
220-
]
221-
print(f"{lookup_path} is allowed to access these pointer-types:")
222-
for type_display in types:
223-
print(f"- {type_display}")
233+
print(f"{lookup_path} is allowed access to the following...")
234+
235+
_print_perm_with_lookup(
236+
"pointer type", perms_pretty.get("types", []), TYPE_ATTRIBUTES
237+
)
238+
239+
_print_perm_with_lookup(
240+
"pointer categories", perms_pretty.get("categories", []), CATEGORY_ATTRIBUTES
241+
)
242+
243+
_print_perm(
244+
"access control",
245+
perms_pretty.get("access_controls", []),
246+
)
224247

225248
_print_perm(
226-
perms_pretty,
227-
lookup_path,
228-
perm_pretty_name="access control",
229-
perm_key="access_controls",
249+
"API interaction",
250+
perms_pretty.get("interaction", []),
230251
)
231252

232-
# _print_perm(
233-
# perms_pretty,
234-
# lookup_path,
235-
# perm_pretty_name="API interaction",
236-
# perm_key="interaction",
237-
# )
253+
_print_perm(
254+
"Produce for authors",
255+
perms_pretty.get("produce_for_authors", []),
256+
)
257+
258+
_print_perm(
259+
"Produce for custodians",
260+
perms_pretty.get("produce_for_custodians", []),
261+
)
238262

239263

240264
if __name__ == "__main__":

0 commit comments

Comments
 (0)