Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SF2][SF]Security Users from the Database - problem z zalogowaniem.
basso
post
Post #1





Grupa: Zarejestrowani
Postów: 155
Pomógł: 1
Dołączył: 12.12.2010

Ostrzeżenie: (0%)
-----


Witam.
Co krok to problem, może Wy pomożecie, bo gotowy kod z dokumentacji po prostu nie działa.
Podłączyłem bazę do security zgodnie z: http://symfony.com/doc/2.2/cookbook/securi...y_provider.html
No wziąłem ich kod analogicznie wprowadziłem zmiany i błąd.
Formularz się pojawia, wszytko śmiga bez baz z bazą jest problem.
  1. [u]FatalErrorException: Error: Call to undefined method Backend\CmsBundle\Entity\User::getId() in C:\wamp\www\zw\src\Backend\CmsBundle\Entity\UserRepository.php line 50[/u]


Kojarzy ktoś może dlaczego nie może załadować $user->getId() z lini 50? Przecież jest UserInterface $user wyżej. Załadowany też jest. No to jest ich kod... no nie działa.
No z nieba sobie nie wymyśle, co oni zrobili źle, może Wy spotkaliście się z tym Bug-iem?


Mój security.
  1. security:
  2. encoders:
  3. Backend\CmsBundle\Entity\User:
  4. algorithm: sha1
  5. encode_as_base64: false
  6. iterations: 1
  7.  
  8. role_hierarchy:
  9. ROLE_ADMIN: ROLE_USER
  10. ROLE_SUPER_ADMIN: [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
  11.  
  12. providers:
  13. administrators:
  14. entity: { class: BackendCmsBundle:User, property: username }
  15.  
  16. firewalls:
  17. admin_area:
  18. pattern: ^/admin
  19. http_basic: ~
  20. secured_area:
  21. pattern: ^/
  22. form_login:
  23. check_path: login_check
  24. login_path: login
  25. logout:
  26. path: _logout
  27. target: _logout_place
  28. anonymous: ~
  29.  
  30. access_control:
  31. - { path: ^/admin, roles: ROLE_ADMIN }
  32.  


Moj User.php => ENTITY

  1. // src/Backend/CmsBundle/Entity/User.php
  2. namespace Backend\CmsBundle\Entity;
  3.  
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\User\EquatableInterface;
  7.  
  8. /**
  9.  * Acme\UserBundle\Entity\User
  10.  *
  11.  * @ORM\Table(name="sz_users")
  12.  * @ORM\Entity(repositoryClass="Backend\CmsBundle\Entity\UserRepository")
  13.  */
  14. class User implements UserInterface, \Serializable
  15. {
  16. /**
  17.   * @ORM\Column(type="integer")
  18.   * @ORM\Id
  19.   * @ORM\GeneratedValue(strategy="AUTO")
  20.   */
  21. private $id;
  22.  
  23. /**
  24.   * @ORM\Column(type="string", length=25, unique=true)
  25.   */
  26. private $username;
  27.  
  28. /**
  29.   * @ORM\Column(type="string", length=32)
  30.   */
  31. private $salt;
  32.  
  33. /**
  34.   * @ORM\Column(type="string", length=40)
  35.   */
  36. private $password;
  37.  
  38. /**
  39.   * @ORM\Column(type="string", length=60, unique=true)
  40.   */
  41. private $email;
  42.  
  43. /**
  44.   * @ORM\Column(name="is_active", type="boolean")
  45.   */
  46. private $isActive;
  47.  
  48. public function __construct()
  49. {
  50. $this->isActive = true;
  51. $this->salt = md5(uniqid(null, true));
  52. }
  53.  
  54. /**
  55.   * @inheritDoc
  56.   */
  57. public function getUsername()
  58. {
  59. return $this->username;
  60. }
  61.  
  62. /**
  63.   * @inheritDoc
  64.   */
  65. public function getSalt()
  66. {
  67. return $this->salt;
  68. }
  69.  
  70. /**
  71.   * @inheritDoc
  72.   */
  73. public function getPassword()
  74. {
  75. return $this->password;
  76. }
  77.  
  78. /**
  79.   * @inheritDoc
  80.   */
  81. public function getRoles()
  82. {
  83. return array('ROLE_USER');
  84. }
  85.  
  86. /**
  87.   * @inheritDoc
  88.   */
  89. public function eraseCredentials()
  90. {
  91. }
  92.  
  93. /**
  94.   * @see \Serializable::serialize()
  95.   */
  96. public function serialize()
  97. {
  98. return serialize(array(
  99. $this->id,
  100. ));
  101. }
  102.  
  103. /**
  104.   * @see \Serializable::unserialize()
  105.   */
  106. public function unserialize($serialized)
  107. {
  108. list (
  109. $this->id,
  110. ) = unserialize($serialized);
  111. }
  112.  
  113. public function isEqualTo(UserInterface $user)
  114. {
  115. return $this->id === $user->getId();
  116. }
  117.  


I mój UserRepository
  1. // src/Backend/CmsBundle/Entity/UserRepository.php
  2. namespace Backend\CmsBundle\Entity;
  3.  
  4. use Symfony\Component\Security\Core\User\UserInterface;
  5. use Symfony\Component\Security\Core\User\UserProviderInterface;
  6. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  7. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  8. use Doctrine\ORM\EntityRepository;
  9. use Doctrine\ORM\NoResultException;
  10.  
  11. class UserRepository extends EntityRepository implements UserProviderInterface
  12. {
  13. public function loadUserByUsername($username)
  14. {
  15. $q = $this
  16. ->createQueryBuilder('u')
  17. ->where('u.username = :username OR u.email = :email')
  18. ->setParameter('username', $username)
  19. ->setParameter('email', $username)
  20. ->getQuery();
  21.  
  22. try {
  23. // The Query::getSingleResult() method throws an exception
  24. // if there is no record matching the criteria.
  25. $user = $q->getSingleResult();
  26. } catch (NoResultException $e) {
  27. $message = sprintf(
  28. 'Unable to find an active admin BackendCmsBundle:User object identified by "%s".',
  29. $username
  30. );
  31. throw new UsernameNotFoundException($message, 0, $e);
  32. }
  33.  
  34. return $user;
  35. }
  36.  
  37. public function refreshUser(UserInterface $user)
  38. {
  39. $class = get_class($user);
  40. if (!$this->supportsClass($class)) {
  41. throw new UnsupportedUserException(
  42. 'Instances of "%s" are not supported.',
  43. $class
  44. )
  45. );
  46. }
  47.  
  48. return $this->find($user->getId());
  49. }
  50.  
  51. public function supportsClass($class)
  52. {
  53. return $this->getEntityName() === $class
  54. || is_subclass_of($class, $this->getEntityName());
  55. }
  56.  
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 22.08.2025 - 02:28