security.rst 27 KB

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