Advertisement
Guest User

Untitled

a guest
Jun 5th, 2018
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1.  
  2. Neither the property "plainPassword" nor one of the methods "getPlainPassword()", "plainPassword()", "isPlainPassword()", "hasPlainPassword()", "__get()" exist and have public access in class "App\Entity\Users".
  3.  
  4.  
  5. // Users.php
  6.  
  7. namespace App\Entity;
  8.  
  9. use Doctrine\ORM\Mapping as ORM;
  10. use Symfony\Component\Security\Core\User\UserInterface;
  11.  
  12.  
  13. /**
  14. * @ORM\Entity(repositoryClass="App\Repository\UsersRepository")
  15. * @ORM\Table(name="users")
  16. */
  17. class Users implements UserInterface, \Serializable
  18. {
  19. /**
  20. * @ORM\Id()
  21. * @ORM\GeneratedValue()
  22. * @ORM\Column(type="integer")
  23. */
  24. private $id;
  25.  
  26. /**
  27. * @ORM\Column(type="string", length=255)
  28. * @Assert\Length(max=100);
  29. */
  30. private $username;
  31.  
  32. /**
  33. * @ORM\Column(type="string", length=255)
  34. * @Assert\Length(max=100);
  35. */
  36. private $firstname;
  37.  
  38. /**
  39. * @ORM\Column(type="string", length=255)
  40. * @Assert\Length(max=100);
  41. */
  42. private $lastname;
  43.  
  44. /**
  45. * @ORM\Column(type="string", length=255)
  46. * @Assert\Length(max=100);
  47. * @Assert\Email()
  48. */
  49. private $email;
  50.  
  51. /**
  52. * @ORM\Column(type="string", length=255)
  53. * @Assert\Length(max=255);
  54. */
  55. private $password;
  56.  
  57. /**
  58. * @ORM\Column(type="string", length=255)
  59. * @Assert\Length(max=255);
  60. */
  61. private $token;
  62.  
  63. /**
  64. * @ORM\Column(type="integer")
  65. */
  66. private $roles;
  67.  
  68. /**
  69. * @ORM\Column(type="datetime")
  70. */
  71. private $created_at;
  72.  
  73. public function getId()
  74. {
  75. return $this->id;
  76. }
  77.  
  78. public function getUsername(): ?string
  79. {
  80. return $this->username;
  81. }
  82.  
  83. public function setUsername(string $username): self
  84. {
  85. $this->username = $username;
  86.  
  87. return $this;
  88. }
  89.  
  90. public function getFirstname(): ?string
  91. {
  92. return $this->firstname;
  93. }
  94.  
  95. public function setFirstname(string $firstname): self
  96. {
  97. $this->firstname = $firstname;
  98.  
  99. return $this;
  100. }
  101.  
  102. public function getLastname(): ?string
  103. {
  104. return $this->lastname;
  105. }
  106.  
  107. public function setLastname(string $lastname): self
  108. {
  109. $this->lastname = $lastname;
  110.  
  111. return $this;
  112. }
  113.  
  114. public function getEmail(): ?string
  115. {
  116. return $this->email;
  117. }
  118.  
  119. public function setEmail(string $email): self
  120. {
  121. $this->email = $email;
  122.  
  123. return $this;
  124. }
  125.  
  126. public function getPassword(): ?string
  127. {
  128. return $this->password;
  129. }
  130.  
  131. public function setPassword(string $password): self
  132. {
  133. $this->password = $password;
  134.  
  135. return $this;
  136. }
  137.  
  138. public function getToken(): ?string
  139. {
  140. return $this->token;
  141. }
  142.  
  143. public function setToken(string $token): self
  144. {
  145. $this->token = $token;
  146.  
  147. return $this;
  148. }
  149.  
  150. public function getRoles(): ?int
  151. {
  152. return $this->roles;
  153. }
  154.  
  155. public function setRoles(int $roles): self
  156. {
  157. $this->roles = $roles;
  158.  
  159. return $this;
  160. }
  161.  
  162. public function getCreatedAt(): ?\DateTimeInterface
  163. {
  164. return $this->created_at;
  165. }
  166.  
  167. public function setCreatedAt(\DateTimeInterface $created_at): self
  168. {
  169. $this->created_at = $created_at;
  170.  
  171. return $this;
  172. }
  173.  
  174.  
  175. /**
  176. * Retour le salt qui a servi à coder le mot de passe
  177. *
  178. * {@inheritdoc}
  179. */
  180. public function getSalt(): ?string
  181. {
  182. // See "Do you need to use a Salt?" at https://symfony.com/doc/current/cookbook/security/entity_provider.html
  183. // we're using bcrypt in security.yml to encode the password, so
  184. // the salt value is built-in and you don't have to generate one
  185.  
  186. return null;
  187. }
  188.  
  189. /**
  190. * Removes sensitive data from the user.
  191. *
  192. * {@inheritdoc}
  193. */
  194. public function eraseCredentials(): void
  195. {
  196. // Nous n'avons pas besoin de cette methode car nous n'utilions pas de plainPassword
  197. // Mais elle est obligatoire car comprise dans l'interface UserInterface
  198. // $this->plainPassword = null;
  199. }
  200.  
  201. /**
  202. * {@inheritdoc}
  203. */
  204. public function serialize(): string
  205. {
  206. return serialize([$this->id, $this->username, $this->password]);
  207. }
  208.  
  209. /**
  210. * {@inheritdoc}
  211. */
  212. public function unserialize($serialized): void
  213. {
  214. [$this->id, $this->username, $this->password] = unserialize($serialized, ['allowed_classes' => false]);
  215. }
  216. }
  217.  
  218. // UsersType.php
  219.  
  220.  
  221. namespace App\Form;
  222.  
  223. use App\Entity\Users;
  224. use Symfony\Component\Form\AbstractType;
  225. use Symfony\Component\Form\FormBuilderInterface;
  226. use Symfony\Component\OptionsResolver\OptionsResolver;
  227. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  228. use Symfony\Component\Form\Extension\Core\Type\TextType;
  229. use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
  230. use Symfony\Component\Form\Extension\Core\Type\PasswordType;
  231.  
  232. class UsersType extends AbstractType
  233. {
  234. public function buildForm(FormBuilderInterface $builder, array $options)
  235. {
  236. $builder
  237. ->add('email', EmailType::class)
  238. ->add('username', TextType::class)
  239. ->add('firstname', TextType::class)
  240. ->add('lastname', TextType::class)
  241. ->add('plainPassword', RepeatedType::class, array(
  242. 'type' => PasswordType::class,
  243. 'first_options' => array('label' => 'Password'),
  244. 'second_options' => array('label' => 'Repeat Password')))
  245. // ->add('termsAccepted', CheckboxType::class, array(
  246. // 'mapped' => false,
  247. // 'constraints' => new IsTrue(),
  248. // ))
  249. ;
  250. }
  251.  
  252. public function configureOptions(OptionsResolver $resolver)
  253. {
  254. $resolver->setDefaults(array(
  255. 'data_class' => Users::class,
  256. ));
  257. }
  258. }
  259.  
  260. // UsersController
  261. namespace App\Controller;
  262.  
  263. use Symfony\Component\Routing\Annotation\Route;
  264. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  265.  
  266. class UsersController extends Controller
  267. {
  268. /**
  269. * @Route("/users", name="users")
  270. */
  271. public function index()
  272. {
  273. return $this->render('users/index.html.twig', [
  274. 'controller_name' => 'UsersController',
  275. ]);
  276. }
  277. }
  278.  
  279. //UsersRepository.php
  280.  
  281.  
  282. namespace App\Repository;
  283.  
  284. use App\Entity\Users;
  285. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  286. use Symfony\Bridge\Doctrine\RegistryInterface;
  287.  
  288. /**
  289. * @method Users|null find($id, $lockMode = null, $lockVersion = null)
  290. * @method Users|null findOneBy(array $criteria, array $orderBy = null)
  291. * @method Users[] findAll()
  292. * @method Users[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  293. */
  294. class UsersRepository extends ServiceEntityRepository
  295. {
  296. public function __construct(RegistryInterface $registry)
  297. {
  298. parent::__construct($registry, Users::class);
  299. }
  300.  
  301. // /**
  302. // * @return Users[] Returns an array of Users objects
  303. // */
  304. /*
  305. public function findByExampleField($value)
  306. {
  307. return $this->createQueryBuilder('u')
  308. ->andWhere('u.exampleField = :val')
  309. ->setParameter('val', $value)
  310. ->orderBy('u.id', 'ASC')
  311. ->setMaxResults(10)
  312. ->getQuery()
  313. ->getResult()
  314. ;
  315. }
  316. */
  317.  
  318. /*
  319. public function findOneBySomeField($value): ?Users
  320. {
  321. return $this->createQueryBuilder('u')
  322. ->andWhere('u.exampleField = :val')
  323. ->setParameter('val', $value)
  324. ->getQuery()
  325. ->getOneOrNullResult()
  326. ;
  327. }
  328. */
  329. }
  330.  
  331. // RegistrationController.php
  332.  
  333. <?php
  334.  
  335. namespace App\Controller;
  336.  
  337. use App\Form\UsersType;
  338. use App\Entity\Users;
  339. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  340. use Symfony\Component\HttpFoundation\Request;
  341. use Symfony\Component\Routing\Annotation\Route;
  342. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  343.  
  344. class RegistrationController extends Controller
  345. {
  346. /**
  347. * @Route("/register", name="user_registration")
  348. */
  349. public function register(Request $request, UserPasswordEncoderInterface $passwordEncoder)
  350. {
  351. // 1) build the form
  352. $user = new Users();
  353. $form = $this->createForm(UsersType::class, $user);
  354.  
  355. // 2) handle the submit (will only happen on POST)
  356. $form->handleRequest($request);
  357. if ($form->isSubmitted() && $form->isValid()) {
  358.  
  359. // 3) Encode the password (you could also do this via Doctrine listener)
  360. $password = $passwordEncoder->encodePassword($user, $user->getPlainPassword());
  361. $user->setPassword($password);
  362.  
  363. // 4) save the User!
  364. $entityManager = $this->getDoctrine()->getManager();
  365. $entityManager->persist($user);
  366. $entityManager->flush();
  367.  
  368. // ... do any other work - like sending them an email, etc
  369. // maybe set a "flash" success message for the user
  370.  
  371. return $this->redirectToRoute('users/register.html.twig');
  372. }
  373.  
  374. return $this->render(
  375. 'users/register.html.twig',
  376. array('form' => $form->createView())
  377. );
  378. }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement