Guest User

Untitled

a guest
Nov 22nd, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.94 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AppBundleEntity;
  4.  
  5. use DoctrineORMMapping as ORM;
  6. use SymfonyComponentSecurityCoreUserUserInterface;
  7. use SymfonyComponentValidatorConstraints as Assert;
  8. use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
  9.  
  10. /**
  11. * Member
  12. *
  13. * @ORMTable(name="member")
  14. * @ORMEntity(repositoryClass="AppBundleRepositoryMemberRepository")
  15. * @UniqueEntity(fields= "username") - These details match a current user!
  16. *
  17. *
  18. */
  19. class Member implements UserInterface, Serializable
  20. {
  21. /**
  22. * @var int
  23. *
  24. * @ORMColumn(name="id", type="integer")
  25. * @ORMId
  26. * @ORMGeneratedValue(strategy="AUTO")
  27. */
  28. private $id;
  29.  
  30. /**
  31. *
  32. * @var string
  33. * @ORMColumn(name="username", type="string", length=255, unique=true)
  34. *
  35. */
  36. protected $username;
  37.  
  38. /**
  39. *
  40. * @var string
  41. * @ORMColumn(name="email", type="string", length=255, unique=true)
  42. *
  43. */
  44. private $email;
  45.  
  46. private $plainPassword;
  47.  
  48. /**
  49. * @var string
  50. *
  51. * @ORMColumn(name="password", type="string", length=64)
  52. */
  53. private $password;
  54.  
  55.  
  56. /**
  57. * Get id
  58. *
  59. * @return int
  60. */
  61. public function getId()
  62. {
  63. return $this->id;
  64. }
  65.  
  66. /**
  67. * Set username
  68. *
  69. * @param string $username
  70. *
  71. * @return Member
  72. */
  73. public function setUsername($username)
  74. {
  75. $this->username = $username;
  76.  
  77. return $this;
  78. }
  79.  
  80. /**
  81. * Get username
  82. *
  83. * @return string
  84. */
  85. public function getUsername()
  86. {
  87. return $this->username;
  88. }
  89.  
  90. /**
  91. * Set email
  92. *
  93. * @param string $email
  94. *
  95. * @return Member
  96. */
  97. public function setEmail($email)
  98. {
  99. $this->email = $email;
  100.  
  101. return $this;
  102. }
  103.  
  104. /**
  105. * Get email
  106. *
  107. * @return string
  108. */
  109. public function getEmail()
  110. {
  111. return $this->email;
  112. }
  113.  
  114. /**
  115. * Set password
  116. *
  117. * @param string $password
  118. *
  119. * @return Member
  120. */
  121. public function setPassword($password)
  122. {
  123. $this->password = $password;
  124.  
  125. return $this;
  126. }
  127.  
  128. /**
  129. * Get password
  130. *
  131. * @return string
  132. */
  133. public function getPassword()
  134. {
  135. return $this->password;
  136. }
  137.  
  138. /**
  139. * @return mixed
  140. */
  141. public function getPlainPassword()
  142. {
  143. return $this->plainPassword;
  144. }
  145.  
  146. /**
  147. * @param mixed $plainPassword
  148. */
  149. public function setPlainPassword($plainPassword)
  150. {
  151. $this->plainPassword = $plainPassword;
  152. }
  153.  
  154.  
  155.  
  156. public function serialize()
  157. {
  158. return serialize([
  159. $this->id,
  160. $this->username,
  161. $this->password,
  162. ]);
  163. }
  164.  
  165. public function unserialize($serialized)
  166. {
  167. list(
  168. $this->id,
  169. $this->username,
  170. $this->password,
  171. ) = unserialize($serialized);
  172. }
  173.  
  174. public function eraseCredentials()
  175. {
  176. $this->plainPassword = null;
  177. }
  178.  
  179. public function getSalt()
  180. {
  181. return null;
  182. }
  183.  
  184. public function getRoles()
  185. {
  186. return[
  187. 'ROLE_USER',
  188. ];
  189. }
  190. }
  191.  
  192. <?php
  193. /**
  194. * Created by PhpStorm.
  195. * User: dylan
  196. * Date: 20/11/2017
  197. * Time: 19:21
  198. */
  199.  
  200. namespace AppBundleController;
  201.  
  202.  
  203. use AppBundleEntityMember;
  204. use AppBundleFormMemberType;
  205. use SensioBundleFrameworkExtraBundleConfigurationRoute;
  206. use SensioBundleFrameworkExtraBundleConfigurationMethod;
  207. use SymfonyBundleFrameworkBundleControllerController;
  208. use SymfonyComponentHttpFoundationRequest;
  209. use SymfonyComponentHttpFoundationResponse;
  210. use SymfonyComponentSecurityCoreAuthenticationTokenUsernamePasswordToken;
  211.  
  212. class RegistrationController extends Controller
  213. {
  214. /**
  215. * @Route("/register", name="registration")
  216. * @return SymfonyComponentHttpFoundationResponse
  217. * @throws LogicException
  218. */
  219. public function registerAction(Request $request)
  220. {
  221. $member = new Member();
  222.  
  223. $form = $this->createMemberRegistrationForm($member);
  224.  
  225. return $this->render('registration/register.html.twig',[
  226. 'registration_form' => $form->createView(),
  227.  
  228. ]);
  229. }
  230.  
  231. /**
  232. * @param Request $request
  233. * @Route("/registration-form-submission", name="handle_registration_form_submission")
  234. * @Method("POST")
  235. * @return SymfonyComponentHttpFoundationRedirectResponse|SymfonyComponentHttpFoundationResponse
  236. * @throws LogicException
  237. * @throws InvalidArgumentException
  238. */
  239. public function handleFormSubmissionAction(Request $request)
  240. {
  241. $member = new Member();
  242.  
  243. $form = $this->createMemberRegistrationForm($member);
  244.  
  245.  
  246. $form->handleRequest($request);
  247.  
  248. if (! $form->isSubmitted() && ! $form->isValid()) {
  249. return $this->render('registration/register.html.twig',[
  250. 'registration_form' => $form->createView(),
  251.  
  252. ]);
  253. }
  254.  
  255. $password = $this->get('security.password_encoder')
  256. ->encodePassword($member, $member->getPlainPassword());
  257.  
  258. $member->setPassword($password);
  259.  
  260. $em = $this->getDoctrine()->getManager();
  261.  
  262. $em->persist($member);
  263. $em->flush();
  264.  
  265. $token = new UsernamePasswordToken(
  266. $member,
  267. $password,
  268. 'main',
  269. $member->getRoles()
  270. );
  271.  
  272. $this->get('security.token_storage')->setToken($token);
  273. $this->get('session')->set('_security_main', serialize($token));
  274.  
  275. $this->addFlash('success', 'You are now registered');
  276.  
  277. return $this->redirectToRoute('homepage');
  278. }
  279.  
  280. private function createMemberRegistrationForm($member)
  281. {
  282. return $this->createForm(MemberType::class, $member,[
  283. 'action'=> $this->generateUrl('handle_registration_form_submission')
  284. ]);
  285. }
  286. }
Add Comment
Please, Sign In to add comment