@@ -53,8 +53,8 @@ focus on the most important methods that come from the
53
53
54
54
namespace Acme\UserBundle\Entity;
55
55
56
- use Symfony\Component\Security\Core\User\UserInterface;
57
56
use Doctrine\ORM\Mapping as ORM;
57
+ use Symfony\Component\Security\Core\User\UserInterface;
58
58
59
59
/**
60
60
* Acme\UserBundle\Entity\User
@@ -65,29 +65,29 @@ focus on the most important methods that come from the
65
65
class User implements UserInterface
66
66
{
67
67
/**
68
- * @ORM\Column(name="id", type="integer")
69
- * @ORM\Id()
68
+ * @ORM\Column(type="integer")
69
+ * @ORM\Id
70
70
* @ORM\GeneratedValue(strategy="AUTO")
71
71
*/
72
72
private $id;
73
73
74
74
/**
75
- * @ORM\Column(name="username", type="string", length=25, unique=true)
75
+ * @ORM\Column(type="string", length=25, unique=true)
76
76
*/
77
77
private $username;
78
78
79
79
/**
80
- * @ORM\Column(name="salt", type="string", length=40 )
80
+ * @ORM\Column(type="string", length=32 )
81
81
*/
82
82
private $salt;
83
83
84
84
/**
85
- * @ORM\Column(name="password", type="string", length=40)
85
+ * @ORM\Column(type="string", length=40)
86
86
*/
87
87
private $password;
88
88
89
89
/**
90
- * @ORM\Column(name="email", type="string", length=60, unique=true)
90
+ * @ORM\Column(type="string", length=60, unique=true)
91
91
*/
92
92
private $email;
93
93
@@ -99,39 +99,68 @@ focus on the most important methods that come from the
99
99
public function __construct()
100
100
{
101
101
$this->isActive = true;
102
- $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
103
- }
104
-
105
- public function getRoles()
106
- {
107
- return array('ROLE_USER');
108
- }
109
-
110
- public function eraseCredentials()
111
- {
102
+ $this->salt = md5(uniqid(null, true));
112
103
}
113
104
105
+ /**
106
+ * @inheritDoc
107
+ */
114
108
public function getUsername()
115
109
{
116
110
return $this->username;
117
111
}
118
112
113
+ /**
114
+ * @inheritDoc
115
+ */
119
116
public function getSalt()
120
117
{
121
118
return $this->salt;
122
119
}
123
120
121
+ /**
122
+ * @inheritDoc
123
+ */
124
124
public function getPassword()
125
125
{
126
126
return $this->password;
127
127
}
128
+
129
+ /**
130
+ * @inheritDoc
131
+ */
132
+ public function getRoles()
133
+ {
134
+ return array('ROLE_USER');
135
+ }
136
+
137
+ /**
138
+ * @inheritDoc
139
+ */
140
+ public function eraseCredentials()
141
+ {
142
+ }
143
+
144
+ /**
145
+ * @inheritDoc
146
+ */
147
+ public function equals(UserInterface $user)
148
+ {
149
+ return $this->username === $user->getUsername();
150
+ }
128
151
}
129
152
130
153
In order to use an instance of the ``AcmeUserBundle:User `` class in the Symfony
131
154
security layer, the entity class must implement the
132
155
:class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface `. This
133
- interface forces the class to implement the five following methods: ``getRoles() ``,
134
- ``getPassword() ``, ``getSalt() ``, ``getUsername() ``, ``eraseCredentials() ``.
156
+ interface forces the class to implement the five following methods:
157
+
158
+ * ``getRoles() ``,
159
+ * ``getPassword() ``,
160
+ * ``getSalt() ``,
161
+ * ``getUsername() ``,
162
+ * ``eraseCredentials() ``
163
+
135
164
For more details on each of these, see :class: `Symfony\\ Component\\ Security\\ Core\\ User\\ UserInterface `.
136
165
137
166
Below is an export of my ``User `` table from MySQL. For details on how to
@@ -140,14 +169,14 @@ create user records and encode their password, see :ref:`book-security-encoding-
140
169
.. code-block :: text
141
170
142
171
mysql> select * from user;
143
- +----+----------+------------------------------------------ +------------------------------------------+--------------------+-----------+
144
- | id | username | salt | password | email | is_active |
145
- +----+----------+------------------------------------------ +------------------------------------------+--------------------+-----------+
146
- | 1 | hhamon | 7308e59b97f6957fb42d66f894793079c366d7c2 | 09610f61637408828a35d7debee5b38a8350eebe | hhamon@example.com | 1 |
147
- | 2 | jsmith | ce617a6cca9126
10000
bf4036ca0c02e82deea081e564 | 8390105917f3a3d533815250ed7c64b4594d7ebf | jsmith@example.com | 1 |
148
- | 3 | maxime | cd01749bb995dc658fa56ed45458d807b523e4cf | 9764731e5f7fb944de5fd8efad4949b995b72a3c | maxime@example.com | 0 |
149
- | 4 | donald | 6683c2bfd90c0426088402930cadd0f84901f2f4 | 5c3bcec385f59edcc04490d1db95fdb8673bf612 | donald@example.com | 1 |
150
- +----+----------+------------------------------------------ +------------------------------------------+--------------------+-----------+
172
+ +----+----------+----------------------------------+------------------------------------------+--------------------+-----------+
173
+ | id | username | salt | password | email | is_active |
174
+ +----+----------+----------------------------------+------------------------------------------+--------------------+-----------+
175
+ | 1 | hhamon | 7308e59b97f6957fb42d66f894793079 | 09610f61637408828a35d7debee5b38a8350eebe | hhamon@example.com | 1 |
176
+ | 2 | jsmith | ce617a6cca9126bf4036ca0c02e82dee | 8390105917f3a3d533815250ed7c64b4594d7ebf | jsmith@example.com | 1 |
177
+ | 3 | maxime | cd01749bb995dc658fa56ed45458d807 | 9764731e5f7fb944de5fd8efad4949b995b72a3c | maxime@example.com | 0 |
178
+ | 4 | donald | 6683c2bfd90c0426088402930cadd0f8 | 5c3bcec385f59edcc04490d1db95fdb8673bf612 | donald@example.com | 1 |
179
+ +----+----------+----------------------------------+------------------------------------------+--------------------+-----------+
151
180
4 rows in set (0.00 sec)
152
181
153
182
The database now contains four users with different usernames, emails and
@@ -172,12 +201,17 @@ then be checked against our User entity records in the database:
172
201
.. code-block :: yaml
173
202
174
203
# app/config/security.yml
204
+
175
205
security :
176
206
encoders :
177
207
Acme\UserBundle\Entity\User :
178
- algorithm : sha1
208
+ algorithm : sha1
179
209
encode_as_base64 : false
180
- iterations : 1
210
+ iterations : 1
211
+
212
+ role_hierarchy :
213
+ ROLE_ADMIN : ROLE_USER
214
+ ROLE_SUPER_ADMIN : [ ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]
181
215
182
216
providers :
183
217
administrators :
@@ -441,13 +475,19 @@ that forces it to have a ``getRole()`` method::
441
475
*/
442
476
private $id;
443
477
444
- /** @ORM\Column(name="name", type="string", length=30) */
478
+ /**
479
+ * @ORM\Column(name="name", type="string", length=30)
480
+ */
445
481
private $name;
446
482
447
- /** @ORM\Column(name="role", type="string", length=20, unique=true) */
483
+ /**
484
+ * @ORM\Column(name="role", type="string", length=20, unique=true)
485
+ */
448
486
private $role;
449
487
450
- /** @ORM\ManyToMany(targetEntity="User", mappedBy="groups") */
488
+ /**
489
+ * @ORM\ManyToMany(targetEntity="User", mappedBy="groups")
490
+ */
451
491
private $users;
452
492
453
493
public function __construct()
@@ -457,7 +497,9 @@ that forces it to have a ``getRole()`` method::
457
497
458
498
// ... getters and setters for each property
459
499
460
- /** @see RoleInterface */
500
+ /**
501
+ * @see RoleInterface
502
+ */
461
503
public function getRole()
462
504
{
463
505
return $this->role;
0 commit comments