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:
Igor Ilic 2024-12-13 16:54:53 +01:00
parent 67585d0ab1
commit 11634cb58d
3 changed files with 12 additions and 1 deletions

View file

@ -6,4 +6,5 @@ This module defines a set of exceptions for handling various data errors
from .exceptions import (
UnstructuredLibraryImportError,
UnauthorizedDataAccessError,
)

View file

@ -7,5 +7,14 @@ class UnstructuredLibraryImportError(CogneeApiError):
message: str = "Import error. Unstructured library is not installed.",
name: str = "UnstructuredModuleImportError",
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)

View file

@ -1,6 +1,7 @@
from uuid import UUID
from typing import Optional
from cognee.infrastructure.databases.relational import get_relational_engine
from ..exceptions import UnauthorizedDataAccessError
from ..models import 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)
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