domovoy/app/Controllers/DeviceController.php

187 lines
6.8 KiB
PHP

<?php
declare(strict_types=1);
namespace Domovoy\Controllers;
use Domovoy\Models\Device;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class DeviceController
{
private \Domovoy\Services\Inventory\DeviceService $deviceService;
private \Domovoy\Repositories\DiscoveredHostRepository $discoveredHostRepository;
private \Domovoy\Repositories\CredentialRepository $credentialRepository;
public function __construct(
\Domovoy\Services\Inventory\DeviceService $deviceService,
\Domovoy\Repositories\DiscoveredHostRepository $discoveredHostRepository,
\Domovoy\Repositories\CredentialRepository $credentialRepository
) {
$this->deviceService = $deviceService;
$this->discoveredHostRepository = $discoveredHostRepository;
$this->credentialRepository = $credentialRepository;
}
public function index(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
ob_start();
$username = $_SESSION['username'] ?? 'User';
$devices = $this->deviceService->getAllDevices();
require dirname(__DIR__, 2) . '/templates/devices/index.php';
$body = ob_get_clean();
$response->getBody()->write($body);
return $response;
}
public function createForm(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
ob_start();
$username = $_SESSION['username'] ?? 'User';
$device = new Device();
$types = Device::$types;
$importances = Device::$importances;
require dirname(__DIR__, 2) . '/templates/devices/form.php';
$body = ob_get_clean();
$response->getBody()->write($body);
return $response;
}
public function create(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$data = $request->getParsedBody();
$device = new Device();
$device->name = trim($data['name'] ?? '');
$device->type = $data['type'] ?? 'unknown';
$device->description = trim($data['description'] ?? '') ?: null;
$device->primaryIp = trim($data['primary_ip'] ?? '') ?: null;
$device->macAddress = trim($data['mac_address'] ?? '') ?: null;
$device->hostname = trim($data['hostname'] ?? '') ?: null;
$device->vendor = trim($data['vendor'] ?? '') ?: null;
$device->osName = trim($data['os_name'] ?? '') ?: null;
$device->osVersion = trim($data['os_version'] ?? '') ?: null;
$device->location = trim($data['location'] ?? '') ?: null;
$device->importance = $data['importance'] ?? 'normal';
$this->deviceService->updateDevice($device);
return $response
->withHeader('Location', '/devices')
->withStatus(302);
}
public function show(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$id = (int)$args['id'];
$device = $this->deviceService->getDevice($id);
if ($device === null) {
return $response->withStatus(404)->write('Device not found');
}
ob_start();
$username = $_SESSION['username'] ?? 'User';
$types = Device::$types;
$importances = Device::$importances;
$credentials = $this->credentialRepository->findByDevice($id);
require dirname(__DIR__, 2) . '/templates/devices/show.php';
$body = ob_get_clean();
$response->getBody()->write($body);
return $response;
}
public function editForm(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$id = (int)$args['id'];
$device = $this->deviceService->getDevice($id);
if ($device === null) {
return $response->withStatus(404)->write('Device not found');
}
ob_start();
$username = $_SESSION['username'] ?? 'User';
$types = Device::$types;
$importances = Device::$importances;
require dirname(__DIR__, 2) . '/templates/devices/form.php';
$body = ob_get_clean();
$response->getBody()->write($body);
return $response;
}
public function update(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$id = (int)$args['id'];
$device = $this->deviceService->getDevice($id);
if ($device === null) {
return $response->withStatus(404)->write('Device not found');
}
$data = $request->getParsedBody();
$device->name = trim($data['name'] ?? '');
$device->type = $data['type'] ?? 'unknown';
$device->description = trim($data['description'] ?? '') ?: null;
$device->primaryIp = trim($data['primary_ip'] ?? '') ?: null;
$device->macAddress = trim($data['mac_address'] ?? '') ?: null;
$device->hostname = trim($data['hostname'] ?? '') ?: null;
$device->vendor = trim($data['vendor'] ?? '') ?: null;
$device->osName = trim($data['os_name'] ?? '') ?: null;
$device->osVersion = trim($data['os_version'] ?? '') ?: null;
$device->location = trim($data['location'] ?? '') ?: null;
$device->importance = $data['importance'] ?? 'normal';
$device->status = $data['status'] ?? 'active';
$this->deviceService->updateDevice($device);
return $response
->withHeader('Location', '/devices/' . $device->id)
->withStatus(302);
}
public function delete(ServerRequestInterface $request, ResponseInterface $response, array $args): ResponseInterface
{
$id = (int)$args['id'];
$this->deviceService->deleteDevice($id);
return $response
->withHeader('Location', '/devices')
->withStatus(302);
}
public function createFromHost(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
$data = $request->getParsedBody();
$hostId = $data['host_id'] ?? null;
$name = trim($data['name'] ?? '');
if ($hostId === null || $name === '') {
return $response->withStatus(400)->write('Host ID and name required');
}
$host = $this->discoveredHostRepository->findById((int)$hostId);
if ($host === null) {
return $response->withStatus(404)->write('Host not found');
}
$device = $this->deviceService->createFromDiscoveredHost(
$name,
$host->ipAddress,
$host->macAddress,
$host->hostname,
$host->vendor
);
// Mark host as accepted
$host->status = 'accepted';
$host->matchedDeviceId = (string)$device->id;
$this->discoveredHostRepository->save($host);
return $response
->withHeader('Location', '/devices/' . $device->id)
->withStatus(302);
}
}