AclSecurityHandler.php 8.3 KB

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