Skip to content

A modern platform for visual, flexible, and extensible graph-based investigations. For cybersecurity analysts and investigators.

feat(core): add RBAC with ADMIN role, collaborator management and auth user data

dextmorgn1a5d16480589
diff --git a/flowsint-core/src/flowsint_core/core/repositories/base.py b/flowsint-core/src/flowsint_core/core/repositories/base.py
index e8fef10b..e35183f6 100644
--- a/flowsint-core/src/flowsint_core/core/repositories/base.py
+++ b/flowsint-core/src/flowsint_core/core/repositories/base.py
@@ -22,7 +22,7 @@ class BaseRepository(Generic[T]):
self, user_id: UUID, allowed_roles: Optional[List[Role]] = None
) -> Set[UUID]:
if allowed_roles is None:
- allowed_roles = [Role.OWNER, Role.EDITOR, Role.VIEWER]
+ allowed_roles = [Role.OWNER, Role.ADMIN, Role.EDITOR, Role.VIEWER]
role_entries = (
self._db.query(InvestigationUserRole)
.filter(InvestigationUserRole.user_id == user_id)
diff --git a/flowsint-core/src/flowsint_core/core/repositories/investigation_repository.py b/flowsint-core/src/flowsint_core/core/repositories/investigation_repository.py
index 40de3857..a9ab2c85 100644
--- a/flowsint-core/src/flowsint_core/core/repositories/investigation_repository.py
+++ b/flowsint-core/src/flowsint_core/core/repositories/investigation_repository.py
@@ -32,7 +32,7 @@ class InvestigationRepository(BaseRepository[Investigation]):
)
def get_with_relations(
- self, investigation_id: UUID, owner_id: UUID
+ self, investigation_id: UUID
) -> Optional[Investigation]:
return (
self._db.query(Investigation)
@@ -41,10 +41,7 @@ class InvestigationRepository(BaseRepository[Investigation]):
selectinload(Investigation.analyses),
selectinload(Investigation.owner),
)
- .filter(
- Investigation.id == investigation_id,
- Investigation.owner_id == owner_id,
- )
+ .filter(Investigation.id == investigation_id)
.first()
)
@@ -72,3 +69,30 @@ class InvestigationRepository(BaseRepository[Investigation]):
def add_user_role(self, role_entry: InvestigationUserRole) -> InvestigationUserRole:
self._db.add(role_entry)
return role_entry
+
+ def get_collaborators(
+ self, investigation_id: UUID
+ ) -> List[InvestigationUserRole]:
+ return (
+ self._db.query(InvestigationUserRole)
+ .options(selectinload(InvestigationUserRole.user))
+ .filter(InvestigationUserRole.investigation_id == investigation_id)
+ .all()
+ )
+
+ def update_user_role(
+ self, user_id: UUID, investigation_id: UUID, roles: List[Role]
+ ) -> Optional[InvestigationUserRole]:
+ entry = self.get_user_role(user_id, investigation_id)
+ if entry:
+ entry.roles = roles
+ return entry
+
+ def remove_user_role(
+ self, user_id: UUID, investigation_id: UUID
+ ) -> bool:
+ entry = self.get_user_role(user_id, investigation_id)
+ if entry:
+ self._db.delete(entry)
+ return True
+ return False
diff --git a/flowsint-core/src/flowsint_core/core/services/auth_service.py b/flowsint-core/src/flowsint_core/core/services/auth_service.py
index db4015ab..5b4a7af7 100644
--- a/flowsint-core/src/flowsint_core/core/services/auth_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/auth_service.py
@@ -32,8 +32,15 @@ class AuthService(BaseService):
return {
"access_token": access_token,
- "user_id": user.id,
"token_type": "bearer",
+ "user": {
+ "id": str(user.id),
+ "email": user.email,
+ "username": user.email.split("@")[0],
+ "first_name": user.first_name,
+ "last_name": user.last_name,
+ "avatar_url": user.avatar_url,
+ },
}
def register(self, email: str, password: str) -> Dict[str, Any]:
diff --git a/flowsint-core/src/flowsint_core/core/services/base.py b/flowsint-core/src/flowsint_core/core/services/base.py
index ddd504d1..03e585f7 100644
--- a/flowsint-core/src/flowsint_core/core/services/base.py
+++ b/flowsint-core/src/flowsint_core/core/services/base.py
@@ -54,6 +54,8 @@ class BaseService:
for action in actions:
if role == Role.OWNER:
return True
+ if role == Role.ADMIN and action in ["read", "create", "update", "manage"]:
+ return True
if role == Role.EDITOR and action in ["read", "create", "update"]:
return True
if role == Role.VIEWER and action == "read":
diff --git a/flowsint-core/src/flowsint_core/core/services/investigation_service.py b/flowsint-core/src/flowsint_core/core/services/investigation_service.py
index 59a9b1bc..ed6be1a9 100644
--- a/flowsint-core/src/flowsint_core/core/services/investigation_service.py
+++ b/flowsint-core/src/flowsint_core/core/services/investigation_service.py
@@ -11,9 +11,19 @@ from sqlalchemy.orm import Session
from ..models import Investigation, InvestigationUserRole, Sketch, Analysis
from ..types import Role
from ..graph import create_graph_service
-from ..repositories import InvestigationRepository, SketchRepository, AnalysisRepository
+from ..repositories import (
+ InvestigationRepository,
+ SketchRepository,
+ AnalysisRepository,
+ ProfileRepository,
+)
from .base import BaseService
-from .exceptions import NotFoundError, PermissionDeniedError, DatabaseError
+from .exceptions import (
+ NotFoundError,
+ PermissionDeniedError,
+ ConflictError,
+ DatabaseError,
+)
class InvestigationService(BaseService):
@@ -27,12 +37,14 @@ class InvestigationService(BaseService):
investigation_repo: InvestigationRepository,
sketch_repo: SketchRepository,
analysis_repo: AnalysisRepository,
+ profile_repo: ProfileRepository,
**kwargs,
):
super().__init__(db, **kwargs)
self._investigation_repo = investigation_repo
self._sketch_repo = sketch_repo
self._analysis_repo = analysis_repo
+ self._profile_repo = profile_repo
def get_accessible_investigations(
self, user_id: UUID, allowed_roles: Optional[List[Role]] = None
@@ -42,9 +54,7 @@ class InvestigationService(BaseService):
def get_by_id(self, investigation_id: UUID, user_id: UUID) -> Investigation:
self._check_permission(user_id, investigation_id, actions=["read"])
- investigation = self._investigation_repo.get_with_relations(
- investigation_id, user_id
- )
+ investigation = self._investigation_repo.get_with_relations(investigation_id)
if not investigation:
raise NotFoundError("Investigation not found")
return investigation
@@ -90,7 +100,7 @@ class InvestigationService(BaseService):
description: str,
status: str,
) -> Investigation:
- self._check_permission(user_id, investigation_id, actions=["write"])
+ self._check_permission(user_id, investigation_id, actions=["update"])
investigation = self._investigation_repo.get_by_id(investigation_id)
if not investigation:
@@ -137,6 +147,109 @@ class InvestigationService(BaseService):
self._investigation_repo.delete(investigation)
self._commit()
+ # ── Collaborator management ──────────────────────────────────────────
+
+ def get_user_role_for_investigation(
+ self, user_id: UUID, investigation_id: UUID
+ ) -> Optional[InvestigationUserRole]:
+ return self._investigation_repo.get_user_role(user_id, investigation_id)
+
+ def get_collaborators(
+ self, investigation_id: UUID, user_id: UUID
+ ) -> List[InvestigationUserRole]:
+ self._check_permission(user_id, investigation_id, actions=["read"])
+ return self._investigation_repo.get_collaborators(investigation_id)
+
+ def add_collaborator(
+ self,
+ investigation_id: UUID,
+ user_id: UUID,
+ target_email: str,
+ role: Role,
+ ) -> InvestigationUserRole:
+ self._check_permission(user_id, investigation_id, actions=["manage"])
+
+ # Verify investigation exists
+ investigation = self._investigation_repo.get_by_id(investigation_id)
+ if not investigation:
+ raise NotFoundError("Investigation not found")
+
+ # Cannot assign OWNER role
+ if role == Role.OWNER:
+ raise PermissionDeniedError("Cannot assign owner role")
+
+ # Look up target user by email
+ target_user = self._profile_repo.get_by_email(target_email)
+ if not target_user:
+ raise NotFoundError("User not found")
+
+ # Check if already a collaborator
+ existing = self._investigation_repo.get_user_role(
+ target_user.id, investigation_id
+ )
+ if existing:
+ raise ConflictError("User is already a collaborator")
+
+ role_entry = InvestigationUserRole(
+ id=uuid4(),
+ user_id=target_user.id,
+ investigation_id=investigation_id,
+ roles=[role],
+ )
+ self._investigation_repo.add_user_role(role_entry)
+ self._commit()
+ self._db.refresh(role_entry)
+ return role_entry
+
+ def update_collaborator_role(
+ self,
+ investigation_id: UUID,
+ user_id: UUID,
+ target_user_id: UUID,
+ role: Role,
+ ) -> InvestigationUserRole:
+ self._check_permission(user_id, investigation_id, actions=["manage"])
+
+ if role == Role.OWNER:
+ raise PermissionDeniedError("Cannot assign owner role")
+
+ existing = self._investigation_repo.get_user_role(
+ target_user_id, investigation_id
+ )
+ if not existing:
+ raise NotFoundError("Collaborator not found")
+
+ # Cannot change the owner's role
+ if Role.OWNER in existing.roles:
+ raise PermissionDeniedError("Cannot change owner role")
+
+ entry = self._investigation_repo.update_user_role(
+ target_user_id, investigation_id, [role]
+ )
+ self._commit()
+ self._db.refresh(entry)
+ return entry
+
+ def remove_collaborator(
+ self,
+ investigation_id: UUID,
+ user_id: UUID,
+ target_user_id: UUID,
+ ) -> None:
+ self._check_permission(user_id, investigation_id, actions=["manage"])
+
+ existing = self._investigation_repo.get_user_role(
+ target_user_id, investigation_id
+ )
+ if not existing:
+ raise NotFoundError("Collaborator not found")
+
+ if Role.OWNER in existing.roles:
+ raise PermissionDeniedError("Cannot remove owner")
+
+ self._investigation_repo.remove_user_role(target_user_id, investigation_id)
+ self._commit()
+
def create_investigation_service(db: Session) -> InvestigationService:
investigation_repo = InvestigationRepository(db)
@@ -145,4 +258,5 @@ def create_investigation_service(db: Session) -> InvestigationService:
investigation_repo=investigation_repo,
sketch_repo=SketchRepository(db),
analysis_repo=AnalysisRepository(db),
+ profile_repo=ProfileRepository(db),
)
diff --git a/flowsint-core/src/flowsint_core/core/types.py b/flowsint-core/src/flowsint_core/core/types.py
index 1b6cb594..952e8921 100644
--- a/flowsint-core/src/flowsint_core/core/types.py
+++ b/flowsint-core/src/flowsint_core/core/types.py
@@ -103,5 +103,6 @@ class FlowBranch(BaseModel):
class Role(str, enum.Enum):
OWNER = "owner"
+ ADMIN = "admin"
EDITOR = "editor"
VIEWER = "viewer"