Skip to content

Commit 65951f4

Browse files
Update __init__.py for managing restrictions for pages (#1565)
1 parent 7f98667 commit 65951f4

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

atlassian/confluence/__init__.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,125 @@ def get_all_restrictions_for_content(self, content_id):
860860
url = f"rest/api/content/{content_id}/restriction/byOperation"
861861
return self.get(url)
862862

863+
def get_all_restrictions_from_page_json_rpc(self, page_id):
864+
"""
865+
The JSON-RPC APIs for Confluence are provided here to help you browse and discover APIs you have access to.
866+
JSON-RPC APIs operate differently than REST APIs.
867+
To learn more about how to use these APIs,
868+
please refer to the Confluence JSON-RPC documentation on Atlassian Developers.
869+
"""
870+
if self.api_version == "cloud" or self.cloud:
871+
return {}
872+
url = "rpc/json-rpc/confluenceservice-v2"
873+
data = {
874+
"jsonrpc": "2.0",
875+
"method": "getContentPermissionSets",
876+
"id": 9,
877+
"params": [page_id]
878+
}
879+
return self.post(url, data=data).get("result") or {}
880+
881+
def update_restrictions_for_page_json_rpc(self, page_id, permission_type, content_permissions):
882+
"""
883+
The JSON-RPC APIs for Confluence are provided here to help you browse and discover APIs you have access to.
884+
JSON-RPC APIs operate differently than REST APIs.
885+
To learn more about how to use these APIs,
886+
please refer to the Confluence JSON-RPC documentation on Atlassian Developers.
887+
"""
888+
if self.api_version == "cloud" or self.cloud:
889+
return {}
890+
url = "rpc/json-rpc/confluenceservice-v2"
891+
data = {
892+
"jsonrpc": "2.0",
893+
"method": "setContentPermissions",
894+
"id": 9,
895+
"params": [page_id, permission_type, content_permissions]
896+
}
897+
return self.post(url, data=data).get("result") or {}
898+
899+
def get_users_from_restricts_in_page_by_type(self, page_id: str, restriction_type: Literal['View', 'Edit']):
900+
page_name = self.get_page_by_id(page_id=page_id)['title']
901+
restrictions_in_page = self.get_all_restrictions_from_page_json_rpc(page_id=page_id)
902+
try:
903+
if len(restrictions_in_page) > 0:
904+
for restriction_type_in_page in restrictions_in_page:
905+
if dict(restriction_type_in_page).get('type') == restriction_type:
906+
users = dict(restriction_type_in_page).get('contentPermissions')
907+
return users
908+
else:
909+
raise JsonRPCRestrictionsError(f'On page "{page_name}" has no restrictions type of "{restriction_type}"')
910+
except JsonRPCError:
911+
raise
912+
913+
def create_restricts_from_from_user(self, user_name: str, restriction_type: Literal['View', 'Edit']):
914+
content = {'type': restriction_type, 'userName': user_name, 'groupName': None}
915+
916+
return content
917+
918+
def add_user_in_restricted_page(self, user_name: str, page_id: str, restriction_type: Literal['View', 'Edit']):
919+
page_name = self.get_page_by_id(page_id=page_id).get('title')
920+
user_find_view_bool = False
921+
user_find_edit_bool = False
922+
users_content_view: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='View')
923+
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='Edit')
924+
current_user_content_view: dict = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
925+
current_user_content_edit: dict = self.create_restricts_from_from_user(user_name=user_name, restriction_type='Edit')
926+
try:
927+
if None not in [users_content_view, users_content_edit]:
928+
if users_content_view is not None:
929+
for user in users_content_view:
930+
if dict(user).get('userName') == current_user_content_view.get('userName'):
931+
user_find_view_bool = True
932+
if users_content_edit is not None:
933+
for user in users_content_edit:
934+
if dict(user).get('userName') == current_user_content_edit.get('userName'):
935+
user_find_edit_bool = True
936+
if restriction_type == 'View':
937+
if user_find_view_bool == False:
938+
current_user_content = self.create_restricts_from_from_user(user_name=user_name, restriction_type=restriction_type)
939+
users_content_view.append(current_user_content)
940+
self.update_restrictions_for_page_json_rpc(page_id=page_id, user=user_name, permission_type=restriction_type, content_permissions=users_content_view)
941+
elif user_find_view_bool == True:
942+
raise JsonRPCRestrictionsError(f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"')
943+
elif restriction_type == 'Edit':
944+
if user_find_edit_bool == False:
945+
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
946+
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type=restriction_type)
947+
users_content_view.append(current_user_content_view)
948+
users_content_edit.append(current_user_content_edit)
949+
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='View', content_permissions=users_content_view)
950+
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type=restriction_type, content_permissions=users_content_edit)
951+
print(f'User "{user_name}" granted restrictions type of "{restriction_type}" on page "{page_name}"')
952+
elif user_find_edit_bool == True:
953+
raise JsonRPCRestrictionsError(f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"')
954+
except JsonRPCError:
955+
raise
956+
957+
def remove_user_from_restricted_page(self, user_name: str, page_id: str):
958+
page_name = self.get_page_by_id(page_id=page_id).get('title')
959+
user_find_bool = False
960+
users_content_view: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='View')
961+
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='Edit')
962+
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
963+
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type='Edit')
964+
for user_index, user_value in enumerate(users_content_view):
965+
if dict(user_value).get('userName') == current_user_content_view.get('userName'):
966+
user_find_bool = True
967+
users_content_view.pop(user_index)
968+
for user_index, user_value in enumerate(users_content_edit):
969+
if dict(user_value).get('userName') == current_user_content_edit.get('userName'):
970+
user_find_bool = True
971+
users_content_edit.pop(user_index)
972+
try:
973+
if user_find_bool == True:
974+
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='View', content_permissions=users_content_view)
975+
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='Edit', content_permissions=users_content_edit)
976+
print(f'User "{user_name}" has been deleted from restrictions on page "{page_name}"')
977+
elif user_find_bool == False:
978+
raise JsonRPCRestrictionsError(f'User "{user_name}" has not founded in restrictions on page "{page_name}"')
979+
except JsonRPCError:
980+
raise
981+
863982
def remove_page_from_trash(self, page_id):
864983
"""
865984
This method removes a page from trash

0 commit comments

Comments
 (0)