Skip to content

Commit a84441a

Browse files
committed
Order permissions
1 parent 4c2af50 commit a84441a

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

sonar/permissions/permissions.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@
3737
"admin": "Administer System",
3838
"gateadmin": "Administer Quality Gates",
3939
"profileadmin": "Administer Quality Profiles",
40-
"provisioning": "Create Projects",
4140
"scan": "Execute Analysis",
41+
"provisioning": "Create Projects",
4242
}
43-
DEVELOPER_GLOBAL_PERMISSIONS = {**COMMUNITY_GLOBAL_PERMISSIONS, **{"applicationcreator": "Create Applications"}}
44-
ENTERPRISE_GLOBAL_PERMISSIONS = {**DEVELOPER_GLOBAL_PERMISSIONS, **{"portfoliocreator": "Create Portfolios"}}
43+
44+
DEVELOPER_GLOBAL_PERMISSIONS = {**COMMUNITY_GLOBAL_PERMISSIONS, "applicationcreator": "Create Applications"}
45+
ENTERPRISE_GLOBAL_PERMISSIONS = {**DEVELOPER_GLOBAL_PERMISSIONS, "portfoliocreator": "Create Portfolios"}
4546

4647
PROJECT_PERMISSIONS = {
47-
"admin": "Administer Project",
4848
"user": "Browse",
4949
"codeviewer": "See source code",
5050
"issueadmin": "Administer Issues",
5151
"securityhotspotadmin": "Create Projects",
52+
"admin": "Administer Project",
5253
"scan": "Execute Analysis",
5354
}
5455

@@ -81,24 +82,23 @@ def __init__(self, concerned_object: object) -> None:
8182
def __str__(self) -> str:
8283
return f"permissions of {str(self.concerned_object)}"
8384

84-
def to_json(self, perm_type: Optional[str] = None, csv: bool = False) -> types.JsonPermissions:
85+
def to_json(self, perm_type: Optional[str] = None) -> types.JsonPermissions:
8586
"""Converts a permission object to JSON"""
86-
if not csv:
87-
return self.permissions.get(perm_type, {}) if is_valid(perm_type) else self.permissions
8887
perms = []
88+
order = PROJECT_PERMISSIONS if self.concerned_object else ENTERPRISE_GLOBAL_PERMISSIONS
8989
for p in normalize(perm_type):
9090
if p not in self.permissions or len(self.permissions[p]) == 0:
9191
continue
9292
for k, v in self.permissions.get(p, {}).items():
9393
if not v or len(v) == 0:
9494
continue
95-
perms += [{p[:-1]: k, "permissions": encode(v)}]
95+
perms += [{p[:-1]: k, "permissions": encode(v, order)}]
9696
return perms if len(perms) > 0 else None
9797

9898
def export(self) -> types.ObjectJsonRepr:
9999
"""Exports permissions as JSON"""
100-
perms = {k: v for k, v in self.to_json().items() if len(v) > 0}
101-
return None if len(perms) == 0 else perms
100+
perms = self.to_json()
101+
return None if not perms or len(perms) == 0 else perms
102102

103103
@abstractmethod
104104
def read(self) -> Permissions:
@@ -324,11 +324,12 @@ def simplify(perms_dict: dict[str, list[str]]) -> Optional[dict[str, str]]:
324324
return {k: encode(v) for k, v in perms_dict.items() if len(v) > 0}
325325

326326

327-
def encode(perms_array: dict[str, list[str]]) -> dict[str, str]:
327+
def encode(perms_array: dict[str, list[str]], order: list[str]) -> dict[str, str]:
328328
"""
329329
:meta private:
330330
"""
331-
return utilities.list_to_csv(perms_array, ", ", check_for_separator=True)
331+
ordered = utilities.order_list(perms_array, *order)
332+
return utilities.list_to_csv(ordered, ", ", check_for_separator=True)
332333

333334

334335
def decode(encoded_perms: dict[str, str]) -> dict[str, list[str]]:

0 commit comments

Comments
 (0)