AclSecurityHandler.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  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 Sonata\AdminBundle\Admin\AdminInterface;
  12. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  13. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  14. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  15. use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
  16. use Symfony\Component\Security\Acl\Exception\NotAllAclsFoundException;
  17. use Symfony\Component\Security\Acl\Model\AclInterface;
  18. use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
  19. use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
  20. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  21. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  22. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  23. use Symfony\Component\Security\Core\SecurityContextInterface;
  24. /**
  25. * Class AclSecurityHandler.
  26. *
  27. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  28. */
  29. class AclSecurityHandler implements AclSecurityHandlerInterface
  30. {
  31. /**
  32. * @var TokenStorageInterface|SecurityContextInterface
  33. */
  34. protected $tokenStorage;
  35. /**
  36. * @var AuthorizationCheckerInterface|SecurityContextInterface
  37. */
  38. protected $authorizationChecker;
  39. /**
  40. * @var MutableAclProviderInterface
  41. */
  42. protected $aclProvider;
  43. /**
  44. * @var array
  45. */
  46. protected $superAdminRoles;
  47. /**
  48. * @var array
  49. */
  50. protected $adminPermissions;
  51. /**
  52. * @var array
  53. */
  54. protected $objectPermissions;
  55. /**
  56. * @var string
  57. */
  58. protected $maskBuilderClass;
  59. /**
  60. * @param TokenStorageInterface|SecurityContextInterface $tokenStorage
  61. * @param TokenStorageInterface|SecurityContextInterface $authorizationChecker
  62. * @param MutableAclProviderInterface $aclProvider
  63. * @param string $maskBuilderClass
  64. * @param array $superAdminRoles
  65. *
  66. * @todo Go back to signature class check when bumping requirements to SF 2.6+
  67. */
  68. public function __construct($tokenStorage, $authorizationChecker, MutableAclProviderInterface $aclProvider, $maskBuilderClass, array $superAdminRoles)
  69. {
  70. if (!$tokenStorage instanceof TokenStorageInterface && !$tokenStorage instanceof SecurityContextInterface) {
  71. throw new \InvalidArgumentException('Argument 1 should be an instance of Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface or Symfony\Component\Security\Core\SecurityContextInterface');
  72. }
  73. if (!$authorizationChecker instanceof AuthorizationCheckerInterface && !$authorizationChecker instanceof SecurityContextInterface) {
  74. throw new \InvalidArgumentException('Argument 2 should be an instance of Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface or Symfony\Component\Security\Core\SecurityContextInterface');
  75. }
  76. $this->tokenStorage = $tokenStorage;
  77. $this->authorizationChecker = $authorizationChecker;
  78. $this->aclProvider = $aclProvider;
  79. $this->maskBuilderClass = $maskBuilderClass;
  80. $this->superAdminRoles = $superAdminRoles;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function setAdminPermissions(array $permissions)
  86. {
  87. $this->adminPermissions = $permissions;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function getAdminPermissions()
  93. {
  94. return $this->adminPermissions;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function setObjectPermissions(array $permissions)
  100. {
  101. $this->objectPermissions = $permissions;
  102. }
  103. /**
  104. * {@inheritdoc}
  105. */
  106. public function getObjectPermissions()
  107. {
  108. return $this->objectPermissions;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  114. {
  115. if (!is_array($attributes)) {
  116. $attributes = array($attributes);
  117. }
  118. try {
  119. return $this->authorizationChecker->isGranted($this->superAdminRoles) || $this->authorizationChecker->isGranted($attributes, $object);
  120. } catch (AuthenticationCredentialsNotFoundException $e) {
  121. return false;
  122. }
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function getBaseRole(AdminInterface $admin)
  128. {
  129. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  130. }
  131. /**
  132. * {@inheritdoc}
  133. */
  134. public function buildSecurityInformation(AdminInterface $admin)
  135. {
  136. $baseRole = $this->getBaseRole($admin);
  137. $results = array();
  138. foreach ($admin->getSecurityInformation() as $role => $permissions) {
  139. $results[sprintf($baseRole, $role)] = $permissions;
  140. }
  141. return $results;
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function createObjectSecurity(AdminInterface $admin, $object)
  147. {
  148. // retrieving the ACL for the object identity
  149. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  150. $acl = $this->getObjectAcl($objectIdentity);
  151. if (is_null($acl)) {
  152. $acl = $this->createAcl($objectIdentity);
  153. }
  154. // retrieving the security identity of the currently logged-in user
  155. $user = $this->tokenStorage->getToken()->getUser();
  156. $securityIdentity = UserSecurityIdentity::fromAccount($user);
  157. $this->addObjectOwner($acl, $securityIdentity);
  158. $this->addObjectClassAces($acl, $this->buildSecurityInformation($admin));
  159. $this->updateAcl($acl);
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function deleteObjectSecurity(AdminInterface $admin, $object)
  165. {
  166. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  167. $this->deleteAcl($objectIdentity);
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function getObjectAcl(ObjectIdentityInterface $objectIdentity)
  173. {
  174. try {
  175. $acl = $this->aclProvider->findAcl($objectIdentity);
  176. } catch (AclNotFoundException $e) {
  177. return;
  178. }
  179. return $acl;
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function findObjectAcls(\Traversable $oids, array $sids = array())
  185. {
  186. try {
  187. $acls = $this->aclProvider->findAcls(iterator_to_array($oids), $sids);
  188. } catch (\Exception $e) {
  189. if ($e instanceof NotAllAclsFoundException) {
  190. $acls = $e->getPartialResult();
  191. } elseif ($e instanceof AclNotFoundException) {
  192. // if only one oid, this error is thrown
  193. $acls = new \SplObjectStorage();
  194. } else {
  195. throw $e;
  196. }
  197. }
  198. return $acls;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function addObjectOwner(AclInterface $acl, UserSecurityIdentity $securityIdentity = null)
  204. {
  205. if (false === $this->findClassAceIndexByUsername($acl, $securityIdentity->getUsername())) {
  206. // only add if not already exists
  207. $acl->insertObjectAce($securityIdentity, constant("$this->maskBuilderClass::MASK_OWNER"));
  208. }
  209. }
  210. /**
  211. * {@inheritdoc}
  212. */
  213. public function addObjectClassAces(AclInterface $acl, array $roleInformation = array())
  214. {
  215. $builder = new $this->maskBuilderClass();
  216. foreach ($roleInformation as $role => $permissions) {
  217. $aceIndex = $this->findClassAceIndexByRole($acl, $role);
  218. $hasRole = false;
  219. foreach ($permissions as $permission) {
  220. // add only the object permissions
  221. if (in_array($permission, $this->getObjectPermissions())) {
  222. $builder->add($permission);
  223. $hasRole = true;
  224. }
  225. }
  226. if ($hasRole) {
  227. if ($aceIndex === false) {
  228. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  229. } else {
  230. $acl->updateClassAce($aceIndex, $builder->get());
  231. }
  232. $builder->reset();
  233. } elseif ($aceIndex !== false) {
  234. $acl->deleteClassAce($aceIndex);
  235. }
  236. }
  237. }
  238. /**
  239. * {@inheritdoc}
  240. */
  241. public function createAcl(ObjectIdentityInterface $objectIdentity)
  242. {
  243. return $this->aclProvider->createAcl($objectIdentity);
  244. }
  245. /**
  246. * {@inheritdoc}
  247. */
  248. public function updateAcl(AclInterface $acl)
  249. {
  250. $this->aclProvider->updateAcl($acl);
  251. }
  252. /**
  253. * {@inheritdoc}
  254. */
  255. public function deleteAcl(ObjectIdentityInterface $objectIdentity)
  256. {
  257. $this->aclProvider->deleteAcl($objectIdentity);
  258. }
  259. /**
  260. * {@inheritdoc}
  261. */
  262. public function findClassAceIndexByRole(AclInterface $acl, $role)
  263. {
  264. foreach ($acl->getClassAces() as $index => $entry) {
  265. if ($entry->getSecurityIdentity() instanceof RoleSecurityIdentity && $entry->getSecurityIdentity()->getRole() === $role) {
  266. return $index;
  267. }
  268. }
  269. return false;
  270. }
  271. /**
  272. * {@inheritdoc}
  273. */
  274. public function findClassAceIndexByUsername(AclInterface $acl, $username)
  275. {
  276. foreach ($acl->getClassAces() as $index => $entry) {
  277. if ($entry->getSecurityIdentity() instanceof UserSecurityIdentity && $entry->getSecurityIdentity()->getUsername() === $username) {
  278. return $index;
  279. }
  280. }
  281. return false;
  282. }
  283. }