before recepie upgrade
This commit is contained in:
assets
bin
composer.jsoncomposer.lockconfig
migrations
src
Controller
Entity
EntityListener
Form
Security
templates
@ -6,6 +6,7 @@ use App\Entity\Blog;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\SlugField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
|
||||
|
||||
@ -26,7 +27,8 @@ class BlogCrudController extends AbstractCrudController
|
||||
AssociationField::new('author')
|
||||
->autocomplete(),
|
||||
TextField::new('title'),
|
||||
TextField::new('slug'),
|
||||
SlugField::new('slug')
|
||||
->setTargetFieldName('title'),
|
||||
TextEditorField::new('teaser'),
|
||||
TextEditorField::new('content'),
|
||||
DateTimeField::new('createdAt'),
|
||||
|
@ -3,8 +3,12 @@
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Entity\Blog;
|
||||
use App\Form\BlogFormType;
|
||||
use App\Repository\BlogRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
@ -18,22 +22,76 @@ class BlogController extends AbstractController
|
||||
public function index(BlogRepository $blogRepository): Response
|
||||
{
|
||||
return $this->render('blog/index.html.twig', [
|
||||
'blogs' => $blogRepository->findAll()
|
||||
'blogs' => $blogRepository->findAll()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param $slug
|
||||
* @param \App\Repository\BlogRepository $blogRepository
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
#[Route('/blog/{slug}', name: 'blog')]
|
||||
#[Route('/blog_show/{slug}', name: 'blog')]
|
||||
public function show($slug, BlogRepository $blogRepository): Response
|
||||
{
|
||||
return $this->render('blog/show.html.twig', [
|
||||
'blog' => $blogRepository->findOneBy(['slug' => $slug])
|
||||
'blog' => $blogRepository->findOneBy(['slug' => $slug])
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Doctrine\ORM\EntityManagerInterface $entityManager
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
#[Route('/blog/new', name: 'blog_new')]
|
||||
public function new(EntityManagerInterface $entityManager, Request $request): RedirectResponse|Response
|
||||
{
|
||||
$form = $this->createForm(BlogFormType::class);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$blog = $form->getData();
|
||||
$entityManager->persist($blog);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('blogs');
|
||||
}
|
||||
return $this->render('blog/new.html.twig', [
|
||||
'blogForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Entity\Blog $blog
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
*
|
||||
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
#[Route('/blog/edit/{id}', name: 'blog_edit')]
|
||||
public function edit(Blog $blog, Request $request): Response|RedirectResponse
|
||||
{
|
||||
$form = $this->createForm(BlogFormType::class, $blog);
|
||||
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$blog = $form->getData();
|
||||
//$blog->setAuthor($this->getUser());
|
||||
|
||||
$entityManager = $this->getDoctrine()->getManager();
|
||||
$entityManager->persist($blog);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->redirectToRoute('blogs');
|
||||
}
|
||||
return $this->render('blog/new.html.twig', [
|
||||
'blogForm' => $form->createView(),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -7,11 +7,13 @@ use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use App\Repository\SectionRepository;
|
||||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
/**
|
||||
* @ORM\Entity(repositoryClass=BlogRepository::class)
|
||||
* @ORM\HasLifecycleCallbacks()
|
||||
* @UniqueEntity("slug")
|
||||
*/
|
||||
class Blog
|
||||
{
|
||||
@ -79,7 +81,7 @@ class Blog
|
||||
private $comments;
|
||||
|
||||
/**
|
||||
* @ORM\Column(type="string", length=255)
|
||||
* @ORM\Column(type="string", length=255, unique=true)
|
||||
*/
|
||||
private $slug;
|
||||
|
||||
@ -274,7 +276,24 @@ class Blog
|
||||
public function setSlug(string $slug): self
|
||||
{
|
||||
$this->slug = $slug;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @ORM\PrePersist
|
||||
*/
|
||||
public function onPrePersist()
|
||||
{
|
||||
$this->createdAt = new \DateTime();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param SluggerInterface $slugger
|
||||
*/
|
||||
public function computeSlug(SluggerInterface $slugger)
|
||||
{
|
||||
$this->slug = $slugger->slug($this->title);
|
||||
}
|
||||
|
||||
}
|
||||
|
43
src/EntityListener/BlogEntityListener.php
Normal file
43
src/EntityListener/BlogEntityListener.php
Normal file
@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\EntityListener;
|
||||
|
||||
use App\Entity\Blog;
|
||||
use Doctrine\ORM\Event\LifecycleEventArgs;
|
||||
use JetBrains\PhpStorm\NoReturn;
|
||||
use Symfony\Component\String\Slugger\SluggerInterface;
|
||||
|
||||
/**
|
||||
* Class BlogEntityListener
|
||||
* @package App\EntityListener
|
||||
*/
|
||||
class BlogEntityListener
|
||||
{
|
||||
private SluggerInterface $slugger;
|
||||
|
||||
public function __construct(SluggerInterface $slugger)
|
||||
{
|
||||
$this->slugger = $slugger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Entity\Blog $blog
|
||||
* @param \Doctrine\ORM\Event\LifecycleEventArgs $title
|
||||
*/
|
||||
#[NoReturn]
|
||||
public function prePersist(Blog $blog, LifecycleEventArgs $title)
|
||||
{
|
||||
$blog->computeSlug($this->slugger);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \App\Entity\Blog $blog
|
||||
* @param \Doctrine\ORM\Event\LifecycleEventArgs $title
|
||||
*/
|
||||
#[NoReturn]
|
||||
public function preUpdate(Blog $blog, LifecycleEventArgs $title)
|
||||
{
|
||||
//dd($title);
|
||||
$blog->computeSlug($this->slugger);
|
||||
}
|
||||
}
|
40
src/Form/BlogFormType.php
Normal file
40
src/Form/BlogFormType.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
|
||||
namespace App\Form;
|
||||
|
||||
|
||||
use App\Entity\Blog;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/**
|
||||
* Class BlogFormType
|
||||
* @package App\Form
|
||||
*/
|
||||
class BlogFormType extends \Symfony\Component\Form\AbstractType
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('title')
|
||||
->add('teaser')
|
||||
->add('content')
|
||||
->add('author');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => Blog::class
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -7,12 +7,12 @@ use Doctrine\ORM\EntityManagerInterface;
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\Security\Core\Exception\InvalidCsrfTokenException;
|
||||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
||||
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
@ -22,23 +22,27 @@ use Symfony\Component\Security\Guard\Authenticator\AbstractFormLoginAuthenticato
|
||||
use Symfony\Component\Security\Guard\PasswordAuthenticatedInterface;
|
||||
use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
||||
|
||||
/**
|
||||
* Class AppAuthenticator
|
||||
* @package App\Security
|
||||
*/
|
||||
class AppAuthenticator extends AbstractFormLoginAuthenticator implements PasswordAuthenticatedInterface
|
||||
{
|
||||
use TargetPathTrait;
|
||||
|
||||
public const LOGIN_ROUTE = 'app_login';
|
||||
|
||||
private $entityManager;
|
||||
private $urlGenerator;
|
||||
private $csrfTokenManager;
|
||||
private $passwordEncoder;
|
||||
private EntityManagerInterface $entityManager;
|
||||
private UrlGeneratorInterface $urlGenerator;
|
||||
private CsrfTokenManagerInterface $csrfTokenManager;
|
||||
private UserPasswordHasherInterface $passwordHasher;
|
||||
|
||||
public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordEncoderInterface $passwordEncoder)
|
||||
public function __construct(EntityManagerInterface $entityManager, UrlGeneratorInterface $urlGenerator, CsrfTokenManagerInterface $csrfTokenManager, UserPasswordHasherInterface $passwordHasher)
|
||||
{
|
||||
$this->entityManager = $entityManager;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->csrfTokenManager = $csrfTokenManager;
|
||||
$this->passwordEncoder = $passwordEncoder;
|
||||
$this->passwordHasher = $passwordHasher;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -109,7 +113,7 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor
|
||||
* @throws AuthenticationException
|
||||
*
|
||||
*/
|
||||
public function getUser($credentials, UserProviderInterface $userProvider)
|
||||
public function getUser($credentials, UserProviderInterface $userProvider): ?UserInterface
|
||||
{
|
||||
$token = new CsrfToken('authenticate', $credentials['csrf_token']);
|
||||
if (!$this->csrfTokenManager->isTokenValid($token)) {
|
||||
@ -125,7 +129,7 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor
|
||||
}
|
||||
|
||||
if (!$user) {
|
||||
throw new UsernameNotFoundException('Username or email could not be found.');
|
||||
throw new UserNotFoundException('Username or email could not be found.');
|
||||
} else {
|
||||
return $user;
|
||||
}
|
||||
@ -149,7 +153,7 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor
|
||||
public function checkCredentials($credentials, UserInterface $user): bool
|
||||
{
|
||||
//return true;
|
||||
return $this->passwordEncoder->isPasswordValid($user, $credentials['password']);
|
||||
return $this->passwordHasher->isPasswordValid($user, $credentials['password']);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -160,6 +164,17 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor
|
||||
return $credentials['password'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when authentication executed and was successful!
|
||||
*
|
||||
* This should return the Response sent back to the user, like a
|
||||
* RedirectResponse to the last page they visited.
|
||||
*
|
||||
* If you return null, the current request will continue, and the user
|
||||
* will be authenticated. This makes sense, for example, with an API.
|
||||
*
|
||||
* @return Response|null
|
||||
*/
|
||||
public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $providerKey)
|
||||
{
|
||||
if ($targetPath = $this->getTargetPath($request->getSession(), $providerKey)) {
|
||||
@ -174,15 +189,3 @@ class AppAuthenticator extends AbstractFormLoginAuthenticator implements Passwor
|
||||
return $this->urlGenerator->generate(self::LOGIN_ROUTE);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
comment:
|
||||
author => user,
|
||||
createdAt,
|
||||
editedAt,
|
||||
editedby => user,
|
||||
editreason
|
||||
|
||||
*/
|
||||
|
||||
|
Reference in New Issue
Block a user