security.rst 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. Security
  2. ========
  3. Users management
  4. ----------------
  5. By default, the SonataAdminBundle does not come with any user management, however it is most likely the application
  6. requires such feature. The Sonata Project includes a ``SonataUserBundle`` which integrates the ``FOSUserBundle``.
  7. The ``FOSUserBundle`` adds support for a database-backed user system in Symfony2. It provides a flexible framework
  8. for user management that aims to handle common tasks such as user login, registration and password retrieval.
  9. The ``SonataUserBundle`` is just a thin wrapper to include the ``FOSUserBundle`` into the ``AdminBundle``. The
  10. ``SonataUserBundle`` includes :
  11. * A default login area
  12. * A default ``user_block`` template which is used to display the current user and the logout link
  13. * 2 Admin classes : User and Group
  14. * A default class for User and Group.
  15. There is a little magic in the ``SonataAdminBundle`` if the bundle detects the ``SonataUserBundle`` class, then
  16. the default ``user_block`` template will be changed to use the one provided by the ``SonataUserBundle``.
  17. The install process is available on the dedicated `SonataUserBundle's documentation area <http://sonata-project.org/bundles/user/master/doc/reference/installation.html>`_
  18. Security handlers
  19. -----------------
  20. The security part is managed by a ``SecurityHandler``, the bundle has 2 handlers
  21. - ``sonata.admin.security.handler.acl`` : ACL and ROLES to handle permissions
  22. - ``sonata.admin.security.handler.noop`` : always returns true, can be used with the Symfony2 firewall
  23. The default value is ``sonata.admin.security.handler.noop``, if you want to change the default value
  24. you can set the ``security_handler`` to ``sonata.admin.security.handler.acl``.
  25. .. code-block:: yaml
  26. # app/config/config.yml
  27. sonata_admin:
  28. security_handler: sonata.admin.security.handler.acl
  29. The following section explains how to set up ACL with the ``FriendsOfSymfony/UserBundle``.
  30. ACL and FriendsOfSymfony/UserBundle
  31. -----------------------------------
  32. If you want an easy way to handle users, please use :
  33. - https://github.com/FriendsOfSymfony/FOSUserBundle : handle users and groups stored in RDMS or MongoDB
  34. - https://github.com/sonata-project/SonataUserBundle : integrates the ``FriendsOfSymfony/UserBundle`` with
  35. the ``AdminBundle``
  36. The security integration is a work in progress and has some known issues :
  37. - ACL permissions are immutables
  38. - Only one PermissionMap can be defined
  39. Configuration
  40. ~~~~~~~~~~~~~
  41. Before you can use ``FriendsOfSymfony/FOSUserBundle`` you need to set it up as described in the documentation
  42. of the bundle. In step 4 you need to create a User class (in a custom UserBundle). Do it as follows:
  43. .. code-block:: php
  44. <?php
  45. namespace Acme\UserBundle\Entity;
  46. use Sonata\UserBundle\Entity\BaseUser as BaseUser;
  47. use Doctrine\ORM\Mapping as ORM;
  48. /**
  49. * @ORM\Entity
  50. * @ORM\Table(name="fos_user")
  51. \*/
  52. class User extends BaseUser
  53. {
  54. /**
  55. * @ORM\Id
  56. * @ORM\Column(type="integer")
  57. * @ORM\GeneratedValue(strategy="AUTO")
  58. \*/
  59. protected $id;
  60. public function __construct()
  61. {
  62. parent::__construct();
  63. // your own logic
  64. }
  65. }
  66. In your ``app/config/config.yml`` you then need to put the following:
  67. .. code-block:: yaml
  68. fos_user:
  69. db_driver: orm
  70. firewall_name: main
  71. user_class: Acme\UserBundle\Entity\User
  72. The following configuration for the SonataUserBundle defines:
  73. - the ``FriendsOfSymfony/FOSUserBundle`` as a security provider
  74. - the login form for authentification
  75. - the access control : resources with related required roles, the important part is the admin configuration
  76. - the ``acl`` option to enable the ACL.
  77. In ``app/config/config.yml``:
  78. .. code-block:: yaml
  79. parameters:
  80. # ... other parameters
  81. security.acl.permission.map.class: Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap
  82. In ``app/config/security.yml``:
  83. .. code-block:: yaml
  84. security:
  85. providers:
  86. fos_userbundle:
  87. id: fos_user.user_manager
  88. firewalls:
  89. main:
  90. pattern: .*
  91. form-login:
  92. provider: fos_userbundle
  93. login_path: /login
  94. use_forward: false
  95. check_path: /login_check
  96. failure_path: null
  97. logout: true
  98. anonymous: true
  99. access_control:
  100. # The WDT has to be allowed to anonymous users to avoid requiring the login with the AJAX request
  101. - { path: ^/wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  102. - { path: ^/profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  103. # AsseticBundle paths used when using the controller for assets
  104. - { path: ^/js/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  105. - { path: ^/css/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  106. # URL of FOSUserBundle which need to be available to anonymous users
  107. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  108. - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # for the case of a failed login
  109. - { path: ^/user/new$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  110. - { path: ^/user/check-confirmation-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  111. - { path: ^/user/confirm/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  112. - { path: ^/user/confirmed$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  113. - { path: ^/user/request-reset-password$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  114. - { path: ^/user/send-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  115. - { path: ^/user/check-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  116. - { path: ^/user/reset-password/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  117. # Secured part of the site
  118. # This config requires being logged for the whole site and having the admin role for the admin part.
  119. # Change these rules to adapt them to your needs
  120. - { path: ^/admin/, role: ROLE_ADMIN }
  121. - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  122. role_hierarchy:
  123. ROLE_ADMIN: ROLE_USER
  124. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  125. acl:
  126. connection: default
  127. - Install the ACL tables ``php app/console init:acl``
  128. - Create a new user :
  129. .. code-block::
  130. # php app/console fos:user:create --super-admin
  131. Please choose a username:root
  132. Please choose an email:root@domain.com
  133. Please choose a password:root
  134. Created user root
  135. If you have Admin classes, you can install the related CRUD ACL rules :
  136. .. code-block::
  137. # php app/console sonata:admin:setup-acl
  138. Starting ACL AdminBundle configuration
  139. > install ACL for sonata.media.admin.media
  140. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_EDIT, ACL: ["EDIT"]
  141. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_LIST, ACL: ["LIST"]
  142. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_CREATE, ACL: ["CREATE"]
  143. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_DELETE, ACL: ["DELETE"]
  144. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_OPERATOR, ACL: ["OPERATOR"]
  145. ... skipped ...
  146. If you try to access the admin class you should see the login form, just logon with the ``root`` user.
  147. Usage
  148. ~~~~~
  149. Everytime you create a new ``Admin`` class, you should create ACL by using the command ``php app/console sonata:admin:setup-acl``
  150. so the ACL database will be updated with the latest masks and roles informations.