-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
051c29f
commit 85b8083
Showing
8 changed files
with
386 additions
and
0 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
supertokens_python/recipe/dashboard/api/userroles/add_role_to_user.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from typing import Any, Union | ||
from typing_extensions import Literal | ||
from supertokens_python.exceptions import raise_bad_input_exception | ||
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions | ||
from supertokens_python.recipe.userroles.asyncio import add_role_to_user | ||
from supertokens_python.recipe.userroles.interfaces import AddRoleToUserOkResult | ||
from supertokens_python.recipe.userroles.recipe import UserRolesRecipe | ||
from supertokens_python.types import APIResponse | ||
|
||
|
||
class OkResponse(APIResponse): | ||
def __init__(self, did_user_already_have_role: bool): | ||
self.status: Literal["OK"] = "OK" | ||
self.did_user_already_have_role = did_user_already_have_role | ||
|
||
def to_json(self): | ||
return { | ||
"status": self.status, | ||
"didUserAlreadyHaveRole": self.did_user_already_have_role, | ||
} | ||
|
||
|
||
class FeatureNotEnabledErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["FEATURE_NOT_ENABLED_ERROR"] = "FEATURE_NOT_ENABLED_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
class UnknownRoleErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["UNKNOWN_ROLE_ERROR"] = "UNKNOWN_ROLE_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
async def add_role_to_user_api( | ||
_: APIInterface, tenant_id: str, api_options: APIOptions, __: Any | ||
) -> Union[OkResponse, FeatureNotEnabledErrorResponse, UnknownRoleErrorResponse]: | ||
try: | ||
UserRolesRecipe.get_instance() | ||
except Exception: | ||
return FeatureNotEnabledErrorResponse() | ||
|
||
request_body = await api_options.request.json() | ||
if request_body is None: | ||
raise_bad_input_exception("Request body is missing") | ||
|
||
user_id = request_body.get("userId") | ||
role = request_body.get("role") | ||
|
||
if role is None or not isinstance(role, str): | ||
raise_bad_input_exception( | ||
"Required parameter 'role' is missing or has an invalid type" | ||
) | ||
|
||
if user_id is None or not isinstance(user_id, str): | ||
raise_bad_input_exception( | ||
"Required parameter 'userId' is missing or has an invalid type" | ||
) | ||
|
||
response = await add_role_to_user(tenant_id, user_id, role) | ||
|
||
if isinstance(response, AddRoleToUserOkResult): | ||
return OkResponse( | ||
did_user_already_have_role=response.did_user_already_have_role | ||
) | ||
else: | ||
return UnknownRoleErrorResponse() |
46 changes: 46 additions & 0 deletions
46
supertokens_python/recipe/dashboard/api/userroles/get_role_to_user.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
from typing import Any, Union | ||
from typing_extensions import Literal | ||
from supertokens_python.exceptions import raise_bad_input_exception | ||
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions | ||
from supertokens_python.recipe.userroles.asyncio import get_roles_for_user | ||
from supertokens_python.recipe.userroles.recipe import UserRolesRecipe | ||
from supertokens_python.types import APIResponse | ||
|
||
|
||
class OkResponse(APIResponse): | ||
def __init__(self, roles: list[str]): | ||
self.status: Literal["OK"] = "OK" | ||
self.roles = roles | ||
|
||
def to_json(self): | ||
return { | ||
"status": self.status, | ||
"roles": self.roles, | ||
} | ||
|
||
|
||
class FeatureNotEnabledErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["FEATURE_NOT_ENABLED_ERROR"] = "FEATURE_NOT_ENABLED_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
async def get_roles_for_user_api( | ||
_: APIInterface, tenant_id: str, api_options: APIOptions, __: Any | ||
) -> Union[OkResponse, FeatureNotEnabledErrorResponse]: | ||
try: | ||
UserRolesRecipe.get_instance() | ||
except Exception: | ||
return FeatureNotEnabledErrorResponse() | ||
|
||
user_id = api_options.request.get_query_param("userId") | ||
|
||
if user_id is None: | ||
raise_bad_input_exception( | ||
"Required parameter 'userId' is missing or has an invalid type" | ||
) | ||
|
||
response = await get_roles_for_user(tenant_id, user_id) | ||
return OkResponse(roles=response.roles) |
65 changes: 65 additions & 0 deletions
65
supertokens_python/recipe/dashboard/api/userroles/remove_user_role.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from typing import Any, Union | ||
from typing_extensions import Literal | ||
from supertokens_python.exceptions import raise_bad_input_exception | ||
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions | ||
from supertokens_python.recipe.userroles.asyncio import remove_user_role | ||
from supertokens_python.recipe.userroles.interfaces import RemoveUserRoleOkResult | ||
from supertokens_python.recipe.userroles.recipe import UserRolesRecipe | ||
from supertokens_python.types import APIResponse | ||
|
||
|
||
class OkResponse(APIResponse): | ||
def __init__(self, did_user_have_role: bool): | ||
self.status: Literal["OK"] = "OK" | ||
self.did_user_have_role = did_user_have_role | ||
|
||
def to_json(self): | ||
return { | ||
"status": self.status, | ||
"didUserHaveRole": self.did_user_have_role, | ||
} | ||
|
||
|
||
class FeatureNotEnabledErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["FEATURE_NOT_ENABLED_ERROR"] = "FEATURE_NOT_ENABLED_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
class UnknownRoleErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["UNKNOWN_ROLE_ERROR"] = "UNKNOWN_ROLE_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
async def remove_user_role_api( | ||
_: APIInterface, tenant_id: str, api_options: APIOptions, __: Any | ||
) -> Union[OkResponse, FeatureNotEnabledErrorResponse, UnknownRoleErrorResponse]: | ||
try: | ||
UserRolesRecipe.get_instance() | ||
except Exception: | ||
return FeatureNotEnabledErrorResponse() | ||
|
||
user_id = api_options.request.get_query_param("userId") | ||
role = api_options.request.get_query_param("role") | ||
|
||
if role is None: | ||
raise_bad_input_exception( | ||
"Required parameter 'role' is missing or has an invalid type" | ||
) | ||
|
||
if user_id is None: | ||
raise_bad_input_exception( | ||
"Required parameter 'userId' is missing or has an invalid type" | ||
) | ||
|
||
response = await remove_user_role(tenant_id, user_id, role) | ||
|
||
if isinstance(response, RemoveUserRoleOkResult): | ||
return OkResponse(did_user_have_role=response.did_user_have_role) | ||
else: | ||
return UnknownRoleErrorResponse() |
54 changes: 54 additions & 0 deletions
54
supertokens_python/recipe/dashboard/api/userroles/roles/create_role_or_add_permissions.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from typing import Any, List, Union | ||
from supertokens_python.exceptions import raise_bad_input_exception | ||
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions | ||
from supertokens_python.recipe.userroles.asyncio import ( | ||
create_new_role_or_add_permissions, | ||
) | ||
from supertokens_python.recipe.userroles.recipe import UserRolesRecipe | ||
from supertokens_python.types import APIResponse | ||
|
||
|
||
class OkResponse(APIResponse): | ||
def __init__(self, created_new_role: bool): | ||
self.status = "OK" | ||
self.created_new_role = created_new_role | ||
|
||
def to_json(self): | ||
return {"status": self.status, "createdNewRole": self.created_new_role} | ||
|
||
|
||
class FeatureNotEnabledErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status = "FEATURE_NOT_ENABLED_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
async def create_role_or_add_permissions_api( | ||
_: APIInterface, __: str, api_options: APIOptions, ___: Any | ||
) -> Union[OkResponse, FeatureNotEnabledErrorResponse]: | ||
try: | ||
UserRolesRecipe.get_instance() | ||
except Exception: | ||
return FeatureNotEnabledErrorResponse() | ||
|
||
request_body = await api_options.request.json() | ||
if request_body is None: | ||
raise_bad_input_exception("Request body is missing") | ||
|
||
role = request_body.get("role") | ||
permissions: Union[List[str], None] = request_body.get("permissions") | ||
|
||
if role is None or not isinstance(role, str): | ||
raise_bad_input_exception( | ||
"Required parameter 'role' is missing or has an invalid type" | ||
) | ||
|
||
if permissions is None: | ||
raise_bad_input_exception( | ||
"Required parameter 'permissions' is missing or has an invalid type" | ||
) | ||
|
||
response = await create_new_role_or_add_permissions(role, permissions) | ||
return OkResponse(created_new_role=response.created_new_role) |
43 changes: 43 additions & 0 deletions
43
supertokens_python/recipe/dashboard/api/userroles/roles/delete_role.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Any, Union | ||
from typing_extensions import Literal | ||
from supertokens_python.exceptions import raise_bad_input_exception | ||
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions | ||
from supertokens_python.recipe.userroles.asyncio import delete_role | ||
from supertokens_python.recipe.userroles.recipe import UserRolesRecipe | ||
from supertokens_python.types import APIResponse | ||
|
||
|
||
class OkResponse(APIResponse): | ||
def __init__(self, did_role_exist: bool): | ||
self.status: Literal["OK"] = "OK" | ||
self.did_role_exist = did_role_exist | ||
|
||
def to_json(self): | ||
return {"status": self.status, "didRoleExist": self.did_role_exist} | ||
|
||
|
||
class FeatureNotEnabledErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["FEATURE_NOT_ENABLED_ERROR"] = "FEATURE_NOT_ENABLED_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
async def delete_role_api( | ||
_: APIInterface, __: str, api_options: APIOptions, ___: Any | ||
) -> Union[OkResponse, FeatureNotEnabledErrorResponse]: | ||
try: | ||
UserRolesRecipe.get_instance() | ||
except Exception: | ||
return FeatureNotEnabledErrorResponse() | ||
|
||
role = api_options.request.get_query_param("role") | ||
|
||
if role is None: | ||
raise_bad_input_exception( | ||
"Required parameter 'role' is missing or has an invalid type" | ||
) | ||
|
||
response = await delete_role(role) | ||
return OkResponse(did_role_exist=response.did_role_exist) |
35 changes: 35 additions & 0 deletions
35
supertokens_python/recipe/dashboard/api/userroles/roles/get_all_roles.py
This file contains 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from typing import Any, Union | ||
from typing_extensions import Literal | ||
from supertokens_python.recipe.dashboard.interfaces import APIInterface, APIOptions | ||
from supertokens_python.recipe.userroles.asyncio import get_all_roles | ||
from supertokens_python.recipe.userroles.recipe import UserRolesRecipe | ||
from supertokens_python.types import APIResponse | ||
|
||
|
||
class OkResponse(APIResponse): | ||
def __init__(self, roles: list[str]): | ||
self.status: Literal["OK"] = "OK" | ||
self.roles = roles | ||
|
||
def to_json(self): | ||
return {"status": self.status, "roles": self.roles} | ||
|
||
|
||
class FeatureNotEnabledErrorResponse(APIResponse): | ||
def __init__(self): | ||
self.status: Literal["FEATURE_NOT_ENABLED_ERROR"] = "FEATURE_NOT_ENABLED_ERROR" | ||
|
||
def to_json(self): | ||
return {"status": self.status} | ||
|
||
|
||
async def get_all_roles_api( | ||
_: APIInterface, __: str, api_options: APIOptions, ___: Any | ||
) -> Union[OkResponse, FeatureNotEnabledErrorResponse]: | ||
try: | ||
UserRolesRecipe.get_instance() | ||
except Exception: | ||
return FeatureNotEnabledErrorResponse() | ||
|
||
response = await get_all_roles() | ||
return OkResponse(roles=response.roles) |
This file contains 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.