diff --git a/config/packages/security.yaml b/config/packages/security.yaml index 133e1c1..3433203 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -1,7 +1,11 @@ security: # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers providers: - in_memory: { memory: ~ } + app_db_provider: + entity: + class: App\Entity\User + property: username + manager_name: default encoders: App\Entity\User: algorithm: 'argon2i' @@ -12,19 +16,29 @@ security: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false - main: + api: + pattern: ^/api/ anonymous: true + main: + pattern: ^/ + anonymous: ~ + provider: app_db_provider + form_login: + login_path: user_login + check_path: user_login + logout: + path: user_logout + target: / + remember_me: + secret: '%kernel.secret%' + lifetime: 604800 + path: / + always_remember_me: true - # activate different ways to authenticate - - # http_basic: true - # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate - - # form_login: true - # https://symfony.com/doc/current/security/form_login_setup.html # 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: ^/$, roles: IS_AUTHENTICATED_ANONYMOUSLY } + - { path: /login, roles: IS_AUTHENTICATED_ANONYMOUSLY } + - { path: ^/, roles: ROLE_USER } diff --git a/config/routes.yaml b/config/routes.yaml index f75dc68..a386f24 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -4,7 +4,7 @@ index: controller: App\Controller\MainController::index torrents_search: - path: /torrents/search + path: /torrents controller: App\Controller\TorrentController::searchTorrent requirements: method: GET @@ -23,6 +23,13 @@ user_register: method: GET inviteCode: \w{32} +user_login: + path: /login + controller: App\Controller\SecurityController::login + +user_logout: + path: /logout + # API api_v1_torrents: path: /api/v1/torrents diff --git a/src/Controller/MainController.php b/src/Controller/MainController.php index e2125a5..603c639 100644 --- a/src/Controller/MainController.php +++ b/src/Controller/MainController.php @@ -2,16 +2,29 @@ namespace App\Controller; -use App\Magnetico\Repository\TorrentRepository; +use App\Form\LoginType; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Form\Extension\Core\Type\SubmitType; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Response; class MainController extends Controller { - public function index(TorrentRepository $repo): Response + public function index(): Response { return $this->render('index.html.twig', [ - 'torrentsCount' => $repo->getTorrentsTotalCount(), + 'loginForm' => $this->createLoginForm('')->createView(), ]); } + + private function createLoginForm(string $username): FormInterface + { + $form = $this->createForm(LoginType::class, null, [ + 'action' => $this->generateUrl('user_login'), + ]); + $form->get('_username')->setData($username); + $form->add('submit', SubmitType::class); + + return $form; + } } \ No newline at end of file diff --git a/src/Controller/SecurityController.php b/src/Controller/SecurityController.php new file mode 100644 index 0000000..50e6ccf --- /dev/null +++ b/src/Controller/SecurityController.php @@ -0,0 +1,40 @@ +getLastAuthenticationError() ? $authenticationUtils->getLastAuthenticationError()->getMessage() : ''; + $lastUsername = $authenticationUtils->getLastUsername(); + + $form = $this->createLoginForm($lastUsername); + $form->handleRequest($request); + + if ($lastError) { + $form->addError(new FormError($lastError)); + } + + return $this->render('Security/login.html.twig', ['form' => $form->createView()]); + } + + private function createLoginForm(string $username): FormInterface + { + $form = $this->createForm(LoginType::class, null, [ + 'action' => $this->generateUrl('user_login'), + ]); + $form->get('_username')->setData($username); + $form->add('submit', SubmitType::class); + + return $form; + } +} \ No newline at end of file diff --git a/src/Form/LoginType.php b/src/Form/LoginType.php new file mode 100644 index 0000000..d5bb8ed --- /dev/null +++ b/src/Form/LoginType.php @@ -0,0 +1,26 @@ +add('_username', TextType::class, ['mapped' => false]) + ->add('_password', PasswordType::class, ['mapped' => false]) + ; + } + + public function getBlockPrefix() + { + // Empty prefix for default UsernamePasswordFrormAuthenticationListener + return ''; + } + + +} \ No newline at end of file diff --git a/templates/Security/login.html.twig b/templates/Security/login.html.twig new file mode 100644 index 0000000..0ec5005 --- /dev/null +++ b/templates/Security/login.html.twig @@ -0,0 +1,7 @@ +{% extends 'base.html.twig' %} + +{% block content %} +
Torrents indexed: {{ torrentsCount }}
+ {% if not is_granted('ROLE_USER') %} + Login + {% endif %}