Guest User

Untitled

a guest
Jul 14th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.50 KB | None | 0 0
  1. namespace AcmeUserBundleEntity;
  2.  
  3. use SymfonyComponentSecurityCoreUserUserInterface;
  4. use SymfonyBridgeDoctrineValidatorConstraints as DoctrineAssert;
  5. use DoctrineORMMapping as ORM;
  6.  
  7. /**
  8. * @ORMEntity()
  9. * @ORMHasLifecycleCallbacks()
  10. *
  11. * list any fields here that must be unique
  12. * @DoctrineAssertUniqueEntity(
  13. * fields = { "email" }
  14. * )
  15. */
  16. class User implements UserInterface
  17. {
  18. /**
  19. * @ORMId
  20. * @ORMColumn(type="integer")
  21. * @ORMGeneratedValue(strategy="AUTO")
  22. */
  23. protected $id;
  24.  
  25. /**
  26. * @ORMColumn(type="string", length="255", unique="true")
  27. */
  28. protected $email;
  29.  
  30. /**
  31. * @ORMColumn(type="string", length="128")
  32. */
  33. protected $password;
  34.  
  35. /**
  36. * @ORMColumn(type="string", length="5")
  37. */
  38. protected $salt;
  39.  
  40. /**
  41. * Generate a new salt - can't be done as prepersist because we need it before then
  42. */
  43. public function initSalt() {
  44. $this->salt = substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',5)),0,5);
  45. }
  46.  
  47. /**
  48. * Is the provided user the same as "this"?
  49. *
  50. * @return bool
  51. */
  52. public function equals(UserInterface $user) {
  53. if($user->email !== $this->email) {
  54. return false;
  55. }
  56.  
  57. return true;
  58. }
  59.  
  60. /**
  61. * Remove sensitive information from the user object
  62. */
  63. public function eraseCredentials() {
  64. $this->password = "";
  65. $this->salt = "";
  66. }
  67.  
  68.  
  69. /**
  70. * Get the list of roles for the user
  71. *
  72. * @return string array
  73. */
  74. public function getRoles() {
  75. return array("ROLE_USER");
  76. }
  77.  
  78. /**
  79. * Get the user's password
  80. *
  81. * @return string
  82. */
  83. public function getPassword() {
  84. return $this->password;
  85. }
  86.  
  87. /**
  88. * Get the user's username
  89. *
  90. * We MUST have this to fulfill the requirements of UserInterface
  91. *
  92. * @return string
  93. */
  94. public function getUsername() {
  95. return $this->email;
  96. }
  97.  
  98. /**
  99. * Get the user's "email"
  100. *
  101. * @return string
  102. */
  103. public function getEmail() {
  104. return $this->email;
  105. }
  106.  
  107. /**
  108. * Get the user's salt
  109. *
  110. * @return string
  111. */
  112. public function getSalt() {
  113. return $this->salt;
  114. }
  115.  
  116. /**
  117. * Convert this user to a string representation
  118. *
  119. * @return string
  120. */
  121.  
  122. public function __toString() {
  123. return $this->email;
  124. }
  125. }
  126. ?>
  127.  
  128. namespace AcmeUserBundleFormType;
  129.  
  130. use SymfonyComponentFormAbstractType;
  131. use SymfonyComponentFormFormBuilder;
  132.  
  133. class UserType extends AbstractType {
  134. public function buildForm(FormBuilder $builder, array $options) {
  135. $builder->add('email');
  136. /* this field type lets you show two fields that represent just
  137. one field in the model and they both must match */
  138. $builder->add('password', 'repeated', array (
  139. 'type' => 'password',
  140. 'first_name' => "Password",
  141. 'second_name' => "Re-enter Password",
  142. 'invalid_message' => "The passwords don't match!"
  143. ));
  144. }
  145.  
  146. public function getName() {
  147. return 'user';
  148. }
  149.  
  150. public function getDefaultOptions(array $options) {
  151. return array(
  152. 'data_class' => 'AcmeUserBundleEntityUser',
  153. );
  154. }
  155. }
  156. ?>
  157.  
  158. namespace AcmeUserBundleController;
  159.  
  160. use SymfonyBundleFrameworkBundleControllerController;
  161. use SymfonyComponentHttpFoundationRequest;
  162. use AcmeUserBundleEntityUser;
  163. use AcmeUserBundleFormTypeUserType;
  164.  
  165.  
  166. class userController extends Controller
  167. {
  168. public function newAction(Request $request) {
  169. $user = new User();
  170. $form = $this->createForm(new UserType(), $user);
  171.  
  172. if ($request->getMethod() == 'POST') {
  173. $form->bindRequest($request);
  174.  
  175. if ($form->isValid()) {
  176. // encode the password
  177. $user->initSalt();
  178. $factory = $this->get('security.encoder_factory');
  179. $encoder = $factory->getEncoder($user);
  180. $password = $encoder->encodePassword($user->getPassword(), $user->getSalt());
  181. $user->setPassword($password);
  182.  
  183. $em = $this->getDoctrine()->getEntityManager();
  184. $em->persist($user);
  185. $em->flush();
  186.  
  187. return $this->redirect($this->generateUrl('AcmeUserBundle_submitNewSuccess'));
  188. }
  189. }
  190.  
  191. return $this->render('AcmeUserBundle:User:new.html.twig', array (
  192. 'form' => $form->createView()
  193. ));
  194. }
  195.  
  196. public function submitNewSuccessAction() {
  197. return $this->render("AcmeUserBundle:User:submitNewSuccess.html.twig");
  198. }
  199.  
  200. security:
  201. encoders:
  202. AcmeUserBundleEntityUser:
  203. algorithm: sha512
  204. iterations: 1
  205. encode_as_base64: true
  206.  
  207. role_hierarchy:
  208. ROLE_ADMIN: ROLE_USER
  209. ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  210.  
  211. providers:
  212. main:
  213. entity: { class: AcmeUserBundleEntityUser, property: email }
  214.  
  215. firewalls:
  216. secured_area:
  217. pattern: ^/
  218. form_login:
  219. check_path: /login_check
  220. login_path: /login
  221. logout:
  222. path: /logout
  223. target: /demo/
  224. anonymous: ~
  225.  
  226. self::PROPERTY_CONSTRAINT
  227.  
  228. self::CLASS_CONSTRAINT
  229.  
  230. validator.debit_card:
  231. class: MyValidatorConstraintsDebitCardValidator
  232. tags:
  233. - { name: validator.constraint_validator, alias: debit_card }
Add Comment
Please, Sign In to add comment