Added authentication and overview

This commit is contained in:
Jeroen De Meerleer 2021-04-08 12:54:49 +02:00
parent 889f6ba8f0
commit 03a3d6659e
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
6 changed files with 82 additions and 11 deletions

View File

@ -7,8 +7,9 @@ use JeroenED\Framework\Kernel;
require_once '../bootstrap.php';
$kernel = new Kernel();
$kernel->setProjectDir(__DIR__ . '/..');
$kernel->setConfigDir(__DIR__ . '/../config/');
$kernel->setTemplateDir(__DIR__ . '/../templates/');
chdir(__DIR__ . '/..');
$kernel->setProjectDir(getcwd());
$kernel->setConfigDir(getcwd() . '/config/');
$kernel->setTemplateDir(getcwd() . '/templates/');
$kernel->handle()->send();

View File

@ -4,6 +4,7 @@
namespace JeroenED\Webcron\Controller;
use JeroenED\Framework\Controller;
use JeroenED\Webcron\Repository\Job;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
@ -13,6 +14,8 @@ class DefaultController extends Controller
if(!isset($_SESSION['isAuthenticated']) || !$_SESSION['isAuthenticated']) {
return new RedirectResponse($this->generateRoute('login'));
}
return new Response('Not yet implemented', 425);
$jobRepo = new Job($this->getDbCon());
$jobs = $jobRepo->getAllJobs();
return $this->render('job/overview.html.twig', ['jobs' => $jobs]);
}
}

View File

@ -4,6 +4,7 @@
namespace JeroenED\Webcron\Controller;
use JeroenED\Framework\Controller;
use JeroenED\Webcron\Repository\User;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
@ -19,7 +20,13 @@ class SecurityController extends Controller
public function loginCheckAction(): Response
{
$_SESSION['isAuthenticated'] = true;
return new Response('Not yet implemented', 425);
$request = $this->getRequest();
$userRepository = new User($this->getDbCon());
$credentials = $request->request->all();
if($userRepository->checkAuthentication($credentials['name'], $credentials['passwd'])) {
$_SESSION['isAuthenticated'] = true;
return new RedirectResponse($this->generateRoute('default'));
}
return new RedirectResponse($this->generateRoute('login'));
}
}

29
src/Repository/Job.php Normal file
View File

@ -0,0 +1,29 @@
<?php
namespace JeroenED\Webcron\Repository;
use Doctrine\DBAL\Connection;
class Job
{
private Connection $dbcon;
public function __construct(Connection $dbcon)
{
$this->dbcon = $dbcon;
}
public function getAllJobs()
{
$jobsSql = "SELECT * FROM job";
$jobsStmt = $this->dbcon->prepare($jobsSql);
$jobsRslt = $jobsStmt->execute();
$jobs = $jobsRslt->fetchAllAssociative();
foreach ($jobs as $key=>&$job) {
$job['data'] = json_decode($job['data'], true);
}
return $jobs;
}
}

31
src/Repository/User.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace JeroenED\Webcron\Repository;
use Doctrine\DBAL\Connection;
class User
{
private Connection $dbcon;
public function __construct(Connection $dbcon)
{
$this->dbcon = $dbcon;
}
public function checkAuthentication(string $user, string $password): bool
{
$userSql = "SELECT * from user WHERE email = :user";
$userStmt = $this->dbcon->prepare($userSql);
$userRslt = $userStmt->execute([':user' => $user]);
if($user = $userRslt->fetchAssociative()) {
$shaPass = hash('sha256', $password);
if(password_verify($shaPass, $user['password'])) {
return true;
}
}
return false;
}
}

View File

@ -19,14 +19,14 @@
{% for job in jobs %}
<tr{% if(job.norun == true) %} class="norun"{% endif %}>
<td>{{ job.name }}</td>
<td>{{ job.host }}</td>
<td>{{ job.data.host }}</td>
<td>{{ job.delay }}</td>
<td>{{ job.nextrun }}</td>
<td>
<a href="#" data-id="{{ job.jobID }}" class="runcron btn btn-default"><span class="glyphicon glyphicon-play"><span></a>
<a href="runs.php?jobID={{ job.jobID }}" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></a>
<a href="editjob.php?jobID={{ job.jobID }}" class="btn btn-default"><span class="glyphicon glyphicon-edit"><span></a>
<a onclick="return confirm('Are you sure you want to delete this job?')" href="overview.php?jobID={{ job.jobID }}&action=delete" class="btn btn-default"><span class="glyphicon glyphicon-remove"></span></a>
<a href="#" data-id="{{ job.id }}" class="runcron btn btn-default"><span class="glyphicon glyphicon-play"><span></a>
<a href="runs.php?jobID={{ job.id }}" class="btn btn-default"><span class="glyphicon glyphicon-align-justify"></span></a>
<a href="editjob.php?jobID={{ job.id }}" class="btn btn-default"><span class="glyphicon glyphicon-edit"><span></a>
<a onclick="return confirm('Are you sure you want to delete this job?')" href="overview.php?id={{ job.id }}&action=delete" class="btn btn-default"><span class="glyphicon glyphicon-remove"></span></a>
</td>
</tr>
{% endfor %}