Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony]Logowanie na podstawie bazy danych
PawelC
post
Post #1





Grupa: Zarejestrowani
Postów: 1 173
Pomógł: 121
Dołączył: 24.09.2007
Skąd: Toruń

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


Witam,
Od jakiegoś czasu męcze SF3, a dokładnie autoryzacje tylko nic mi nie idzie. Mam taki kod:
AppBundle\Entity\User

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

  1. security.yml

Kod
# app/config/security.yml
security:
    encoders:
        AppBundle\Entity\User:
            algorithm: bcrypt

    # ...

    providers:
        our_db_provider:
            entity:
                class: AppBundle:User
                property: username
                # if you're using multiple entity managers
                # manager_name: customer

    firewalls:
        main:
            pattern:    ^/
            http_basic: ~
            provider: our_db_provider

    # ...


DefaultController
  1. <?php
  2.  
  3. namespace AppBundle\Controller;
  4.  
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  6. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9.  
  10. class DefaultController extends Controller
  11. {
  12. /**
  13.   * @Route("/", name="homepage")
  14.   */
  15. public function indexAction(Request $request)
  16. {
  17. // replace this example code with whatever you need
  18. return $this->render('default/index.html.twig', [
  19. 'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
  20. ]);
  21. }
  22.  
  23. /**
  24.   * @Route("/admin")
  25.   */
  26. public function adminAction()
  27. {
  28. return new Response('<html><body>Admin page!</body></html>');
  29. }
  30. }


ładuje się przez przeglądarkę na adres /admin, po zalogowaniu powinno mi pokazać Admin Page! niestety u mnie po podaniu prawidłowych danych admin/admin i kliknięciu zaloguj się, ponownie pokazuje się formularz logowania. Co robię nie tak? Wszystko robiłem zgodnie z tym poradnikiem https://symfony.com/doc/3.4/security/entity_provider.html

Od 3h nad tym siedzę i brak efektu (IMG:style_emoticons/default/sad.gif)
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 22.08.2025 - 16:42