feat: Add cleanup functionality for pending photos and database management

This commit introduces new API endpoints for cleaning up files and records related to pending photos. The frontend has been updated to include buttons for admins to trigger cleanup operations, allowing for the deletion of files from shared space and records from the pending_photos table. Additionally, the README has been updated with instructions for granting DELETE permissions on auth database tables, and a script has been added to automate this process. Documentation has been updated to reflect these changes.
This commit is contained in:
tanyar09
2025-11-24 13:57:27 -05:00
parent 661e812193
commit 100d39c556
6 changed files with 434 additions and 18 deletions
+24 -4
View File
@@ -1,9 +1,26 @@
#!/bin/bash
# Grant DELETE permission on auth database users table
# Grant DELETE permission on auth database tables
#
# This script grants DELETE permission to the punimtag user on tables in the auth database.
# It requires sudo privileges to run commands as the postgres superuser.
#
# Manual alternative (if script fails):
# sudo -u postgres psql -d punimtag_auth -c "GRANT DELETE ON TABLE pending_photos TO punimtag;"
# sudo -u postgres psql -d punimtag_auth -c "GRANT DELETE ON TABLE users TO punimtag;"
# sudo -u postgres psql -d punimtag_auth -c "GRANT DELETE ON TABLE pending_identifications TO punimtag;"
# sudo -u postgres psql -d punimtag_auth -c "GRANT DELETE ON TABLE inappropriate_photo_reports TO punimtag;"
#
# Or all at once:
# sudo -u postgres psql -d punimtag_auth << 'EOF'
# GRANT DELETE ON TABLE pending_photos TO punimtag;
# GRANT DELETE ON TABLE users TO punimtag;
# GRANT DELETE ON TABLE pending_identifications TO punimtag;
# GRANT DELETE ON TABLE inappropriate_photo_reports TO punimtag;
# EOF
set -e
echo "🔧 Granting DELETE permission on auth database users table..."
echo "🔧 Granting DELETE permission on auth database tables..."
# Check if .env file exists
ENV_FILE=".env"
@@ -49,14 +66,17 @@ echo " User: $DB_USER"
echo ""
# Grant DELETE permission using psql as postgres superuser
echo "🔐 Granting DELETE permission (requires sudo)..."
echo "🔐 Granting DELETE permission on auth database tables (requires sudo)..."
sudo -u postgres psql -d "$DB_NAME" << EOF
GRANT DELETE ON TABLE users TO "$DB_USER";
GRANT DELETE ON TABLE pending_photos TO "$DB_USER";
GRANT DELETE ON TABLE pending_identifications TO "$DB_USER";
GRANT DELETE ON TABLE inappropriate_photo_reports TO "$DB_USER";
\q
EOF
if [ $? -eq 0 ]; then
echo "✅ Successfully granted DELETE permission to user '$DB_USER'"
echo "✅ Successfully granted DELETE permissions to user '$DB_USER'"
else
echo "❌ Failed to grant permission"
exit 1
+28 -13
View File
@@ -37,7 +37,7 @@ def parse_database_url(db_url: str) -> dict:
def grant_delete_permission() -> None:
"""Grant DELETE permission on users table in auth database."""
"""Grant DELETE permission on users and pending_photos tables in auth database."""
auth_db_url = os.getenv("DATABASE_URL_AUTH")
if not auth_db_url:
print("❌ Error: DATABASE_URL_AUTH environment variable not set")
@@ -51,10 +51,13 @@ def grant_delete_permission() -> None:
db_user = db_info["user"]
db_name = db_info["database"]
print(f"📋 Granting DELETE permission on users table...")
print(f"📋 Granting DELETE permission on auth database tables...")
print(f" Database: {db_name}")
print(f" User: {db_user}")
# Tables that need DELETE permission
tables = ["users", "pending_photos", "pending_identifications", "inappropriate_photo_reports"]
# Connect as postgres superuser to grant permissions
# Try to connect as postgres user (superuser)
try:
@@ -63,13 +66,19 @@ def grant_delete_permission() -> None:
engine = create_engine(postgres_url)
with engine.connect() as conn:
# Grant DELETE permission
conn.execute(text(f"""
GRANT DELETE ON TABLE users TO {db_user}
"""))
for table in tables:
try:
# Grant DELETE permission
conn.execute(text(f"""
GRANT DELETE ON TABLE {table} TO {db_user}
"""))
print(f" ✅ Granted DELETE on {table}")
except Exception as e:
# Table might not exist, skip it
print(f" ⚠️ Could not grant DELETE on {table}: {e}")
conn.commit()
print(f"✅ Successfully granted DELETE permission to user '{db_user}'")
print(f"✅ Successfully granted DELETE permissions to user '{db_user}'")
return
except Exception as e:
# If connecting as postgres fails, try with the same user (might have grant privileges)
@@ -79,18 +88,24 @@ def grant_delete_permission() -> None:
try:
engine = create_engine(auth_db_url)
with engine.connect() as conn:
# Try to grant permission
conn.execute(text(f"""
GRANT DELETE ON TABLE users TO {db_user}
"""))
for table in tables:
try:
# Try to grant permission
conn.execute(text(f"""
GRANT DELETE ON TABLE {table} TO {db_user}
"""))
print(f" ✅ Granted DELETE on {table}")
except Exception as e2:
print(f" ⚠️ Could not grant DELETE on {table}: {e2}")
conn.commit()
print(f"✅ Successfully granted DELETE permission to user '{db_user}'")
print(f"✅ Successfully granted DELETE permissions to user '{db_user}'")
return
except Exception as e2:
print(f"❌ Failed to grant permission: {e2}")
print(f"\n💡 To grant permission manually, run as postgres superuser:")
print(f" sudo -u postgres psql -d {db_name} -c \"GRANT DELETE ON TABLE users TO {db_user};\"")
for table in tables:
print(f" sudo -u postgres psql -d {db_name} -c \"GRANT DELETE ON TABLE {table} TO {db_user};\"")
sys.exit(1)