ogcore/src/Controller/PropagateNetworkSettingsAct...

82 lines
3.0 KiB
PHP

<?php
namespace App\Controller;
use App\Entity\Client;
use App\Entity\OrganizationalUnit;
use App\Entity\PxeTemplate;
use App\Repository\ClientRepository;
use App\Repository\OrganizationalUnitRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class PropagateNetworkSettingsAction extends AbstractController
{
public function __construct(
private readonly OrganizationalUnitRepository $organizationalUnitRepository,
private readonly EntityManagerInterface $entityManager,
private readonly \App\Controller\OgBoot\PxeBootFile\PostAction $postAction,
private readonly \App\Controller\OgDhcp\Subnet\PutHostAction $putHostAction,
private readonly ClientRepository $clientRepository,
) {
}
public function __invoke(OrganizationalUnit $organizationalUnit): JsonResponse
{
$networkSettings = $organizationalUnit->getNetworkSettings();
if (!$networkSettings) {
return new JsonResponse(
data: 'No network settings found for this organizational unit',
status: Response::HTTP_UNPROCESSABLE_ENTITY
);
}
foreach ($organizationalUnit->getOrganizationalUnits() as $childUnit) {
if (!$childUnit->isExcludeParentChanges()) {
$childUnit->updateNetworkSettingsRecursively($networkSettings);
}
}
foreach ($organizationalUnit->getClients() as $client) {
$client->setMenu($networkSettings->getMenu());
$client->setHardwareProfile($networkSettings->getHardwareProfile());
$client->setRepository($networkSettings->getRepository());
$client->setBootServer($networkSettings->getBootServer());
$client->setOgLive($networkSettings->getOgLive());
$client->setTemplate($networkSettings->getPxeTemplate());
}
$this->entityManager->flush();
$clients = $this->clientRepository->findClientsByOrganizationalUnitAndDescendants($organizationalUnit->getId(), []);
/** @var Client $client */
foreach ($clients as $client) {
$template = $client->getTemplate() ?
$client->getTemplate() :
$client->getOrganizationalUnit()?->getNetworkSettings()?->getPxeTemplate();
if ($template === null) {
$template = $this->entityManager->getRepository(PxeTemplate::class)->findOneBy(['isDefault' => true]);
}
if ($template !== null) {
$this->postAction->__invoke($client, $template);
}
if ($client->getSubnet()) {
$this->putHostAction->__invoke($client->getMac(), $client);
}
}
return new JsonResponse(
data: 'Network settings propagated successfully',
status: Response::HTTP_OK
);
}
}