gemification_admin bekötve
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
# /opt/docker/dev/service_finder/backend/app/api/v1/endpoints/admin.py
|
||||
import logging
|
||||
from fastapi import APIRouter, Depends, HTTPException, status, Body, BackgroundTasks, Query
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy import select, func, text, delete, or_
|
||||
@@ -6,6 +7,8 @@ from sqlalchemy.orm import selectinload
|
||||
from typing import List, Any, Dict, Optional
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
logger = logging.getLogger("admin-endpoints")
|
||||
|
||||
from app.api import deps
|
||||
from app.models.identity import User, UserRole, Person # JAVÍTVA: Központi import
|
||||
from app.models.identity.address import Address
|
||||
@@ -79,7 +82,7 @@ async def approve_action(
|
||||
_ = Depends(deps.RequirePermission("settings:edit")),
|
||||
):
|
||||
try:
|
||||
await security_service.approve_action(db, admin.id, action_id)
|
||||
await security_service.approve_action(db, current_user.id, action_id)
|
||||
return {"status": "success", "message": "Művelet végrehajtva."}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
@@ -420,8 +423,12 @@ async def apply_gamification_penalty(
|
||||
"""
|
||||
Gamification büntetés kiosztása egy felhasználónak.
|
||||
|
||||
Negatív szintek alkalmazása a frissen létrehozott Gamification rendszerben.
|
||||
REFAKTOR: Most a GamificationService process_activity() metódusát használja
|
||||
is_penalty=True paraméterrel, a direkt level módosítás helyett.
|
||||
A penalty_level (negatív szám) átalakításra kerül penalty_points értékké.
|
||||
"""
|
||||
from app.services.gamification_service import gamification_service
|
||||
|
||||
# Ellenőrizzük, hogy létezik-e a felhasználó
|
||||
user_stmt = select(User).where(User.id == user_id)
|
||||
user_result = await db.execute(user_stmt)
|
||||
@@ -433,50 +440,54 @@ async def apply_gamification_penalty(
|
||||
detail=f"User not found with ID: {user_id}"
|
||||
)
|
||||
|
||||
# Megkeressük a felhasználó gamification profilját (vagy létrehozzuk)
|
||||
gamification_stmt = select(UserStats).where(UserStats.user_id == user_id)
|
||||
gamification_result = await db.execute(gamification_stmt)
|
||||
gamification = gamification_result.scalar_one_or_none()
|
||||
# A penalty_level (pl. -3) átalakítása penalty_points értékké
|
||||
# A régi rendszerben a penalty_level negatív szint volt (-1, -2, -3)
|
||||
# Az új rendszerben penalty_points-ban mérjük (abs(penalty_level) * 100)
|
||||
penalty_amount = abs(penalty.penalty_level) * 100
|
||||
|
||||
if not gamification:
|
||||
# Ha nincs profil, létrehozzuk alapértelmezett értékekkel
|
||||
gamification = UserStats(
|
||||
try:
|
||||
# GamificationService használata a büntetéshez
|
||||
stats = await gamification_service.process_activity(
|
||||
db=db,
|
||||
user_id=user_id,
|
||||
level=0,
|
||||
xp=0,
|
||||
reputation_score=100,
|
||||
created_at=datetime.now(),
|
||||
updated_at=datetime.now()
|
||||
xp_amount=penalty_amount,
|
||||
social_amount=0,
|
||||
reason=f"ADMIN_PENALTY: {penalty.reason}",
|
||||
is_penalty=True,
|
||||
commit=False, # Ne commitoljon, mert még audit logot is hozzáadunk
|
||||
action_key=None,
|
||||
source_type="admin_penalty",
|
||||
source_id=current_user.id,
|
||||
)
|
||||
|
||||
# Audit log - SecurityAuditLog has actor_id, target_id, payload_before/payload_after
|
||||
audit_log = SecurityAuditLog(
|
||||
actor_id=current_user.id,
|
||||
action="apply_gamification_penalty",
|
||||
target_id=user_id,
|
||||
payload_before={},
|
||||
payload_after={"penalty_points_added": penalty_amount, "reason": penalty.reason},
|
||||
is_critical=False,
|
||||
)
|
||||
db.add(audit_log)
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Gamification penalty applied to user {user_id}",
|
||||
"user_id": user_id,
|
||||
"penalty_points_added": penalty_amount,
|
||||
"total_penalty_points": stats.penalty_points,
|
||||
"restriction_level": stats.restriction_level,
|
||||
"reason": penalty.reason
|
||||
}
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
logger.error(f"Failed to apply penalty to user {user_id}: {e}")
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||
detail=f"Failed to apply penalty: {str(e)}",
|
||||
)
|
||||
db.add(gamification)
|
||||
await db.flush()
|
||||
|
||||
# Alkalmazzuk a büntetést (negatív szint módosítása)
|
||||
# A level mező lehet negatív is a büntetések miatt
|
||||
new_level = gamification.level + penalty.penalty_level
|
||||
gamification.level = new_level
|
||||
gamification.updated_at = datetime.now()
|
||||
|
||||
# Audit log
|
||||
audit_log = SecurityAuditLog(
|
||||
user_id=current_user.id,
|
||||
action="apply_gamification_penalty",
|
||||
target_user_id=user_id,
|
||||
details=f"Gamification penalty applied: level change {penalty.penalty_level}, reason: {penalty.reason}",
|
||||
is_critical=False,
|
||||
ip_address="admin_api"
|
||||
)
|
||||
db.add(audit_log)
|
||||
await db.commit()
|
||||
|
||||
return {
|
||||
"status": "success",
|
||||
"message": f"Gamification penalty applied to user {user_id}",
|
||||
"user_id": user_id,
|
||||
"penalty_level": penalty.penalty_level,
|
||||
"new_level": new_level,
|
||||
"reason": penalty.reason
|
||||
}
|
||||
|
||||
|
||||
# ==================== USER MANAGEMENT (EPIC 10: Admin Frontend) ====================
|
||||
|
||||
Reference in New Issue
Block a user