security.rst 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 return 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. sonata_admin:
  10. security_handler: sonata.admin.security.handler.acl
  11. The following section explains how to set up ACL with the ``FriendsOfSymfony/UserBundle``.
  12. ACL and FriendsOfSymfony/UserBundle
  13. -----------------------------------
  14. If you want an easy way to handle users, please use :
  15. - https://github.com/FriendsOfSymfony/UserBundle : handle users and group stored from RDMS or MongoDB
  16. - https://github.com/sonata-project/UserBundle : integrate the ``FriendsOfSymfony/UserBundle`` with
  17. the ``AdminBundle``
  18. The security integration is a work in progress and have some knows issues :
  19. - ACL permissions are immutables
  20. - Only one PermissionMap can be defined
  21. Configuration
  22. ~~~~~~~~~~~~~
  23. - The following configuration defines :
  24. - the ``FriendsOfSymfony/UserBundle`` as a security provider
  25. - the login form for authentification
  26. - the access control : resources with related required roles, the important part is the admin configuration
  27. - the ``acl`` option enable the ACL.
  28. .. code-block:: yaml
  29. parameters:
  30. # ... other parameters
  31. security.acl.permission.map.class: Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap
  32. security:
  33. providers:
  34. fos_userbundle:
  35. id: fos_user.user_manager
  36. firewalls:
  37. main:
  38. pattern: .*
  39. form-login:
  40. provider: fos_userbundle
  41. login_path: /login
  42. use_forward: false
  43. check_path: /login_check
  44. failure_path: null
  45. logout: true
  46. anonymous: true
  47. access_control:
  48. # The WDT has to be allowed to anonymous users to avoid requiring the login with the AJAX request
  49. - { path: ^/wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  50. - { path: ^/profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  51. # AsseticBundle paths used when using the controller for assets
  52. - { path: ^/js/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  53. - { path: ^/css/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  54. # URL of FOSUserBundle which need to be available to anonymous users
  55. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  56. - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # for the case of a failed login
  57. - { path: ^/user/new$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  58. - { path: ^/user/check-confirmation-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  59. - { path: ^/user/confirm/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  60. - { path: ^/user/confirmed$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  61. - { path: ^/user/request-reset-password$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  62. - { path: ^/user/send-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  63. - { path: ^/user/check-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  64. - { path: ^/user/reset-password/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  65. # Secured part of the site
  66. # This config requires being logged for the whole site and having the admin role for the admin part.
  67. # Change these rules to adapt them to your needs
  68. - { path: ^/admin/, role: ROLE_ADMIN }
  69. - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  70. role_hierarchy:
  71. ROLE_ADMIN: ROLE_USER
  72. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  73. acl:
  74. connection: default
  75. - Install the ACL tables ``php app/console init:acl``
  76. - Create a new user :
  77. .. code-block::
  78. # php app/console fos:user:create
  79. Please choose a username:root
  80. Please choose an email:root@domain.com
  81. Please choose a password:root
  82. Created user root
  83. - Promote an user as super admin :
  84. .. code-block::
  85. # php app/console fos:user:promote root
  86. User "root" has been promoted as a super administrator.
  87. If you have Admin classes, you can install the related CRUD ACL rules :
  88. .. code-block::
  89. # php app/console sonata:admin:setup-acl
  90. Starting ACL AdminBundle configuration
  91. > install ACL for sonata.media.admin.media
  92. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_EDIT, ACL: ["EDIT"]
  93. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_LIST, ACL: ["LIST"]
  94. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_CREATE, ACL: ["CREATE"]
  95. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_DELETE, ACL: ["DELETE"]
  96. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_OPERATOR, ACL: ["OPERATOR"]
  97. ... skipped ...
  98. If you try to access to the admin class you should see the login form, just logon with the ``root`` user.
  99. Usage
  100. ~~~~~
  101. Everytime you create a new ``Admin`` class, you should create start the command ``php app/console sonata:admin:setup-acl``
  102. so the ACL database will be updated with the latest masks and roles informations.