Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [SF2][SF]Security Users from the Database - problem z zalogowaniem.
Forum PHP.pl > Forum > PHP > Frameworki
basso
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.  
Crozin
No przecież jak byk napisane Call to undefined method Backend\CmsBundle\Entity\User::getId()! Widzisz gdzieś w swojej klasie User metodę getId()?
basso
Hej, no ja wiem, że jej nie ma nawet nie dodawałem => to jest kod Z DOKUMENTACJI => na Boga,jak można uszkodzony niesprawdzony kod wrzucać na międzynarodową platformę ?

Dzięki za pomoc.

Dodałem i działa połowicznie, bo nie ma Autentykacji. Zalogowano jako admin ale on nie wie, że to admin smile.gif


Kojarzysz może dlaczego? Raczkuję w ACL także każda podpowiedź się przyda.
Crozin
Cytat
to jest kod Z DOKUMENTACJI => na Boga,jak można uszkodzony niesprawdzony kod wrzucać na międzynarodową platformę ?
Widać autorów trochę poniosło... założyli, że ludzie czytają dokumentację, a nie kopiują na pałę:
Cytat("http://symfony.com/doc/2.2/cookbook/security/entity_provider.html#the-data-model")
To make it shorter, the getter and setter methods for each have been removed to focus on the most important methods that come from the UserInterface.
Cytat
You can generate the missing getter and setters by running:
Kod
$ php app/console doctrine:generate:entities Acme/UserBundle/Entity/User


Cytat
Dodałem i działa połowicznie, bo nie ma Autentykacji. Zalogowano jako admin ale on nie wie, że to admin smile.gif
Dosłownie 7 sekund wyszukiwania: https://www.google.com/search?q=symfony+log...me&ie=UTF-8
m44
Cytat(basso @ 18.07.2013, 08:03:56 ) *
Dodałem i działa połowicznie, bo nie ma Autentykacji. Zalogowano jako admin ale on nie wie, że to admin smile.gif


Nie ma takiego słowa jak autentykacja. W języku polskim nazywamy ten proces uwierzytelnianiem.
basso
Jakbym na pałę bez skonfigurowania przekopiował to by nie działało. Po drugie generator mi osobiście nie wygenerował metody Equals...
Spoko, potem się pomęczę, ale znowu męczarnia i tak non stop co krok... no framework ma pomagać, a ja się męczę smile.gif

Dzięki za pomoc.

[edit] m44 => to jest forum. Ja wiem, że my Informatycy tak mamy, że musi być wszystko albo wcale (0 albo 1).
W życiu codziennym wystarczy się zrozumieć używając języka potocznego.
Tylko się nie obraź i nie odbieraj tego jako "atakiren" smile.gif

Kliknąłem Ci w pomógł bo nie wiedziałem nawet, że takiego słowa nie ma.
m44
Z mojej strony to nie był zarzut, tylko taka mała dygresja. smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.