Improved setup and new data inicialization.
CI / Docker Lint (push) Successful in 30s

This commit is contained in:
2024-10-09 20:50:23 +02:00
parent e93a5ff3a9
commit d9489f2558
21 changed files with 2814 additions and 67 deletions
+120
View File
@@ -0,0 +1,120 @@
<?php
namespace App\Controller;
use App\DTO\AppSetupConfig;
use App\DTO\ExampleDataConfig;
use App\Entity\ConfigVariable;
use App\Form\AppSetupFormType;
use App\Form\ExampleDataFormType;
use App\Repository\RoomCategoryRepository;
use App\Service\AppSetupService;
use App\Service\ExampleDataService;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
class AppSetupController extends AbstractController
{
#[Route(path: '/setup', name: 'app_setup_setup')]
public function addAdmin(
AppSetupService $setupService,
Request $request,
EntityManagerInterface $entityManager
): Response {
$configRepo = $entityManager->getRepository(ConfigVariable::class);
$setupStatus = $configRepo->findBy(["key" => "SETUP_STATUS"]);
if ($setupStatus != null && $setupStatus[0] != null && $setupStatus[0]->getValue() == 1) {
return $this->GoToResultPage(
isError: false,
message: "SomniaRooms already setup.",
linkRoute: "admin",
linkLabel: "Go to admin panel"
);
}
$config = new AppSetupConfig();
$form = $this->createForm(AppSetupFormType::class, $config);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$rs = $setupService->InitialSetup($config);
if (!$rs) {
return $this->GoToResultPage(
isError: false,
message: "An error occurred during setup proccess, try again."
);
}
if ($config->getGenExampleData()) {
return $this->redirectToRoute("app_setup_gendata");
}
return $this->GoToResultPage(
isError: false,
message: "SomniaRooms is setup! Go to admin panel to start!",
linkRoute: "admin",
linkLabel: "Go to admin panel"
);
}
return $this->render('app_setup/setup.html.twig', [
'form' => $form,
]);
}
#[Route('/debug/exampleData', name: 'app_setup_gendata')]
public function generateData(
Request $request,
ExampleDataService $dataService,
RoomCategoryRepository $roomCategoryRepository
): Response {
$config = new ExampleDataConfig();
$form = $this->createForm(ExampleDataFormType::class, $config);
$isFormInvalid = $form->isSubmitted() && !$form->isValid();
//Check for RoomCategories on db.
$roomCategoryCount = $roomCategoryRepository->count();
$hasRoomCategories = $roomCategoryCount > 0;
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$rs = $dataService->AddExampleData($config);
$isOk = $rs == "OK";
$msg = $isOk ? "Example data generated! Go to admin panel to start!" : $rs;
return $this->GoToResultPage(
isError: !$isOk,
message: $msg,
linkRoute: $isOk ? "admin" : "",
linkLabel: $isOk ? "Go to admin panel" : ""
);
}
return $this->render('app_setup/exampleData.html.twig', [
'form' => $form,
'isFormInvalid' => $isFormInvalid,
'hasRoomCategories' => $hasRoomCategories
]);
}
private function GoToResultPage(bool $isError, string $message, string $linkRoute = null, string $linkLabel = null): Response
{
return $this->render('app_setup/result.html.twig', [
'isError' => $isError,
'message' => $message,
'linkLabel' => $linkLabel,
'linkRoute' => $linkRoute
]);
}
}
+1 -55
View File
@@ -2,13 +2,8 @@
namespace App\Controller;
use App\Entity\User;
use App\Entity\ConfigVariable;
use App\Enum\ConfigSectionType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
@@ -34,53 +29,4 @@ class SecurityController extends AbstractController
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
#[Route(path: '/setup', name: 'app_security_addAdmin')]
public function addAdmin(UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager): Response
{
$configRepo = $entityManager->getRepository(ConfigVariable::class);
$setupStatus = $configRepo->findBy(["key" => "SETUP_STATUS"]);
if($setupStatus != null && $setupStatus[0] != null && $setupStatus[0]->getValue() != 1) {
return new Response("Setup was already done");
}
$entityManager->beginTransaction();
try {
//Create system variables.
$var_setup = new ConfigVariable();
$var_setup->setKey("SETUP_STATUS");
$var_setup->setValue("1");
$var_setup->setSection(ConfigSectionType::Internal);
$entityManager->persist($var_setup);
//Create user Admin.
$user = new User();
$user->setUsername("admin");
$user->setRoles(["ROLE_USER", "ROLE_ADMIN", "ROLE_SUPERADMIN"]);
$plaintextPassword = "admin";
// hash the password (based on the security.yaml config for the $user class)
$hashedPassword = $passwordHasher->hashPassword(
$user,
$plaintextPassword
);
$user->setPassword($hashedPassword);
$entityManager->persist($user);
$entityManager->flush();
$entityManager->commit();
} catch (\Throwable $th) {
$entityManager->rollback();
throw $th;
}
return new Response("Setup completed :)");
}
}
}