Files
whatsapp/api/create_admin_user.php
T
2026-02-03 21:30:55 -05:00

66 lines
1.8 KiB
PHP

<?php
/**
* API - Crear nuevo usuario administrador
* Fecha: 3 de febrero de 2026
*/
require_once '../config/config.php';
// Verificar autenticación
requireAuthentication();
header('Content-Type: application/json; charset=utf-8');
header('Cache-Control: no-cache, no-store, must-revalidate');
try {
$db = Database::getInstance();
// Leer datos del request
$input = json_decode(file_get_contents('php://input'), true);
$username = trim($input['username'] ?? '');
$fullName = trim($input['full_name'] ?? '');
$email = trim($input['email'] ?? '');
$password = $input['password'] ?? '';
if (empty($username)) {
throw new Exception('El nombre de usuario es requerido');
}
if (strlen($password) < 6) {
throw new Exception('La contraseña debe tener al menos 6 caracteres');
}
// Verificar si el username ya existe
$existing = $db->fetch(
"SELECT id FROM admin_users WHERE username = ?",
[$username]
);
if ($existing) {
throw new Exception('El nombre de usuario ya está en uso');
}
// Encriptar contraseña
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Insertar usuario
$db->execute(
"INSERT INTO admin_users (username, password_hash, full_name, email, is_active, created_at)
VALUES (?, ?, ?, ?, 1, NOW())",
[$username, $hashedPassword, $fullName, $email]
);
echo json_encode([
'success' => true,
'message' => 'Usuario creado correctamente'
]);
} catch (Exception $e) {
error_log('Error creating admin user: ' . $e->getMessage());
http_response_code(500);
echo json_encode([
'success' => false,
'error' => $e->getMessage()
]);
}