Created
May 26, 2011 16:21
-
-
Save Problematic/993460 to your computer and use it in GitHub Desktop.
Symfony2 user registration mockup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Problematic\UserBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Problematic\UserBundle\Entity\User; | |
class RegistrationController extends Controller | |
{ | |
public function registerAction() | |
{ | |
$em = $this->get('doctrine')->getEntityManager(); | |
$request = $this->get('request'); | |
$user = new User(); | |
$form = $this->get('form.factory')->create(new UserType(), $user); | |
if ('POST' == $request->getMethod()) { | |
$form->bindRequest($request); | |
$user->setSalt(/* some salt you've generated */); | |
$user->setRoles(array('ROLE_USER')); | |
$factory = $this->container->get('security.encoder_factory'); | |
$encoder = $factory->getEncoder($user); | |
$password = $encoder->encodePassword($user->getPassword(), $user->getSalt()); //where $user->password has been bound in plaintext by the form | |
$user->setPassword($password); | |
if ($form->isValid()) { | |
$em->persist($user); | |
$em->persist($profile); | |
$em->flush(); | |
// creates a token and assigns it, effectively logging the user in with the credentials they just registered | |
$token = new UsernamePasswordToken($user, null, 'main'); | |
$this->get('security.context')->setToken($token); | |
return $this->redirect($this->generateUrl('Problematic_UserBundle_registration_thanks')); | |
} | |
} | |
return $this->render('ProblematicUserBundle:Registration:register.html.twig', array( | |
'form' => $form->createView(), | |
)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
security: | |
encoders: | |
Problematic\UserBundle\Entity\User: sha512 | |
# ... | |
providers: | |
main: | |
entity: { class: Problematic\UserBundle\Entity\User, property: username } | |
# ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Problematic\UserBundle\Entity; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* @ORM\Entity | |
*/ | |
class User implements UserInterface | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(type="array") | |
*/ | |
protected $roles; | |
// ... rest of the User entity, including implementations for methods inherited from UserInterface | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment