Skip to content

Update __init__.py for managing restrictions for pages #1565

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
119 changes: 119 additions & 0 deletions atlassian/confluence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,125 @@ def get_all_restrictions_for_content(self, content_id):
url = f"rest/api/content/{content_id}/restriction/byOperation"
return self.get(url)

def get_all_restrictions_from_page_json_rpc(self, page_id):
"""
The JSON-RPC APIs for Confluence are provided here to help you browse and discover APIs you have access to.
JSON-RPC APIs operate differently than REST APIs.
To learn more about how to use these APIs,
please refer to the Confluence JSON-RPC documentation on Atlassian Developers.
"""
if self.api_version == "cloud" or self.cloud:
return {}
url = "rpc/json-rpc/confluenceservice-v2"
data = {
"jsonrpc": "2.0",
"method": "getContentPermissionSets",
"id": 9,
"params": [page_id]
}
return self.post(url, data=data).get("result") or {}

def update_restrictions_for_page_json_rpc(self, page_id, permission_type, content_permissions):
"""
The JSON-RPC APIs for Confluence are provided here to help you browse and discover APIs you have access to.
JSON-RPC APIs operate differently than REST APIs.
To learn more about how to use these APIs,
please refer to the Confluence JSON-RPC documentation on Atlassian Developers.
"""
if self.api_version == "cloud" or self.cloud:
return {}
url = "rpc/json-rpc/confluenceservice-v2"
data = {
"jsonrpc": "2.0",
"method": "setContentPermissions",
"id": 9,
"params": [page_id, permission_type, content_permissions]
}
return self.post(url, data=data).get("result") or {}

def get_users_from_restricts_in_page_by_type(self, page_id: str, restriction_type: Literal['View', 'Edit']):
page_name = self.get_page_by_id(page_id=page_id)['title']
restrictions_in_page = self.get_all_restrictions_from_page_json_rpc(page_id=page_id)
try:
if len(restrictions_in_page) > 0:
for restriction_type_in_page in restrictions_in_page:
if dict(restriction_type_in_page).get('type') == restriction_type:
users = dict(restriction_type_in_page).get('contentPermissions')
return users
else:
raise JsonRPCRestrictionsError(f'On page "{page_name}" has no restrictions type of "{restriction_type}"')
except JsonRPCError:
raise

def create_restricts_from_from_user(self, user_name: str, restriction_type: Literal['View', 'Edit']):
content = {'type': restriction_type, 'userName': user_name, 'groupName': None}

return content

def add_user_in_restricted_page(self, user_name: str, page_id: str, restriction_type: Literal['View', 'Edit']):
page_name = self.get_page_by_id(page_id=page_id).get('title')
user_find_view_bool = False
user_find_edit_bool = False
users_content_view: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='View')
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='Edit')
current_user_content_view: dict = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
current_user_content_edit: dict = self.create_restricts_from_from_user(user_name=user_name, restriction_type='Edit')
try:
if None not in [users_content_view, users_content_edit]:
if users_content_view is not None:
for user in users_content_view:
if dict(user).get('userName') == current_user_content_view.get('userName'):
user_find_view_bool = True
if users_content_edit is not None:
for user in users_content_edit:
if dict(user).get('userName') == current_user_content_edit.get('userName'):
user_find_edit_bool = True
if restriction_type == 'View':
if user_find_view_bool == False:
current_user_content = self.create_restricts_from_from_user(user_name=user_name, restriction_type=restriction_type)
users_content_view.append(current_user_content)
self.update_restrictions_for_page_json_rpc(page_id=page_id, user=user_name, permission_type=restriction_type, content_permissions=users_content_view)
elif user_find_view_bool == True:
raise JsonRPCRestrictionsError(f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"')
elif restriction_type == 'Edit':
if user_find_edit_bool == False:
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type=restriction_type)
users_content_view.append(current_user_content_view)
users_content_edit.append(current_user_content_edit)
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='View', content_permissions=users_content_view)
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type=restriction_type, content_permissions=users_content_edit)
print(f'User "{user_name}" granted restrictions type of "{restriction_type}" on page "{page_name}"')
elif user_find_edit_bool == True:
raise JsonRPCRestrictionsError(f'User "{user_name}" already have restrictions type of "{restriction_type}" on page "{page_name}"')
except JsonRPCError:
raise

def remove_user_from_restricted_page(self, user_name: str, page_id: str):
page_name = self.get_page_by_id(page_id=page_id).get('title')
user_find_bool = False
users_content_view: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='View')
users_content_edit: list = self.get_users_from_restricts_in_page_by_type(page_id=page_id, restriction_type='Edit')
current_user_content_view = self.create_restricts_from_from_user(user_name=user_name, restriction_type='View')
current_user_content_edit = self.create_restricts_from_from_user(user_name=user_name, restriction_type='Edit')
for user_index, user_value in enumerate(users_content_view):
if dict(user_value).get('userName') == current_user_content_view.get('userName'):
user_find_bool = True
users_content_view.pop(user_index)
for user_index, user_value in enumerate(users_content_edit):
if dict(user_value).get('userName') == current_user_content_edit.get('userName'):
user_find_bool = True
users_content_edit.pop(user_index)
try:
if user_find_bool == True:
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='View', content_permissions=users_content_view)
self.update_restrictions_for_page_json_rpc(page_id=page_id, permission_type='Edit', content_permissions=users_content_edit)
print(f'User "{user_name}" has been deleted from restrictions on page "{page_name}"')
elif user_find_bool == False:
raise JsonRPCRestrictionsError(f'User "{user_name}" has not founded in restrictions on page "{page_name}"')
except JsonRPCError:
raise

def remove_page_from_trash(self, page_id):
"""
This method removes a page from trash
Expand Down
Loading