- Added detailed API endpoint information in SETUP-INSTRUCTIONS.md - Documented API usage with Ente CLI - Enhanced routing configuration for auth/cast/accounts apps - Updated to version 0.1.64
133 lines
3.9 KiB
Bash
133 lines
3.9 KiB
Bash
#!/bin/bash
|
|
# Direct Database Admin Helper for Ente Cloudron
|
|
# This script directly updates the database for admin operations
|
|
|
|
# Function to update user subscription directly in database
|
|
update_subscription() {
|
|
local user_email="$1"
|
|
local storage_gb="$2"
|
|
local valid_days="$3"
|
|
|
|
if [ -z "$user_email" ] || [ -z "$storage_gb" ] || [ -z "$valid_days" ]; then
|
|
echo "Usage: $0 update-subscription <user-email> <storage-gb> <valid-days>"
|
|
echo "Example: $0 update-subscription user@example.com 100 365"
|
|
return 1
|
|
fi
|
|
|
|
echo "Updating subscription for: $user_email"
|
|
echo "Storage: ${storage_gb}GB"
|
|
echo "Valid for: ${valid_days} days"
|
|
|
|
# Convert GB to bytes (1 GB = 1073741824 bytes)
|
|
local storage_bytes=$((storage_gb * 1073741824))
|
|
|
|
# Calculate expiry timestamp (current time + valid_days)
|
|
local current_timestamp=$(date +%s)
|
|
local expiry_timestamp=$((current_timestamp + (valid_days * 86400)))
|
|
# Convert to microseconds for the database
|
|
local expiry_microseconds="${expiry_timestamp}000000"
|
|
|
|
# Update the database directly
|
|
PGPASSWORD="$CLOUDRON_POSTGRESQL_PASSWORD" psql \
|
|
-h "$CLOUDRON_POSTGRESQL_HOST" \
|
|
-p "$CLOUDRON_POSTGRESQL_PORT" \
|
|
-U "$CLOUDRON_POSTGRESQL_USERNAME" \
|
|
-d "$CLOUDRON_POSTGRESQL_DATABASE" << EOF
|
|
-- Update user's storage and subscription
|
|
UPDATE users
|
|
SET
|
|
storage_bonus = $storage_bytes,
|
|
subscription_expiry = $expiry_microseconds
|
|
WHERE email = '$user_email';
|
|
|
|
-- Show the updated values
|
|
SELECT
|
|
email,
|
|
storage_bonus / 1073741824.0 as storage_gb,
|
|
to_timestamp(subscription_expiry / 1000000) as subscription_expires
|
|
FROM users
|
|
WHERE email = '$user_email';
|
|
EOF
|
|
|
|
if [ $? -eq 0 ]; then
|
|
echo "✓ Subscription updated successfully"
|
|
else
|
|
echo "✗ Failed to update subscription"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Function to get user details
|
|
get_user_details() {
|
|
local user_email="$1"
|
|
|
|
if [ -z "$user_email" ]; then
|
|
echo "Usage: $0 get-user <user-email>"
|
|
return 1
|
|
fi
|
|
|
|
PGPASSWORD="$CLOUDRON_POSTGRESQL_PASSWORD" psql \
|
|
-h "$CLOUDRON_POSTGRESQL_HOST" \
|
|
-p "$CLOUDRON_POSTGRESQL_PORT" \
|
|
-U "$CLOUDRON_POSTGRESQL_USERNAME" \
|
|
-d "$CLOUDRON_POSTGRESQL_DATABASE" << EOF
|
|
SELECT
|
|
email,
|
|
storage_bonus / 1073741824.0 as storage_gb,
|
|
storage_consumed / 1073741824.0 as used_gb,
|
|
to_timestamp(subscription_expiry / 1000000) as subscription_expires,
|
|
CASE
|
|
WHEN subscription_expiry > (EXTRACT(EPOCH FROM NOW()) * 1000000) THEN 'Active'
|
|
ELSE 'Expired'
|
|
END as status
|
|
FROM users
|
|
WHERE email = '$user_email';
|
|
EOF
|
|
}
|
|
|
|
# Function to list all users
|
|
list_users() {
|
|
PGPASSWORD="$CLOUDRON_POSTGRESQL_PASSWORD" psql \
|
|
-h "$CLOUDRON_POSTGRESQL_HOST" \
|
|
-p "$CLOUDRON_POSTGRESQL_PORT" \
|
|
-U "$CLOUDRON_POSTGRESQL_USERNAME" \
|
|
-d "$CLOUDRON_POSTGRESQL_DATABASE" << EOF
|
|
SELECT
|
|
email,
|
|
storage_bonus / 1073741824.0 as storage_gb,
|
|
storage_consumed / 1073741824.0 as used_gb,
|
|
to_timestamp(subscription_expiry / 1000000) as expires,
|
|
CASE
|
|
WHEN subscription_expiry > (EXTRACT(EPOCH FROM NOW()) * 1000000) THEN 'Active'
|
|
ELSE 'Expired'
|
|
END as status
|
|
FROM users
|
|
ORDER BY email;
|
|
EOF
|
|
}
|
|
|
|
# Main command handler
|
|
case "$1" in
|
|
"update-subscription")
|
|
update_subscription "$2" "$3" "$4"
|
|
;;
|
|
"get-user")
|
|
get_user_details "$2"
|
|
;;
|
|
"list-users")
|
|
list_users
|
|
;;
|
|
*)
|
|
echo "Ente Direct Admin Helper"
|
|
echo ""
|
|
echo "Usage:"
|
|
echo " $0 update-subscription <user-email> <storage-gb> <valid-days>"
|
|
echo " $0 get-user <user-email>"
|
|
echo " $0 list-users"
|
|
echo ""
|
|
echo "Examples:"
|
|
echo " $0 update-subscription user@example.com 100 365"
|
|
echo " $0 get-user user@example.com"
|
|
echo " $0 list-users"
|
|
;;
|
|
esac |