Setup - Create new setup endpoint to init DB and instance config.
CI / Docker Lint (push) Successful in 30s
CI / Docker Lint (push) Successful in 30s
This commit is contained in:
@@ -2,15 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
|
use App\Entity\User;
|
||||||
|
use App\Entity\ConfigVariable;
|
||||||
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
|
||||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
|
||||||
|
|
||||||
use App\Entity\User;
|
|
||||||
use App\Repository\UserRepository;
|
|
||||||
|
|
||||||
class SecurityController extends AbstractController
|
class SecurityController extends AbstractController
|
||||||
{
|
{
|
||||||
@@ -35,24 +34,51 @@ class SecurityController extends AbstractController
|
|||||||
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route(path: '/addAdmin', name: 'app_security_addAdmin')]
|
#[Route(path: '/setup', name: 'app_security_addAdmin')]
|
||||||
public function addAdmin(UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager): Response
|
public function addAdmin(UserPasswordHasherInterface $passwordHasher, EntityManagerInterface $entityManager): Response
|
||||||
{
|
{
|
||||||
$user = new User();
|
$setupStatus = $entityManager->find(ConfigVariable::class, "SETUP_STATUS");
|
||||||
$user->setUsername("admin");
|
|
||||||
$user->setRoles(["ROLE_USER", "ROLE_ADMIN"]);
|
|
||||||
$plaintextPassword = "admin";
|
|
||||||
|
|
||||||
// hash the password (based on the security.yaml config for the $user class)
|
if($setupStatus == null || $setupStatus == 1) {
|
||||||
$hashedPassword = $passwordHasher->hashPassword(
|
return new Response("Setup was already done");
|
||||||
$user,
|
}
|
||||||
$plaintextPassword
|
|
||||||
);
|
|
||||||
$user->setPassword($hashedPassword);
|
|
||||||
|
|
||||||
$entityManager->persist($user);
|
|
||||||
$entityManager->flush();
|
|
||||||
|
|
||||||
return new Response("Created user - Admin");
|
$entityManager->beginTransaction();
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
//Create system variables.
|
||||||
|
$var_setup = new ConfigVariable();
|
||||||
|
$var_setup->setKey("SETUP_STATUS");
|
||||||
|
$var_setup->setValue("1");
|
||||||
|
$var_setup->setSection("SYSTEM");
|
||||||
|
$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 :)");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user