Skip to content

Commit 9eb3858

Browse files
authored
Update security.py
1 parent a50918f commit 9eb3858

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

main/api/mcp/lib/security.py

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import logging
22
from datetime import datetime, timedelta
3-
from typing import Dict, Any, Optional
3+
from typing import Dict, Any, Optional, List
44
from config.config import DatabaseConfig
5+
from pydantic import BaseModel
56
import json
67
import hashlib
78
import smtplib
@@ -11,6 +12,16 @@
1112
logger = logging.getLogger("mcp.security")
1213
logger.setLevel(logging.INFO)
1314

15+
class UserActionsInput(BaseModel):
16+
user_id: str
17+
page: int = 1
18+
page_size: int = 50
19+
20+
class UserActionsOutput(BaseModel):
21+
actions: List[Dict[str, Any]]
22+
total_pages: int
23+
current_page: int
24+
1425
class SecurityHandler:
1526
def __init__(self, db: DatabaseConfig):
1627
self.db = db
@@ -47,7 +58,7 @@ async def log_event(self, event_type: str, user_id: Optional[str], details: Dict
4758
except Exception as e:
4859
logger.error(f"Error logging security event: {str(e)}")
4960

50-
async def log_user_action(self, user_id: str, action: str, details: Dict[str, Any], ip_address: Optional[str] = None):
61+
async def log_user_action(self, user_id: Optional[str], action: str, details: Dict[str, Any], ip_address: Optional[str] = None):
5162
"""Log user actions for auditing purposes."""
5263
try:
5364
await self.db.query(
@@ -238,3 +249,36 @@ async def detect_anomalies(self, event_type: str, user_id: Optional[str], ip_add
238249
logger.warning(f"Anomaly detected: High cash-out attempts for user {user_id}")
239250
except Exception as e:
240251
logger.error(f"Error detecting anomalies: {str(e)}")
252+
253+
async def get_user_actions(self, input: UserActionsInput) -> UserActionsOutput:
254+
try:
255+
offset = (input.page - 1) * input.page_size
256+
total_count = await self.db.query(
257+
"SELECT COUNT(*) FROM audit_logs WHERE user_id = $1",
258+
[input.user_id]
259+
)
260+
total_pages = (total_count.rows[0]["count"] + input.page_size - 1) // input.page_size
261+
262+
actions = await self.db.query(
263+
"SELECT action, details, created_at FROM audit_logs WHERE user_id = $1 ORDER BY created_at DESC LIMIT $2 OFFSET $3",
264+
[input.user_id, input.page_size, offset]
265+
)
266+
await self.log_user_action(
267+
user_id=input.user_id,
268+
action="view_action_history",
269+
details={"page": input.page, "page_size": input.page_size, "action_count": len(actions.rows)}
270+
)
271+
logger.info(f"Retrieved action history for user {input.user_id}, page {input.page}")
272+
return UserActionsOutput(
273+
actions=[{"action": row["action"], "details": json.loads(row["details"]), "created_at": row["created_at"].isoformat()} for row in actions.rows],
274+
total_pages=total_pages,
275+
current_page=input.page
276+
)
277+
except Exception as e:
278+
logger.error(f"Error retrieving user actions: {str(e)}")
279+
await self.log_event(
280+
event_type="action_history_error",
281+
user_id=input.user_id,
282+
details={"error": str(e)}
283+
)
284+
raise HTTPException(status_code=500, detail=str(e))

0 commit comments

Comments
 (0)