Commit bf464fe8 authored by dj3c1t's avatar dj3c1t

installer

parent 4d005456
......@@ -8,15 +8,34 @@ use Symfony\Component\HttpFoundation\Response;
class InstallController extends Controller {
public function indexAction(Request $request, $step = 0) {
/*
public function indexAction(Request $request) {
$im = $this->container->get('dj3c1t_app.install_manager');
$parameters = $im->readParameters();
$parameters["database_user"] = 'install_user';
$im->writeParameters($parameters);
*/
return new Response("install step ".$step);
$parameters = $im->getParameters();
if($request->getMethod() == 'POST') {
try {
$im->setParameters($request);
$im->connectToDatabase();
$im->installDatabase();
$im->setSiteTitle();
$im->createAdminUser();
$im->saveParameters();
return $this->redirect($this->generateUrl('install_success'));
}
catch(\Exception $e) {
$this->get('session')->getFlashBag()->add('error', $e->getMessage());
}
}
return $this->render(
'Dj3c1tAppBundle:Install:index.html.twig',
array(
"parameters" => $im->getParameters(),
)
);
}
public function successAction() {
$this->get('session')->getFlashBag()->add('success', "Le site a été installé");
return $this->render('Dj3c1tAppBundle:Install:success.html.twig');
}
}
install:
path: /install/{step}
defaults: { _controller: Dj3c1tAppBundle:Install:index, step: 0 }
path: /install
defaults: { _controller: Dj3c1tAppBundle:Install:index }
install_success:
path: /install/success
defaults: { _controller: Dj3c1tAppBundle:Install:success }
login:
path: /login
......
{% extends '::base.html.twig' %}
{% block header %}
<h1>Installation</h1>
{% endblock %}
{% block main %}
{{ parent() }}
<div class="row">
<div class="box col-xs-6 col-xs-offset-3">
<form action="{{ path('install') }}" method="post">
<h3>Connexion à la base de données</h3>
<div class="form-group">
<label for="database_host">Serveur</label>
<input class="form-control" type="text" id="database_host" name="database_host" value="{{ parameters['database_host'] }}" />
</div>
<div class="form-group">
<label for="database_name">Nom de la base</label>
<input class="form-control" type="text" id="database_name" name="database_name" value="{{ parameters['database_name'] }}" />
</div>
<div class="form-group">
<label for="database_user">Login</label>
<input class="form-control" type="text" id="database_user" name="database_user" value="{{ parameters['database_user'] }}" />
</div>
<div class="form-group">
<label for="database_pass">Mot de passe</label>
<input class="form-control" type="password" id="database_pass" name="database_pass" value="{{ parameters['database_pass'] }}" />
</div>
<h3>Titre du site</h3>
<div class="form-group">
<input class="form-control" type="text" id="site_title" name="site_title" value="{{ parameters['site_title'] }}" />
</div>
<h3>Nouvel utilisateur (admin)</h3>
<div class="form-group">
<label for="admin_login">Login</label>
<input class="form-control" type="text" id="admin_login" name="admin_login" value="{{ parameters['admin_login'] }}" />
</div>
<div class="form-group">
<label for="admin_email">Email</label>
<input class="form-control" type="text" id="admin_email" name="admin_email" value="{{ parameters['admin_email'] }}" />
</div>
<div class="form-group">
<label for="admin_pass">Mot de passe</label>
<input class="form-control" type="password" id="admin_pass" name="admin_pass" value="{{ parameters['admin_pass'] }}" />
</div>
<button type="submit" class="btn btn-primary">Installer</button>
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
{% extends '::base.html.twig' %}
{% block header %}
<h1>Installation</h1>
{% endblock %}
{% block main %}
{{ parent() }}
<div class="row">
<div class="col-xs-6 col-xs-offset-3">
<a href="{{ app.request.getSchemeAndHttpHost() ~ app.request.getBasePath() }}" class="btn btn-default">Aller sur le site</a>
</form>
</div>
</div>
{% endblock %}
\ No newline at end of file
......@@ -6,13 +6,153 @@ use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\Response;
use Dj3c1t\AppBundle\Entity\User;
use Dj3c1t\AppBundle\Entity\Role;
class InstallManager {
private $container;
private $parameters;
public function __construct(Container $container) {
$this->container = $container;
$this->parameters = array(
"database_host" => "",
"database_name" => "",
"database_user" => "",
"database_pass" => "",
"site_title" => "",
"admin_login" => "",
"admin_email" => "",
"admin_pass" => "",
);
}
public function getParameters() {
return $this->parameters;
}
public function setParameters(Request $request) {
$data = $request->request->all();
if(
!isset($data["database_host"])
|| !isset($data["database_name"])
|| !isset($data["database_user"])
|| !isset($data["database_pass"])
|| !isset($data["site_title"])
|| !isset($data["admin_login"])
|| !isset($data["admin_email"])
|| !isset($data["admin_pass"])
) {
throw new \Exception("missing parameter");
}
$this->parameters = array(
"database_host" => $data["database_host"],
"database_name" => $data["database_name"],
"database_user" => $data["database_user"],
"database_pass" => $data["database_pass"],
"site_title" => $data["site_title"],
"admin_login" => $data["admin_login"],
"admin_email" => $data["admin_email"],
"admin_pass" => $data["admin_pass"],
);
}
public function connectToDatabase() {
try {
$em = $this->container->get('doctrine')->getManager();
$connection = $em->getConnection();
$params = $connection->getParams();
$params['host'] = $this->parameters['database_host'];
$params['dbname'] = $this->parameters['database_name'];
$params['user'] = $this->parameters['database_user'];
$params['password'] = $this->parameters['database_pass'];
if($connection->isConnected()) {
$connection->close();
}
$connection->__construct(
$params, $connection->getDriver(), $connection->getConfiguration(),
$connection->getEventManager()
);
$connection->connect();
}
catch(\Exception $e) {
throw new \Exception("Impossible de se connecter à la base de données");
}
}
public function installDatabase() {
$kernel = $this->container->get('kernel');
$application = new Application($kernel);
$application->setAutoExit(false);
$resultCode = $application->run(
new ArrayInput(
array(
'command' => 'doctrine:migration:migrate',
)
),
new NullOutput()
);
if($resultCode != 0) {
throw new \Exception("Impossible de créer les tables dans la base de données");
}
$application = new Application($kernel);
$application->setAutoExit(false);
$resultCode = $application->run(
new ArrayInput(
array(
'command' => 'doctrine:fixtures:load',
)
),
new NullOutput()
);
if($resultCode != 0) {
throw new \Exception("Impossible de remplir la base de données");
}
}
public function setSiteTitle() {
$em = $this->container->get('doctrine')->getManager();
$configurationRepo = $em->getRepository("Dj3c1tAppBundle:Configuration");
$configurationEntity = $configurationRepo->setConfiguration(
'site_title',
$this->parameters['site_title']
);
try {
$em->persist($configurationEntity);
$em->flush();
}
catch(\Exception $e) {
throw new \Exception("Impossible d'enregistrer le titre du site");
}
}
public function createAdminUser() {
$em = $this->container->get('doctrine')->getManager();
$roleRepo = $em->getRepository("Dj3c1tAppBundle:Role");
if(!($adminRole = $roleRepo->findOneByName('admin'))) {
throw new \Exception("Impossible de trouver le role admin");
}
$user = new User();
$user->setIsActive(true);
$user->setUsername($this->parameters['admin_login']);
$user->setEmail($this->parameters['admin_email']);
$user->setPassword($this->parameters['admin_pass']);
$user->setSalt("");
$factory = $this->container->get('security.encoder_factory');
$encoder = $factory->getEncoder($user);
$user->setPassword($encoder->encodePassword($user->getPassword(), $user->getSalt()));
$user->addRole($adminRole);
$em->persist($user);
$em->flush();
}
public function isInstalled() {
......@@ -38,6 +178,28 @@ class InstallManager {
return true;
}
public function isInstallRequest() {
return preg_match(
"/^install/",
$this->container->get('request_stack')->getMasterRequest()->get('_route')
);
}
public function redirectToInstall() {
return new RedirectResponse(
$this->container->get('router')->generate('install')
);
}
public function saveParameters() {
$parameters = $this->readParameters();
$parameters['database_host'] = $this->parameters['database_host'];
$parameters['database_name'] = $this->parameters['database_name'];
$parameters['database_user'] = $this->parameters['database_user'];
$parameters['database_password'] = $this->parameters['database_pass'];
$this->writeParameters($parameters);
}
public function readParameters() {
$parameters_file = $this->container->get('kernel')->getRootDir().'/config/parameters.yml';
if(!@file_exists($parameters_file)) {
......
......@@ -17,13 +17,10 @@ class RequestListener {
public function onKernelRequest(GetResponseEvent $event) {
$im = $this->container->get('dj3c1t_app.install_manager');
if(!$im->isInstalled()) {
$request = $event->getRequest();
if($request->get('_route') != 'install') {
$url = $this->container->get('router')->generate('install');
$response = new RedirectResponse($url);
$event->setResponse($response);
}
if(!$im->isInstalled() && !$im->isInstallRequest()) {
$event->setResponse(
$im->redirectToInstall()
);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment