user_manager.rst 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. About FOSUserBundle User Manager
  2. ================================
  3. In order to be storage agnostic, all operations on the user instances are
  4. handled by a user manager implementing ``FOS\UserBundle\Model\UserManagerInterface``.
  5. Using it ensures that your code will continue to work if you change the storage.
  6. The controllers provided by the bundle use the configured user manager instead
  7. of interacting directly with the storage layer.
  8. If you configure the ``db_driver`` option to ``orm``, this service is an instance
  9. of ``FOS\UserBundle\Entity\UserManager``.
  10. If you configure the ``db_driver`` option to ``mongodb``, this service is an
  11. instance of ``FOS\UserBundle\Document\UserManager``.
  12. If you configure the ``db_driver`` option to ``couchdb``, this service is an
  13. instance of ``FOS\UserBundle\CouchDocument\UserManager``.
  14. If you configure the ``db_driver`` option to ``propel``, this service is an instance
  15. of ``FOS\UserBundle\Propel\UserManager``.
  16. Accessing the User Manager service
  17. ----------------------------------
  18. The user manager is available in the container as the ``fos_user.user_manager``
  19. service.
  20. .. code-block:: php
  21. $userManager = $container->get('fos_user.user_manager');
  22. Creating a new User
  23. -------------------
  24. A new instance of your User class can be created by the user manager.
  25. .. code-block:: php
  26. $user = $userManager->createUser();
  27. ``$user`` is now an instance of your user class.
  28. .. note::
  29. This method will not work if your user class has some mandatory constructor
  30. arguments.
  31. Retrieving the users
  32. --------------------
  33. The user manager has a few methods to find users based on the unique fields
  34. (username, email and confirmation token) and a method to retrieve all existing
  35. users.
  36. - ``findUserByUsername($username)``
  37. - ``findUserByEmail($email)``
  38. - ``findUserByUsernameOrEmail($value)`` (check if the value looks like an email to choose)
  39. - ``findUserByConfirmationToken($token)``
  40. - ``findUserBy(array('id'=>$id))``
  41. - ``findUsers()``
  42. To save a user object, you can use the ``updateUser`` method of the user manager.
  43. This method will update the encoded password and the canonical fields and
  44. then persist the changes.
  45. Updating a User object
  46. ----------------------
  47. .. code-block:: php
  48. $user = $userManager->createUser();
  49. $user->setUsername('John');
  50. $user->setEmail('john.doe@example.com');
  51. $userManager->updateUser($user);
  52. .. note::
  53. To make it easier, the bundle comes with a Doctrine listener handling
  54. the update of the password and the canonical fields for you behind the
  55. scenes. If you always save the user through the user manager, you may
  56. want to disable it to improve performance.
  57. .. configuration-block::
  58. .. code-block:: yaml
  59. # app/config/config.yml
  60. fos_user:
  61. # ...
  62. use_listener: false
  63. .. code-block:: xml
  64. <!-- app/config/config.xml -->
  65. <fos_user:config
  66. db-driver="orm"
  67. firewall-name="main"
  68. use-listener="false"
  69. user-class="MyProject\MyBundle\Entity\User"
  70. />
  71. .. caution::
  72. The Propel implementation does not have such a listener so you have to
  73. call the method of the user manager to save the user.
  74. .. note::
  75. For the Doctrine implementations, the default behavior is to flush the
  76. unit of work when calling the ``updateUser`` method. You can disable the
  77. flush by passing a second argument set to ``false``.
  78. This will then be equivalent to calling ``updateCanonicalFields`` and
  79. ``updatePassword``.
  80. An ORM example::
  81. public function MainController extends Controller
  82. {
  83. public function updateAction($id)
  84. {
  85. $user = // get a user from the datastore
  86. $user->setEmail($newEmail);
  87. $this->get('fos_user.user_manager')->updateUser($user, false);
  88. // make more modifications to the database
  89. $this->getDoctrine()->getManager()->flush();
  90. }
  91. }
  92. Overriding the User Manager
  93. ---------------------------
  94. You can replace the default implementation of the user manager by defining
  95. a service implementing ``FOS\UserBundle\Model\UserManagerInterface`` and
  96. setting its id in the configuration.
  97. The id of the default implementation is ``fos_user.user_manager.default``
  98. .. code-block:: yaml
  99. fos_user:
  100. # ...
  101. service:
  102. user_manager: custom_user_manager_id
  103. Your custom implementation can extend ``FOS\UserBundle\Model\UserManager``
  104. to reuse the common logic.
  105. SecurityBundle integration
  106. --------------------------
  107. The bundle provides several implementation of ``Symfony\Component\Security\Core\UserProviderInterface``
  108. on top of the ``UserManagerInterface``.
  109. Although the built-in user managers also implement
  110. ``Symfony\Component\Security\Core\User\UserProviderInterface``, using the
  111. UserManager as user provider is deprecated and will tbe removed in future
  112. versions. Use ``FOS\UserBundle\Security\UserProvider`` instead.