Skip to content

Commit

Permalink
fix(misc): functional grant and revoke in Console
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba committed Nov 14, 2024
1 parent 0524037 commit 6470fb0
Show file tree
Hide file tree
Showing 19 changed files with 261 additions and 393 deletions.
2 changes: 0 additions & 2 deletions console/api/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
fetch_organization_users,
fetch_organization_workspaces,
fetch_organization_groups,
update_organization,
fetch_organization_count,
)
from .snapshot import fetch_snapshot, fetch_snapshots
Expand All @@ -31,7 +30,6 @@
from .workspace import (
fetch_workspace,
fetch_workspaces,
update_workspace,
fetch_workspace_count,
)
from .overview import fetch_version
Expand Down
12 changes: 0 additions & 12 deletions console/api/database/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,6 @@ def fetch_groups(page=1, size=10) -> Tuple[Iterable[Dict], int]:
raise error


# --- UPDATE --- #
def update_group(data: dict) -> None:
try:
with conn.cursor() as curs:
if not exists(curs=curs, _id=data["id"], tablename="group"):
raise NotFoundException(f"Group with id={data['id']} does not exist!")

curs.execute(parse_sql_update_query("group", data))
except DatabaseError as error:
raise error


# --- CREATE --- #

# --- DELETE --- #
13 changes: 0 additions & 13 deletions console/api/database/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,6 @@ def fetch_organization_groups(
raise error


# --- UPDATE --- #
def update_organization(data: Dict) -> None:
try:
with conn.cursor() as curs:
if not exists(curs=curs, _id=data["id"], tablename="organization"):
raise NotFoundException(
f"Organization with id={data['id']} does not exist!"
)
curs.execute(parse_sql_update_query("organization", data))
except DatabaseError as error:
raise error


# --- CREATE --- #

# --- DELETE --- #
9 changes: 0 additions & 9 deletions console/api/database/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,6 @@ def fetch_workspaces(page=1, size=10) -> Tuple[Iterable[Dict], int]:
raise error


# --- UPDATE --- #
def update_workspace(data: dict) -> None:
try:
with conn.cursor() as curs:
curs.execute(parse_sql_update_query("workspace", data))
except DatabaseError as error:
raise error


# --- CREATE --- #

# --- DELETE --- #
3 changes: 0 additions & 3 deletions console/api/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
GroupListRequest,
GroupResponse,
GroupListResponse,
UpdateGroupRequest,
GroupSearchRequest,
)
from .grouppermission import (
Expand All @@ -51,7 +50,6 @@
OrganizationListRequest,
OrganizationResponse,
OrganizationListResponse,
UpdateOrganizationRequest,
OrganizationWorkspaceResponse,
OrganizationUserListResponse,
OrganizationUserResponse,
Expand Down Expand Up @@ -106,7 +104,6 @@
WorkspaceResponse,
WorkspaceListResponse,
WorkspaceOrganizationListRequest,
UpdateWorkspaceRequest,
WorkspaceSearchRequest,
)
from .token import TokenResponse, TokenPayload
Expand Down
5 changes: 0 additions & 5 deletions console/api/models/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ class GroupSearchRequest(GenericSearchRequest):
pass


class UpdateGroupRequest(GenericRequest):
name: str | None = Field(None)
updateTime: datetime.datetime | None = Field(default_factory=datetime.datetime.now)


# --- RESPONSE MODELS --- #
class GroupResponse(GenericResponse):
name: str
Expand Down
5 changes: 0 additions & 5 deletions console/api/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,6 @@ class OrganizationGroupListRequest(GenericRequest, GenericPaginationRequest):
pass


class UpdateOrganizationRequest(GenericRequest):
name: str = Field(None)
updateTime: datetime.datetime = Field(default_factory=datetime.datetime.now)


# --- RESPONSE MODELS --- #
class OrganizationResponse(GenericResponse):
name: str
Expand Down
12 changes: 6 additions & 6 deletions console/api/models/userpermission.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ class UserPermissionListRequest(GenericPaginationRequest):


class UserPermissionGrantRequest(BaseModel):
user_id: str
resource_id: str
resource_type: Literal["file", "group", "organization", "workspace"]
userId: str
resourceId: str
resourceType: Literal["file", "group", "organization", "workspace"]
permission: str


class UserPermissionRevokeRequest(BaseModel):
user_id: str
resource_id: str
resource_type: Literal["file", "group", "organization", "workspace"]
userId: str
resourceId: str
resourceType: Literal["file", "group", "organization", "workspace"]


# --- RESPONSE MODELS --- #
Expand Down
9 changes: 0 additions & 9 deletions console/api/models/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ class WorkspaceRequest(GenericRequest):
pass


class UpdateWorkspaceRequest(GenericRequest):
name: str | None = Field(None)
organizationId: str | None = Field(None)
storageCapacity: float | None = Field(None)
rootId: str | None = Field(None)
bucket: str | None = Field(None)
updateTime: datetime.datetime | None = Field(default_factory=datetime.datetime.now)


class WorkspaceListRequest(GenericPaginationRequest):
pass

Expand Down
29 changes: 1 addition & 28 deletions console/api/routers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from fastapi import APIRouter, Depends, status, Response

from ..database import fetch_groups, fetch_group
from ..database.group import update_group, fetch_group_count
from ..database.group import fetch_group_count
from ..dependencies import JWTBearer, meilisearch_client
from ..log import base_logger
from ..errors import (
Expand All @@ -28,7 +28,6 @@
GroupListRequest,
GroupListResponse,
GroupRequest,
UpdateGroupRequest,
CountResponse,
GroupSearchRequest,
)
Expand Down Expand Up @@ -123,32 +122,6 @@ async def get_search_groups(data: Annotated[GroupSearchRequest, Depends()]):
return UnknownApiError()


# --- PATCH --- #
@group_api_router.patch(path="", status_code=status.HTTP_202_ACCEPTED)
async def patch_group(data: UpdateGroupRequest, response: Response):
try:
await redis_conn.delete(f"group:{data.id}")
update_group(data=data.model_dump(exclude_none=True))
meilisearch_client.index("group").update_documents(
[
{
"id": data.id,
"name": data.name,
"updateTime": data.updateTime.strftime("%Y-%m-%dT%H:%M:%SZ"),
}
]
)
except NotFoundException as e:
logger.error(e)
return NotFoundError(message=str(e))
except Exception as e:
logger.exception(e)
return UnknownApiError()

response.status_code = status.HTTP_202_ACCEPTED
return None


# --- POST --- #

# --- PUT --- #
Expand Down
28 changes: 0 additions & 28 deletions console/api/routers/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
fetch_organizations,
fetch_organization_users,
fetch_organization_workspaces,
update_organization,
fetch_organization_groups,
fetch_organization_count,
)
Expand All @@ -36,7 +35,6 @@
OrganizationListResponse,
OrganizationListRequest,
OrganizationWorkspaceListRequest,
UpdateOrganizationRequest,
OrganizationWorkspaceListResponse,
OrganizationGroupListResponse,
OrganizationGroupListRequest,
Expand Down Expand Up @@ -229,32 +227,6 @@ async def get_organization_groups(
return UnknownApiError()


# --- PATCH --- #
@organization_api_router.patch(path="", status_code=status.HTTP_202_ACCEPTED)
async def patch_organization(data: UpdateOrganizationRequest, response: Response):
try:
await redis_conn.delete(f"organization:{data.id}")
update_organization(data=data.model_dump(exclude_none=True))
meilisearch_client.index("organization").update_documents(
[
{
"id": data.id,
"name": data.name,
"updateTime": data.updateTime.strftime("%Y-%m-%dT%H:%M:%SZ"),
}
]
)
except NotFoundException as e:
logger.error(e)
return NotFoundError(message=str(e))
except Exception as e:
logger.exception(e)
return UnknownApiError()

response.status_code = status.HTTP_202_ACCEPTED
return None


# --- POST --- #

# --- PUT --- #
Expand Down
12 changes: 6 additions & 6 deletions console/api/routers/userpermission.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@

@user_permission_api_router.post(path="/grant", status_code=status.HTTP_200_OK)
async def post_grant_user_permission(data: UserPermissionGrantRequest):
await redis_conn.delete(f"{data.resource_type}:{data.resource_id}")
await redis_conn.delete(f"{data.resourceType}:{data.resourceId}")
grant_user_permission(
user_id=data.user_id,
resource_id=data.resource_id,
user_id=data.userId,
resource_id=data.resourceId,
permission=data.permission,
)
return Response(status_code=status.HTTP_200_OK)


@user_permission_api_router.post(path="/revoke", status_code=status.HTTP_200_OK)
async def post_revoke_user_permission(data: UserPermissionRevokeRequest):
await redis_conn.delete(f"{data.resource_type}:{data.resource_id}")
await redis_conn.delete(f"{data.resourceType}:{data.resourceId}")
revoke_user_permission(
user_id=data.user_id,
resource_id=data.resource_id,
user_id=data.userId,
resource_id=data.resourceId,
)
return Response(status_code=status.HTTP_200_OK)
29 changes: 0 additions & 29 deletions console/api/routers/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from ..database import (
fetch_workspace,
fetch_workspaces,
update_workspace,
fetch_workspace_count,
)
from ..dependencies import JWTBearer, meilisearch_client, redis_conn
Expand All @@ -32,7 +31,6 @@
WorkspaceRequest,
WorkspaceListResponse,
WorkspaceListRequest,
UpdateWorkspaceRequest,
GenericUnexpectedErrorResponse,
GenericAcceptedResponse,
CountResponse,
Expand Down Expand Up @@ -137,33 +135,6 @@ async def get_search_workspaces(data: Annotated[WorkspaceSearchRequest, Depends(
return UnknownApiError()


# --- PATCH --- #
@workspace_api_router.patch(path="", status_code=status.HTTP_202_ACCEPTED)
async def patch_workspace(data: UpdateWorkspaceRequest, response: Response):
try:
await redis_conn.delete(f"workspace:{data.id}")
update_workspace(data=data.model_dump(exclude_none=True))
meilisearch_client.index("workspace").update_documents(
[
{
"id": data.id,
"name": data.name,
"storageCapacity": data.storageCapacity,
"updateTime": data.updateTime.strftime("%Y-%m-%dT%H:%M:%SZ"),
}
]
)
except NotFoundException as e:
logger.error(e)
return NotFoundError(message=str(e))
except Exception as e:
logger.exception(e)
return UnknownApiError()

response.status_code = status.HTTP_202_ACCEPTED
return None


# --- POST --- #

# --- PUT --- #
Expand Down
29 changes: 29 additions & 0 deletions ui/src/client/console/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,19 @@ export interface InvitationStatusRequest extends BaseIDRequest {
accept: boolean
}

export type GrantUserPermissionOptions = {
userId: string
resourceId: string
resourceType: 'file' | 'group' | 'organization' | 'workspace'
permission: string
}

export type RevokeUserPermissionOptions = {
userId: string
resourceId: string
resourceType: 'file' | 'group' | 'organization' | 'workspace'
}

export interface CountResponse {
count: number
}
Expand Down Expand Up @@ -382,6 +395,22 @@ export default class ConsoleAPI {
}
}

static grantUserPermission(options: GrantUserPermissionOptions) {
return consoleFetcher({
url: '/user_permission/grant',
method: 'POST',
body: JSON.stringify(options),
}) as Promise<void>
}

static revokeUserPermission(options: RevokeUserPermissionOptions) {
return consoleFetcher({
url: '/user_permission/revoke',
method: 'POST',
body: JSON.stringify(options),
}) as Promise<void>
}

static paramsFromListOptions = (options: ListOptions): URLSearchParams => {
const params: ListQueryParams = {}
if (options.id) {
Expand Down
Loading

0 comments on commit 6470fb0

Please sign in to comment.