add templates for account

This commit is contained in:
Tykayn 2024-07-15 23:04:23 +02:00 committed by tykayn
parent ef4339d88c
commit c55d5766b1
12 changed files with 302 additions and 77 deletions

View File

@ -1,7 +1,8 @@
# see https://symfony.com/doc/current/reference/configuration/framework.html
framework:
secret: '%env(APP_SECRET)%'
#csrf_protection: true
ide: 'myide://open?url=file://%%f&line=%%l'
csrf_protection: true
http_method_override: false
# Enables session support. Note that the session will ONLY be started if you read or write from it.
@ -22,3 +23,4 @@ when@test:
test: true
session:
storage_factory_id: session.storage.factory.mock_file

View File

@ -17,17 +17,26 @@ security:
lazy: true
provider: app_user_provider
# activate different ways to authenticate
# https://symfony.com/doc/current/security.html#the-firewall
# https://symfony.com/doc/current/security/impersonating_user.html
# switch_user: true
switch_user: true
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
# Easy way to control access for large sections of your site
# Note: Only the *first* access control that matches will be used
access_control:
# - { path: ^/admin, roles: ROLE_ADMIN }
# - { path: ^/profile, roles: ROLE_USER }
- { path: ^/admin, roles: ROLE_ADMIN }
- { path: ^/profile, roles: ROLE_USER }
when@test:
security:

View File

@ -0,0 +1,18 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class AccountController extends AbstractController
{
#[Route('/account', name: 'app_account')]
public function index(): Response
{
return $this->render('account/index.html.twig', [
'controller_name' => 'AccountController',
]);
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class IndexController extends AbstractController
{
#[Route('/', name: 'app_index')]
public function index(): Response
{
return $this->render('index/index.html.twig', [
'controller_name' => 'IndexController',
]);
}
}

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\Annotation\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.');
}
}

View File

@ -40,28 +40,4 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader
$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

@ -0,0 +1,43 @@
{% extends 'base.html.twig' %}
{% block title %}Votre compte{% endblock %}
{% block body %}
<style>
.example-wrapper {
margin: 1em auto;
max-width: 800px;
width: 95%;
font: 18px/1.5 sans-serif;
}
.example-wrapper code {
background: #F5F5F5;
padding: 2px 6px;
}
</style>
<div class="example-wrapper">
<div id="account">
<h1>
Mon compte
</h1>
<p>
Coucou!
</p>
<menu>
Un menu
<ul>
<li>un choix</li>
<li>un autre</li>
<li>un bidule</li>
</ul>
</menu>
<footer>
<p>
<a href="{{ path('app_logout') }}">Logout</a>
</p>
</footer>
</div>
</div>
{% endblock body %}

View File

@ -0,0 +1,43 @@
{% extends 'base.html.twig' %}
{% block title %}Votre compte{% endblock %}
{% block body %}
<style>
.example-wrapper {
margin: 1em auto;
max-width: 800px;
width: 95%;
font: 18px/1.5 sans-serif;
}
.example-wrapper code {
background: #F5F5F5;
padding: 2px 6px;
}
</style>
<div class="example-wrapper">
<div id="account">
<h1>
Mon compte
</h1>
<p>
Coucou!
</p>
<menu>
Un menu
<ul>
<li>un choix</li>
<li>un autre</li>
<li>un bidule</li>
</ul>
</menu>
<footer>
<p>
<a href="{{ path('app_logout') }}">Logout</a>
</p>
</footer>
</div>
</div>
{% endblock body %}

View File

@ -1,9 +1,10 @@
<!DOCTYPE html>
<html>
<html lang="fr">
<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></svg>">
<title>{% block title %}Bienvenu!{% 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></svg>">
{# Run `composer require symfony/webpack-encore-bundle` to start using Symfony UX #}
{% block stylesheets %}
{{ encore_entry_link_tags('app') }}
@ -14,6 +15,9 @@
{% endblock %}
</head>
<body>
<div id="main_content">
{% import 'nav' %}
{% block body %}{% endblock %}
</div>
</body>
</html>

View File

@ -0,0 +1,14 @@
<div id="nav_menu">
<nav>
{% if app.user %}
<div class="mb-3">
You are logged in as
<a href="{{path('account')}}">Account</a>
{{ app.user.userIdentifier }}, <a href="{{ path('app_logout') }}">Logout</a>
</div>
{% else %}
<a href="{{path('login')}}">Login</a>
{% endif %}
<a href="{{path('admin')}}">Admin</a>
</nav>
</div>

View File

@ -0,0 +1,20 @@
{% extends 'base.html.twig' %}
{% block title %}Hello IndexController!{% endblock %}
{% block body %}
<style>
.example-wrapper { margin: 1em auto; max-width: 800px; width: 95%; font: 18px/1.5 sans-serif; }
.example-wrapper code { background: #F5F5F5; padding: 2px 6px; }
</style>
<div class="example-wrapper">
<h1>Hello {{ controller_name }}! ✅</h1>
This friendly message is coming from:
<ul>
<li>Your controller at <code><a href="{{ '/home/poule/encrypted/stockage-syncable/www/development/html/sf-with-users/userlands/src/Controller/IndexController.php'|file_link(0) }}">src/Controller/IndexController.php</a></code></li>
<li>Your template at <code><a href="{{ '/home/poule/encrypted/stockage-syncable/www/development/html/sf-with-users/userlands/templates/index/index.html.twig'|file_link(0) }}">templates/index/index.html.twig</a></code></li>
</ul>
</div>
{% endblock %}

View File

@ -0,0 +1,46 @@
{% extends 'base.html.twig' %}
{% block title %}Log in!{% endblock %}
{% block body %}
<form method="post">
{% 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>
<label for="username">Email</label>
<input type="email" value="{{ last_username }}" name="_username" id="username" class="form-control" autocomplete="email" required autofocus>
<label for="password">Password</label>
<input type="password" name="_password" id="password" class="form-control" autocomplete="current-password" required>
<input type="hidden" name="_csrf_token"
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">
<label>
<input type="checkbox" name="_remember_me"> Remember me
</label>
</div>
#}
{#
If you want to control the URL the user is redirected to on success
#}
<input type="hidden" name="_target_path" value="/account">
<button class="btn btn-lg btn-primary" type="submit">
Sign in
</button>
</form>
{% endblock %}