installation.rst 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. Installation
  2. ============
  3. Prerequisites
  4. -------------
  5. PHP 5.3 and Symfony 2 are needed to make this bundle work ; there are also some
  6. Sonata dependencies that need to be installed and configured beforehand :
  7. - `SonataAdminBundle <http://sonata-project.org/bundles/admin>`_
  8. - `SonataEasyExtendsBundle <http://sonata-project.org/bundles/easy-extends>`_
  9. You will need to install those in their 2.0 branches (or master if they don't
  10. have a similar branch). Follow also their configuration step ; you will find
  11. everything you need in their installation chapter.
  12. .. note::
  13. If a dependency is already installed somewhere in your project or in
  14. another dependency, you won't need to install it again.
  15. Enable the Bundle
  16. -----------------
  17. Add the following lines to your ``deps`` file :
  18. .. code-block:: ini
  19. [SonataUserBundle]
  20. git=git://github.com/sonata-project/SonataUserBundle.git
  21. target=/bundles/Sonata/UserBundle
  22. version=origin/2.0
  23. [SonataDoctrineExtension]
  24. git=git://github.com/sonata-project/sonata-doctrine-extensions.git
  25. and run::
  26. bin/vendors install
  27. Next, be sure to enable the bundles in your autoload.php and AppKernel.php
  28. files:
  29. .. code-block:: php
  30. <?php
  31. // app/autoload.php
  32. $loader->registerNamespaces(array(
  33. // ...
  34. 'Sonata' => array(
  35. __DIR__.'/../vendor/bundles',
  36. __DIR__.'/../sonata-doctrine-extensions/src'
  37. ),
  38. // ...
  39. ));
  40. // app/appkernel.php
  41. public function registerbundles()
  42. {
  43. return array(
  44. // ...
  45. // You have 2 options to initialize the SonataUserBundle in your AppKernel,
  46. // you can select which bundle SonataUserBundle extends
  47. // extend the ``FOSUserBundle``
  48. new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
  49. // OR
  50. // the bundle will NOT extend ``FOSUserBundle``
  51. new Sonata\UserBundle\SonataUserBundle(),
  52. // ...
  53. );
  54. }
  55. .. note::
  56. If you already have installed a Sonata dependency, you may ignore the step
  57. on the modification of the ``autoload.php`` file.
  58. Configuration
  59. -------------
  60. When using ACL, the UserBundle can prevent ``normal`` user to change settings
  61. of ``super-admin`` users, to enable this add to the configuration:
  62. .. code-block:: yaml
  63. # app/config/config.yml
  64. sonata_user:
  65. security_acl: true
  66. # app/config/security.yml
  67. security:
  68. # [...]
  69. acl:
  70. connection: default
  71. Doctrine Configuration
  72. ~~~~~~~~~~~~~~~~~~~~~~
  73. Then add these bundles in the config mapping definition (or enable `auto_mapping <http://symfony.com/doc/2.0/reference/configuration/doctrine.html#configuration-overview>`_):
  74. .. code-block:: yaml
  75. # app/config/config.yml
  76. fos_user:
  77. db_driver: orm # can be orm or odm
  78. firewall_name: main
  79. user_class: Application\Sonata\UserBundle\Entity\User
  80. group:
  81. group_class: Application\Sonata\UserBundle\Entity\Group
  82. doctrine:
  83. orm:
  84. entity_managers:
  85. default:
  86. mappings:
  87. ApplicationSonataUserBundle: ~
  88. SonataUserBundle: ~
  89. dbal:
  90. types:
  91. json: Sonata\Doctrine\Types\JsonType
  92. Integrating the bundle into the Sonata Admin Bundle
  93. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  94. Add the related security routing information
  95. .. code-block:: yaml
  96. sonata_user:
  97. resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
  98. prefix: /admin
  99. You also need to define a ``sonata_user_impersonating`` route, used as a
  100. redirection after an user impersonating.
  101. Then add a new custom firewall handlers for the admin
  102. .. code-block:: yaml
  103. security:
  104. role_hierarchy:
  105. ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
  106. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  107. SONATA:
  108. - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT # if you are using acl then this line must be commented
  109. providers:
  110. fos_userbundle:
  111. id: fos_user.user_manager
  112. firewalls:
  113. # -> custom firewall for the admin area of the URL
  114. admin:
  115. switch_user: true
  116. context: user
  117. pattern: /admin(.*)
  118. form_login:
  119. provider: fos_userbundle
  120. login_path: /admin/login
  121. use_forward: false
  122. check_path: /admin/login_check
  123. failure_path: null
  124. use_referer: true
  125. logout:
  126. path: /admin/logout
  127. target: /admin/login
  128. anonymous: true
  129. # -> end custom configuration
  130. # defaut login area for standard users
  131. main:
  132. switch_user: true
  133. context: user
  134. pattern: .*
  135. form_login:
  136. provider: fos_userbundle
  137. login_path: /login
  138. use_forward: false
  139. check_path: /login_check
  140. failure_path: null
  141. logout: true
  142. anonymous: true
  143. The last part is to define 3 new access control rules :
  144. .. code-block:: yaml
  145. security:
  146. access_control:
  147. # URL of FOSUserBundle which need to be available to anonymous users
  148. - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }
  149. - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }
  150. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  151. # -> custom access control for the admin area of the URL
  152. - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  153. - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  154. - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  155. # -> end
  156. - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
  157. - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
  158. # Secured part of the site
  159. # This config requires being logged for the whole site and having the admin role for the admin part.
  160. # Change these rules to adapt them to your needs
  161. - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
  162. - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  163. Using the roles
  164. ---------------
  165. Each admin has its own roles, use the user form to assign them to other users.
  166. The available roles to assign to others are limited to the roles available to
  167. the user editing the form.
  168. Extending the Bundle
  169. --------------------
  170. At this point, the bundle is functionnal, but not quite ready yet. You need to
  171. generate the correct entities for the media::
  172. php app/console sonata:easy-extends:generate SonataUserBundle
  173. If you specify no parameter, the files are generated in app/Application/Sonata...
  174. but you can specify the path with ``--dest=src``
  175. .. note::
  176. The command will generate domain objects in an ``Application`` namespace.
  177. So you can point entities' associations to a global and common namespace.
  178. This will make Entities sharing easier as your models will allow to
  179. point to a global namespace. For instance the user will be
  180. ``Application\Sonata\UserBundle\Entity\User``.
  181. Now, add the new `Application` Bundle into the kernel and your autoload :
  182. .. code-block:: php
  183. <?php
  184. // autoload.php
  185. $loader->registerNamespaces(array(
  186. // ...
  187. 'Application' => __DIR__ . '/../src/',
  188. // ...
  189. ));
  190. // AppKernel.php
  191. class AppKernel {
  192. public function registerbundles()
  193. {
  194. return array(
  195. // Application Bundles
  196. // ...
  197. new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
  198. // ...
  199. )
  200. }
  201. }