Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Neither the property "plainPassword" nor one of the methods "getPlainPassword()", "plainPassword()", "isPlainPassword()", "hasPlainPassword()", "__get()" exist and have public access in class "App\Entity\Users".
- // Users.php
- namespace App\Entity;
- use Doctrine\ORM\Mapping as ORM;
- use Symfony\Component\Security\Core\User\UserInterface;
- /**
- * @ORM\Entity(repositoryClass="App\Repository\UsersRepository")
- * @ORM\Table(name="users")
- */
- class Users implements UserInterface, \Serializable
- {
- /**
- * @ORM\Id()
- * @ORM\GeneratedValue()
- * @ORM\Column(type="integer")
- */
- private $id;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\Length(max=100);
- */
- private $username;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\Length(max=100);
- */
- private $firstname;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\Length(max=100);
- */
- private $lastname;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\Length(max=100);
- * @Assert\Email()
- */
- private $email;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\Length(max=255);
- */
- private $password;
- /**
- * @ORM\Column(type="string", length=255)
- * @Assert\Length(max=255);
- */
- private $token;
- /**
- * @ORM\Column(type="integer")
- */
- private $roles;
- /**
- * @ORM\Column(type="datetime")
- */
- private $created_at;
- public function getId()
- {
- return $this->id;
- }
- public function getUsername(): ?string
- {
- return $this->username;
- }
- public function setUsername(string $username): self
- {
- $this->username = $username;
- return $this;
- }
- public function getFirstname(): ?string
- {
- return $this->firstname;
- }
- public function setFirstname(string $firstname): self
- {
- $this->firstname = $firstname;
- return $this;
- }
- public function getLastname(): ?string
- {
- return $this->lastname;
- }
- public function setLastname(string $lastname): self
- {
- $this->lastname = $lastname;
- return $this;
- }
- public function getEmail(): ?string
- {
- return $this->email;
- }
- public function setEmail(string $email): self
- {
- $this->email = $email;
- return $this;
- }
- public function getPassword(): ?string
- {
- return $this->password;
- }
- public function setPassword(string $password): self
- {
- $this->password = $password;
- return $this;
- }
- public function getToken(): ?string
- {
- return $this->token;
- }
- public function setToken(string $token): self
- {
- $this->token = $token;
- return $this;
- }
- public function getRoles(): ?int
- {
- return $this->roles;
- }
- public function setRoles(int $roles): self
- {
- $this->roles = $roles;
- return $this;
- }
- public function getCreatedAt(): ?\DateTimeInterface
- {
- return $this->created_at;
- }
- public function setCreatedAt(\DateTimeInterface $created_at): self
- {
- $this->created_at = $created_at;
- return $this;
- }
- /**
- * Retour le salt qui a servi à coder le mot de passe
- *
- * {@inheritdoc}
- */
- public function getSalt(): ?string
- {
- // See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html
- // we're using bcrypt in security.yml to encode the password, so
- // the salt value is built-in and you don't have to generate one
- return null;
- }
- /**
- * Removes sensitive data from the user.
- *
- * {@inheritdoc}
- */
- public function eraseCredentials(): void
- {
- // Nous n'avons pas besoin de cette methode car nous n'utilions pas de plainPassword
- // Mais elle est obligatoire car comprise dans l'interface UserInterface
- // $this->plainPassword = null;
- }
- /**
- * {@inheritdoc}
- */
- public function serialize(): string
- {
- return serialize([$this->id, $this->username, $this->password]);
- }
- /**
- * {@inheritdoc}
- */
- public function unserialize($serialized): void
- {
- [$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
- }
- }
- // UsersType.php
- namespace App\Form;
- use App\Entity\Users;
- use Symfony\Component\Form\AbstractType;
- use Symfony\Component\Form\FormBuilderInterface;
- use Symfony\Component\OptionsResolver\OptionsResolver;
- use Symfony\Component\Form\Extension\Core\Type\EmailType;
- use Symfony\Component\Form\Extension\Core\Type\TextType;
- use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
- use Symfony\Component\Form\Extension\Core\Type\PasswordType;
- class UsersType extends AbstractType
- {
- public function buildForm(FormBuilderInterface $builder, array $options)
- {
- $builder
- ->add('email', EmailType::class)
- ->add('username', TextType::class)
- ->add('firstname', TextType::class)
- ->add('lastname', TextType::class)
- ->add('plainPassword', RepeatedType::class, array(
- 'type' => PasswordType::class,
- 'first_options' => array('label' => 'Password'),
- 'second_options' => array('label' => 'Repeat Password')))
- // ->add('termsAccepted', CheckboxType::class, array(
- // 'mapped' => false,
- // 'constraints' => new IsTrue(),
- // ))
- ;
- }
- public function configureOptions(OptionsResolver $resolver)
- {
- $resolver->setDefaults(array(
- 'data_class' => Users::class,
- ));
- }
- }
- // UsersController
- namespace App\Controller;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- class UsersController extends Controller
- {
- /**
- * @Route("/users", name="users")
- */
- public function index()
- {
- return $this->render('users/index.html.twig', [
- 'controller_name' => 'UsersController',
- ]);
- }
- }
- //UsersRepository.php
- namespace App\Repository;
- use App\Entity\Users;
- use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
- use Symfony\Bridge\Doctrine\RegistryInterface;
- /**
- * @method Users|null find($id, $lockMode = null, $lockVersion = null)
- * @method Users|null findOneBy(array $criteria, array $orderBy = null)
- * @method Users[] findAll()
- * @method Users[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
- */
- class UsersRepository extends ServiceEntityRepository
- {
- public function __construct(RegistryInterface $registry)
- {
- parent::__construct($registry, Users::class);
- }
- // /**
- // * @return Users[] Returns an array of Users objects
- // */
- /*
- public function findByExampleField($value)
- {
- return $this->createQueryBuilder('u')
- ->andWhere('u.exampleField = :val')
- ->setParameter('val', $value)
- ->orderBy('u.id', 'ASC')
- ->setMaxResults(10)
- ->getQuery()
- ->getResult()
- ;
- }
- */
- /*
- public function findOneBySomeField($value): ?Users
- {
- return $this->createQueryBuilder('u')
- ->andWhere('u.exampleField = :val')
- ->setParameter('val', $value)
- ->getQuery()
- ->getOneOrNullResult()
- ;
- }
- */
- }
- // RegistrationController.php
- <?php
- namespace App\Controller;
- use App\Form\UsersType;
- use App\Entity\Users;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\Routing\Annotation\Route;
- use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
- class RegistrationController extends Controller
- {
- /**
- * @Route("/register", name="user_registration")
- */
- public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
- {
- // 1) build the form
- $user = new Users();
- $form = $this->createForm(UsersType::class, $user);
- // 2) handle the submit (will only happen on POST)
- $form->handleRequest($request);
- if ($form->isSubmitted() && $form->isValid()) {
- // 3) Encode the password (you could also do this via Doctrine listener)
- $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
- $user->setPassword($password);
- // 4) save the User!
- $entityManager = $this->getDoctrine()->getManager();
- $entityManager->persist($user);
- $entityManager->flush();
- // ... do any other work - like sending them an email, etc
- // maybe set a "flash" success message for the user
- return $this->redirectToRoute('users/register.html.twig');
- }
- return $this->render(
- 'users/register.html.twig',
- array('form' => $form->createView())
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement