Basis Implementierung der Formulare
Bisher kein Design, nur '@chaospott.de' Adresse, Model WIP entworfen
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
// src/Controller/LuckyController.php
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\WireguardRequest;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
|
||||
#[Route('/success', name: 'app_default_form_success')]
|
||||
public function form_success() {
|
||||
|
||||
return $this->render('success.html.twig', []);
|
||||
}
|
||||
|
||||
#[Route('/fail', name: 'app_default_form_fail')]
|
||||
public function form_fail() {
|
||||
|
||||
return $this->render('fail.html.twig', []);
|
||||
}
|
||||
|
||||
#[Route('/', name: 'app_default_form')]
|
||||
public function form(Request $request): Response
|
||||
{
|
||||
$wireguardRequest = new WireguardRequest();
|
||||
$wireguardRequest->setAuthToken(sha1(random_bytes(255)));
|
||||
|
||||
$form = $this->createFormBuilder($wireguardRequest)
|
||||
->add('email', EmailType::class)
|
||||
//->add('publicKey', TextType::class)
|
||||
->add('submit', SubmitType::class)
|
||||
->getForm();
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
// $form->getData() holds the submitted values
|
||||
// but, the original `$task` variable has also been updated
|
||||
$wireguardRequest->setEmail($form->get('email')->getData());
|
||||
|
||||
if(str_ends_with($wireguardRequest->getEmail(), '@chaospott.de')) {
|
||||
|
||||
$model = new \App\Model\RequestsSqlite();
|
||||
$model->addRequest($wireguardRequest);
|
||||
|
||||
// send E-Mail
|
||||
return $this->redirectToRoute('app_default_form_success');
|
||||
} else {
|
||||
|
||||
return $this->redirectToRoute('app_default_form_fail');
|
||||
}
|
||||
|
||||
// ... perform some action, such as saving the task to the database
|
||||
|
||||
//return $this->redirectToRoute('task_success');
|
||||
}
|
||||
|
||||
|
||||
return $this->render('wireguardRequest.html.twig', [
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
class WireguardRequest
|
||||
{
|
||||
protected $email;
|
||||
protected $publicKey;
|
||||
protected $authToken;
|
||||
protected $validUntil;
|
||||
|
||||
public function __construct() {
|
||||
$dt = new \DateTime('now');
|
||||
$dt->modify('+30 minute');
|
||||
|
||||
$this->validUntil = $dt;
|
||||
}
|
||||
|
||||
public function setEmail($email):self
|
||||
{
|
||||
|
||||
$this->email = $email;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getEmail():String
|
||||
{
|
||||
return $this->email;
|
||||
}
|
||||
|
||||
public function setPublicKey($publicKey):self
|
||||
{
|
||||
|
||||
$this->publicKey = $publicKey;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getPublicKey():String
|
||||
{
|
||||
return $this->publicKey;
|
||||
}
|
||||
|
||||
public function setAuthToken($authToken):self
|
||||
{
|
||||
|
||||
$this->authToken = $authToken;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getAuthToken():String
|
||||
{
|
||||
return $this->authToken;
|
||||
}
|
||||
|
||||
public function getValidUntil():\DateTime
|
||||
{
|
||||
return $this->validUntil;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Entity\WireguardRequest;
|
||||
|
||||
class RequestsSqlite
|
||||
{
|
||||
private $db;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$db_existed = false;
|
||||
if(file_exists( 'Requests.db')) $db_existed = true;
|
||||
$this->db = new \SQLite3('Requests.db');
|
||||
|
||||
if(!$db_existed) $this->initializeDatabase();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function initializeDatabase() {
|
||||
$query = '
|
||||
CREATE TABLE requests
|
||||
(
|
||||
ID INT PRIMARY KEY NOT NULL,
|
||||
Email TEXT NOT NULL,
|
||||
AuthToken TEXT NOT NULL,
|
||||
ValidUntil TEXT NOT NULL,
|
||||
PublicKey TEXT
|
||||
)
|
||||
';
|
||||
|
||||
return $this->query($query);
|
||||
}
|
||||
|
||||
private function query($query) {
|
||||
|
||||
$ret = $this->db->exec($query);
|
||||
if(!$ret) {
|
||||
throw new \Exception($this->db->lastErrorMsg());
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function addRequest(WireguardRequest $wireguardRequest) {
|
||||
|
||||
$query = '
|
||||
INSERT INTO requests (AuthToken, ValidUntil, Email) VALUES
|
||||
(
|
||||
'.$wireguardRequest->getAuthToken().',
|
||||
'.$wireguardRequest->getValidUntil().',
|
||||
'.$wireguardRequest->getEmail().'
|
||||
);
|
||||
';
|
||||
|
||||
return $this->query($query);
|
||||
}
|
||||
|
||||
// TODO implement update validate ect
|
||||
}
|
||||
Reference in New Issue
Block a user