security.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 comes with 3 handlers
  21. - ``sonata.admin.security.handler.role`` : ROLES to handle permissions
  22. - ``sonata.admin.security.handler.acl`` : ACL and ROLES to handle permissions
  23. - ``sonata.admin.security.handler.noop`` : always returns true, can be used with the Symfony2 firewall
  24. The default value is ``sonata.admin.security.handler.noop``, if you want to change the default value
  25. you can set the ``security_handler`` to ``sonata.admin.security.handler.acl`` or ``sonata.admin.security.handler.role``.
  26. To quickly secure an admin the role security can be used. It allows to specify the actions a user can with the admin. The ACL
  27. security system is more advanced and allows to secure the objects. For people using the previous ACL implementation,
  28. you can switch the security_handler to the role security handler.
  29. Configuration
  30. ~~~~~~~~~~~~~
  31. Only the security handler is required to determine which type of security to use. The other parameters are set as default,
  32. change them if needed.
  33. Using roles:
  34. .. code-block:: yaml
  35. sonata_admin:
  36. security:
  37. handler: sonata.admin.security.handler.role
  38. # role security information
  39. information:
  40. EDIT: EDIT
  41. LIST: LIST
  42. CREATE: CREATE
  43. VIEW: VIEW
  44. DELETE: DELETE
  45. OPERATOR: OPERATOR
  46. MASTER: MASTER
  47. Using ACL:
  48. .. code-block:: yaml
  49. # app/config/config.yml
  50. sonata_admin:
  51. security:
  52. handler: sonata.admin.security.handler.acl
  53. # acl security information
  54. information:
  55. GUEST: [VIEW, LIST]
  56. STAFF: [EDIT, LIST, CREATE]
  57. EDITOR: [OPERATOR]
  58. ADMIN: [MASTER]
  59. # permissions not related to an object instance and also to be available when objects do not exist
  60. # the DELETE admin permission means the user is allowed to batch delete objects
  61. admin_permissions: [CREATE, LIST, DELETE, UNDELETE, OPERATOR, MASTER]
  62. # permission related to the objects
  63. object_permissions: [VIEW, EDIT, DELETE, UNDELETE, OPERATOR, MASTER, OWNER]
  64. The following section explains how to set up ACL with the ``FriendsOfSymfony/UserBundle``.
  65. ACL and FriendsOfSymfony/UserBundle
  66. -----------------------------------
  67. If you want an easy way to handle users, please use :
  68. - https://github.com/FriendsOfSymfony/FOSUserBundle : handle users and groups stored in RDMS or MongoDB
  69. - https://github.com/sonata-project/SonataUserBundle : integrates the ``FriendsOfSymfony/UserBundle`` with
  70. the ``AdminBundle``
  71. The security integration is a work in progress and has some known issues :
  72. - ACL permissions are immutables
  73. - A listener must be implemented that creates the object Access Control List with the required rules if objects are
  74. created outside the Admin
  75. Configuration
  76. ~~~~~~~~~~~~~
  77. Before you can use ``FriendsOfSymfony/FOSUserBundle`` you need to set it up as described in the documentation
  78. of the bundle. In step 4 you need to create a User class (in a custom UserBundle). Do it as follows:
  79. .. code-block:: php
  80. <?php
  81. namespace Acme\UserBundle\Entity;
  82. use Sonata\UserBundle\Entity\BaseUser as BaseUser;
  83. use Doctrine\ORM\Mapping as ORM;
  84. /**
  85. * @ORM\Entity
  86. * @ORM\Table(name="fos_user")
  87. \*/
  88. class User extends BaseUser
  89. {
  90. /**
  91. * @ORM\Id
  92. * @ORM\Column(type="integer")
  93. * @ORM\GeneratedValue(strategy="AUTO")
  94. \*/
  95. protected $id;
  96. public function __construct()
  97. {
  98. parent::__construct();
  99. // your own logic
  100. }
  101. }
  102. In your ``app/config/config.yml`` you then need to put the following:
  103. .. code-block:: yaml
  104. fos_user:
  105. db_driver: orm
  106. firewall_name: main
  107. user_class: Acme\UserBundle\Entity\User
  108. The following configuration for the SonataUserBundle defines:
  109. - the ``FriendsOfSymfony/FOSUserBundle`` as a security provider
  110. - the login form for authentification
  111. - the access control : resources with related required roles, the important part is the admin configuration
  112. - the ``acl`` option to enable the ACL.
  113. - the ``AdminPermissionMap`` defines the permissions of the Admin class
  114. .. code-block:: yaml
  115. # app/config/security.yml
  116. parameters:
  117. # ... other parameters
  118. security.acl.permission.map.class: Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap
  119. # optionally use a custom MaskBuilder
  120. #sonata.admin.security.mask.builder.class: Sonata\AdminBundle\Security\Acl\Permission\MaskBuilder
  121. In ``app/config/security.yml``:
  122. .. code-block:: yaml
  123. security:
  124. providers:
  125. fos_userbundle:
  126. id: fos_user.user_manager
  127. firewalls:
  128. main:
  129. pattern: .*
  130. form-login:
  131. provider: fos_userbundle
  132. login_path: /login
  133. use_forward: false
  134. check_path: /login_check
  135. failure_path: null
  136. logout: true
  137. anonymous: true
  138. access_control:
  139. # The WDT has to be allowed to anonymous users to avoid requiring the login with the AJAX request
  140. - { path: ^/wdt/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  141. - { path: ^/profiler/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  142. # AsseticBundle paths used when using the controller for assets
  143. - { path: ^/js/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  144. - { path: ^/css/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  145. # URL of FOSUserBundle which need to be available to anonymous users
  146. - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  147. - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY } # for the case of a failed login
  148. - { path: ^/user/new$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  149. - { path: ^/user/check-confirmation-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  150. - { path: ^/user/confirm/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  151. - { path: ^/user/confirmed$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  152. - { path: ^/user/request-reset-password$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  153. - { path: ^/user/send-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  154. - { path: ^/user/check-resetting-email$, role: IS_AUTHENTICATED_ANONYMOUSLY }
  155. - { path: ^/user/reset-password/, role: IS_AUTHENTICATED_ANONYMOUSLY }
  156. # Secured part of the site
  157. # This config requires being logged for the whole site and having the admin role for the admin part.
  158. # Change these rules to adapt them to your needs
  159. - { path: ^/admin/, role: ROLE_ADMIN }
  160. - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }
  161. role_hierarchy:
  162. ROLE_ADMIN: [ROLE_USER, ROLE_SONATA_ADMIN]
  163. ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
  164. acl:
  165. connection: default
  166. - Install the ACL tables ``php app/console init:acl``
  167. - Create a new root user :
  168. .. code-block:: sh
  169. # php app/console fos:user:create --super-admin
  170. Please choose a username:root
  171. Please choose an email:root@domain.com
  172. Please choose a password:root
  173. Created user root
  174. If you have Admin classes, you can install or update the related CRUD ACL rules :
  175. .. code-block:: sh
  176. # php app/console sonata:admin:setup-acl
  177. Starting ACL AdminBundle configuration
  178. > install ACL for sonata.media.admin.media
  179. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_GUEST, permissions: ["VIEW","LIST"]
  180. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_STAFF, permissions: ["EDIT","LIST","CREATE"]
  181. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_EDITOR, permissions: ["OPERATOR"]
  182. - add role: ROLE_SONATA_MEDIA_ADMIN_MEDIA_ADMIN, permissions: ["MASTER"]
  183. ... skipped ...
  184. If you already have objects, you can generate the object ACL rules for each object of an admin:
  185. .. code-block:: sh
  186. $ php app/console sonata:admin:generate-object-acl
  187. Optionally, you can specify an object owner, and step through each admin. See the help of the command for more information.
  188. If you try to access to the admin class you should see the login form, just logon with the ``root`` user.
  189. An Admin is displayed in the dashboard (and menu) when the user has the role ``LIST``. To change this override the ``showIn``
  190. method in the Admin class.
  191. Roles and Access control lists
  192. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  193. A user can have several roles when working with an application. Each Admin class has several roles, and each role specifies the permissions
  194. of the user for the Admin class. Or more specific, what the user can do with the domain object(s) the Admin class is created for.
  195. By default each Admin class contains the following roles, override the property ``$securityInformation`` to change this:
  196. - ROLE_SONATA_..._GUEST: a guest that is allowed to view an object and a list of objects;
  197. - ROLE_SONATA_..._STAFF: probably the biggest part of the users, a staff user has the same permissions as guests and is additionally
  198. allowed to EDIT and CREATE new objects;
  199. - ROLE_SONATA_..._EDITOR: an editor is granted all access and, compared to the staff users, is allowed to DELETE;
  200. - ROLE_SONATA_..._ADMIN: an administrative user is granted all access and on top of that, the user is allowed to grant other users access.
  201. Owner:
  202. - when an object is created, the currently logged in user is set as owner for that object and is granted all access for that object;
  203. - this means the user owning the object is always allowed to DELETE the object, even when it only has the staff role.
  204. Vocabulary used for Access Control Lists:
  205. - Role: a user role;
  206. - ACL: a list of access rules, the Admin uses 2 types:
  207. - Admin ACL: created from the Security information of the Admin class for each admin and shares the Access Control Entries that specify
  208. what the user can do (permissions) with the admin
  209. - Object ACL: also created from the security information of the Admin class however created for each object, it uses 2 scopes:
  210. - Class-Scope: the class scope contains the rules that are valid for all object of a certain class;
  211. - Object-Scope: specifies the owner;
  212. - Sid: Security identity, an ACL role for the Class-Scope ACL and the user for the Object-Scope ACL;
  213. - Oid: Object identity, identifies the ACL, for the admin ACL this is the admin code, for the object ACL this is the object id;
  214. - ACE: a role (or sid) and its permissions;
  215. - Permission: this tells what the user is allowed to do with the Object identity;
  216. - Bitmask: a permission can have several bitmasks, each bitmask represents a permission. When permission VIEW is requested and
  217. it contains the VIEW and EDIT bitmask and the user only has the EDIT permission, then the permission VIEW is granted.
  218. - PermissionMap: configures the bitmasks for each permission, to change the default mapping create a voter for the domain class of the Admin.
  219. There can be many voters that may have different permission maps. However, prevent that multiple voters vote on the same class with
  220. overlapping bitmasks.
  221. See the cookbook article "Advanced ACL concepts" for the meaning of the different permissions:
  222. http://symfony.com/doc/current/cookbook/security/acl_advanced.html#pre-authorization-decisions.
  223. How is access granted?
  224. ~~~~~~~~~~~~~~~~~~~~~~
  225. In the application the security context is asked if access is granted for a role or a permission (admin.isGranted):
  226. - Token: a token identifies a user between requests;
  227. - Voter: sort of judge that returns if access is granted of denied, if the voter should not vote for a case, it returns abstrain;
  228. - AccessDecisionManager: decides if access is granted or denied according a specific strategy. It grants access if at least one (affirmative
  229. strategy), all (unanimous strategy) or more then half (consensus strategy) of the counted votes granted access;
  230. - RoleVoter: votes for all attributes stating with "ROLE_" and grants access if the user has this role;
  231. - RoleHierarchieVoter: when the role ROLE_SONATA_ADMIN is voted for, it also votes "granted" if the user has the role ROLE_SUPER_ADMIN;
  232. - AclVoter: grants access for the permissions of the Admin class if the user has the permission, the user has a permission that is
  233. included in the bitmasks of the permission requested to vote for or the user owns the object.
  234. Create a custom voter or a custom permission map
  235. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  236. In some occasions you need to create a custom voter or a custom permission map because for example you want to restrict access using extra rules:
  237. - create a custom voter class that extends the AclVoter
  238. .. code-block:: php
  239. namespace Acme\DemoBundle\Security\Authorization\Voter;
  240. use FOS\UserBundle\Model\UserInterface;
  241. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  242. use Symfony\Component\Security\Acl\Voter\AclVoter;
  243. class UserAclVoter extends AclVoter
  244. {
  245. /**
  246. * {@InheritDoc}
  247. */
  248. public function supportsClass($class)
  249. {
  250. // support the Class-Scope ACL for votes with the custom permission map
  251. // return $class === 'Sonata\UserBundle\Admin\Entity\UserAdmin' || $is_subclass_of($class, 'FOS\UserBundle\Model\UserInterface');
  252. // if you use php >=5.3.7 you can check the inheritance with is_a($class, 'Sonata\UserBundle\Admin\Entity\UserAdmin');
  253. // support the Object-Scope ACL
  254. return is_subclass_of($class, 'FOS\UserBundle\Model\UserInterface');
  255. }
  256. public function supportsAttribute($attribute)
  257. {
  258. return $attribute === 'EDIT' || $attribute === 'DELETE';
  259. }
  260. public function vote(TokenInterface $token, $object, array $attributes)
  261. {
  262. if (!$this->supportsClass(get_class($object))) {
  263. return self::ACCESS_ABSTAIN;
  264. }
  265. foreach ($attributes as $attribute) {
  266. if ($this->supportsAttribute($attribute) && $object instanceof UserInterface) {
  267. if ($object->isSuperAdmin() && !$token->getUser()->isSuperAdmin()) {
  268. // deny a non super admin user to edit a super admin user
  269. return self::ACCESS_DENIED;
  270. }
  271. }
  272. }
  273. // use the parent vote with the custom permission map:
  274. // return parent::vote($token, $object, $attributes);
  275. // otherwise leave the permission voting to the AclVoter that is using the default permission map
  276. return self::ACCESS_ABSTAIN;
  277. }
  278. }
  279. - optionally create a custom permission map, copy to start the Sonata\AdminBundle\Security\Acl\Permission\AdminPermissionMap.php to your bundle
  280. - declare the voter and permission map as a service
  281. .. code-block:: xml
  282. <!-- src/Acme/DemoBundle/Resources/config/services.xml -->
  283. <parameters>
  284. <parameter key="security.acl.user_voter.class">Acme\DemoBundle\Security\Authorization\Voter\UserAclVoter</parameter>
  285. <!-- <parameter key="security.acl.user_permission.map.class">Acme\DemoBundle\Security\Acl\Permission\UserAdminPermissionMap</parameter> -->
  286. </parameters>
  287. <services>
  288. <!-- <service id="security.acl.user_permission.map" class="%security.acl.permission.map.class%" public="false"></service> -->
  289. <service id="security.acl.voter.user_permissions" class="%security.acl.user_voter.class%" public="false">
  290. <tag name="monolog.logger" channel="security" />
  291. <argument type="service" id="security.acl.provider" />
  292. <argument type="service" id="security.acl.object_identity_retrieval_strategy" />
  293. <argument type="service" id="security.acl.security_identity_retrieval_strategy" />
  294. <argument type="service" id="security.acl.permission.map" />
  295. <argument type="service" id="logger" on-invalid="null" />
  296. <tag name="security.voter" priority="255" />
  297. </service>
  298. </services>
  299. - change the access decission strategy to ``unanimous``
  300. .. code-block:: yaml
  301. # app/config/security.yml
  302. security:
  303. access_decision_manager:
  304. # Strategy can be: affirmative, unanimous or consensus
  305. strategy: unanimous
  306. - to make this work the permission needs to be checked using the Object ACL
  307. - modify the template (or code) where applicable:
  308. .. code-block:: html
  309. {% if admin.isGranted('EDIT', user_object) %} {# ... #} {% endif %}
  310. - because the object ACL permission is checked, the ACL for the object must have been created, otherwise the AclVoter
  311. will deny EDIT access for a non super admin user trying to edit another non super admin user. This is automatically done when the object is
  312. created using the Admin. If objects are also created outside the Admin, have a look at the ``createSecurityObject`` method in the
  313. AclSecurityHandler.
  314. Usage
  315. ~~~~~
  316. Everytime you create a new ``Admin`` class, you should create start the command ``php app/console sonata:admin:setup-acl``
  317. so the ACL database will be updated with the latest roles and permissions.
  318. In the templates, or in your code, you can use the Admin method ``isGranted``:
  319. - check for an admin that the user is allowed to EDIT:
  320. .. code-block:: html
  321. {# use the admin security method #}
  322. {% if admin.isGranted('EDIT') %} {# ... #} {% endif %}
  323. {# or use the default is_granted symfony helper, the following will give the same result #}
  324. {% if is_granted('ROLE_SUPER_ADMIN') or is_granted('EDIT', admin) %} {# ... #} {% endif %}
  325. - check for an admin that the user is allowed to DELETE, the object is added to also check if the object owner is allowed to DELETE:
  326. .. code-block:: html
  327. {# use the admin security method #}
  328. {% if admin.isGranted('DELETE', object) %} {# ... #} {% endif %}
  329. {# or use the default is_granted symfony helper, the following will give the same result #}
  330. {% if is_granted('ROLE_SUPER_ADMIN') or is_granted('DELETE', object) %} {# ... #} {% endif %}