summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/src/meshbay_hub/api/admin.py
blob: 164885dfd8f0ecca57bc3d7be8e604176da4909f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""
MeshBay Hub — admin/moderation panel endpoints.

All endpoints require moderator or admin role.
Separate from moderation.py (which handles public reporting and content blocklist).
"""

import logging
from datetime import datetime, timezone

from fastapi import APIRouter, Depends, HTTPException, Query
from pydantic import BaseModel
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession

from meshbay_hub.auth import decrypt_email
from meshbay_hub.api.deps import require_moderator
from meshbay_hub.api.revocation import get_connected_node_count
from meshbay_hub.db.engine import get_db
from meshbay_hub.db.models import Group, GroupMember, IPLog, Node, User

log = logging.getLogger(__name__)

router = APIRouter(prefix="/v1/admin", tags=["admin"])


# ── Request models ───────────────────────────────────────────────────────────

class UserPatchRequest(BaseModel):
    role: str | None = None
    status: str | None = None


class GroupPatchRequest(BaseModel):
    status: str | None = None


# ── Stats ────────────────────────────────────────────────────────────────────

@router.get("/stats")
async def admin_stats(
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
):
    user_count = (await db.execute(select(func.count()).select_from(User))).scalar_one()
    group_count = (await db.execute(select(func.count()).select_from(Group))).scalar_one()
    node_count = (await db.execute(select(func.count()).select_from(Node))).scalar_one()
    return {
        "users": user_count,
        "groups": group_count,
        "nodes": node_count,
        "online_nodes": get_connected_node_count(),
    }


# ── Users ────────────────────────────────────────────────────────────────────

@router.get("/users")
async def admin_list_users(
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
    q: str = "",
    offset: int = 0,
    limit: int = Query(default=50, le=200),
):
    query = select(User).order_by(User.created_at.desc())
    if q:
        query = query.where(User.username.ilike(f"%{q}%"))
    query = query.offset(offset).limit(limit)
    result = await db.execute(query)
    users = result.scalars().all()

    total_query = select(func.count()).select_from(User)
    if q:
        total_query = total_query.where(User.username.ilike(f"%{q}%"))
    total = (await db.execute(total_query)).scalar_one()

    return {
        "users": [
            {
                "id": u.id,
                "username": u.username,
                "role": u.role,
                "status": u.status,
                "created_at": u.created_at.isoformat(),
            }
            for u in users
        ],
        "total": total,
    }


@router.get("/users/{user_id}")
async def admin_get_user(
    user_id: str,
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
):
    user = await db.get(User, user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")

    group_count = (await db.execute(
        select(func.count()).where(GroupMember.user_id == user_id)
    )).scalar_one()

    try:
        email = decrypt_email(user.email)
    except Exception:
        email = "(encrypted)"

    return {
        "id": user.id,
        "username": user.username,
        "email": email,
        "role": user.role,
        "status": user.status,
        "created_at": user.created_at.isoformat(),
        "group_count": group_count,
    }


@router.patch("/users/{user_id}")
async def admin_patch_user(
    user_id: str,
    body: UserPatchRequest,
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
):
    user = await db.get(User, user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")

    if user.id == current_user.id:
        raise HTTPException(status_code=400, detail="Cannot modify your own account")

    from meshbay_hub.api.notifications import create_notification

    if body.role is not None:
        if body.role not in ("user", "moderator", "admin"):
            raise HTTPException(status_code=422, detail="role must be user, moderator, or admin")
        user.role = body.role
        log.info("User %s role changed to %s by %s", user.username, body.role, current_user.username)
        await create_notification(
            db, user.id, "role_change",
            f"Your role has been changed to {body.role}",
        )

    if body.status is not None:
        if body.status not in ("active", "suspended", "revoked"):
            raise HTTPException(status_code=422, detail="status must be active, suspended, or revoked")
        user.status = body.status
        log.info("User %s status changed to %s by %s",
                 user.username, body.status, current_user.username)
        if body.status == "suspended":
            await create_notification(
                db, user.id, "account_suspended",
                "Your account has been suspended",
            )
        elif body.status == "active":
            await create_notification(
                db, user.id, "account_restored",
                "Your account has been restored",
            )

    db.add(IPLog(
        user_id=current_user.id,
        event="admin_user_update",
        ip_address="admin",
        detail=f"{user.username}: role={user.role} status={user.status}",
    ))
    await db.commit()

    return {
        "id": user.id,
        "username": user.username,
        "role": user.role,
        "status": user.status,
    }


# ── Groups ───────────────────────────────────────────────────────────────────

@router.get("/groups")
async def admin_list_groups(
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
    offset: int = 0,
    limit: int = Query(default=50, le=200),
):
    query = (
        select(
            Group,
            func.count(GroupMember.user_id).label("member_count"),
        )
        .outerjoin(GroupMember, Group.id == GroupMember.group_id)
        .group_by(Group.id)
        .order_by(Group.created_at.desc())
        .offset(offset)
        .limit(limit)
    )
    result = await db.execute(query)
    rows = result.all()

    total = (await db.execute(select(func.count()).select_from(Group))).scalar_one()

    return {
        "groups": [
            {
                "id": g.id,
                "name": g.name,
                "admin_id": g.admin_id,
                "visibility": g.visibility,
                "description": g.description or "",
                "status": g.status,
                "created_at": g.created_at.isoformat(),
                "member_count": mc,
            }
            for g, mc in rows
        ],
        "total": total,
    }


@router.patch("/groups/{group_id}")
async def admin_patch_group(
    group_id: str,
    body: GroupPatchRequest,
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
):
    group = await db.get(Group, group_id)
    if not group:
        raise HTTPException(status_code=404, detail="Group not found")

    if body.status is not None:
        if body.status not in ("active", "suspended", "revoked"):
            raise HTTPException(status_code=422, detail="status must be active, suspended, or revoked")
        group.status = body.status
        log.info("Group %s status changed to %s by %s",
                 group.name, body.status, current_user.username)

    db.add(IPLog(
        user_id=current_user.id,
        event="admin_group_update",
        ip_address="admin",
        detail=f"{group.name}: status={group.status}",
    ))
    await db.commit()

    return {
        "id": group.id,
        "name": group.name,
        "status": group.status,
    }


# ── IP Audit Logs ────────────────────────────────────────────────────────────

@router.get("/logs")
async def admin_list_logs(
    current_user: User = Depends(require_moderator),
    db: AsyncSession = Depends(get_db),
    user_id: str | None = None,
    event: str | None = None,
    offset: int = 0,
    limit: int = Query(default=50, le=200),
):
    query = (
        select(IPLog, User.username)
        .outerjoin(User, IPLog.user_id == User.id)
        .order_by(IPLog.timestamp.desc())
    )
    if user_id:
        query = query.where(IPLog.user_id == user_id)
    if event:
        query = query.where(IPLog.event == event)
    query = query.offset(offset).limit(limit)
    result = await db.execute(query)
    rows = result.all()

    return {
        "logs": [
            {
                "id": lg.id,
                "user_id": lg.user_id,
                "username": uname or "",
                "event": lg.event,
                "ip_address": lg.ip_address,
                "detail": lg.detail,
                "timestamp": lg.timestamp.isoformat(),
            }
            for lg, uname in rows
        ],
    }