Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Symfony][Symfony2] Problem z autoryzacją użytkowników z bazy danych
Forum PHP.pl > Forum > PHP > Frameworki
m.e.n.t.o.s.
Witam

Zaczynam się uczyć Symwony2 i mam problem z autoryzacją użytkowników z bazy danych. Gdy użytkownicy są zapisani w pliku security.yml logowanie działa. Natomiast gdy przenoszę użytkowników do bazy danych i próbuje się zalogować, dostaję:

An exception occurred while executing 'SELECT t0.id AS id1, t0.username AS username2, t0.password AS password3, t0.email AS email4, t0.salt AS salt5 FROM user t0 WHERE t0.username = ? LIMIT 1' with params ["admin"]: SQLSTATE[42703]: Undefined column: 7 BŁĄD: kolumna t0.id nie istnieje LINE 1: SELECT t0.id AS id1, t0.username AS username2, t0.password A... ^

Poniżej kod pliku security.yml
Kod
security:
    encoders:
        #Symfony\Component\Security\Core\User\User:
        Madan\AdminBundle\Entity\User:
            algorithm: md5
            iterations: 1
            encode_as_base64: false

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

    providers:
        administrators:
            entity: { class: Madan\AdminBundle\Entity\User, property: username }
        in_memory:
            memory:
                users:
                    user:  { password: 63e780c3f321d13109c71bf81805476e, roles: [ 'ROLE_USER' ] }
                    # userpass
                    admin: { password: 25e4ee4e9229397b6b17776bfceaf8e7, roles: [ 'ROLE_ADMIN' ] }
                    # adminpass

    firewalls:
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern:  ^/demo/secured/login$
            security: false

        secured_area:
            pattern:    ^/
            form_login: ~
            #    check_path: login
            #    login_path: login_check
            logout:
                path:   /logout
                target: /login
            anonymous: ~
            http_basic:
                realm: "Secured Demo Area"

    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }
        #- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }


routing:
Kod
login:
    pattern:   /login
    defaults:  { _controller: MadanAdminBundle:Security:login }
login_check:
    pattern:   /login_check
logout:
    pattern:   /logout
admin:
    pattern:   /admin
    defaults: { _controller: MadanAdminBundle:Security:index }


Entity\User.php
  1. <?php
  2.  
  3. namespace Madan\AdminBundle\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\Security\Core\User\UserInterface;
  7. //use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  8. use Doctrine\ORM\Mapping as ORM;
  9.  
  10. /**
  11.  * User
  12.  *
  13.  * @ORM\Table(name="user")
  14.  * @ORM\Entity
  15.  */
  16. class User implements UserInterface
  17. {
  18. /**
  19.   * @ORM\ManyToMany(targetEntity="Role", inversedBy="users")
  20.   *
  21.   */
  22. private $roles;
  23.  
  24. public function __construct()
  25. {
  26. $this->roles = new ArrayCollection();
  27. }
  28.  
  29. /**
  30.   * Returns an array of roles granted to the user
  31. *
  32.   * @return type Role[]
  33.   */
  34. public function getRoles()
  35. {
  36. return $this->roles->toArray();
  37. }
  38.  
  39. /**
  40.   * @var integer
  41.   *
  42.   * @ORM\Column(name="id", type="integer")
  43.   * @ORM\Id
  44.   * @ORM\GeneratedValue(strategy="AUTO")
  45.   */
  46. private $id;
  47.  
  48. /**
  49. * @var string
  50.   *
  51.   * @ORM\Column(name="username", type="string", length=255)
  52.   */
  53. protected $username;
  54.  
  55. /**
  56.   * @var string
  57.   *
  58.   * @ORM\Column(name="password", type="string", length=50)
  59.   */
  60. private $password;
  61.  
  62. /**
  63.   * @var string
  64.   *
  65.   * @ORM\Column(name="email", type="string", length=127, unique=true)
  66.   */
  67. private $email;
  68.  
  69. /**
  70. * @var string
  71.   *
  72. * @ORM\Column(type="string", length=50)
  73. */
  74. private $salt;
  75.  
  76. /**
  77.   * Returns the username used to authenticate the user.
  78.   *
  79.   * @return string
  80.   */
  81. public function getUsername()
  82. {
  83. return $this->username;
  84. }
  85.  
  86. /**
  87.   * Returns the password used to authenticate the user.
  88.   *
  89.   * @return string
  90.   */
  91. public function getPassword()
  92. {
  93. return $this->password;
  94. }
  95.  
  96. /**
  97.   * Returns the salt that was originally used to encode the password.
  98.   *
  99.   * @return string|null
  100.   */
  101. public function getSalt()
  102. {
  103. return $this->salt;
  104. }
  105.  
  106. /**
  107.   * Removes sensitive data from the user.
  108.   *
  109.   *
  110.   */
  111. public function eraseCredentials()
  112. {
  113.  
  114. }
  115.  
  116. /**
  117.   * Get id
  118.   *
  119.   * @return integer
  120.   */
  121. public function getId()
  122. {
  123. return $this->id;
  124. }
  125.  
  126. /**
  127.   * Set username
  128.   *
  129.   * @param string $username
  130.   * @return User
  131.   */
  132. public function setUsername($username)
  133. {
  134. $this->username = $username;
  135.  
  136. return $this;
  137. }
  138.  
  139. /**
  140.   * Set password
  141.   *
  142.   * @param string $password
  143.   * @return User
  144.   */
  145. public function setPassword($password)
  146. {
  147. $this->password = $password;
  148.  
  149. return $this;
  150. }
  151.  
  152. /**
  153.   * Set email
  154.   *
  155.   * @param string $email
  156.   * @return User
  157.   */
  158. public function setEmail($email)
  159. {
  160. $this->email = $email;
  161.  
  162. return $this;
  163. }
  164.  
  165. /**
  166.   * Get email
  167.   *
  168.   * @return string
  169.   */
  170. public function getEmail()
  171. {
  172. return $this->email;
  173. }
  174.  
  175. /**
  176.   * Set salt
  177.   *
  178.   * @param string $salt
  179.   * @return User
  180.   */
  181. public function setSalt($salt = null)
  182. {
  183. $this->salt = $salt;
  184.  
  185. return $this;
  186. }
  187.  
  188. /**
  189.   * Add roles
  190.   *
  191.   * @param \Madan\AdminBundle\Entity\Role $roles
  192.   * @return User
  193.   */
  194. public function addRole(\Madan\AdminBundle\Entity\Role $roles)
  195. {
  196. $this->roles[] = $roles;
  197.  
  198. return $this;
  199. }
  200.  
  201. /**
  202.   * Remove roles
  203.   *
  204.   * @param \Madan\AdminBundle\Entity\Role $roles
  205.   */
  206. public function removeRole(\Madan\AdminBundle\Entity\Role $roles)
  207. {
  208. $this->roles->removeElement($roles);
  209. }
  210. }


Entity\Role.php
  1. <?php
  2.  
  3. namespace Madan\AdminBundle\Entity;
  4.  
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Symfony\Component\Security\Core\Role\RoleInterface;
  7. use Doctrine\ORM\Mapping as ORM;
  8.  
  9. /**
  10.  * User
  11.  *
  12.  * @ORM\Table(name="role")
  13.  * @ORM\Entity
  14.  */
  15. class Role implements RoleInterface
  16. {
  17. /**
  18.   * @var integer
  19.   *
  20.   * @ORM\Column(name="id", type="integer")
  21.   * @ORM\Id
  22.   * @ORM\GeneratedValue(strategy="AUTO")
  23.   */
  24. private $id;
  25.  
  26. /**
  27. * @var string
  28.   *
  29.   * @ORM\Column(name="name", type="string", length=100)
  30.   */
  31. protected $name;
  32.  
  33. /**
  34. * @var string
  35.   *
  36.   * @ORM\Column(name="role", type="string", length=100, unique=true)
  37.   */
  38. private $role;
  39.  
  40. /**
  41.   * @ORM\ManyToMany(targetEntity="User", mappedBy="roles")
  42.   */
  43. private $users;
  44.  
  45. public function __construct()
  46. {
  47. $this->users = new ArrayCollection();
  48. }
  49.  
  50. /**
  51.   * @see RoleInterface
  52.   */
  53. public function getRole()
  54. {
  55. return $this->role;
  56. }
  57.  
  58. /**
  59.   * Get id
  60.   *
  61.   * @return integer
  62.   */
  63. public function getId()
  64. {
  65. return $this->id;
  66. }
  67.  
  68. /**
  69.   * Set name
  70.   *
  71.   * @param string $name
  72.   * @return Role
  73.   */
  74. public function setName($name)
  75. {
  76. $this->name = $name;
  77.  
  78. return $this;
  79. }
  80.  
  81. /**
  82.   * Get name
  83.   *
  84.   * @return string
  85.   */
  86. public function getName()
  87. {
  88. return $this->name;
  89. }
  90.  
  91. /**
  92.   * Set role
  93.   *
  94.   * @param string $role
  95.   * @return Role
  96.   */
  97. public function setRole($role)
  98. {
  99. $this->role = $role;
  100.  
  101. return $this;
  102. }
  103.  
  104. /**
  105.   * Add users
  106.   *
  107.   * @param \Madan\AdminBundle\Entity\User $users
  108.   * @return Role
  109.   */
  110. public function addUser(\Madan\AdminBundle\Entity\User $users)
  111. {
  112. $this->users[] = $users;
  113.  
  114. return $this;
  115. }
  116.  
  117. /**
  118.   * Remove users
  119.   *
  120.   * @param \Madan\AdminBundle\Entity\User $users
  121.   */
  122. public function removeUser(\Madan\AdminBundle\Entity\User $users)
  123. {
  124. $this->users->removeElement($users);
  125. }
  126.  
  127. /**
  128.   * Get users
  129.   *
  130.   * @return \Doctrine\Common\Collections\Collection
  131.   */
  132. public function getUsers()
  133. {
  134. return $this->users;
  135. }
  136. }


Pomożecie? smile.gif
destroyerr
Dla encji User nie masz zdefiniowanego id.
m.e.n.t.o.s.
Jest zdefiniowane id
destroyerr
Oj przepraszam. Nie pomyślałem o tym, że można tak kod sformatować.
Sprawdzałeś w bazie danych, że masz tą kolumnę w tej tabeli?
pyro
Niech zgadnę... nie zrobiłeś

Kod
php app/console doctrine:schema:update --force


?
m.e.n.t.o.s.
Wykonałem to polecenie.

W bazie danych mam wszystko ok
Crozin
Ten błąd nie jest bezpośrednio związany z Sf czy Doctrine, występuje on na poziomie połączenia PDO. Sprawdź czy na pewno w tabeli user znajduje się kolumna id w bazie danych zdefiniowanej w połączeniu PDO. Jeżeli jest, a taki błąd nadal występuje wygląda to na bug PDO/MySQL w co szczerze wątpię. Sprawdź też czy wywołanie takiego zapytania z poziomu konsoli/jakiegoś phpMyAdmina zwraca błąd.
m.e.n.t.o.s.
Baza stoi na PostgreSQL.

id username password email salt
1 admin 25e4ee4e9229397b6b17776bfceaf8e7 op@op.pl


Kolumna Typ Not Null Domyślny Więzy integralności Akcje Komentarz
id integer
NOT NULL
[pk] Przeglądaj Zmień Uprawnienia Usuń
username character varying(255)
NOT NULL
Przeglądaj Zmień Uprawnienia Usuń
password character varying(50)
NOT NULL
Przeglądaj Zmień Uprawnienia Usuń
email character varying(127)
NOT NULL
Przeglądaj Zmień Uprawnienia Usuń
salt character varying(50)
Przeglądaj Zmień Uprawnienia Usuń





Błąd po wykonaniu zapytania:

SELECT t0.id AS id1, t0.username AS username2, t0.password AS password3, t0.email AS email4, t0.salt AS salt5 FROM user t0 WHERE t0.username = 'admin' LIMIT 1;

w phpPgAdminie:

Błąd SQL:

BŁĄD: kolumna t0.id nie istnieje
LINE 1: SELECT t0.id AS id1, t0.username AS username2, t0.password A...
^
W poleceniu:
SELECT t0.id AS id1, t0.username AS username2, t0.password AS password3, t0.email AS email4, t0.salt AS salt5 FROM user t0 WHERE t0.username = 'admin' LIMIT 1;


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.