feat: Implement base functionality

This commit is contained in:
Dendy 2025-10-28 07:46:20 +01:00
parent c71eb9e61b
commit 711529d360
25 changed files with 3337 additions and 2249 deletions

View File

@ -10,7 +10,7 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[{compose.yaml,compose.*.yaml}]
[{compose.yaml,compose.*.yaml,*.html.*}]
indent_size = 2
[*.md]

4
.env
View File

@ -29,10 +29,10 @@ DEFAULT_URI=http://localhost
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# IMPORTANT: You MUST configure your server version, either here or in config/packages/doctrine.yaml
#
# DATABASE_URL="sqlite:///%kernel.project_dir%/var/data_%kernel.environment%.db"
DATABASE_URL="sqlite:///%kernel.project_dir%/var/data_%kernel.environment%.db"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=8.0.32&charset=utf8mb4"
# DATABASE_URL="mysql://app:!ChangeMe!@127.0.0.1:3306/app?serverVersion=10.11.2-MariaDB&charset=utf8mb4"
DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8"
# DATABASE_URL="postgresql://app:!ChangeMe!@127.0.0.1:5432/app?serverVersion=16&charset=utf8"
###< doctrine/doctrine-bundle ###
###> symfony/messenger ###

View File

@ -1,10 +1,6 @@
import './bootstrap.js';
/*
* Welcome to your app's main JavaScript file!
*
* This file will be included onto the page via the importmap() Twig function,
* which should already be in your base.html.twig.
*/
import './styles/vars.css';
import './styles/demdtrap.css';
import './styles/app.css';
console.log('This log comes from assets/app.js - welcome to AssetMapper! 🎉');
import htmx from 'htmx.org';
htmx.config.refreshOnHistoryMiss = true;

View File

@ -1,3 +0,0 @@
body {
background-color: skyblue;
}

236
assets/styles/demdtrap.css Normal file
View File

@ -0,0 +1,236 @@
* { margin: 0px; padding: 0px; }
h1 {
margin-bottom: var(--dt-spacing-2);
}
hr {
margin: var(--dt-spacing-2) var(--dt-spacing-0);
border: 0px;
border-bottom: 1px solid var(--dt-neutral-4);
}
a {
color: var(--dt-primary-lighter);
text-decoration: none;
&:hover {
background-color: var(--dt-primary-lighter);
color: var(--dt-neutral-2);
}
}
/* ------------------------------------------------------------- Margins --- */
.m-auto { margin: auto; }
.m-0 { margin: var(--dt-spacing-0); }
.m-1 { margin: var(--dt-spacing-1); }
.m-2 { margin: var(--dt-spacing-2); }
.m-3 { margin: var(--dt-spacing-3); }
.m-4 { margin: var(--dt-spacing-4); }
.m-5 { margin: var(--dt-spacing-5); }
.m-6 { margin: var(--dt-spacing-6); }
.mt-auto { margin: auto; }
.mt-0 { margin-top: var(--dt-spacing-0); }
.mt-1 { margin-top: var(--dt-spacing-1); }
.mt-2 { margin-top: var(--dt-spacing-2); }
.mt-3 { margin-top: var(--dt-spacing-3); }
.mt-4 { margin-top: var(--dt-spacing-4); }
.mt-5 { margin-top: var(--dt-spacing-5); }
.mt-6 { margin-top: var(--dt-spacing-6); }
.mb-auto { margin-bottom: auto; }
.mb-0 { margin-bottom: var(--dt-spacing-0); }
.mb-1 { margin-bottom: var(--dt-spacing-1); }
.mb-2 { margin-bottom: var(--dt-spacing-2); }
.mb-3 { margin-bottom: var(--dt-spacing-3); }
.mb-4 { margin-bottom: var(--dt-spacing-4); }
.mb-5 { margin-bottom: var(--dt-spacing-5); }
.mb-6 { margin-bottom: var(--dt-spacing-6); }
.ms-auto { margin-left: auto; }
.ms-0 { margin-left: var(--dt-spacing-0); }
.ms-1 { margin-left: var(--dt-spacing-1); }
.ms-2 { margin-left: var(--dt-spacing-2); }
.ms-3 { margin-left: var(--dt-spacing-3); }
.ms-4 { margin-left: var(--dt-spacing-4); }
.ms-5 { margin-left: var(--dt-spacing-5); }
.ms-6 { margin-left: var(--dt-spacing-6); }
.me-auto { margin-right: auto; }
.me-0 { margin-right: var(--dt-spacing-0); }
.me-1 { margin-right: var(--dt-spacing-1); }
.me-2 { margin-right: var(--dt-spacing-2); }
.me-3 { margin-right: var(--dt-spacing-3); }
.me-4 { margin-right: var(--dt-spacing-4); }
.me-5 { margin-right: var(--dt-spacing-5); }
.me-6 { margin-right: var(--dt-spacing-6); }
/* -------------------------------------------------------------- Padding --- */
.p-0 { padding: var(--dt-spacing-0); }
.p-1 { padding: var(--dt-spacing-1); }
.p-2 { padding: var(--dt-spacing-2); }
.p-3 { padding: var(--dt-spacing-3); }
.p-4 { padding: var(--dt-spacing-4); }
.p-5 { padding: var(--dt-spacing-5); }
.p-6 { padding: var(--dt-spacing-6); }
.pt-0 { padding-top: var(--dt-spacing-0); }
.pt-1 { padding-top: var(--dt-spacing-1); }
.pt-2 { padding-top: var(--dt-spacing-2); }
.pt-3 { padding-top: var(--dt-spacing-3); }
.pt-4 { padding-top: var(--dt-spacing-4); }
.pt-5 { padding-top: var(--dt-spacing-5); }
.pt-6 { padding-top: var(--dt-spacing-6); }
.pb-0 { padding-bottom: var(--dt-spacing-0); }
.pb-1 { padding-bottom: var(--dt-spacing-1); }
.pb-2 { padding-bottom: var(--dt-spacing-2); }
.pb-3 { padding-bottom: var(--dt-spacing-3); }
.pb-4 { padding-bottom: var(--dt-spacing-4); }
.pb-5 { padding-bottom: var(--dt-spacing-5); }
.pb-6 { padding-bottom: var(--dt-spacing-6); }
.ps-0 { padding-left: var(--dt-spacing-0); }
.ps-1 { padding-left: var(--dt-spacing-1); }
.ps-2 { padding-left: var(--dt-spacing-2); }
.ps-3 { padding-left: var(--dt-spacing-3); }
.ps-4 { padding-left: var(--dt-spacing-4); }
.ps-5 { padding-left: var(--dt-spacing-5); }
.ps-6 { padding-left: var(--dt-spacing-6); }
.pe-0 { padding-right: var(--dt-spacing-0); }
.pe-1 { padding-right: var(--dt-spacing-1); }
.pe-2 { padding-right: var(--dt-spacing-2); }
.pe-3 { padding-right: var(--dt-spacing-3); }
.pe-4 { padding-right: var(--dt-spacing-4); }
.pe-5 { padding-right: var(--dt-spacing-5); }
.pe-6 { padding-right: var(--dt-spacing-6); }
/* ------------------------------------------------------------ Object Fit ---*/
.fit-contain { object-fit: contain; }
.fit-cover { object-fit: cover; }
.fit-fill { object-fit: fill; }
.fit-none { object-fit: none; }
.fit-scale-down { object-fit: scale-down; }
/* ---------------------------------------------------------------- Floats ---*/
.float-end { float: right; }
.float-start { float: left; }
/* --------------------------------------------------------------- Borders ---*/
.border { border: 1px solid var(--dt-neutral-4); }
.border-top { border-top: 1px solid var(--dt-neutral-4); }
.border-bottom { border-bottom: 1px solid var(--dt-neutral-4); }
.border-start { border-left: 1px solid var(--dt-neutral-4); }
.border-end { border-right: 1px solid var(--dt-neutral-4); }
/* --------------------------------------------------------------- Colors --- */
.bg-neutral-1 { background-color: var(--dt-neutral-1) }
.bg-neutral-2 { background-color: var(--dt-neutral-2) }
.bg-neutral-3 { background-color: var(--dt-neutral-3) }
.bg-neutral-4 { background-color: var(--dt-neutral-4) }
.bg-neutral-5 { background-color: var(--dt-neutral-5) }
.bg-neutral-6 { background-color: var(--dt-neutral-6) }
.color-white { color: white; }
/* ------------------------------------------------------------------ Text ---*/
.fw-bold { font-weight: bold; }
.text-center { text-align: center; }
.text-start { text-align: left; }
.text-end { text-align: right; }
.text-muted { color: var(--dt-neutral-5); }
/* -------------------------------------------------------------- Display --- */
.d-contents { display: contents; }
.d-inline-block { display: inline-block; }
.d-block { display: block; }
.d-flex { display: flex; }
/* ----------------------------------------------------------------- Flex --- */
.flex-column { flex-direction: column; }
.flex-row { flex-direction: row; }
.flex-grow-1 { flex-grow: 1; }
.gap-0 { gap: var(--dt-spacing-0); }
.gap-1 { gap: var(--dt-spacing-1); }
.gap-2 { gap: var(--dt-spacing-2); }
.gap-3 { gap: var(--dt-spacing-3); }
.gap-4 { gap: var(--dt-spacing-4); }
.gap-5 { gap: var(--dt-spacing-5); }
.gap-6 { gap: var(--dt-spacing-6); }
.align-self-start { align-self: flex-start; }
.align-self-end { align-self: flex-end; }
.align-self-center { align-self: center; }
.align-self-baseline { align-self: baseline; }
.align-self-stretch { align-self: stretch; }
.align-content-start { align-content: flex-start; }
.align-content-end { align-content: flex-end; }
.align-content-center { align-content: center; }
.align-content-baseline { align-content: baseline; }
.align-content-stretch { align-content: stretch; }
.align-items-start { align-items: flex-start; }
.align-items-end { align-items: flex-end; }
.align-items-center { align-items: center; }
.align-items-baseline { align-items: baseline; }
.align-items-stretch { align-items: stretch; }
.justify-self-start { justify-self: flex-start; }
.justify-self-end { justify-self: flex-end; }
.justify-self-center { justify-self: center; }
.justify-self-baseline { justify-self: baseline; }
.justify-self-stretch { justify-self: stretch; }
.justify-content-start { justify-content: flex-start; }
.justify-content-end { justify-content: flex-end; }
.justify-content-center { justify-content: center; }
.justify-content-baseline { justify-content: baseline; }
.justify-content-stretch { justify-content: stretch; }
.justify-items-start { justify-items: flex-start; }
.justify-items-end { justify-items: flex-end; }
.justify-items-center { justify-items: center; }
.justify-items-baseline { justify-items: baseline; }
.justify-items-stretch { justify-items: stretch; }
/* ---------------------------------------------------------------- Sizes --- */
.w-100 { width: 100%; }
/* -------------------------------------------------------------- Buttons --- */
.btn {
border: 0px;
padding: var(--dt-spacing-1) var(--dt-spacing-2);
color: white;
border: 1px solid red;
}
.btn-primary { background-color: var(--dt-primary); border-color: var(--dt-primary-darker); }
.btn-secondary { background-color: var(--dt-secondary); border-color: var(--dt-secondary-darker); }
.btn-success { background-color: var(--dt-success); border-color: var(--dt-success-darker); }
.btn-warning { background-color: var(--dt-warning); border-color: var(--dt-warning-darker); }
.btn-danger { background-color: var(--dt-danger); border-color: var(--dt-danger-darker); }
.btn-primary:hover { background-color: var(--dt-primary-darker); }
.btn-secondary:hover { background-color: var(--dt-secondary-darker); }
.btn-success:hover { background-color: var(--dt-success-darker); }
.btn-warning:hover { background-color: var(--dt-warning-darker); }
.btn-danger:hover { background-color: var(--dt-danger-darker); }
.btn-group {
display: flex;
gap: 0px;
}
.btn-group > * {
width: 30px;
height: 30px;
border: 1px solid black;
border-left: none;
text-align: center;
line-height: 41px;
display: block;
color: white;
text-decoration: none;
font-weight: bold;
background-position: center;
background-repeat: no-repeat;
background-size: 15px;
}
.btn-group > :first-child {
border-left: 1px solid black;
}

48
assets/styles/vars.css Normal file
View File

@ -0,0 +1,48 @@
:root {
--dt-primary-darker: hsl(var(--dt-primary-darker-hsl));
--dt-primary: hsl(var(--dt-primary-hsl));
--dt-primary-lighter: hsl(var(--dt-primary-lighter-hsl));
--dt-secondary-darker: hsl(var(--dt-secondary-darker-hsl));
--dt-secondary: hsl(var(--dt-secondary-hsl));
--dt-secondary-lighter: hsl(var(--dt-secondary-lighter-hsl));
--dt-success-darker: hsl(var(--dt-success-darker-hsl));
--dt-success: hsl(var(--dt-success-hsl));
--dt-success-lighter: hsl(var(--dt-success-lighter-hsl));
--dt-warning-darker: hsl(var(--dt-warning-darker-hsl));
--dt-warning: hsl(var(--dt-warning-hsl));
--dt-warning-lighter: hsl(var(--dt-warning-lighter-hsl));
--dt-danger-darker: hsl(var(--dt-danger-darker-hsl));
--dt-danger: hsl(var(--dt-danger-hsl));
--dt-danger-lighter: hsl(var(--dt-danger-lighter-hsl));
--dt-primary-darker-hsl: 226deg 36% 19%;
--dt-primary-hsl: 226deg 36% 39%;
--dt-primary-lighter-hsl: 226deg 36% 59%;
--dt-secondary-darker-hsl: 219deg 87% 37%;
--dt-secondary-hsl: 219deg 87% 57%;
--dt-secondary-lighter-hsl: 219deg 87% 77%;
--dt-success-darker-hsl: 172deg 89% 17%;
--dt-success-hsl: 172deg 89% 37%;
--dt-success-lighter-hsl: 172deg 89% 57%;
--dt-warning-darker-hsl: 38deg 91% 43%;
--dt-warning-hsl: 38deg 91% 63%;
--dt-warning-lighter-hsl: 38deg 91% 83%;
--dt-danger-darker-hsl: 10deg 84% 41%;
--dt-danger-hsl: 10deg 84% 61%;
--dt-danger-lighter-hsl: 10deg 84% 81%;
--dt-neutral-1: hsl(230deg 20% 10%);
--dt-neutral-2: hsl(230deg 10% 20%);
--dt-neutral-3: hsl(230deg 5% 30%);
--dt-neutral-4: hsl(230deg 2% 40%);
--dt-neutral-5: hsl(230deg 1% 50%);
--dt-neutral-6: hsl(230deg 1% 60%);
--dt-spacing-0: 0.00rem;
--dt-spacing-1: 0.25rem;
--dt-spacing-2: 0.50rem;
--dt-spacing-3: 1.00rem;
--dt-spacing-4: 1.50rem;
--dt-spacing-5: 3.00rem;
--dt-spacing-6: 6.00rem;
}

4419
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -4,14 +4,26 @@ security:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
# https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
providers:
users_in_memory: { memory: null }
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
lazy: true
provider: users_in_memory
provider: app_user_provider
form_login:
login_path: app_login
check_path: app_login
enable_csrf: true
logout:
path: app_logout
# where to redirect after logout
# target: app_any_route
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall
@ -23,7 +35,9 @@ security:
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/register, roles: }
- { path: ^/login, roles: }
- { path: ^/, roles: ROLE_USER }
when@test:
security:

View File

@ -16,13 +16,6 @@ return [
'path' => './assets/app.js',
'entrypoint' => true,
],
'@hotwired/stimulus' => [
'version' => '3.2.2',
],
'@symfony/stimulus-bundle' => [
'path' => './vendor/symfony/stimulus-bundle/assets/dist/loader.js',
],
'@hotwired/turbo' => [
'version' => '7.3.0',
],
'htmx.org' => ['version' => '2.0.8'],
];

View File

@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20251028053521 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create User Entity & messenger messages, apparently';
}
public function up(Schema $schema): void
{
$this->addSql(
<<<SQL
CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
,username VARCHAR(180) NOT NULL
,password VARCHAR(255) NOT NULL
,roles CLOB NOT NULL
--(DC2Type:json)
)
SQL
);
$this->addSql('CREATE UNIQUE INDEX UNIQ_IDENTIFIER_USERNAME ON user (username)');
$this->addSql('CREATE TABLE messenger_messages (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, body CLOB NOT NULL, headers CLOB NOT NULL, queue_name VARCHAR(190) NOT NULL, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, available_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, delivered_at DATETIME DEFAULT NULL --(DC2Type:datetime_immutable)
)');
$this->addSql('CREATE INDEX IDX_75EA56E0FB7336F0 ON messenger_messages (queue_name)');
$this->addSql('CREATE INDEX IDX_75EA56E0E3BD61CE ON messenger_messages (available_at)');
$this->addSql('CREATE INDEX IDX_75EA56E016BA31DB ON messenger_messages (delivered_at)');
}
public function down(Schema $schema): void
{
$this->addSql('DROP TABLE user');
$this->addSql('DROP TABLE messenger_messages');
}
}

View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20251028060845 extends AbstractMigration
{
public function getDescription(): string
{
return 'Create minimal Entry Entity';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE entry (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id INTEGER NOT NULL, date_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, updated_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_2B219D70A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('CREATE INDEX IDX_2B219D70A76ED395 ON entry (user_id)');
}
public function down(Schema $schema): void
{
$this->addSql('DROP TABLE entry');
}
}

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20251028063716 extends AbstractMigration
{
public function getDescription(): string
{
return 'Oops, Entry->updatedAt shouldve been nullable';
}
public function up(Schema $schema): void
{
$this->addSql('CREATE TEMPORARY TABLE __temp__entry AS SELECT id, user_id, date_at, created_at, updated_at FROM entry');
$this->addSql('DROP TABLE entry');
$this->addSql('CREATE TABLE entry (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id INTEGER NOT NULL, date_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, updated_at DATETIME DEFAULT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_2B219D70A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON UPDATE NO ACTION ON DELETE NO ACTION NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('INSERT INTO entry (id, user_id, date_at, created_at, updated_at) SELECT id, user_id, date_at, created_at, updated_at FROM __temp__entry');
$this->addSql('DROP TABLE __temp__entry');
$this->addSql('CREATE INDEX IDX_2B219D70A76ED395 ON entry (user_id)');
}
public function down(Schema $schema): void
{
$this->addSql('CREATE TEMPORARY TABLE __temp__entry AS SELECT id, user_id, date_at, created_at, updated_at FROM entry');
$this->addSql('DROP TABLE entry');
$this->addSql('CREATE TABLE entry (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, user_id INTEGER NOT NULL, date_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, updated_at DATETIME NOT NULL --(DC2Type:datetime_immutable)
, CONSTRAINT FK_2B219D70A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) NOT DEFERRABLE INITIALLY IMMEDIATE)');
$this->addSql('INSERT INTO entry (id, user_id, date_at, created_at, updated_at) SELECT id, user_id, date_at, created_at, updated_at FROM __temp__entry');
$this->addSql('DROP TABLE __temp__entry');
$this->addSql('CREATE INDEX IDX_2B219D70A76ED395 ON entry (user_id)');
}
}

View File

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
abstract class CustomAbstractController extends AbstractController
{
function __construct() {}
protected function getUser(): ?User
{
$user = parent::getUser();
return $user instanceof User ? $user : null;
}
/** @param mixed[] $parameters */
protected function renderBlock(
string $view,
?string $block,
array $parameters = [],
?Response $response = null,
): Response {
return $block !== null
? parent::renderBlock(
$view,
$block,
$parameters + ['partial' => true],
$response,
)
: parent::render($view, $parameters, $response);
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Controller;
use App\Entity\Entry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
final class MainController extends CustomAbstractController
{
function __construct(
private EntityManagerInterface $em,
) {}
public static function tmpl(string $path): string
{
return "main/$path.html.twig";
}
#[Route('/', name: 'app_index')]
public function index(): Response
{
return $this->render(self::tmpl('index'), [
'controller_name' => 'MainController',
]);
}
#[Route('/fap', name: 'app_fap')]
public function fap(): Response
{
$entry = (new Entry())
->setUser($this->getUser())
->setDateAt(new \DateTimeImmutable('now'));
$this->em->persist($entry);
$this->em->flush();
return $this->renderBlock(
self::tmpl('index'),
'fap_statistics_container',
[],
);
}
#[Route('/fap/{id}/delete', 'app_fap_delete')]
public function fap_delete(Entry $entry): Response
{
$this->em->remove($entry);
$this->em->flush();
return $this->renderBlock(
self::tmpl('index'),
'fap_statistics_container',
[],
);
}
}

View File

@ -0,0 +1,42 @@
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\RegistrationFormType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Routing\Attribute\Route;
class RegistrationController extends AbstractController
{
#[Route('/register', name: 'app_register')]
public function register(Request $request, UserPasswordHasherInterface $userPasswordHasher, Security $security, EntityManagerInterface $entityManager): Response
{
$user = new User();
$form = $this->createForm(RegistrationFormType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var string $plainPassword */
$plainPassword = $form->get('plainPassword')->getData();
// encode the plain password
$user->setPassword($userPasswordHasher->hashPassword($user, $plainPassword));
$entityManager->persist($user);
$entityManager->flush();
// do anything else you need here, like send an email
return $security->login($user, 'form_login', 'main');
}
return $this->render('registration/register.html.twig', [
'registrationForm' => $form,
]);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
#[Route(path: '/login', name: 'app_login')]
public function login(AuthenticationUtils $authenticationUtils): Response
{
// get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();
// last username entered by the user
$lastUsername = $authenticationUtils->getLastUsername();
return $this->render('security/login.html.twig', [
'last_username' => $lastUsername,
'error' => $error,
]);
}
#[Route(path: '/logout', name: 'app_logout')]
public function logout(): void
{
throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
}
}

94
src/Entity/Entry.php Normal file
View File

@ -0,0 +1,94 @@
<?php
namespace App\Entity;
use App\Repository\EntryRepository;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: EntryRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Entry
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column]
private ?\DateTimeImmutable $dateAt = null;
#[ORM\Column]
private ?\DateTimeImmutable $createdAt = null;
#[ORM\Column(nullable: true)]
private ?\DateTimeImmutable $updatedAt = null;
#[ORM\ManyToOne(inversedBy: 'entries')]
#[ORM\JoinColumn(nullable: false)]
private ?User $user = null;
#[ORM\PrePersist]
public function prePresist(): void
{
$this->createdAt ??= new \DateTimeImmutable();
}
#[ORM\PreUpdate]
public function preUpdate(): void
{
$this->updatedAt = new \DateTimeImmutable;
}
public function getId(): ?int
{
return $this->id;
}
public function getDateAt(): ?\DateTimeImmutable
{
return $this->dateAt;
}
public function setDateAt(\DateTimeImmutable $dateAt): static
{
$this->dateAt = $dateAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): static
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeImmutable $updatedAt): static
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getUser(): ?User
{
return $this->user;
}
public function setUser(?User $user): static
{
$this->user = $user;
return $this;
}
}

159
src/Entity/User.php Normal file
View File

@ -0,0 +1,159 @@
<?php
namespace App\Entity;
use App\Repository\UserRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[ORM\UniqueConstraint(name: 'UNIQ_IDENTIFIER_USERNAME', fields: ['username'])]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
class User implements UserInterface, PasswordAuthenticatedUserInterface
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 180)]
private ?string $username = null;
/**
* @var list<string> The user roles
*/
#[ORM\Column]
private array $roles = [];
/**
* @var string The hashed password
*/
#[ORM\Column]
private ?string $password = null;
/**
* @var Collection<int, Entry>
*/
#[ORM\OneToMany(targetEntity: Entry::class, mappedBy: 'user', orphanRemoval: true)]
private Collection $entries;
public function __construct()
{
$this->entries = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getUsername(): ?string
{
return $this->username;
}
public function setUsername(string $username): static
{
$this->username = $username;
return $this;
}
/**
* A visual identifier that represents this user.
*
* @see UserInterface
*/
public function getUserIdentifier(): string
{
return (string) $this->username;
}
/**
* @see UserInterface
*/
public function getRoles(): array
{
$roles = $this->roles;
// guarantee every user at least has ROLE_USER
$roles[] = 'ROLE_USER';
return array_unique($roles);
}
/**
* @param list<string> $roles
*/
public function setRoles(array $roles): static
{
$this->roles = $roles;
return $this;
}
/**
* @see PasswordAuthenticatedUserInterface
*/
public function getPassword(): ?string
{
return $this->password;
}
public function setPassword(string $password): static
{
$this->password = $password;
return $this;
}
/**
* Ensure the session doesn't contain actual password hashes by CRC32C-hashing them, as supported since Symfony 7.3.
*/
public function __serialize(): array
{
$data = (array) $this;
$data["\0" . self::class . "\0password"] = hash('crc32c', $this->password);
return $data;
}
#[\Deprecated]
public function eraseCredentials(): void
{
// @deprecated, to be removed when upgrading to Symfony 8
}
/**
* @return Collection<int, Entry>
*/
public function getEntries(): Collection
{
return $this->entries;
}
public function addEntry(Entry $entry): static
{
if (!$this->entries->contains($entry)) {
$this->entries->add($entry);
$entry->setUser($this);
}
return $this;
}
public function removeEntry(Entry $entry): static
{
if ($this->entries->removeElement($entry)) {
// set the owning side to null (unless already changed)
if ($entry->getUser() === $this) {
$entry->setUser(null);
}
}
return $this;
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
class RegistrationFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('username')
->add('plainPassword', PasswordType::class, [
'mapped' => false,
'attr' => ['autocomplete' => 'new-password'],
'constraints' => [
new NotBlank([
'message' => 'Please enter a password',
]),
new Length([
'min' => 6,
'minMessage' => 'Your password should be at least {{ limit }} characters',
// max length allowed by Symfony for security reasons
'max' => 4096,
]),
],
])
;
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}

View File

@ -0,0 +1,43 @@
<?php
namespace App\Repository;
use App\Entity\Entry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Entry>
*/
class EntryRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Entry::class);
}
// /**
// * @return Entry[] Returns an array of Entry objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('e')
// ->andWhere('e.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('e.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Entry
// {
// return $this->createQueryBuilder('e')
// ->andWhere('e.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@ -0,0 +1,60 @@
<?php
namespace App\Repository;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
/**
* @extends ServiceEntityRepository<User>
*/
class UserRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, User::class);
}
/**
* Used to upgrade (rehash) the user's password automatically over time.
*/
public function upgradePassword(PasswordAuthenticatedUserInterface $user, string $newHashedPassword): void
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', $user::class));
}
$user->setPassword($newHashedPassword);
$this->getEntityManager()->persist($user);
$this->getEntityManager()->flush();
}
// /**
// * @return User[] Returns an array of User objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('u.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?User
// {
// return $this->createQueryBuilder('u')
// ->andWhere('u.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}

View File

@ -1,17 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text><text y=%221.3em%22 x=%220.2em%22 font-size=%2276%22 fill=%22%23fff%22>sf</text></svg>">
{% block stylesheets %}
{% endblock %}
<html class="bg-neutral-1 color-white">
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 128 128%22><text y=%221.2em%22 font-size=%2296%22>⚫️</text><text y=%221.3em%22 x=%220.2em%22 font-size=%2276%22 fill=%22%23fff%22>sf</text></svg>">
{% block stylesheets %}
{% endblock %}
{% block javascripts %}
{% block importmap %}{{ importmap('app') }}{% endblock %}
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
</body>
{% block javascripts %}
{% block importmap %}{{ importmap('app') }}{% endblock %}
{% endblock %}
</head>
<body class="bg-neutral-2 p-3 m-auto mt-4 border" style="width: 80vw;">
{% block body %}{% endblock %}
</body>
</html>

View File

@ -0,0 +1,43 @@
{% extends 'base.html.twig' %}
{% block title %}Hello MainController!{% endblock %}
{% block body %}
<h1>{{ app.user.username }}'s fap history</h1>
<form
hx-post="{{ path('app_fap') }}"
hx-target="#fap-statistics-container"
hx-swap="outerHTML"
>
<button
class="btn btn-success"
type="submit"
>Fap</button>
</form>
<hr>
{% block fap_statistics_container %}
<div class="d-flex flex-column gap-3" id="fap-statistics-container">
<div>
<b>total:</b> {{ app.user.entries|length }}
</div>
<div class="d-flex flex-column">
{% for entry in app.user.entries %}
<div>
{{ entry.dateAt|date('Y-m-d H:i:s') }}
<a
hx-delete="{{ path('app_fap_delete', { id: entry.id }) }}"
hx-target="#fap-statistics-container"
hx-swap="outerHTML"
hx-confirm="Are you sure you wanna delete your hard-worked-for masturbation log entry?"
>
(delete)
</a>
</div>
{% endfor %}
</div>
</div>
{% endblock %}
{% endblock body %}

View File

@ -0,0 +1,29 @@
{% extends 'base.html.twig' %}
{% block title %}Register{% endblock %}
{% block body %}
{{ form_start(registrationForm, { attr: {
class: 'text-ceinter d-flex flex-column gap-3 align-items-center',
} }) }}
<h1>Register</h1>
{{ form_errors(registrationForm) }}
{{ form_row(registrationForm.username, {
row_attr: {
class: 'd-flex flex-column',
},
}) }}
{{ form_row(registrationForm.plainPassword, {
row_attr: {
class: 'd-flex flex-column',
},
label: 'Password',
}) }}
<button type="submit" class="btn btn-success">
Register
</button>
{{ form_end(registrationForm) }}
{% endblock %}

View File

@ -0,0 +1,46 @@
{% extends 'base.html.twig' %}
{% block title %}Log in!{% endblock %}
{% block body %}
<form method="post" class="text-center d-flex flex-column gap-3 align-items-center">
{% if error %}
<div class="alert alert-danger">
{{ error.messageKey|trans(error.messageData, 'security') }}
</div>
{% endif %}
{% if app.user %}
<div class="mb-3">
You are logged in as {{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Logout</a>
</div>
{% endif %}
<h1 class="h3 mb-3 font-weight-normal">
Please sign in
</h1>
<div class="d-flex flex-column gap-1 align-items-start">
<label for="username">Username</label>
<input type="text" value="{{ last_username }}" name="_username" id="username" class="form-control" autocomplete="username" required autofocus>
</div>
<div class="d-flex flex-column gap-1 align-items-start">
<label for="password">Password</label>
<input type="password" name="_password" id="password" class="form-control" autocomplete="current-password" required>
</div>
<input type="hidden" name="_csrf_token" data-controller="csrf-protection" value="{{ csrf_token('authenticate') }}">
{#
Uncomment this section and add a remember_me option below your firewall to activate remember me functionality.
See https://symfony.com/doc/current/security/remember_me.html
<div class="checkbox mb-3">
<input type="checkbox" name="_remember_me" id="_remember_me">
<label for="_remember_me">Remember me</label>
</div>
#}
<button class="btn btn-lg btn-primary" type="submit">
Sign in
</button>
</form>
{% endblock body %}