Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 12
V2 endpoints for user and access management#231
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
Merged
MarcelGeo
merged 11 commits into
master
from
v2_endpoints_for_user_and_access_managementFeb 24, 2025
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
054023a
Typing fix for Python3.8 compatibility
harminius 2feb93c
Add v2 endpoint for access management
harminius e528c5b
black formatting
harminius c92a145
Address reviews
harminius cccddff
Improve arguments description
harminius 68b5405
Add deprecated coming soon warning
harminius e1b806e
Trying to fix coveralls
MarcelGeo 93971bd
Use new method in editor test
harminius 451449d
Improve error handling - show custom details
harminius 0e448f0
black
harminius e7aed8e
Improve do request handling
MarcelGeo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -17,7 +17,9 @@ | ||
| import typing | ||
| import warnings | ||
| from .common import ClientError, LoginError, InvalidProject, ErrorCode | ||
| from typing import List | ||
| from .common import ClientError, LoginError, WorkspaceRole, ProjectRole | ||
| from .merginproject import MerginProject | ||
| from .client_pull import ( | ||
| download_file_finalize, | ||
| @@ -36,6 +38,7 @@ | ||
| from .version import __version__ | ||
| this_dir = os.path.dirname(os.path.realpath(__file__)) | ||
| json_headers = {"Content-Type": "application/json"} | ||
| class TokenError(Exception): | ||
| @@ -207,9 +210,23 @@ def _do_request(self, request): | ||
| except urllib.error.HTTPError as e: | ||
| server_response = json.load(e) | ||
| # We first to try to get the value from the response otherwise we set a default value | ||
| err_detail = server_response.get("detail", e.read().decode("utf-8")) | ||
| server_code = server_response.get("code", None) | ||
| err_detail = None | ||
| server_code = None | ||
| # Try to get error detail | ||
| if isinstance(server_response, dict): | ||
| server_code = server_response.get("code") | ||
| err_detail = server_response.get("detail") | ||
| if not err_detail: | ||
| # Extract all field-specific errors and format them | ||
| err_detail = "\n".join( | ||
| f"{key}: {', '.join(map(str, value))}" | ||
| for key, value in server_response.items() | ||
| if isinstance(value, list) | ||
| ) or str( | ||
| server_response | ||
| ) # Fallback to raw response if structure is unexpected | ||
| else: | ||
| err_detail = str(server_response) | ||
| raise ClientError( | ||
| detail=err_detail, | ||
| @@ -244,6 +261,11 @@ def patch(self, path, data=None, headers={}): | ||
| request = urllib.request.Request(url, data, headers, method="PATCH") | ||
| return self._do_request(request) | ||
| def delete(self, path): | ||
| url = urllib.parse.urljoin(self.url, urllib.parse.quote(path)) | ||
| request = urllib.request.Request(url, method="DELETE") | ||
| return self._do_request(request) | ||
| def login(self, login, password): | ||
| """ | ||
| Authenticate login credentials and store session token | ||
| @@ -796,6 +818,12 @@ def add_user_permissions_to_project(self, project_path, usernames, permission_le | ||
| if permission_level in ("writer", "owner", "editor", "reader"): | ||
| access.get("readersnames").append(name) | ||
| self.set_project_access(project_path, access) | ||
| warnings.warn( | ||
| "This method will be deprecated in the next major release (1.0.0)" | ||
| "Use `add_project_collaborator` to create a project permission and " | ||
| "`update_project_collaborator` to change it instead.", | ||
| category=DeprecationWarning, | ||
| ) | ||
| def remove_user_permissions_from_project(self, project_path, usernames): | ||
| """ | ||
| @@ -815,6 +843,11 @@ def remove_user_permissions_from_project(self, project_path, usernames): | ||
| if name in access.get("readersnames", []): | ||
| access.get("readersnames").remove(name) | ||
| self.set_project_access(project_path, access) | ||
| warnings.warn( | ||
| "This method will be deprecated in the next major release (1.0.0)" | ||
| "Use `remove_project_collaborator` instead.", | ||
| category=DeprecationWarning, | ||
| ) | ||
| def project_user_permissions(self, project_path): | ||
| """ | ||
| @@ -1228,3 +1261,102 @@ def has_editor_support(self): | ||
| Returns whether the server version is acceptable for editor support. | ||
| """ | ||
| return is_version_acceptable(self.server_version(), "2024.4.0") | ||
| def create_user( | ||
| self, | ||
| email: str, | ||
| password: str, | ||
| workspace_id: int, | ||
| workspace_role: WorkspaceRole, | ||
| username: str = None, | ||
| notify_user: bool = False, | ||
| ) -> dict: | ||
| """ | ||
| Create a new user in a workspace. The username is generated from the email address. | ||
| param email: email of the new user - must be unique | ||
| param password: password - must meet the requirements | ||
| param workspace_id: id of the workspace user is created in | ||
| param workspace_role: workspace role of the user | ||
| param username: username - will be autogenerated from the email if not provided | ||
| param notify_user: flag for email notifications - confirmation email will be sent | ||
| """ | ||
MarcelGeo marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| params = { | ||
| "email": email, | ||
| "password": password, | ||
| "workspace_id": workspace_id, | ||
| "role": workspace_role.value, | ||
| "notify_user": notify_user, | ||
| } | ||
| if username: | ||
| params["username"] = username | ||
| user_info = self.post("v2/users", params, json_headers) | ||
| return json.load(user_info) | ||
| def get_workspace_member(self, workspace_id: int, user_id: int) -> dict: | ||
| """ | ||
| Get a workspace member detail | ||
| """ | ||
| resp = self.get(f"v2/workspaces/{workspace_id}/members/{user_id}") | ||
| return json.load(resp) | ||
| def list_workspace_members(self, workspace_id: int) -> List[dict]: | ||
| """ | ||
| Get a list of workspace members | ||
| """ | ||
| resp = self.get(f"v2/workspaces/{workspace_id}/members") | ||
| return json.load(resp) | ||
| def update_workspace_member( | ||
| self, workspace_id: int, user_id: int, workspace_role: WorkspaceRole, reset_projects_roles: bool = False | ||
| ) -> dict: | ||
| """ | ||
| Update workspace role of a workspace member, optionally resets the projects role | ||
| param reset_projects_roles: all project specific roles will be removed | ||
| """ | ||
| params = { | ||
| "reset_projects_roles": reset_projects_roles, | ||
| "workspace_role": workspace_role.value, | ||
| } | ||
| workspace_member = self.patch(f"v2/workspaces/{workspace_id}/members/{user_id}", params, json_headers) | ||
| return json.load(workspace_member) | ||
| def remove_workspace_member(self, workspace_id: int, user_id: int): | ||
| """ | ||
| Remove a user from workspace members | ||
| """ | ||
| self.delete(f"v2/workspaces/{workspace_id}/members/{user_id}") | ||
| def list_project_collaborators(self, project_id: int) -> List[dict]: | ||
| """ | ||
| Get a list of project collaborators | ||
| """ | ||
| project_collaborators = self.get(f"v2/projects/{project_id}/collaborators") | ||
| return json.load(project_collaborators) | ||
| def add_project_collaborator(self, project_id: int, user: str, project_role: ProjectRole) -> dict: | ||
| """ | ||
| Add a user to project collaborators and grant them a project role. | ||
| Fails if user is already a member of the project. | ||
| param user: login (username or email) of the user | ||
| """ | ||
| params = {"role": project_role.value, "user": user} | ||
| project_collaborator = self.post(f"v2/projects/{project_id}/collaborators", params, json_headers) | ||
| return json.load(project_collaborator) | ||
| def update_project_collaborator(self, project_id: int, user_id: int, project_role: ProjectRole) -> dict: | ||
| """ | ||
| Update project role of the existing project collaborator. | ||
| Fails if user is not a member of the project yet. | ||
| """ | ||
| params = {"role": project_role.value} | ||
| project_collaborator = self.patch(f"v2/projects/{project_id}/collaborators/{user_id}", params, json_headers) | ||
| return json.load(project_collaborator) | ||
| def remove_project_collaborator(self, project_id: int, user_id: int): | ||
| """ | ||
| Remove a user from project collaborators | ||
| """ | ||
| self.delete(f"v2/projects/{project_id}/collaborators/{user_id}") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.