I am performing an ODK migration through the API and facing issues transferring users and roles. The API detects the users but not the roles. Any idea what might be causing this? Additionally, I’m unsure whether migrating passwords while preserving their hash is possible.
Any ideas?
import requests
from requests.auth import HTTPBasicAuth
# Configuración del entorno antiguo (origen)
OLD_ODK_URL = "https://old-odk-central.org/v1"
OLD_ADMIN_USER = "admin_old"
OLD_ADMIN_PASS = "old_password"
# Configuración del entorno nuevo (destino)
NEW_ODK_URL = "https://new-odk-central.org/v1"
NEW_ADMIN_USER = "admin_new"
NEW_ADMIN_PASS = "new_password"
# Contraseña temporal para los nuevos usuarios
TEMP_PASSWORD = "Temporal1234"
def get_users():
""" Obtiene la lista de usuarios del entorno antiguo. """
response = requests.get(f"{OLD_ODK_URL}/users", auth=HTTPBasicAuth(OLD_ADMIN_USER, OLD_ADMIN_PASS), verify=False)
if response.status_code == 200:
return response.json()
else:
print(f"❌ Error al obtener usuarios: {response.status_code} - {response.text}")
return []
def create_user(user):
""" Crea un usuario en el nuevo entorno sin asignar roles. """
user_data = {
"email": user["email"],
"displayName": user["displayName"],
"password": TEMP_PASSWORD # Se asigna una contraseña temporal
}
response = requests.post(f"{NEW_ODK_URL}/users", json=user_data, auth=HTTPBasicAuth(NEW_ADMIN_USER, NEW_ADMIN_PASS), verify=False)
if response.status_code == 201:
print(f"✅ Usuario {user['email']} migrado correctamente.")
else:
print(f"❌ Error al migrar {user['email']}: {response.status_code} - {response.text}")
def migrate_users():
""" Migra todos los usuarios sin roles. """
users = get_users()
if not users:
print("⚠️ No se encontraron usuarios en el entorno antiguo.")
return
for user in users:
if user["email"] != OLD_ADMIN_USER: # Evita migrar el usuario administrador
create_user(user)
if __name__ == "__main__":
print("\n🔄 Migrando usuarios sin roles...")
migrate_users()