feat: v1.5.0 + service actions + remove duplicate apk
- Services tab: start/stop/restart/reload via dialog - Backend: POST /api/servers/:id/service-action - Removed duplicate APK download from dashboard view - Version bump to 1.5.0
This commit is contained in:
@@ -644,6 +644,68 @@ class ApiController
|
||||
}
|
||||
}
|
||||
|
||||
public function serverServiceAction(int $id): void
|
||||
{
|
||||
$user = $this->authenticateRequest();
|
||||
|
||||
$serverModel = new Server();
|
||||
if (!$serverModel->canManage($id, (int) $user['id'])) {
|
||||
$this->view->json(['error' => 'Forbidden'], 403);
|
||||
}
|
||||
|
||||
$server = $serverModel->findById($id);
|
||||
if (!$server) {
|
||||
$this->view->json(['error' => 'Server not found'], 404);
|
||||
}
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'), true) ?? $_POST;
|
||||
$service = $input['service'] ?? '';
|
||||
$action = $input['action'] ?? '';
|
||||
$allowed = ['start', 'stop', 'restart', 'reload'];
|
||||
|
||||
if (empty($service) || !in_array($action, $allowed, true)) {
|
||||
$this->view->json(['error' => 'Invalid service or action'], 400);
|
||||
}
|
||||
|
||||
$sanitized = preg_replace('/[^a-zA-Z0-9\-_]/', '', $service);
|
||||
|
||||
try {
|
||||
$ssh = new SSHService();
|
||||
if (!$ssh->connect($server)) {
|
||||
throw new \RuntimeException('SSH connection failed');
|
||||
}
|
||||
|
||||
$result = $ssh->exec("sudo systemctl {$action} {$sanitized} 2>&1");
|
||||
$ssh->disconnect();
|
||||
|
||||
$exitCode = (int) ($result['exit_status'] ?? -1);
|
||||
|
||||
$auditService = new AuditService();
|
||||
$auditService->logServerAction($id, "service_{$action}", "{$sanitized} {$action}");
|
||||
|
||||
if ($exitCode === 0) {
|
||||
$this->view->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
'message' => "Service '{$sanitized}' {$action}ed.",
|
||||
'output' => trim($result['output'] ?? ''),
|
||||
],
|
||||
]);
|
||||
} else {
|
||||
$this->view->json([
|
||||
'success' => false,
|
||||
'error' => "Failed to {$action} '{$sanitized}'.",
|
||||
'data' => ['output' => trim($result['output'] ?? '')],
|
||||
], 500);
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->view->json([
|
||||
'success' => false,
|
||||
'error' => $e->getMessage(),
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function executeServerAction(int $id, string $action, string $command): void
|
||||
{
|
||||
$user = $this->authenticateRequest();
|
||||
|
||||
Reference in New Issue
Block a user