installation.rst 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. Then add a new custom firewall handlers for the admin
  100. .. code-block:: yaml
  101. security:
  102. role_hierarchy:
  103. ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
  104. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  105. SONATA:
  106. - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT # if you are using acl then this line must be commented
  107. providers:
  108. fos_userbundle:
  109. id: fos_user.user_manager
  110. firewalls:
  111. # -> custom firewall for the admin area of the URL
  112. admin:
  113. switch_user: true
  114. context: user
  115. pattern: /admin(.*)
  116. form_login:
  117. provider: fos_userbundle
  118. login_path: /admin/login
  119. use_forward: false
  120. check_path: /admin/login_check
  121. failure_path: null
  122. use_referer: true
  123. logout:
  124. path: /admin/logout
  125. target: /admin/login
  126. anonymous: true
  127. # -> end custom configuration
  128. # defaut login area for standard users
  129. main:
  130. switch_user: true
  131. context: user
  132. pattern: .*
  133. form_login:
  134. provider: fos_userbundle
  135. login_path: /login
  136. use_forward: false
  137. check_path: /login_check
  138. failure_path: null
  139. logout: true
  140. anonymous: true
  141. The last part is to define 3 new access control rules :
  142. .. code-block:: yaml
  143. security:
  144. access_control:
  145. # URL of FOSUserBundle which need to be available to anonymous users
  146. - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY }
  147. - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY }
  148. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  149. # -> custom access control for the admin area of the URL
  150. - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  151. - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  152. - { path: ^/admin/login-check$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  153. # -> end
  154. - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
  155. - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
  156. # Secured part of the site
  157. # This config requires being logged for the whole site and having the admin role for the admin part.
  158. # Change these rules to adapt them to your needs
  159. - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
  160. - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  161. Using the roles
  162. ---------------
  163. Each admin has its own roles, use the user form to assign them to other users.
  164. The available roles to assign to others are limited to the roles available to
  165. the user editing the form.
  166. Extending the Bundle
  167. --------------------
  168. At this point, the bundle is functionnal, but not quite ready yet. You need to
  169. generate the correct entities for the media::
  170. php app/console sonata:easy-extends:generate SonataUserBundle
  171. If you specify no parameter, the files are generated in app/Application/Sonata...
  172. but you can specify the path with ``--dest=src``
  173. .. note::
  174. The command will generate domain objects in an ``Application`` namespace.
  175. So you can point entities' associations to a global and common namespace.
  176. This will make Entities sharing easier as your models will allow to
  177. point to a global namespace. For instance the user will be
  178. ``Application\Sonata\UserBundle\Entity\User``.
  179. Now, add the new `Application` Bundle into the kernel and your autoload :
  180. .. code-block:: php
  181. <?php
  182. // autoload.php
  183. $loader->registerNamespaces(array(
  184. // ...
  185. 'Application' => __DIR__ . '/../src/',
  186. // ...
  187. ));
  188. // AppKernel.php
  189. class AppKernel {
  190. public function registerbundles()
  191. {
  192. return array(
  193. // Application Bundles
  194. // ...
  195. new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
  196. // ...
  197. )
  198. }
  199. }