Backend: - Item model (title, url, points, username, content, created_at) - is_admin flag on User + startup migration for pre-existing DBs - get_current_admin dependency (401 without/invalid token, 403 for non-admins); server-side check on every admin endpoint - /api/v1/admin/items CRUD: pagination, case-insensitive title/url search, sort by title/created_at/points, in-memory rate limiting (1000 req/60s per admin -> 429) - seed_admin.py (ADMIN_USERNAME/ADMIN_PASSWORD), documented in backend/.env.example Frontend: - Auth context exposes is_admin; RequireAdmin route guard (redirects signed-out users, renders 403 page for non-admins) - AdminLayout with responsive dark sidebar; Admin link in header for admins only - AdminDashboard: paginated table (page size 10/20/50), 300ms debounced search, sortable title/points/created_at columns, edit/delete row actions - EditItemPage: create + edit with client-side validation and a Lexical (0.25) WYSIWYG editor (bold/italic/underline, H1-H6, lists, code blocks, links, undo/redo), optional debounced auto-save - DeleteConfirmation modal with warning icon and explicit "Yes, delete" confirmation - Unit tests (34) and mocked E2E workflow tests (10) All acceptance criteria for milestones/m6-setup-admin-area-prompt.md verified: 108 backend, 87 frontend unit and 29 E2E tests pass.
58 lines
1.7 KiB
Python
58 lines
1.7 KiB
Python
"""Create (or promote) an administrator account.
|
|
|
|
Usage (inside the backend container or with the backend venv)::
|
|
|
|
ADMIN_USERNAME=admin ADMIN_PASSWORD='secret' python seed_admin.py
|
|
|
|
Reads ``ADMIN_USERNAME`` and ``ADMIN_PASSWORD`` from the environment (see
|
|
``.env.example``). When the user already exists the account is promoted to
|
|
admin and its password is reset to ``ADMIN_PASSWORD``. Safe to re-run.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
|
|
from core import auth
|
|
from db.session import SessionLocal
|
|
from models import User
|
|
|
|
|
|
def main() -> int:
|
|
username = os.getenv("ADMIN_USERNAME", "").strip()
|
|
password = os.getenv("ADMIN_PASSWORD", "")
|
|
if not username or not password:
|
|
print(
|
|
"error: set ADMIN_USERNAME and ADMIN_PASSWORD "
|
|
"(see .env.example), e.g.\n"
|
|
" ADMIN_USERNAME=admin ADMIN_PASSWORD='secret' python seed_admin.py",
|
|
file=sys.stderr,
|
|
)
|
|
return 1
|
|
|
|
db = SessionLocal()
|
|
try:
|
|
user = db.query(User).filter(User.username == username).first()
|
|
if user is None:
|
|
user = User(
|
|
username=username,
|
|
password_hash=auth.hash_password(password),
|
|
is_admin=True,
|
|
)
|
|
db.add(user)
|
|
db.commit()
|
|
print(f"Created admin user '{username}'.")
|
|
else:
|
|
user.is_admin = True
|
|
user.password_hash = auth.hash_password(password)
|
|
db.commit()
|
|
print(f"Promoted existing user '{username}' to admin (password reset).")
|
|
return 0
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|