security.rst 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. Security
  2. ========
  3. User management
  4. ---------------
  5. By default, the SonataAdminBundle does not come with any user management,
  6. however it is most likely the application requires such a feature. The Sonata
  7. Project includes a ``SonataUserBundle`` which integrates the ``FOSUserBundle``.
  8. The ``FOSUserBundle`` adds support for a database-backed user system in Symfony.
  9. It provides a flexible framework for user management that aims to handle common
  10. tasks such as user login, registration and password retrieval.
  11. The ``SonataUserBundle`` is just a thin wrapper to include the ``FOSUserBundle``
  12. into the ``AdminBundle``. The ``SonataUserBundle`` includes:
  13. * A default login area
  14. * A default ``user_block`` template which is used to display the current user
  15. and the logout link
  16. * 2 Admin classes: User and Group
  17. * A default class for User and Group.
  18. There is a little magic in the ``SonataAdminBundle``: if the bundle detects the
  19. ``SonataUserBundle`` class, then the default ``user_block`` template will be
  20. changed to use the one provided by the ``SonataUserBundle``.
  21. The install process is available on the dedicated
  22. `SonataUserBundle's documentation area`_.
  23. Security handlers
  24. -----------------
  25. The security part is managed by a ``SecurityHandler``, the bundle comes with 3 handlers:
  26. - ``sonata.admin.security.handler.role``: ROLES to handle permissions
  27. - ``sonata.admin.security.handler.acl``: ACL and ROLES to handle permissions
  28. - ``sonata.admin.security.handler.noop``: always returns true, can be used
  29. with the Symfony firewall
  30. The default value is ``sonata.admin.security.handler.noop``, if you want to
  31. change the default value you can set the ``security_handler`` to
  32. ``sonata.admin.security.handler.acl`` or ``sonata.admin.security.handler.role``.
  33. To quickly secure an admin the role security can be used. It allows to specify
  34. the actions a user can do with the admin. The ACL security system is more advanced
  35. and allows to secure the objects. For people using the previous ACL
  36. implementation, you can switch the ``security_handler`` to the role security handler.
  37. Configuration
  38. ~~~~~~~~~~~~~
  39. Only the security handler is required to determine which type of security to use.
  40. The other parameters are set as default, change them if needed.
  41. Using roles:
  42. .. configuration-block::
  43. .. code-block:: yaml
  44. # app/config/config.yml
  45. sonata_admin:
  46. security:
  47. handler: sonata.admin.security.handler.role
  48. Using ACL:
  49. .. configuration-block::
  50. .. code-block:: yaml
  51. # app/config/config.yml
  52. sonata_admin:
  53. security:
  54. handler: sonata.admin.security.handler.acl
  55. # acl security information
  56. information:
  57. GUEST: [VIEW, LIST]
  58. STAFF: [EDIT, LIST, CREATE]
  59. EDITOR: [OPERATOR, EXPORT]
  60. ADMIN: [MASTER]
  61. # permissions not related to an object instance and also to be available when objects do not exist
  62. # the DELETE admin permission means the user is allowed to batch delete objects
  63. admin_permissions: [CREATE, LIST, DELETE, UNDELETE, EXPORT, OPERATOR, MASTER]
  64. # permission related to the objects
  65. object_permissions: [VIEW, EDIT, DELETE, UNDELETE, OPERATOR, MASTER, OWNER]
  66. Later, we will explain how to set up ACL with the ``FriendsOfSymfony/UserBundle``.
  67. Role handler
  68. ------------
  69. The ``sonata.admin.security.handler.role`` allows you to operate finely on the
  70. actions that can be done (depending on the entity class), without requiring to set up ACL.
  71. Configuration
  72. ~~~~~~~~~~~~~
  73. First, activate the role security handler as described above.
  74. Each time a user tries to do an action in the admin, Sonata checks if he is
  75. either a super admin (``ROLE_SUPER_ADMIN``) **or** has the permission.
  76. The permissions are:
  77. ========== ========================================
  78. Permission Description
  79. ========== ========================================
  80. LIST view the list of objects
  81. VIEW view the detail of one object
  82. CREATE create a new object
  83. EDIT update an existing object
  84. DELETE delete an existing object
  85. EXPORT (for the native Sonata export links)
  86. ========== ========================================
  87. Each permission is relative to an admin: if you try to get a list in FooAdmin (declared as ``app.admin.foo``
  88. service), Sonata will check if the user has the ``ROLE_APP_ADMIN_FOO_EDIT`` role.
  89. The role name will be based on the name of your admin service. For instance, ``acme.blog.post.admin`` will become ``ROLE_ACME_BLOG_POST_ADMIN_{ACTION}``.
  90. .. note::
  91. If your admin service is named like ``my.blog.admin.foo_bar`` (note the underscore ``_``) it will become: ``ROLE_MY_BLOG_ADMIN_FOO_BAR_{ACTION}``
  92. So our ``security.yml`` file may look something like this:
  93. .. configuration-block::
  94. .. code-block:: yaml
  95. # app/config/security.yml
  96. security:
  97. # ...
  98. role_hierarchy:
  99. # for convenience, I decided to gather Sonata roles here
  100. ROLE_SONATA_FOO_READER:
  101. - ROLE_SONATA_ADMIN_DEMO_FOO_LIST
  102. - ROLE_SONATA_ADMIN_DEMO_FOO_VIEW
  103. ROLE_SONATA_FOO_EDITOR:
  104. - ROLE_SONATA_ADMIN_DEMO_FOO_CREATE
  105. - ROLE_SONATA_ADMIN_DEMO_FOO_EDIT
  106. ROLE_SONATA_FOO_ADMIN:
  107. - ROLE_SONATA_ADMIN_DEMO_FOO_DELETE
  108. - ROLE_SONATA_ADMIN_DEMO_FOO_EXPORT
  109. # those are the roles I will use (less verbose)
  110. ROLE_STAFF: [ROLE_USER, ROLE_SONATA_FOO_READER]
  111. ROLE_ADMIN: [ROLE_STAFF, ROLE_SONATA_FOO_EDITOR, ROLE_SONATA_FOO_ADMIN]
  112. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  113. # set access_strategy to unanimous, else you may have unexpected behaviors
  114. access_decision_manager:
  115. strategy: unanimous
  116. Note that we also set ``access_strategy`` to unanimous.
  117. It means that if one voter (for example Sonata) refuses access, access will be denied.
  118. For more information on this subject, please see `changing the access decision strategy`_
  119. in the Symfony documentation.
  120. Usage
  121. ~~~~~
  122. You can now test if a user is authorized from an Admin class:
  123. .. code-block:: php
  124. if ($this->isGranted('LIST')) {
  125. // ...
  126. }
  127. From a controller extending ``Sonata\AdminBundle\Controller\CRUDController``:
  128. .. code-block:: php
  129. if ($this->admin->isGranted('LIST')) {
  130. // ...
  131. }
  132. Or from a Twig template:
  133. .. code-block:: jinja
  134. {% if is_granted('VIEW') %}
  135. <p>Hello there!</p>
  136. {% endif %}
  137. Note that you do not have to re-specify the prefix.
  138. Sonata checks those permissions for the action it handles internally.
  139. Of course you will have to recheck them in your own code.
  140. Yon can also create your own permissions, for example ``EMAIL``
  141. (which will turn into role ``ROLE_APP_ADMIN_FOO_EMAIL``).
  142. Going further
  143. ~~~~~~~~~~~~~
  144. Because Sonata role handler supplements Symfony security, but does not override it, you are free to do more advanced operations.
  145. For example, you can `create your own voter`_
  146. Customizing the handler behavior
  147. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  148. If you want to change the handler behavior (for example, to pass the current object to voters), extend
  149. ``Sonata\AdminBundle\Security\Handler\RoleSecurityHandler``, and override the ``isGranted`` method.
  150. Then declare your handler as a service:
  151. .. configuration-block::
  152. .. code-block:: xml
  153. <service id="app.security.handler.role" class="AppBundle\Security\Handler\RoleSecurityHandler" public="false">
  154. <argument type="service" id="security.context" on-invalid="null" />
  155. <argument type="collection">
  156. <argument>ROLE_SUPER_ADMIN</argument>
  157. </argument>
  158. </service>
  159. And specify it as Sonata security handler on your configuration:
  160. .. configuration-block::
  161. .. code-block:: yaml
  162. # app/config/config.yml
  163. sonata_admin:
  164. security:
  165. handler: app.security.handler.role
  166. ACL and FriendsOfSymfony/UserBundle
  167. -----------------------------------
  168. If you want an easy way to handle users, please use:
  169. - `FOSUserBundle <https://github.com/FriendsOfSymfony/FOSUserBundle>`_: handles
  170. users and groups stored in RDBMS or MongoDB
  171. - `SonataUserBundle <https://github.com/sonata-project/SonataUserBundle>`_: integrates the
  172. ``FriendsOfSymfony/UserBundle`` with the ``AdminBundle``
  173. The security integration is a work in progress and has some known issues:
  174. - ACL permissions are immutables
  175. - A listener must be implemented that creates the object Access Control List
  176. with the required rules if objects are created outside the Admin
  177. Configuration
  178. ~~~~~~~~~~~~~
  179. Before you can use ``FriendsOfSymfony/FOSUserBundle`` you need to set it up as
  180. described in the documentation of the bundle. In step 4 you need to create a
  181. User class (in a custom UserBundle). Do it as follows:
  182. .. code-block:: php
  183. <?php
  184. // src/AppBundle/Entity/User.php
  185. namespace AppBundle\Entity;
  186. use Sonata\UserBundle\Entity\BaseUser as BaseUser;
  187. use Doctrine\ORM\Mapping as ORM;
  188. /**
  189. * @ORM\Entity
  190. * @ORM\Table(name="fos_user")
  191. */
  192. class User extends BaseUser
  193. {
  194. /**
  195. * @ORM\Id
  196. * @ORM\Column(type="integer")
  197. * @ORM\GeneratedValue(strategy="AUTO")
  198. */
  199. protected $id;
  200. public function __construct()
  201. {
  202. parent::__construct();
  203. // your own logic
  204. }
  205. }
  206. In your ``app/config/config.yml`` you then need to put the following:
  207. .. configuration-block::
  208. .. code-block:: yaml
  209. # app/config/config.yml
  210. fos_user:
  211. db_driver: orm
  212. firewall_name: main
  213. user_class: AppBundle\Entity\User
  214. The following configuration for the SonataUserBundle defines:
  215. - the ``FriendsOfSymfony/FOSUserBundle`` as a security provider
  216. - the login form for authentication
  217. - the access control: resources with related required roles, the important
  218. part is the admin configuration
  219. - the ``acl`` option to enable the ACL
  220. - the ``AdminPermissionMap`` defines the permissions of the Admin class
  221. .. configuration-block::
  222. .. code-block:: yaml
  223. # src/AppBundle/Resources/config/services.yml
  224. parameters:
  225. # ...
  226. security.acl.permission.map.class: Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap
  227. # optionally use a custom MaskBuilder
  228. #sonata.admin.security.mask.builder.class: Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder
  229. In ``app/config/security.yml``:
  230. .. configuration-block::
  231. .. code-block:: yaml
  232. # app/config/security.yml
  233. security:
  234. providers:
  235. fos_userbundle:
  236. id: fos_user.user_manager
  237. firewalls:
  238. main:
  239. pattern: .*
  240. form-login:
  241. provider: fos_userbundle
  242. login_path: /login
  243. use_forward: false
  244. check_path: /login_check
  245. failure_path: null
  246. logout: true
  247. anonymous: true
  248. access_control:
  249. # The WDT has to be allowed to anonymous users to avoid requiring the login with the AJAX request
  250. - { path: ^/wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  251. - { path: ^/profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  252. # AsseticBundle paths used when using the controller for assets
  253. - { path: ^/js/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  254. - { path: ^/css/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  255. # URL of FOSUserBundle which need to be available to anonymous users
  256. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  257. - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # for the case of a failed login
  258. - { path: ^/user/new$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  259. - { path: ^/user/check-confirmation-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  260. - { path: ^/user/confirm/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  261. - { path: ^/user/confirmed$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  262. - { path: ^/user/request-reset-password$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  263. - { path: ^/user/send-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  264. - { path: ^/user/check-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  265. - { path: ^/user/reset-password/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  266. # Secured part of the site
  267. # This config requires being logged for the whole site and having the admin role for the admin part.
  268. # Change these rules to adapt them to your needs
  269. - { path: ^/admin/, role: ROLE_ADMIN }
  270. - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  271. role_hierarchy:
  272. ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
  273. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  274. acl:
  275. connection: default
  276. - Install the ACL tables ``php app/console init:acl``
  277. - Create a new root user:
  278. .. code-block:: bash
  279. $ php app/console fos:user:create --super-admin
  280. Please choose a username:root
  281. Please choose an email:root@domain.com
  282. Please choose a password:root
  283. Created user root
  284. If you have Admin classes, you can install or update the related CRUD ACL rules:
  285. .. code-block:: bash
  286. $ php app/console sonata:admin:setup-acl
  287. Starting ACL AdminBundle configuration
  288. > install ACL for sonata.media.admin.media
  289. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_GUEST, permissions: ["VIEW","LIST"]
  290. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_STAFF, permissions: ["EDIT","LIST","CREATE"]
  291. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_EDITOR, permissions: ["OPERATOR","EXPORT"]
  292. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_ADMIN, permissions: ["MASTER"]
  293. ... skipped ...
  294. If you already have objects, you can generate the object ACL rules for each
  295. object of an admin:
  296. .. code-block:: bash
  297. $ php app/console sonata:admin:generate-object-acl
  298. Optionally, you can specify an object owner, and step through each admin. See
  299. the help of the command for more information.
  300. If you try to access to the admin class you should see the login form, just
  301. log in with the ``root`` user.
  302. An Admin is displayed in the dashboard (and menu) when the user has the role
  303. ``LIST``. To change this override the ``showIn`` method in the Admin class.
  304. Roles and Access control lists
  305. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  306. A user can have several roles when working with an application. Each Admin class
  307. has several roles, and each role specifies the permissions of the user for the
  308. ``Admin`` class. Or more specifically, what the user can do with the domain object(s)
  309. the ``Admin`` class is created for.
  310. By default each ``Admin`` class contains the following roles, override the
  311. property ``$securityInformation`` to change this:
  312. - ``ROLE_SONATA_..._GUEST``
  313. a guest that is allowed to ``VIEW`` an object and a ``LIST`` of objects;
  314. - ``ROLE_SONATA_..._STAFF``
  315. probably the biggest part of the users, a staff user has the same permissions
  316. as guests and is additionally allowed to ``EDIT`` and ``CREATE`` new objects;
  317. - ``ROLE_SONATA_..._EDITOR``
  318. an editor is granted all access and, compared to the staff users, is allowed to ``DELETE``;
  319. - ``ROLE_SONATA_..._ADMIN``
  320. an administrative user is granted all access and on top of that, the user is allowed to grant other users access.
  321. Owner:
  322. - when an object is created, the currently logged in user is set as owner for
  323. that object and is granted all access for that object;
  324. - this means the user owning the object is always allowed to ``DELETE`` the
  325. object, even when they only have the staff role.
  326. Vocabulary used for Access Control Lists:
  327. - **Role:** a user role;
  328. - **ACL:** a list of access rules, the Admin uses 2 types;
  329. - **Admin ACL:** created from the Security information of the Admin class
  330. for each admin and shares the Access Control Entries that specify what
  331. the user can do (permissions) with the admin;
  332. - **Object ACL:** also created from the security information of the ``Admin``
  333. class however created for each object, it uses 2 scopes:
  334. - **Class-Scope:** the class scope contains the rules that are valid
  335. for all object of a certain class;
  336. - **Object-Scope:** specifies the owner;
  337. - **Sid:** Security identity, an ACL role for the Class-Scope ACL and the
  338. user for the Object-Scope ACL;
  339. - **Oid:** Object identity, identifies the ACL, for the admin ACL this is
  340. the admin code, for the object ACL this is the object id;
  341. - **ACE:** a role (or sid) and its permissions;
  342. - **Permission:** this tells what the user is allowed to do with the Object
  343. identity;
  344. - **Bitmask:** a permission can have several bitmasks, each bitmask
  345. represents a permission. When permission ``VIEW`` is requested and it
  346. contains the ``VIEW`` and ``EDIT`` bitmask and the user only has the
  347. ``EDIT`` permission, then the permission ``VIEW`` is granted.
  348. - **PermissionMap:** configures the bitmasks for each permission, to change
  349. the default mapping create a voter for the domain class of the Admin.
  350. There can be many voters that may have different permission maps. However,
  351. prevent that multiple voters vote on the same class with overlapping bitmasks.
  352. See the cookbook article "`Advanced ACL concepts
  353. <http://symfony.com/doc/current/cookbook/security/acl_advanced.html#pre-authorization-decisions.>`_"
  354. for the meaning of the different permissions.
  355. How is access granted?
  356. ~~~~~~~~~~~~~~~~~~~~~~
  357. In the application the security context is asked if access is granted for a role
  358. or a permission (``admin.isGranted``):
  359. - **Token:** a token identifies a user between requests;
  360. - **Voter:** sort of judge that returns whether access is granted or denied, if the
  361. voter should not vote for a case, it returns abstain;
  362. - **AccessDecisionManager:** decides whether access is granted or denied according
  363. a specific strategy. It grants access if at least one (affirmative strategy),
  364. all (unanimous strategy) or more then half (consensus strategy) of the
  365. counted votes granted access;
  366. - **RoleVoter:** votes for all attributes stating with ``ROLE_`` and grants
  367. access if the user has this role;
  368. - **RoleHierarchyVoter:** when the role ``ROLE_SONATA_ADMIN`` is voted for,
  369. it also votes "granted" if the user has the role ``ROLE_SUPER_ADMIN``;
  370. - **AclVoter:** grants access for the permissions of the ``Admin`` class if
  371. the user has the permission, the user has a permission that is included in
  372. the bitmasks of the permission requested to vote for or the user owns the
  373. object.
  374. Create a custom voter or a custom permission map
  375. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  376. In some occasions you need to create a custom voter or a custom permission map
  377. because for example you want to restrict access using extra rules:
  378. - create a custom voter class that extends the ``AclVoter``
  379. .. code-block:: php
  380. <?php
  381. // src/AppBundle/Security/Authorization/Voter/UserAclVoter.php
  382. namespace AppBundle\Security\Authorization\Voter;
  383. use FOS\UserBundle\Model\UserInterface;
  384. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  385. use Symfony\Component\Security\Acl\Voter\AclVoter;
  386. class UserAclVoter extends AclVoter
  387. {
  388. public function supportsClass($class)
  389. {
  390. // support the Class-Scope ACL for votes with the custom permission map
  391. // return $class === 'Sonata\UserBundle\Admin\Entity\UserAdmin' || is_subclass_of($class, 'FOS\UserBundle\Model\UserInterface');
  392. // if you use php >=5.3.7 you can check the inheritance with is_a($class, 'Sonata\UserBundle\Admin\Entity\UserAdmin');
  393. // support the Object-Scope ACL
  394. return is_subclass_of($class, 'FOS\UserBundle\Model\UserInterface');
  395. }
  396. public function supportsAttribute($attribute)
  397. {
  398. return $attribute === 'EDIT' || $attribute === 'DELETE';
  399. }
  400. public function vote(TokenInterface $token, $object, array $attributes)
  401. {
  402. if (!$this->supportsClass(get_class($object))) {
  403. return self::ACCESS_ABSTAIN;
  404. }
  405. foreach ($attributes as $attribute) {
  406. if ($this->supportsAttribute($attribute) && $object instanceof UserInterface) {
  407. if ($object->isSuperAdmin() && !$token->getUser()->isSuperAdmin()) {
  408. // deny a non super admin user to edit a super admin user
  409. return self::ACCESS_DENIED;
  410. }
  411. }
  412. }
  413. // use the parent vote with the custom permission map:
  414. // return parent::vote($token, $object, $attributes);
  415. // otherwise leave the permission voting to the AclVoter that is using the default permission map
  416. return self::ACCESS_ABSTAIN;
  417. }
  418. }
  419. - optionally create a custom permission map, copy to start the
  420. ``Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap.php`` to
  421. your bundle
  422. - declare the voter and permission map as a service
  423. .. configuration-block::
  424. .. code-block:: xml
  425. <!-- src/AppBundle/Resources/config/services.xml -->
  426. <!-- <service id="security.acl.user_permission.map" class="AppBundle\Security\Acl\Permission\UserAdminPermissionMap" public="false"></service> -->
  427. <service id="security.acl.voter.user_permissions" class="AppBundle\Security\Authorization\Voter\UserAclVoter" public="false">
  428. <tag name="monolog.logger" channel="security" />
  429. <argument type="service" id="security.acl.provider" />
  430. <argument type="service" id="security.acl.object_identity_retrieval_strategy" />
  431. <argument type="service" id="security.acl.security_identity_retrieval_strategy" />
  432. <argument type="service" id="security.acl.permission.map" />
  433. <argument type="service" id="logger" on-invalid="null" />
  434. <tag name="security.voter" priority="255" />
  435. </service>
  436. - change the access decision strategy to ``unanimous``
  437. .. configuration-block::
  438. .. code-block:: yaml
  439. # app/config/security.yml
  440. security:
  441. access_decision_manager:
  442. # strategy value can be: affirmative, unanimous or consensus
  443. strategy: unanimous
  444. - to make this work the permission needs to be checked using the Object ACL
  445. - modify the template (or code) where applicable:
  446. .. code-block:: html+jinja
  447. {% if admin.isGranted('EDIT', user_object) %}
  448. {# ... #}
  449. {% endif %}
  450. - because the object ACL permission is checked, the ACL for the object must
  451. have been created, otherwise the ``AclVoter`` will deny ``EDIT`` access
  452. for a non super admin user trying to edit another non super admin user.
  453. This is automatically done when the object is created using the Admin.
  454. If objects are also created outside the Admin, have a look at the
  455. ``createSecurityObject`` method in the ``AclSecurityHandler``.
  456. Usage
  457. ~~~~~
  458. Every time you create a new ``Admin`` class, you should start with the command
  459. ``php app/console sonata:admin:setup-acl`` so the ACL database will be updated
  460. with the latest roles and permissions.
  461. In the templates, or in your code, you can use the Admin method ``isGranted()``:
  462. - check for an admin that the user is allowed to ``EDIT``:
  463. .. code-block:: html+jinja
  464. {# use the admin security method #}
  465. {% if admin.isGranted('EDIT') %}
  466. {# ... #}
  467. {% endif %}
  468. {# or use the default is_granted Symfony helper, the following will give the same result #}
  469. {% if is_granted('ROLE_SUPER_ADMIN') or is_granted('EDIT', admin) %}
  470. {# ... #}
  471. {% endif %}
  472. - check for an admin that the user is allowed to ``DELETE``, the object is added
  473. to also check if the object owner is allowed to ``DELETE``:
  474. .. code-block:: html+jinja
  475. {# use the admin security method #}
  476. {% if admin.isGranted('DELETE', object) %}
  477. {# ... #}
  478. {% endif %}
  479. {# or use the default is_granted Symfony helper, the following will give the same result #}
  480. {% if is_granted('ROLE_SUPER_ADMIN') or is_granted('DELETE', object) %}
  481. {# ... #}
  482. {% endif %}
  483. List filtering
  484. ~~~~~~~~~~~~~~
  485. List filtering using ACL is available as a third party bundle:
  486. `CoopTilleulsAclSonataAdminExtensionBundle <https://github.com/coopTilleuls/CoopTilleulsAclSonataAdminExtensionBundle>`_.
  487. When enabled, the logged in user will only see the objects for which it has the `VIEW` right (or superior).
  488. ACL editor
  489. ----------
  490. SonataAdminBundle provides a user-friendly ACL editor
  491. interface.
  492. It will be automatically available if the ``sonata.admin.security.handler.acl``
  493. security handler is used and properly configured.
  494. The ACL editor is only available for users with `OWNER` or `MASTER` permissions
  495. on the object instance.
  496. The `OWNER` and `MASTER` permissions can only be edited by an user with the
  497. `OWNER` permission on the object instance.
  498. .. figure:: ../images/acl_editor.png
  499. :align: center
  500. :alt: The ACL editor
  501. :width: 700px
  502. User list customization
  503. ~~~~~~~~~~~~~~~~~~~~~~~
  504. By default, the ACL editor allows to set permissions for all users managed by
  505. ``FOSUserBundle``.
  506. To customize displayed user override
  507. ``Sonata\AdminBundle\Controller\CRUDController::getAclUsers()``. This method must
  508. return an iterable collection of users.
  509. .. code-block:: php
  510. protected function getAclUsers()
  511. {
  512. $userManager = $container->get('fos_user.user_manager');
  513. // Display only kevin and anne
  514. $users[] = $userManager->findUserByUsername('kevin');
  515. $users[] = $userManager->findUserByUsername('anne');
  516. return new \ArrayIterator($users);
  517. }
  518. Role list customization
  519. ~~~~~~~~~~~~~~~~~~~~~~~
  520. By default, the ACL editor allows to set permissions for all roles.
  521. To customize displayed role override
  522. ``Sonata\AdminBundle\Controller\CRUDController::getAclRoles()``. This method must
  523. return an iterable collection of roles.
  524. .. code-block:: php
  525. protected function getAclRoles()
  526. {
  527. // Display only ROLE_BAPTISTE and ROLE_HELENE
  528. $roles = array(
  529. 'ROLE_BAPTISTE',
  530. 'ROLE_HELENE'
  531. );
  532. return new \ArrayIterator($roles);
  533. }
  534. Custom user manager
  535. ~~~~~~~~~~~~~~~~~~~
  536. If your project does not use `FOSUserBundle`, you can globally configure another
  537. service to use when retrieving your users.
  538. - Create a service with a method called ``findUsers()`` returning an iterable
  539. collection of users
  540. - Update your admin configuration to reference your service name
  541. .. configuration-block::
  542. .. code-block:: yaml
  543. # app/config/config.yml
  544. sonata_admin:
  545. security:
  546. # the name of your service
  547. acl_user_manager: my_user_manager
  548. .. _`SonataUserBundle's documentation area`: https://sonata-project.org/bundles/user/master/doc/reference/installation.html
  549. .. _`changing the access decision strategy`: http://symfony.com/doc/2.2/cookbook/security/voters.html#changing-the-access-decision-strategy
  550. .. _`create your own voter`: http://symfony.com/doc/2.2/cookbook/security/voters.html