feat: Add unauth access error to getting data
Raise unauth access error when trying to read data without access Feature COG-656
This commit is contained in:
parent
67585d0ab1
commit
11634cb58d
3 changed files with 12 additions and 1 deletions
|
|
@ -6,4 +6,5 @@ This module defines a set of exceptions for handling various data errors
|
||||||
|
|
||||||
from .exceptions import (
|
from .exceptions import (
|
||||||
UnstructuredLibraryImportError,
|
UnstructuredLibraryImportError,
|
||||||
|
UnauthorizedDataAccessError,
|
||||||
)
|
)
|
||||||
|
|
@ -7,5 +7,14 @@ class UnstructuredLibraryImportError(CogneeApiError):
|
||||||
message: str = "Import error. Unstructured library is not installed.",
|
message: str = "Import error. Unstructured library is not installed.",
|
||||||
name: str = "UnstructuredModuleImportError",
|
name: str = "UnstructuredModuleImportError",
|
||||||
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
|
||||||
|
):
|
||||||
|
super().__init__(message, name, status_code)
|
||||||
|
|
||||||
|
class UnauthorizedDataAccessError(CogneeApiError):
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
message: str = "Usesr does not have permission to access this data.",
|
||||||
|
name: str = "UnauthorizedDataAccessError",
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
):
|
):
|
||||||
super().__init__(message, name, status_code)
|
super().__init__(message, name, status_code)
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from cognee.infrastructure.databases.relational import get_relational_engine
|
from cognee.infrastructure.databases.relational import get_relational_engine
|
||||||
|
from ..exceptions import UnauthorizedDataAccessError
|
||||||
from ..models import Data
|
from ..models import Data
|
||||||
|
|
||||||
async def get_data(user_id: UUID, data_id: UUID) -> Optional[Data]:
|
async def get_data(user_id: UUID, data_id: UUID) -> Optional[Data]:
|
||||||
|
|
@ -19,6 +20,6 @@ async def get_data(user_id: UUID, data_id: UUID) -> Optional[Data]:
|
||||||
data = await session.get(Data, data_id)
|
data = await session.get(Data, data_id)
|
||||||
|
|
||||||
if data and data.owner_id != user_id:
|
if data and data.owner_id != user_id:
|
||||||
return None
|
raise UnauthorizedDataAccessError(message=f"User {user_id} is not authorized to access data {data_id}")
|
||||||
|
|
||||||
return data
|
return data
|
||||||
Loading…
Add table
Reference in a new issue