PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Security/Handler/AclSecurityHandler.php

http://github.com/sonata-project/SonataAdminBundle
PHP | 321 lines | 177 code | 43 blank | 101 comment | 16 complexity | b105ec8718d6fc01607c52074814a9b1 MD5 | raw file
Possible License(s): JSON, Apache-2.0, MIT
  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 (NotAllAclsFoundException $e) {
  189. $acls = $e->getPartialResult();
  190. } catch (AclNotFoundException $e) { // if only one oid, this error is thrown
  191. $acls = new \SplObjectStorage();
  192. }
  193. return $acls;
  194. }
  195. /**
  196. * {@inheritdoc}
  197. */
  198. public function addObjectOwner(AclInterface $acl, UserSecurityIdentity $securityIdentity = null)
  199. {
  200. if (false === $this->findClassAceIndexByUsername($acl, $securityIdentity->getUsername())) {
  201. // only add if not already exists
  202. $acl->insertObjectAce($securityIdentity, constant("$this->maskBuilderClass::MASK_OWNER"));
  203. }
  204. }
  205. /**
  206. * {@inheritdoc}
  207. */
  208. public function addObjectClassAces(AclInterface $acl, array $roleInformation = array())
  209. {
  210. $builder = new $this->maskBuilderClass();
  211. foreach ($roleInformation as $role => $permissions) {
  212. $aceIndex = $this->findClassAceIndexByRole($acl, $role);
  213. $hasRole = false;
  214. foreach ($permissions as $permission) {
  215. // add only the object permissions
  216. if (in_array($permission, $this->getObjectPermissions())) {
  217. $builder->add($permission);
  218. $hasRole = true;
  219. }
  220. }
  221. if ($hasRole) {
  222. if ($aceIndex === false) {
  223. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  224. } else {
  225. $acl->updateClassAce($aceIndex, $builder->get());
  226. }
  227. $builder->reset();
  228. } elseif ($aceIndex !== false) {
  229. $acl->deleteClassAce($aceIndex);
  230. }
  231. }
  232. }
  233. /**
  234. * {@inheritdoc}
  235. */
  236. public function createAcl(ObjectIdentityInterface $objectIdentity)
  237. {
  238. return $this->aclProvider->createAcl($objectIdentity);
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function updateAcl(AclInterface $acl)
  244. {
  245. $this->aclProvider->updateAcl($acl);
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function deleteAcl(ObjectIdentityInterface $objectIdentity)
  251. {
  252. $this->aclProvider->deleteAcl($objectIdentity);
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function findClassAceIndexByRole(AclInterface $acl, $role)
  258. {
  259. foreach ($acl->getClassAces() as $index => $entry) {
  260. if ($entry->getSecurityIdentity() instanceof RoleSecurityIdentity && $entry->getSecurityIdentity()->getRole() === $role) {
  261. return $index;
  262. }
  263. }
  264. return false;
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function findClassAceIndexByUsername(AclInterface $acl, $username)
  270. {
  271. foreach ($acl->getClassAces() as $index => $entry) {
  272. if ($entry->getSecurityIdentity() instanceof UserSecurityIdentity && $entry->getSecurityIdentity()->getUsername() === $username) {
  273. return $index;
  274. }
  275. }
  276. return false;
  277. }
  278. }