security.rst 6.3 KB

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