Skip to content

Commit

Permalink
more apis
Browse files Browse the repository at this point in the history
  • Loading branch information
rishabhpoddar committed Sep 17, 2024
1 parent 051c29f commit 85b8083
Show file tree
Hide file tree
Showing 8 changed files with 386 additions and 0 deletions.
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()
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)
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()
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)
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)
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)
2 changes: 2 additions & 0 deletions supertokens_python/recipe/dashboard/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
UNLINK_USER = "/api/user/unlink"
USERROLES_PERMISSIONS_API = "/api/userroles/role/permissions"
USERROLES_REMOVE_PERMISSIONS_API = "/api/userroles/role/permissions/remove"
USERROLES_ROLE_API = "/api/userroles/role"
USERROLES_USER_API = "/api/userroles/user/roles"
Loading

0 comments on commit 85b8083

Please sign in to comment.