Account Controller
<?php // src/Acme/LogowanieBundle/Controller/AccountController.php; namespace Acme\LogowanieBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Acme\LogowanieBundle\Form\Type\RegistrationType; use Acme\LogowanieBundle\Form\Model\Registration; use Symfony\Component\HttpFoundation\Request; class AccountController extends Controller { public function registerAction() { $registration = new Registration(); 'action' => $this->generateUrl('account_create'), )); return $this->render( 'AcmeLogowanieBundle:Account:register.html.twig', ); } public function createAction(Request $request) { $em = $this->getDoctrine()->getManager(); $form = $this->createForm(new RegistrationType(), new Registration()); $form->handleRequest($request); if ($form->isValid()) { $registration = $form->getData(); $em->persist($registration->getUser()); $em->flush(); // return $this->redirect(''); } return $this->render( 'AcmeLogowanieBundle:Account:register.html.twig', ); } }
Registration.php
<?php // src/Acme/LogowanieBundle/Controller/AccountController.php; namespace Acme\LogowanieBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Acme\LogowanieBundle\Form\Type\RegistrationType; use Acme\LogowanieBundle\Form\Model\Registration; use Symfony\Component\HttpFoundation\Request; class AccountController extends Controller { public function registerAction() { $registration = new Registration(); 'action' => $this->generateUrl('account_create'), )); return $this->render( 'AcmeLogowanieBundle:Account:register.html.twig', ); } public function createAction(Request $request) { $em = $this->getDoctrine()->getManager(); $form = $this->createForm(new RegistrationType(), new Registration()); $form->handleRequest($request); if ($form->isValid()) { $registration = $form->getData(); $em->persist($registration->getUser()); $em->flush(); // return $this->redirect(''); } return $this->render( 'AcmeLogowanieBundle:Account:register.html.twig', ); } }
RegistrationType.php
<?php // src/Acme/LogowanieBundle/Controller/AccountController.php; namespace Acme\LogowanieBundle\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Acme\LogowanieBundle\Form\Type\RegistrationType; use Acme\LogowanieBundle\Form\Model\Registration; use Symfony\Component\HttpFoundation\Request; class AccountController extends Controller { public function registerAction() { $registration = new Registration(); 'action' => $this->generateUrl('account_create'), )); return $this->render( 'AcmeLogowanieBundle:Account:register.html.twig', ); } public function createAction(Request $request) { $em = $this->getDoctrine()->getManager(); $form = $this->createForm(new RegistrationType(), new Registration()); $form->handleRequest($request); if ($form->isValid()) { $registration = $form->getData(); $em->persist($registration->getUser()); $em->flush(); // return $this->redirect(''); } return $this->render( 'AcmeLogowanieBundle:Account:register.html.twig', ); } }
UserType.php
<?php // src/Acme/LogowanieBundle/Form/Type/UserType.php namespace Acme\LogowanieBundle\Form\Type; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; class UserType extends AbstractType { { $builder->add('username','text'); $builder->add('email_address', 'email'); $builder->add('role','text'); 'first_name' => 'password', 'second_name' => 'confirm', 'type' => 'password', )); } public function setDefaultOptions(OptionsResolverInterface $resolver) { 'data_class' => 'Acme\LogowanieBundle\Entity\User' )); } public function getName() { return 'user'; } }
User.php
<?php namespace Acme\LogowanieBundle\Entity; use Symfony\Component\Security\Core\User\UserInterface; use Doctrine\ORM\Mapping as ORM; /** * Users * * @ORM\Table(name="user") * @ORM\Entity */ class User implements UserInterface { /** * @var integer * * @ORM\Column(name="id_user", type="integer") * @ORM\id * @ORM\GeneratedValue(strategy="AUTO") */ private $id_user; /** * @var string * * @ORM\Column(name="username", type="string", length=25) */ private $username; /** * @var string * * @ORM\Column(name="salt", type="string", length=32) */ private $salt; /** * @var string * * @ORM\Column(name="password", type="string", length=40) */ private $password; /** * @var string * * @ORM\Column(name="email_address", type="string", length=60) */ private $emailAddress; /** * @var string * * @ORM\Column(name="role", type="string", length=20) */ public $role; public function __construct() { // $this->isActive = true; } /** * Get id_user * * @return integer */ public function getId() { return $this->id_user; } /** * Set username * * @param string $username * @return Users */ public function setUsername($username) { $this->username = $username; return $this; } /** * Get username * * @return string */ public function getUsername() { return $this->username; } /** * Set salt * * @param string $salt * @return Users */ public function setSalt($salt) { $this->salt = $salt; return $this; } /** * Get salt * * @return string */ public function getSalt() { return $this->salt; } /** * Set password * * @param string $password * @return Users */ public function setPassword($password) { $this->password = $password; return $this; } /** * Get password * * @return string */ public function getPassword() { return $this->password; } /** * Set emailAddress * * @param string $emailAddress * @return Users */ public function setEmailAddress($emailAddress) { $this->emailAddress = $emailAddress; return $this; } /** * Get emailAddress * * @return string */ public function getEmailAddress() { return $this->emailAddress; } /** * Set role * * @param string $role * @return Users */ public function setRoles($role) { $this->role = $role; return $this; } /** * Get role * * @return string */ public function getRoles() { // return $this->role; } /** * @inheritDoc */ public function eraseCredentials() {} }
Opis problemu:
Użytkownicy są dodawani do BD ale nie mam funkcjonalności haszowania haseł. Prosiłbym o wskazanie, gdzie mógłbym w sposób poprawny takie haszowanie ustawiać i nakierowanie na to jak to zrobić. Do tej pory dodawałem testowo użytkowników w taki sposób:
public function dodajAction(){ $user = new User(); $factory = $this->container->get('security.encoder_factory'); $encoder = $factory->getEncoder($user); $pwd = $encoder->encodePassword('zenek', $user->getSalt()); $user->setUsername('zenek'); $user->setEmailAddress('exampleas@example.com'); $user->setRoles('ROLE_SUPER_ADMIN'); $user->setSalt($user->getSalt()); $user->setPassword($pwd); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); return $this->render('AcmeLogowanieBundle:Default:dodano.html.twig'); }