Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace AppBundleEntity;
- use DoctrineORMMapping as ORM;
- use SymfonyComponentSecurityCoreUserUserInterface;
- use SymfonyComponentValidatorConstraints as Assert;
- use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
- /**
- * Member
- *
- * @ORMTable(name="member")
- * @ORMEntity(repositoryClass="AppBundleRepositoryMemberRepository")
- * @UniqueEntity(fields= "username") - These details match a current user!
- *
- *
- */
- class Member implements UserInterface, Serializable
- {
- /**
- * @var int
- *
- * @ORMColumn(name="id", type="integer")
- * @ORMId
- * @ORMGeneratedValue(strategy="AUTO")
- */
- private $id;
- /**
- *
- * @var string
- * @ORMColumn(name="username", type="string", length=255, unique=true)
- *
- */
- protected $username;
- /**
- *
- * @var string
- * @ORMColumn(name="email", type="string", length=255, unique=true)
- *
- */
- private $email;
- private $plainPassword;
- /**
- * @var string
- *
- * @ORMColumn(name="password", type="string", length=64)
- */
- private $password;
- /**
- * Get id
- *
- * @return int
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * Set username
- *
- * @param string $username
- *
- * @return Member
- */
- public function setUsername($username)
- {
- $this->username = $username;
- return $this;
- }
- /**
- * Get username
- *
- * @return string
- */
- public function getUsername()
- {
- return $this->username;
- }
- /**
- * Set email
- *
- * @param string $email
- *
- * @return Member
- */
- public function setEmail($email)
- {
- $this->email = $email;
- return $this;
- }
- /**
- * Get email
- *
- * @return string
- */
- public function getEmail()
- {
- return $this->email;
- }
- /**
- * Set password
- *
- * @param string $password
- *
- * @return Member
- */
- public function setPassword($password)
- {
- $this->password = $password;
- return $this;
- }
- /**
- * Get password
- *
- * @return string
- */
- public function getPassword()
- {
- return $this->password;
- }
- /**
- * @return mixed
- */
- public function getPlainPassword()
- {
- return $this->plainPassword;
- }
- /**
- * @param mixed $plainPassword
- */
- public function setPlainPassword($plainPassword)
- {
- $this->plainPassword = $plainPassword;
- }
- public function serialize()
- {
- return serialize([
- $this->id,
- $this->username,
- $this->password,
- ]);
- }
- public function unserialize($serialized)
- {
- list(
- $this->id,
- $this->username,
- $this->password,
- ) = unserialize($serialized);
- }
- public function eraseCredentials()
- {
- $this->plainPassword = null;
- }
- public function getSalt()
- {
- return null;
- }
- public function getRoles()
- {
- return[
- 'ROLE_USER',
- ];
- }
- }
- <?php
- /**
- * Created by PhpStorm.
- * User: dylan
- * Date: 20/11/2017
- * Time: 19:21
- */
- namespace AppBundleController;
- use AppBundleEntityMember;
- use AppBundleFormMemberType;
- use SensioBundleFrameworkExtraBundleConfigurationRoute;
- use SensioBundleFrameworkExtraBundleConfigurationMethod;
- use SymfonyBundleFrameworkBundleControllerController;
- use SymfonyComponentHttpFoundationRequest;
- use SymfonyComponentHttpFoundationResponse;
- use SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
- class RegistrationController extends Controller
- {
- /**
- * @Route("/register", name="registration")
- * @return SymfonyComponentHttpFoundationResponse
- * @throws LogicException
- */
- public function registerAction(Request $request)
- {
- $member = new Member();
- $form = $this->createMemberRegistrationForm($member);
- return $this->render('registration/register.html.twig',[
- 'registration_form' => $form->createView(),
- ]);
- }
- /**
- * @param Request $request
- * @Route("/registration-form-submission", name="handle_registration_form_submission")
- * @Method("POST")
- * @return SymfonyComponentHttpFoundationRedirectResponse|SymfonyComponentHttpFoundationResponse
- * @throws LogicException
- * @throws InvalidArgumentException
- */
- public function handleFormSubmissionAction(Request $request)
- {
- $member = new Member();
- $form = $this->createMemberRegistrationForm($member);
- $form->handleRequest($request);
- if (! $form->isSubmitted() && ! $form->isValid()) {
- return $this->render('registration/register.html.twig',[
- 'registration_form' => $form->createView(),
- ]);
- }
- $password = $this->get('security.password_encoder')
- ->encodePassword($member, $member->getPlainPassword());
- $member->setPassword($password);
- $em = $this->getDoctrine()->getManager();
- $em->persist($member);
- $em->flush();
- $token = new UsernamePasswordToken(
- $member,
- $password,
- 'main',
- $member->getRoles()
- );
- $this->get('security.token_storage')->setToken($token);
- $this->get('session')->set('_security_main', serialize($token));
- $this->addFlash('success', 'You are now registered');
- return $this->redirectToRoute('homepage');
- }
- private function createMemberRegistrationForm($member)
- {
- return $this->createForm(MemberType::class, $member,[
- 'action'=> $this->generateUrl('handle_registration_form_submission')
- ]);
- }
- }
Add Comment
Please, Sign In to add comment