regirstation works but shared links broken
This commit is contained in:
@@ -6,11 +6,13 @@ from fastapi import HTTPException, status
|
||||
from geoalchemy2.shape import from_shape, to_shape
|
||||
from shapely.geometry import shape, Point, LineString
|
||||
import json
|
||||
import asyncio
|
||||
|
||||
from app.models.map_item import MapItem
|
||||
from app.models.user import User
|
||||
from app.schemas.map_item import MapItemCreate, MapItemUpdate
|
||||
from app.services.map_service import get_map_by_id
|
||||
from app.websocket.connection_manager import manager
|
||||
|
||||
|
||||
def get_map_items(db: Session, map_id: UUID, user: Optional[User] = None) -> List[MapItem]:
|
||||
@@ -58,6 +60,19 @@ def geography_to_geojson(geography) -> dict:
|
||||
return json.loads(json.dumps(geom.__geo_interface__))
|
||||
|
||||
|
||||
def item_to_dict(item: MapItem) -> dict:
|
||||
"""Convert MapItem to JSON-serializable dict for WebSocket broadcast."""
|
||||
return {
|
||||
"id": str(item.id),
|
||||
"map_id": str(item.map_id),
|
||||
"type": item.type,
|
||||
"geometry": geography_to_geojson(item.geometry),
|
||||
"properties": item.properties,
|
||||
"created_at": item.created_at.isoformat(),
|
||||
"updated_at": item.updated_at.isoformat()
|
||||
}
|
||||
|
||||
|
||||
def create_map_item(db: Session, map_id: UUID, item_data: MapItemCreate, user: User) -> MapItem:
|
||||
"""Create a new map item."""
|
||||
# Verify user has access to the map
|
||||
@@ -93,6 +108,14 @@ def create_map_item(db: Session, map_id: UUID, item_data: MapItemCreate, user: U
|
||||
print(f"Updating port connections for end device: {end_device_id}")
|
||||
update_device_connections(db, UUID(end_device_id), item.id)
|
||||
|
||||
# Broadcast item creation to WebSocket clients
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(manager.send_item_created(map_id, item_to_dict(item)))
|
||||
except RuntimeError:
|
||||
# No event loop running, skip WebSocket broadcast
|
||||
pass
|
||||
|
||||
return item
|
||||
|
||||
|
||||
@@ -155,6 +178,14 @@ def update_map_item(db: Session, item_id: UUID, item_data: MapItemUpdate, user:
|
||||
db.commit()
|
||||
db.refresh(item)
|
||||
|
||||
# Broadcast item update to WebSocket clients
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(manager.send_item_updated(item.map_id, item_to_dict(item)))
|
||||
except RuntimeError:
|
||||
# No event loop running, skip WebSocket broadcast
|
||||
pass
|
||||
|
||||
return item
|
||||
|
||||
|
||||
@@ -162,6 +193,10 @@ def delete_map_item(db: Session, item_id: UUID, user: User) -> None:
|
||||
"""Delete a map item."""
|
||||
item = get_map_item_by_id(db, item_id, user)
|
||||
|
||||
# Capture map_id and item_id before deletion for WebSocket broadcast
|
||||
map_id = item.map_id
|
||||
deleted_item_id = str(item.id)
|
||||
|
||||
# If deleting a cable, remove it from device connections
|
||||
if item.type == 'cable':
|
||||
start_device_id = item.properties.get('start_device_id')
|
||||
@@ -192,6 +227,14 @@ def delete_map_item(db: Session, item_id: UUID, user: User) -> None:
|
||||
db.delete(item)
|
||||
db.commit()
|
||||
|
||||
# Broadcast item deletion to WebSocket clients
|
||||
try:
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.create_task(manager.send_item_deleted(map_id, deleted_item_id))
|
||||
except RuntimeError:
|
||||
# No event loop running, skip WebSocket broadcast
|
||||
pass
|
||||
|
||||
|
||||
def remove_device_connection(db: Session, device_id: UUID, cable_id: UUID) -> None:
|
||||
"""Remove cable connection from device's connections array."""
|
||||
|
||||
299
app/services/map_share_service.py
Normal file
299
app/services/map_share_service.py
Normal file
@@ -0,0 +1,299 @@
|
||||
"""Map sharing service for business logic."""
|
||||
from typing import List, Optional
|
||||
from uuid import UUID
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi import HTTPException, status
|
||||
from datetime import datetime
|
||||
import secrets
|
||||
|
||||
from app.models.map_share import MapShare, MapShareLink, SharePermission
|
||||
from app.models.user import User
|
||||
from app.models.map import Map
|
||||
from app.schemas.map_share import MapShareCreate, MapShareUpdate, MapShareLinkCreate
|
||||
from app.services.map_service import get_map_by_id
|
||||
|
||||
|
||||
def generate_share_token() -> str:
|
||||
"""Generate a random share token."""
|
||||
return secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
def create_map_share(
|
||||
db: Session,
|
||||
map_id: UUID,
|
||||
share_data: MapShareCreate,
|
||||
current_user: User
|
||||
) -> MapShare:
|
||||
"""Share a map with a specific user."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can share it"
|
||||
)
|
||||
|
||||
# Check if user exists
|
||||
target_user = db.query(User).filter(User.id == share_data.user_id).first()
|
||||
if not target_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found"
|
||||
)
|
||||
|
||||
# Check if already shared
|
||||
existing_share = db.query(MapShare).filter(
|
||||
MapShare.map_id == map_id,
|
||||
MapShare.user_id == share_data.user_id
|
||||
).first()
|
||||
|
||||
if existing_share:
|
||||
# Update existing share
|
||||
existing_share.permission = share_data.permission
|
||||
existing_share.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(existing_share)
|
||||
return existing_share
|
||||
|
||||
# Create new share
|
||||
share = MapShare(
|
||||
map_id=map_id,
|
||||
user_id=share_data.user_id,
|
||||
permission=share_data.permission,
|
||||
shared_by=current_user.id
|
||||
)
|
||||
|
||||
db.add(share)
|
||||
db.commit()
|
||||
db.refresh(share)
|
||||
|
||||
return share
|
||||
|
||||
|
||||
def get_map_shares(db: Session, map_id: UUID, current_user: User) -> List[MapShare]:
|
||||
"""Get all shares for a map."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can view shares"
|
||||
)
|
||||
|
||||
shares = db.query(MapShare).filter(MapShare.map_id == map_id).all()
|
||||
return shares
|
||||
|
||||
|
||||
def update_map_share(
|
||||
db: Session,
|
||||
map_id: UUID,
|
||||
share_id: UUID,
|
||||
update_data: MapShareUpdate,
|
||||
current_user: User
|
||||
) -> MapShare:
|
||||
"""Update map share permissions."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can update shares"
|
||||
)
|
||||
|
||||
share = db.query(MapShare).filter(
|
||||
MapShare.id == share_id,
|
||||
MapShare.map_id == map_id
|
||||
).first()
|
||||
|
||||
if not share:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Share not found"
|
||||
)
|
||||
|
||||
share.permission = update_data.permission
|
||||
share.updated_at = datetime.utcnow()
|
||||
|
||||
db.commit()
|
||||
db.refresh(share)
|
||||
|
||||
return share
|
||||
|
||||
|
||||
def delete_map_share(
|
||||
db: Session,
|
||||
map_id: UUID,
|
||||
share_id: UUID,
|
||||
current_user: User
|
||||
) -> None:
|
||||
"""Revoke map share."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can revoke shares"
|
||||
)
|
||||
|
||||
share = db.query(MapShare).filter(
|
||||
MapShare.id == share_id,
|
||||
MapShare.map_id == map_id
|
||||
).first()
|
||||
|
||||
if not share:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Share not found"
|
||||
)
|
||||
|
||||
db.delete(share)
|
||||
db.commit()
|
||||
|
||||
|
||||
def create_share_link(
|
||||
db: Session,
|
||||
map_id: UUID,
|
||||
link_data: MapShareLinkCreate,
|
||||
current_user: User
|
||||
) -> MapShareLink:
|
||||
"""Create a public/guest share link."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can create share links"
|
||||
)
|
||||
|
||||
token = generate_share_token()
|
||||
|
||||
link = MapShareLink(
|
||||
map_id=map_id,
|
||||
token=token,
|
||||
permission=link_data.permission,
|
||||
is_active=True,
|
||||
created_by=current_user.id,
|
||||
expires_at=link_data.expires_at
|
||||
)
|
||||
|
||||
db.add(link)
|
||||
db.commit()
|
||||
db.refresh(link)
|
||||
|
||||
return link
|
||||
|
||||
|
||||
def get_share_links(db: Session, map_id: UUID, current_user: User) -> List[MapShareLink]:
|
||||
"""Get all share links for a map."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can view share links"
|
||||
)
|
||||
|
||||
links = db.query(MapShareLink).filter(MapShareLink.map_id == map_id).all()
|
||||
return links
|
||||
|
||||
|
||||
def delete_share_link(
|
||||
db: Session,
|
||||
map_id: UUID,
|
||||
link_id: UUID,
|
||||
current_user: User
|
||||
) -> None:
|
||||
"""Delete a share link."""
|
||||
# Verify user owns the map
|
||||
map_obj = get_map_by_id(db, map_id, current_user)
|
||||
if map_obj.owner_id != current_user.id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Only the map owner can delete share links"
|
||||
)
|
||||
|
||||
link = db.query(MapShareLink).filter(
|
||||
MapShareLink.id == link_id,
|
||||
MapShareLink.map_id == map_id
|
||||
).first()
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Share link not found"
|
||||
)
|
||||
|
||||
db.delete(link)
|
||||
db.commit()
|
||||
|
||||
|
||||
def get_map_by_share_token(db: Session, token: str) -> tuple[Map, SharePermission]:
|
||||
"""Get map by share token (for guest access)."""
|
||||
link = db.query(MapShareLink).filter(
|
||||
MapShareLink.token == token,
|
||||
MapShareLink.is_active == True
|
||||
).first()
|
||||
|
||||
if not link:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Invalid or expired share link"
|
||||
)
|
||||
|
||||
# Check if link is expired
|
||||
if link.expires_at and link.expires_at < datetime.utcnow():
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_410_GONE,
|
||||
detail="Share link has expired"
|
||||
)
|
||||
|
||||
map_obj = db.query(Map).filter(Map.id == link.map_id).first()
|
||||
if not map_obj:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Map not found"
|
||||
)
|
||||
|
||||
return map_obj, link.permission
|
||||
|
||||
|
||||
def check_map_access(
|
||||
db: Session,
|
||||
map_id: UUID,
|
||||
user: Optional[User] = None,
|
||||
token: Optional[str] = None
|
||||
) -> tuple[bool, SharePermission]:
|
||||
"""
|
||||
Check if user has access to a map and return their permission level.
|
||||
Returns (has_access, permission_level)
|
||||
"""
|
||||
map_obj = db.query(Map).filter(Map.id == map_id).first()
|
||||
if not map_obj:
|
||||
return False, SharePermission.READ
|
||||
|
||||
# Owner has full edit access
|
||||
if user and map_obj.owner_id == user.id:
|
||||
return True, SharePermission.EDIT
|
||||
|
||||
# Check user share
|
||||
if user:
|
||||
share = db.query(MapShare).filter(
|
||||
MapShare.map_id == map_id,
|
||||
MapShare.user_id == user.id
|
||||
).first()
|
||||
if share:
|
||||
return True, share.permission
|
||||
|
||||
# Check share token
|
||||
if token:
|
||||
try:
|
||||
_, permission = get_map_by_share_token(db, token)
|
||||
return True, permission
|
||||
except HTTPException:
|
||||
pass
|
||||
|
||||
# Check if map is public
|
||||
if map_obj.is_default_public:
|
||||
return True, SharePermission.READ
|
||||
|
||||
return False, SharePermission.READ
|
||||
Reference in New Issue
Block a user