AclSecurityHandler.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Security\Handler;
  11. use Symfony\Component\Security\Core\SecurityContextInterface;
  12. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  13. use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
  14. use Symfony\Component\Security\Acl\Model\AclInterface;
  15. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  16. use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
  17. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  18. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  19. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  20. use Sonata\AdminBundle\Admin\AdminInterface;
  21. class AclSecurityHandler implements AclSecurityHandlerInterface
  22. {
  23. protected $securityContext;
  24. protected $aclProvider;
  25. protected $superAdminRoles;
  26. protected $adminPermissions;
  27. protected $objectPermissions;
  28. protected $maskBuilderClass;
  29. /**
  30. * @param SecurityContextInterface $securityContext
  31. * @param AclProviderInterface $aclProvider
  32. * @param string $maskBuilderClass
  33. * @param array $superAdminRoles
  34. */
  35. public function __construct(SecurityContextInterface $securityContext, MutableAclProviderInterface $aclProvider, $maskBuilderClass, array $superAdminRoles)
  36. {
  37. $this->securityContext = $securityContext;
  38. $this->aclProvider = $aclProvider;
  39. $this->maskBuilderClass = $maskBuilderClass;
  40. $this->superAdminRoles = $superAdminRoles;
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function setAdminPermissions(array $permissions)
  46. {
  47. $this->adminPermissions = $permissions;
  48. }
  49. /**
  50. * {@inheritDoc}
  51. */
  52. public function getAdminPermissions()
  53. {
  54. return $this->adminPermissions;
  55. }
  56. /**
  57. * {@inheritDoc}
  58. */
  59. public function setObjectPermissions(array $permissions)
  60. {
  61. $this->objectPermissions = $permissions;
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function getObjectPermissions()
  67. {
  68. return $this->objectPermissions;
  69. }
  70. /**
  71. * {@inheritDoc}
  72. */
  73. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  74. {
  75. if (!is_array($attributes)) {
  76. $attributes = array($attributes);
  77. }
  78. try {
  79. return $this->securityContext->isGranted($this->superAdminRoles) || $this->securityContext->isGranted($attributes, $object);
  80. } catch (AuthenticationCredentialsNotFoundException $e) {
  81. return false;
  82. } catch (\Exception $e) {
  83. throw $e;
  84. }
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function getBaseRole(AdminInterface $admin)
  90. {
  91. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function buildSecurityInformation(AdminInterface $admin)
  97. {
  98. $baseRole = $this->getBaseRole($admin);
  99. $results = array();
  100. foreach ($admin->getSecurityInformation() as $role => $permissions) {
  101. $results[sprintf($baseRole, $role)] = $permissions;
  102. }
  103. return $results;
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function createObjectSecurity(AdminInterface $admin, $object)
  109. {
  110. // retrieving the ACL for the object identity
  111. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  112. $acl = $this->getObjectAcl($objectIdentity);
  113. if (is_null($acl)) {
  114. $acl = $this->createAcl($objectIdentity);
  115. }
  116. // retrieving the security identity of the currently logged-in user
  117. $user = $this->securityContext->getToken()->getUser();
  118. $securityIdentity = UserSecurityIdentity::fromAccount($user);
  119. $this->addObjectOwner($acl, $securityIdentity);
  120. $this->addObjectClassAces($acl, $this->buildSecurityInformation($admin));
  121. $this->updateAcl($acl);
  122. }
  123. /**
  124. * {@inheritDoc}
  125. */
  126. public function deleteObjectSecurity(AdminInterface $admin, $object)
  127. {
  128. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  129. $this->deleteAcl($objectIdentity);
  130. }
  131. /**
  132. * {@inheritDoc}
  133. */
  134. public function getObjectAcl(ObjectIdentityInterface $objectIdentity)
  135. {
  136. try {
  137. $acl = $this->aclProvider->findAcl($objectIdentity);
  138. } catch(AclNotFoundException $e) {
  139. return null;
  140. }
  141. return $acl;
  142. }
  143. /**
  144. * {@inheritDoc}
  145. */
  146. public function findObjectAcls(array $oids, array $sids = array())
  147. {
  148. try {
  149. $acls = $this->aclProvider->findAcls($oids, $sids);
  150. } catch(\Exception $e) {
  151. if ($e instanceof NotAllAclsFoundException) {
  152. $acls = $e->getPartialResult();
  153. } elseif ($e instanceof AclNotFoundException) {
  154. // if only one oid, this error is thrown
  155. $acls = new \SplObjectStorage();
  156. } else {
  157. throw $e;
  158. }
  159. }
  160. return $acls;
  161. }
  162. /**
  163. * {@inheritDoc}
  164. */
  165. public function addObjectOwner(AclInterface $acl, UserSecurityIdentity $securityIdentity = null)
  166. {
  167. if (false === $this->findClassAceIndexByUsername($acl, $securityIdentity->getUsername())) {
  168. // only add if not already exists
  169. $acl->insertObjectAce($securityIdentity, constant("$this->maskBuilderClass::MASK_OWNER"));
  170. }
  171. }
  172. /**
  173. * {@inheritDoc}
  174. */
  175. public function addObjectClassAces(AclInterface $acl, array $roleInformation = array())
  176. {
  177. $builder = new $this->maskBuilderClass();
  178. foreach ($roleInformation as $role => $permissions) {
  179. $aceIndex = $this->findClassAceIndexByRole($acl, $role);
  180. $hasRole = false;
  181. foreach ($permissions as $permission) {
  182. // add only the object permissions
  183. if (in_array($permission, $this->getObjectPermissions())) {
  184. $builder->add($permission);
  185. $hasRole = true;
  186. }
  187. }
  188. if ($hasRole) {
  189. if ($aceIndex === false) {
  190. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  191. } else {
  192. $acl->updateClassAce($aceIndex, $builder->get());
  193. }
  194. $builder->reset();
  195. } elseif ($aceIndex !== false) {
  196. $acl->deleteClassAce($aceIndex);
  197. }
  198. }
  199. }
  200. /**
  201. * {@inheritDoc}
  202. */
  203. public function createAcl(ObjectIdentityInterface $objectIdentity)
  204. {
  205. return $this->aclProvider->createAcl($objectIdentity);
  206. }
  207. /**
  208. * {@inheritDoc}
  209. */
  210. public function updateAcl(AclInterface $acl)
  211. {
  212. $this->aclProvider->updateAcl($acl);
  213. }
  214. /**
  215. * {@inheritDoc}
  216. */
  217. public function deleteAcl(ObjectIdentityInterface $objectIdentity)
  218. {
  219. $this->aclProvider->deleteAcl($objectIdentity);
  220. }
  221. /**
  222. * {@inheritDoc}
  223. */
  224. public function findClassAceIndexByRole(AclInterface $acl, $role)
  225. {
  226. foreach ($acl->getClassAces() as $index => $entry) {
  227. if ($entry->getSecurityIdentity() instanceof RoleSecurityIdentity && $entry->getSecurityIdentity()->getRole() === $role) {
  228. return $index;
  229. }
  230. }
  231. return false;
  232. }
  233. /**
  234. * {@inheritDoc}
  235. */
  236. public function findClassAceIndexByUsername(AclInterface $acl, $username)
  237. {
  238. foreach ($acl->getClassAces() as $index => $entry) {
  239. if ($entry->getSecurityIdentity() instanceof UserSecurityIdentity && $entry->getSecurityIdentity()->getUsername() === $username) {
  240. return $index;
  241. }
  242. }
  243. return false;
  244. }
  245. }