before switch to turbo
This commit is contained in:
.envTODOsymfony.lock
assets
composer.jsoncomposer.lockconfig
package.jsonpublic/uploads/avatars
src
Controller
Entity
EventSubscriber
Exception
Form
Security
Twig
templates/themes/default
webpack.config.jsyarn.lock@ -4,6 +4,8 @@ namespace App\Controller\Admin;
|
||||
|
||||
use App\Entity\Projects;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\Field;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\ImageField;
|
||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
|
||||
@ -20,11 +22,13 @@ class ProjectsCrudController extends AbstractCrudController
|
||||
yield IdField::new(propertyName: 'id')
|
||||
->onlyOnIndex();
|
||||
yield TextField::new(propertyName: 'name');
|
||||
yield TextField::new(propertyName: 'description');
|
||||
yield AssociationField::new('developer');
|
||||
yield TextField::new(propertyName: 'description');
|
||||
yield ImageField::new(propertyName: 'teaserImage')
|
||||
->setBasePath(path: 'uploads/projects')
|
||||
->setUploadDir(uploadDirPath: 'public/uploads/projects')
|
||||
->setUploadedFileNamePattern(patternOrCallable: '[timestamp]-[slug].[extension]');
|
||||
yield Field::new('createdAt')
|
||||
->hideOnForm();
|
||||
}
|
||||
}
|
||||
|
@ -87,18 +87,7 @@ class SecurityController extends AbstractController
|
||||
} // no else, we already confirmed in the form itself
|
||||
$entityManager->persist(entity: $user);
|
||||
$entityManager->flush();
|
||||
|
||||
// generate a signed url and email it to the user
|
||||
$this->sendEmailConfirmation(verifyEmailRouteName: 'security_verify_email', user: $user,
|
||||
email: (new TemplatedEmail())
|
||||
->from(new Address(address: 'info@24unix.net', name: '24unix.net'))
|
||||
->to($user->getEmail())
|
||||
->subject(subject: 'Please Confirm your Email')
|
||||
->htmlTemplate(template: '@default/security/mail/registration.html.twig')
|
||||
->context(context: [
|
||||
'username' => $user->getUsername()
|
||||
])
|
||||
);
|
||||
$this->generateSignedUrlAndEmailToTheUser($user);
|
||||
|
||||
return $this->render(view: '@default/security/registration_finished.html.twig');
|
||||
}
|
||||
@ -127,7 +116,7 @@ class SecurityController extends AbstractController
|
||||
try {
|
||||
$this->handleEmailConfirmation(request: $request, user: $user);
|
||||
} catch (VerifyEmailExceptionInterface $exception) {
|
||||
$this->addFlash(type: 'verify_email_error', message: $translator->trans(id: $exception->getReason(), parameters: [], domain: 'VerifyEmailBundle'));
|
||||
$this->addFlash(type: 'error', message: $translator->trans(id: $exception->getReason(), parameters: [], domain: 'VerifyEmailBundle'));
|
||||
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
}
|
||||
@ -315,11 +304,48 @@ class SecurityController extends AbstractController
|
||||
*/
|
||||
public function handleEmailConfirmation(Request $request, User /*UserInterface*/ $user): void
|
||||
{
|
||||
$this->verifyEmailHelper->validateEmailConfirmation(signedUrl: $request->getUri(), userId: $user->getId(), userEmail: $user->getEmail());
|
||||
$this->verifyEmailHelper->validateEmailConfirmation(signedUrl: $request->getUri(), userId: $user->getId(), userEmail: $user->getEmail());
|
||||
$user->setIsVerified(isVerified: true);
|
||||
$this->entityManager->persist(entity: $user);
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
|
||||
$user->setIsVerified(isVerified: true);
|
||||
/**
|
||||
* @param mixed $user
|
||||
* @return void
|
||||
*/
|
||||
public function generateSignedUrlAndEmailToTheUser(mixed $user): void
|
||||
{
|
||||
$this->sendEmailConfirmation(verifyEmailRouteName: 'security_verify_email', user: $user,
|
||||
email: (new TemplatedEmail())
|
||||
->from(new Address(address: 'info@24unix.net', name: '24unix.net'))
|
||||
->to($user->getEmail())
|
||||
->subject(subject: 'Please Confirm your Email')
|
||||
->htmlTemplate(template: '@default/security/mail/registration.html.twig')
|
||||
->context(context: [
|
||||
'username' => $user->getUsername()
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
$this->entityManager->persist(entity: $user);
|
||||
$this->entityManager->flush();
|
||||
|
||||
#[Route('/security/resend/verify_email', name: 'security_resend_verify_email')]
|
||||
public function resendVerifyEmail(Request $request, UserRepository $userRepository)
|
||||
{
|
||||
|
||||
if ($request->isMethod('POST')) {
|
||||
|
||||
$email = $request->getSession()->get('non_verified_email');
|
||||
$user = $userRepository->findOneBy(['email' => $email]);
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException('user not found for email');
|
||||
}
|
||||
|
||||
$this->generateSignedUrlAndEmailToTheUser(user: $user);
|
||||
$this->addFlash('success', 'eMail has been sent.');
|
||||
|
||||
return $this->redirectToRoute('app_main');
|
||||
}
|
||||
return $this->render('@default/security/resend_activation.html.twig');
|
||||
}
|
||||
}
|
||||
|
@ -6,13 +6,18 @@ use App\Entity\User;
|
||||
use App\Form\EditProfileFormType;
|
||||
use App\Repository\UserRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Sunrise\Slugger\Slugger;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
use Symfony\Component\Validator\Constraints\File;
|
||||
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
|
||||
/**
|
||||
* Class UserController.
|
||||
@ -56,10 +61,6 @@ class UserController extends BaseController
|
||||
return $this->redirectToRoute(route: 'app_main');
|
||||
};
|
||||
|
||||
$user = $form->getData();
|
||||
// hash the plain password
|
||||
|
||||
|
||||
return $this->renderForm(view: '@default/user/edit_profile.html.twig', parameters: [
|
||||
'user' => $user,
|
||||
'userForm' => $form
|
||||
@ -93,4 +94,48 @@ class UserController extends BaseController
|
||||
'users' => $users,
|
||||
]);
|
||||
}
|
||||
|
||||
// TODO move to a helper class
|
||||
function humanFilesize($bytes, $decimals = 2)
|
||||
{
|
||||
$sz = 'BKMGTP';
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];
|
||||
}
|
||||
|
||||
|
||||
#[Route(path: '/user/upload/avatar/{id}', name: 'user_upload_avatar')]
|
||||
public function uploadAvatar(
|
||||
Request $request,
|
||||
UserRepository $userRepository,
|
||||
EntityManagerInterface $entityManager,
|
||||
ValidatorInterface $validator,
|
||||
int $id)
|
||||
{
|
||||
$user = $userRepository->find($id);
|
||||
|
||||
if (!$user) {
|
||||
return $this->json('User not found.', 404);
|
||||
}
|
||||
|
||||
$postMaxSize = UploadedFile::getMaxFilesize();
|
||||
$contentLength = $request->headers->get('Content-length');
|
||||
|
||||
if ($contentLength > $postMaxSize) {
|
||||
return $this->json('File is bigger than the allowed ' . $this->humanFilesize($postMaxSize) . ' Bytes.', 400);
|
||||
}
|
||||
|
||||
$uploadedAvatar = $request->files->get('file');
|
||||
$destination = $this->getParameter(name: 'kernel.project_dir') . '/public/uploads/avatars';
|
||||
$originalFilename = pathinfo($uploadedAvatar->getClientOriginalName(), PATHINFO_FILENAME);
|
||||
$slugger = new Slugger();
|
||||
$cleanFilename = $slugger->slugify($originalFilename);
|
||||
$newFilename = $cleanFilename . '-' . uniqid() . '.' . $uploadedAvatar->guessExtension();
|
||||
$uploadedAvatar->move($destination, $newFilename);
|
||||
$user->setAvatar($newFilename);
|
||||
$entityManager->persist(entity: $user);
|
||||
$entityManager->flush();
|
||||
|
||||
return $this->json(data: 'OK', status: 201);
|
||||
}
|
||||
}
|
||||
|
@ -2,15 +2,14 @@
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use ApiPlatform\Core\Annotation\ApiResource;
|
||||
use App\Repository\ProjectsRepository;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Stringable;
|
||||
|
||||
#[ORM\Entity(repositoryClass: ProjectsRepository::class)]
|
||||
#[ApiResource]
|
||||
class Projects implements \Stringable
|
||||
class Projects implements Stringable
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
@ -33,7 +32,7 @@ class Projects implements \Stringable
|
||||
private ?string $teaserImage = null;
|
||||
|
||||
#[ORM\ManyToMany(targetEntity: User::class, inversedBy: 'projects')]
|
||||
private $developer;
|
||||
private Collection $developer;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
61
src/EventSubscriber/CheckVerifiedUserSubscriber.php
Normal file
61
src/EventSubscriber/CheckVerifiedUserSubscriber.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\EventSubscriber;
|
||||
|
||||
use App\Exception\UserNotVerifiedException;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
|
||||
use Symfony\Component\Security\Http\Event\CheckPassportEvent;
|
||||
use Symfony\Component\Security\Http\Event\LoginFailureEvent;
|
||||
|
||||
class CheckVerifiedUserSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
|
||||
public function __construct(private readonly RouterInterface $router)
|
||||
{
|
||||
// empty body
|
||||
}
|
||||
|
||||
|
||||
public function onCheckPassport(CheckPassportEvent $event)
|
||||
{
|
||||
$passport = $event->getPassport();
|
||||
/*
|
||||
* var User $user
|
||||
*/
|
||||
$user = $passport->getUser();
|
||||
|
||||
if (!$user->isVerified()) {
|
||||
throw new UserNotVerifiedException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function onValidationFailure(LoginFailureEvent $failureEvent)
|
||||
{
|
||||
if (!$failureEvent->getException() instanceof UserNotVerifiedException) {
|
||||
return;
|
||||
}
|
||||
|
||||
$request = $failureEvent->getRequest();
|
||||
$email = $failureEvent->getPassport()->getUser()->getEmail();
|
||||
$request->getSession()->set('non_verified_email', $email);
|
||||
|
||||
$response = new RedirectResponse(
|
||||
$this->router->generate('security_resend_verify_email')
|
||||
);
|
||||
$failureEvent->setResponse($response);
|
||||
}
|
||||
|
||||
|
||||
public static function getSubscribedEvents(): array
|
||||
{
|
||||
return [
|
||||
CheckPassportEvent::class => ['onCheckPassport', -10],
|
||||
LoginFailureEvent::class => 'onValidationFailure'
|
||||
];
|
||||
}
|
||||
}
|
@ -2,62 +2,10 @@
|
||||
|
||||
namespace App\Exception;
|
||||
|
||||
use JetBrains\PhpStorm\ArrayShape;
|
||||
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use function is_array;
|
||||
|
||||
class UserNotVerifiedException extends AuthenticationException
|
||||
{
|
||||
private ?string $identifier = null;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getMessageKey(): string
|
||||
{
|
||||
return 'User is not verified.';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user identifier (e.g. username or email address).
|
||||
*/
|
||||
public function getUserIdentifier(): ?string
|
||||
{
|
||||
return $this->identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the user identifier (e.g. username or email address).
|
||||
*/
|
||||
public function setUserIdentifier(string $identifier): void
|
||||
{
|
||||
$this->identifier = $identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
#[ArrayShape(shape: ['{{ username }}' => "null|string", '{{ user_identifier }}' => "null|string"])]
|
||||
public function getMessageData(): array
|
||||
{
|
||||
return ['{{ username }}' => $this->identifier, '{{ user_identifier }}' => $this->identifier];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __serialize(): array
|
||||
{
|
||||
return [$this->identifier, parent::__serialize()];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function __unserialize(array $data): void
|
||||
{
|
||||
[$this->identifier, $parentData] = $data;
|
||||
$parentData = is_array(value: $parentData) ? $parentData : unserialize(data: $parentData);
|
||||
parent::__unserialize(data: $parentData);
|
||||
}
|
||||
}
|
||||
// empty body
|
||||
}
|
@ -5,10 +5,12 @@ namespace App\Form;
|
||||
use App\Entity\User;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FileType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Validator\Constraints\Image;
|
||||
use Symfony\Component\Validator\Constraints\Length;
|
||||
|
||||
class EditProfileFormType extends AbstractType
|
||||
@ -27,7 +29,7 @@ class EditProfileFormType extends AbstractType
|
||||
'options' => ['attr' => ['class' => 'password-field', 'autocomplete' => 'off']],
|
||||
'required' => false,
|
||||
'first_options' => ['label' => 'Password'],
|
||||
'second_options' => ['label' => 'Repeat Password (only needed if you want to update the password'],
|
||||
'second_options' => ['label' => 'Repeat Password (only needed if you want to update the password)'],
|
||||
'constraints' => [new Length(exactly: ['min' => 6])]
|
||||
])
|
||||
;
|
||||
|
@ -9,6 +9,7 @@ use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\RouterInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
|
||||
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator;
|
||||
@ -51,10 +52,6 @@ class LoginFormAuthenticator extends AbstractLoginFormAuthenticator
|
||||
throw new UserNotFoundException();
|
||||
}
|
||||
|
||||
if (!$user->isVerified()) {
|
||||
throw new UserNotVerifiedException();
|
||||
}
|
||||
|
||||
return $user;
|
||||
}),
|
||||
|
||||
|
27
src/Twig/AppExtension.php
Normal file
27
src/Twig/AppExtension.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
// public function __construct(private ContainerInterface $container)
|
||||
// {
|
||||
// }
|
||||
|
||||
public function getFunctions(): array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('avatar_asset', [$this, 'getAvatarPath'])
|
||||
];
|
||||
}
|
||||
|
||||
public function getAvatarPath(string $path): string
|
||||
{
|
||||
return '/uploads/avatars/' . $path;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user