From 3446fae36dc2b7e4d01f5cda342cfde88eb00951 Mon Sep 17 00:00:00 2001 From: Razco Date: Sun, 2 Jul 2023 13:48:32 +0300 Subject: [PATCH 1/6] add user invites api and approve user method --- permit/api/users.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/permit/api/users.py b/permit/api/users.py index b5d0ca6..ada57a2 100644 --- a/permit/api/users.py +++ b/permit/api/users.py @@ -40,6 +40,15 @@ def __role_assignments(self) -> SimpleHttpClient: ) ) + @property + def __user_invites(self) -> SimpleHttpClient: + return self._build_http_client( + "/v2/facts/{proj_id}/{env_id}/user_invites".format( + proj_id=self.config.api_context.project, + env_id=self.config.api_context.environment, + ) + ) + @required_permissions(ApiKeyAccessLevel.ENVIRONMENT_LEVEL_API_KEY) @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments @@ -284,3 +293,27 @@ async def get_assigned_roles( model=List[RoleAssignmentRead], params=params, ) + + @required_permissions(ApiKeyAccessLevel.ENVIRONMENT_LEVEL_API_KEY) + @required_context(ApiContextLevel.ENVIRONMENT) + @validate_arguments + async def approve_user(self, email: str, invite_code: str) -> UserRead: + """ + Approves a user. + + Args: + email: The email address of the user. + invite_code: The invite code of the user. + + Returns: + the approved new created user object. + + Raises: + PermitApiError: If the API returns an error HTTP status code. + PermitContextError: If the configured ApiContext does not match the required endpoint context. + """ + return await self.__user_invites.post( + f"/{invite_code}/approve", + model=UserRead, + json={"email": email}, + ) \ No newline at end of file From d07c0417bf7fe8a33dd74a8b5ece0d33b024d113 Mon Sep 17 00:00:00 2001 From: Razco Date: Sun, 2 Jul 2023 13:49:10 +0300 Subject: [PATCH 2/6] precommit --- permit/api/users.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/permit/api/users.py b/permit/api/users.py index ada57a2..620979f 100644 --- a/permit/api/users.py +++ b/permit/api/users.py @@ -316,4 +316,4 @@ async def approve_user(self, email: str, invite_code: str) -> UserRead: f"/{invite_code}/approve", model=UserRead, json={"email": email}, - ) \ No newline at end of file + ) From 570b39253adc64c485db3d81acba61e45d36155a Mon Sep 17 00:00:00 2001 From: Razco Date: Sun, 2 Jul 2023 17:04:23 +0300 Subject: [PATCH 3/6] update approve user to send key also --- permit/api/users.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/permit/api/users.py b/permit/api/users.py index 620979f..c90f778 100644 --- a/permit/api/users.py +++ b/permit/api/users.py @@ -261,11 +261,11 @@ async def unassign_role(self, unassignment: RoleAssignmentRemove) -> None: @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments async def get_assigned_roles( - self, - user: str, - tenant: Optional[str] = None, - page: int = 1, - per_page: int = 100, + self, + user: str, + tenant: Optional[str] = None, + page: int = 1, + per_page: int = 100, ) -> List[RoleAssignmentRead]: """ Retrieves the roles assigned to a user in a given tenant (if the tenant filter is provided) @@ -297,7 +297,7 @@ async def get_assigned_roles( @required_permissions(ApiKeyAccessLevel.ENVIRONMENT_LEVEL_API_KEY) @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments - async def approve_user(self, email: str, invite_code: str) -> UserRead: + async def approve_user(self, user_key: str, email: str, invite_code: str) -> UserRead: """ Approves a user. @@ -315,5 +315,5 @@ async def approve_user(self, email: str, invite_code: str) -> UserRead: return await self.__user_invites.post( f"/{invite_code}/approve", model=UserRead, - json={"email": email}, + json={"email": email, "key": user_key}, ) From a64bce1c3c0c7c487677f3181fc8ccb495afa915 Mon Sep 17 00:00:00 2001 From: Razco Date: Fri, 7 Jul 2023 19:08:29 +0300 Subject: [PATCH 4/6] precommit --- permit/api/users.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/permit/api/users.py b/permit/api/users.py index c90f778..5b06a57 100644 --- a/permit/api/users.py +++ b/permit/api/users.py @@ -261,11 +261,11 @@ async def unassign_role(self, unassignment: RoleAssignmentRemove) -> None: @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments async def get_assigned_roles( - self, - user: str, - tenant: Optional[str] = None, - page: int = 1, - per_page: int = 100, + self, + user: str, + tenant: Optional[str] = None, + page: int = 1, + per_page: int = 100, ) -> List[RoleAssignmentRead]: """ Retrieves the roles assigned to a user in a given tenant (if the tenant filter is provided) @@ -297,7 +297,9 @@ async def get_assigned_roles( @required_permissions(ApiKeyAccessLevel.ENVIRONMENT_LEVEL_API_KEY) @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments - async def approve_user(self, user_key: str, email: str, invite_code: str) -> UserRead: + async def approve_user( + self, user_key: str, email: str, invite_code: str + ) -> UserRead: """ Approves a user. From 01af3301264e4032176352512583e97def412919 Mon Sep 17 00:00:00 2001 From: Razco Date: Wed, 12 Jul 2023 14:44:18 +0300 Subject: [PATCH 5/6] add attributes to approve --- permit/api/users.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/permit/api/users.py b/permit/api/users.py index 5b06a57..ec0e02d 100644 --- a/permit/api/users.py +++ b/permit/api/users.py @@ -297,8 +297,8 @@ async def get_assigned_roles( @required_permissions(ApiKeyAccessLevel.ENVIRONMENT_LEVEL_API_KEY) @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments - async def approve_user( - self, user_key: str, email: str, invite_code: str + async def approve( + self, user_key: str, email: str, invite_code: str, attributes: Optional[dict] = None ) -> UserRead: """ Approves a user. @@ -317,5 +317,5 @@ async def approve_user( return await self.__user_invites.post( f"/{invite_code}/approve", model=UserRead, - json={"email": email, "key": user_key}, + json={"email": email, "key": user_key, "attributes": attributes or None}, ) From 06b9e586d0ba8df89d1c3aa0bf3c8d8248564d00 Mon Sep 17 00:00:00 2001 From: Razco Date: Wed, 12 Jul 2023 14:44:59 +0300 Subject: [PATCH 6/6] precommit --- permit/api/users.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/permit/api/users.py b/permit/api/users.py index ec0e02d..b293a4b 100644 --- a/permit/api/users.py +++ b/permit/api/users.py @@ -298,7 +298,11 @@ async def get_assigned_roles( @required_context(ApiContextLevel.ENVIRONMENT) @validate_arguments async def approve( - self, user_key: str, email: str, invite_code: str, attributes: Optional[dict] = None + self, + user_key: str, + email: str, + invite_code: str, + attributes: Optional[dict] = None, ) -> UserRead: """ Approves a user.