Advertisement
Guest User

Untitled

a guest
Nov 10th, 2013
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2. // src/Acme/AccountBundle/Controller/AccountController.php
  3. namespace Acme\AccountBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\Request;
  8.  
  9. use Acme\AccountBundle\Form\Type\RegistrationType;
  10. use Acme\AccountBundle\Form\Model\Registration;
  11. //use Acme\AccountBundle\Entity\User;
  12. use Acme\AccountBundle\Entity\UserRole;
  13.  
  14. class AccountController extends Controller
  15. {
  16.    
  17.    
  18.     public function createAction(Request $request)
  19.     {  
  20.        
  21.     $em = $this->getDoctrine()->getManager();
  22.    
  23.     $registrationForm = $this->createForm(new RegistrationType(), new Registration());
  24.  
  25.     $registrationForm->handleRequest($request);
  26.    
  27.  
  28.     if ($registrationForm->isValid()) {
  29.        
  30.         $registration = $registrationForm->getData();
  31.        
  32.        
  33.         $user = $registration->getUser();
  34.        
  35.         $user->setSalt($this->generateSalt());
  36.         $user->setIsActive(1);
  37.        
  38.    
  39.        
  40.         $factory = $this->get('security.encoder_factory');
  41.  
  42.         $encoder = $factory->getEncoder($user);
  43.         $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
  44.            
  45.         $user->setPassword($password);
  46.         $role = new UserRole();
  47.         $role->setUserId($user->getId());
  48.         $role->setRoleId(2);
  49.  
  50.         $roles = new \Doctrine\Common\Collections\ArrayCollection;
  51.         $roles->add($role);
  52.  
  53.         $user->setRoles($roles);
  54.        
  55.        
  56.         $em->persist($user);
  57.         $em->flush();
  58.        
  59.         return $this->redirect($this->generateUrl('acme_front_homepage'));
  60.     }
  61.    
  62.  
  63.     return $this->render(
  64.         'AcmeFrontBundle:Registration:register.html.php',
  65.         array('registrationForm' => $registrationForm->createView())
  66.     );
  67. }
  68.  
  69.  
  70.     /*
  71.      * vygeneruje sul na hashovani hesla
  72.      */
  73.      public function generateSalt($len = 6){    
  74.     $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';                      
  75.     $salt = '';                      
  76.     for ($i=0; $i < $len; $i++) {
  77.         $salt .= substr($pool, mt_rand(0, strlen($pool) -1), 1);
  78.     }
  79.          return $salt;
  80.      }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement