Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.2k
cleaning#2753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
cleaning#2753
Changes fromall commits
50905ff
c3c3e98
931091d
abe537f
438c824
a981ae7
a2bc822
d7ea3a5
2b7dcf9
4c06860
030a6f8
37c86d6
458c392
ebdcaba
26215a7
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -363,6 +363,8 @@ see the :ref:`book-doctrine-field-types` section. | ||
class Product | ||
// ... | ||
.. _book-doctrine-generating-getters-and-setters: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. there should be an empty line after this one | ||
Generating Getters and Setters | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
@@ -425,6 +427,8 @@ mapping information) of a bundle or an entire namespace: | ||
The getters and setters are generated here only because you'll need them | ||
to interact with your PHP object. | ||
.. _book-doctrine-creating-the-database-tables-schema: | ||
Creating the Database Tables/Schema | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -167,21 +167,6 @@ interface forces the class to implement the five following methods: | ||
For more details on each of these, see :class:`Symfony\\Component\\Security\\Core\\User\\UserInterface`. | ||
.. note:: | ||
The :phpclass:`Serializable` interface and its ``serialize`` and ``unserialize`` | ||
@@ -191,24 +176,32 @@ For more details on each of these, see :class:`Symfony\\Component\\Security\\Cor | ||
because the :method:`Symfony\\Bridge\\Doctrine\\Security\\User\\EntityUserProvider::refreshUser` | ||
method reloads the user on each request by using the ``id``. | ||
.. tip:: | ||
To generate missing setters and getters for your ``User`` entity, you | ||
can use ``php app/console doctrine:generate:entities Acme/UserBundle/Entity/User``. | ||
For more details, see Doctrine's :ref:`book-doctrine-generating-getters-and-setters`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. AFAIK, this code is stil relevant. But an explanation/introduction should be placed before this snippet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. totally agree. i personally dont know what is this code good for and it was not needed to make functionality covered by this tutorial to work, so from my perspective it is useless, unless ofc the explanation will be added. i would suggest remove the code for now, and once someone decide to add explanation, the code can be attached again. | ||
Below is an export of my ``User`` table from MySQL with user `admin` | ||
and password `admin`. For details on how to create user records and | ||
encode their password, see :ref:`book-security-encoding-user-password`. | ||
.. code-block:: bash | ||
$ mysql> select * from acme_users; | ||
+----+----------+------+------------------------------------------+--------------------+-----------+ | ||
| id | username | salt | password | email | is_active | | ||
+----+----------+------+------------------------------------------+--------------------+-----------+ | ||
| 1 | admin | | d033e22ae348aeb5660fc2140aec35850c4da997 | admin@example.com | 1 | | ||
+----+----------+------+------------------------------------------+--------------------+-----------+ | ||
.. tip:: | ||
To generate database table from your ``User`` entity, you can run | ||
``php app/console doctrine:schema:update --force``. | ||
For mor details, see Doctrine's :ref:`book-doctrine-creating-the-database-tables-schema`. | ||
The next part will focus on how to authenticate one of these users | ||
thanks to the Doctrine entity user provider and a couple of lines of | ||
configuration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. why did you remove the rows? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. because the data are irrelevant. as mentioned here#2756 there is no explanation how they were created, what are the raw passwords = not usable for testing. | ||
@@ -323,9 +316,8 @@ entity user provider to load User entity objects from the database by using | ||
the ``username`` unique field. In other words, this tells Symfony how to | ||
fetch the user from the database before checking the password validity. | ||
This code is not enough to secure the application for **active** users. | ||
The next section explains how to forbid non active users. | ||
Forbid non Active Users | ||
----------------------- | ||
@@ -355,10 +347,10 @@ For this example, the first three methods will return ``true`` whereas the | ||
// src/Acme/UserBundle/Entity/User.php | ||
namespace Acme\UserBundle\Entity; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Security\Core\User\AdvancedUserInterface; | ||
class User implements AdvancedUserInterface, \Serializable | ||
{ | ||
// ... | ||
@@ -383,10 +375,8 @@ For this example, the first three methods will return ``true`` whereas the | ||
} | ||
} | ||
The next session will focus on how to write a custom entity provider | ||
to authenticate a user with his username or his email address. | ||
Authenticating Someone with a Custom Entity Provider | ||
---------------------------------------------------- | ||
@@ -428,8 +418,7 @@ The code below shows the implementation of the | ||
->where('u.username = :username OR u.email = :email') | ||
->setParameter('username', $username) | ||
->setParameter('email', $username) | ||
->getQuery(); | ||
try { | ||
// The Query::getSingleResult() method throws an exception | ||
@@ -537,10 +526,11 @@ about in this section. | ||
authenticated at all. | ||
In this example, the ``AcmeUserBundle:User`` entity class defines a | ||
many-to-many relationship with a ``AcmeUserBundle:Role`` entity class. | ||
A user can be related to several roles and a role can be composed of | ||
one or more users. The previous ``getRoles()`` method now returns | ||
the list of related roles. | ||
Notice that methods ``__construcotor()`` and ``getRoles()`` had changed:: | ||
// src/Acme/UserBundle/Entity/User.php | ||
namespace Acme\UserBundle\Entity; | ||
@@ -550,63 +540,46 @@ returns the list of related groups:: | ||
class User implements AdvancedUserInterface, \Serializable | ||
{ | ||
//... | ||
/** | ||
* @ORM\ManyToMany(targetEntity="Role", inversedBy="users") | ||
* | ||
*/ | ||
private $roles; | ||
public function __construct() | ||
{ | ||
$this->roles = new ArrayCollection(); | ||
} | ||
public function getRoles() | ||
{ | ||
return $this->roles->toArray(); | ||
} | ||
// ... | ||
} | ||
The ``AcmeUserBundle:Role`` entity class defines three table fields (``id``, | ||
``name`` and ``role``). The unique ``role`` field contains the role name used by | ||
the Symfony security layer to secure parts of the application. The most | ||
important thing to notice is that the ``AcmeUserBundle:Role`` entity class | ||
extends the :class:`Symfony\\Component\\Security\\Core\\Role\\Role`:: | ||
// src/Acme/Bundle/UserBundle/Entity/Role.php | ||
namespace Acme\UserBundle\Entity; | ||
use Symfony\Component\Security\Core\Role\RoleInterface; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
/** | ||
* @ORM\Table(name="acme_roles") | ||
* @ORM\Entity() | ||
*/ | ||
classRole implements RoleInterface | ||
{ | ||
/** | ||
* @ORM\Column(name="id", type="integer") | ||
@@ -626,7 +599,7 @@ extends the :class:`Symfony\\Component\\Security\\Core\\Role\\Role`:: | ||
private $role; | ||
/** | ||
* @ORM\ManyToMany(targetEntity="User", mappedBy="roles") | ||
*/ | ||
private $users; | ||
@@ -635,21 +608,27 @@ extends the :class:`Symfony\\Component\\Security\\Core\\Role\\Role`:: | ||
$this->users = new ArrayCollection(); | ||
} | ||
/** | ||
* @see RoleInterface | ||
*/ | ||
public function getRole() | ||
{ | ||
return $this->role; | ||
} | ||
// ... getters and setters for each property | ||
} | ||
.. tip:: | ||
To generate missing setters and getters for your ``Role`` entity, you | ||
can use ``php app/console doctrine:generate:entities Acme/UserBundle/Entity/User``. | ||
For more details, see Doctrine's :ref:`book-doctrine-generating-getters-and-setters`. | ||
To improve performances and avoid lazy loading of roles when retrieving a user | ||
from the custom entity provider, the best solution is to join the roles | ||
relationship in the ``UserRepository::loadUserByUsername()`` method. This will | ||
fetch the user and his associated roles with a single query:: | ||
// src/Acme/UserBundle/Entity/UserRepository.php | ||
namespace Acme\UserBundle\Entity; | ||
@@ -662,8 +641,8 @@ fetch the user and his associated roles / groups with a single query:: | ||
{ | ||
$q = $this | ||
->createQueryBuilder('u') | ||
->select('u,r') | ||
->leftJoin('u.roles', 'r') | ||
->where('u.username = :username OR u.email = :email') | ||
->setParameter('username', $username) | ||
->setParameter('email', $username) | ||
@@ -675,6 +654,29 @@ fetch the user and his associated roles / groups with a single query:: | ||
// ... | ||
} | ||
The ``QueryBuilder::leftJoin()`` method joins and fetches relatedroles from | ||
the ``AcmeUserBundle:User`` model class when a user is retrieved with his email | ||
address or username. | ||
To re-generate all database tables, you can run ``php app/console doctrine:schema:update --force``. | ||
This will also create additional table ``user_role`` what holds | ||
relations between users and roles. | ||
For mor details, see Doctrine's :ref:`book-doctrine-creating-the-database-tables-schema`. | ||
Below is an export of my ``Roles`` and ``user_role`` tables from MySQL: | ||
.. code-block:: bash | ||
$ mysql> select * from acme_users; | ||
+----+-------+------------+ | ||
| id | name | role | | ||
+----+-------+------------+ | ||
| 1 | admin | ROLE_ADMIN | | ||
+----+-------+------------+ | ||
mysql> select * from user_role; | ||
+---------+---------+ | ||
| user_id | role_id | | ||
+---------+---------+ | ||
| 1 | 1 | | ||
+---------+---------+ |