PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/sonata-project/admin-bundle/Security/Handler/AclSecurityHandler.php

https://gitlab.com/cuza/Clinic_Recods
PHP | 328 lines | 183 code | 43 blank | 102 comment | 18 complexity | 74f065c8f48dd76301d8f5203baf9ae6 MD5 | raw file
  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. } catch (\Exception $e) {
  123. throw $e;
  124. }
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function getBaseRole(AdminInterface $admin)
  130. {
  131. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function buildSecurityInformation(AdminInterface $admin)
  137. {
  138. $baseRole = $this->getBaseRole($admin);
  139. $results = array();
  140. foreach ($admin->getSecurityInformation() as $role => $permissions) {
  141. $results[sprintf($baseRole, $role)] = $permissions;
  142. }
  143. return $results;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function createObjectSecurity(AdminInterface $admin, $object)
  149. {
  150. // retrieving the ACL for the object identity
  151. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  152. $acl = $this->getObjectAcl($objectIdentity);
  153. if (is_null($acl)) {
  154. $acl = $this->createAcl($objectIdentity);
  155. }
  156. // retrieving the security identity of the currently logged-in user
  157. $user = $this->tokenStorage->getToken()->getUser();
  158. $securityIdentity = UserSecurityIdentity::fromAccount($user);
  159. $this->addObjectOwner($acl, $securityIdentity);
  160. $this->addObjectClassAces($acl, $this->buildSecurityInformation($admin));
  161. $this->updateAcl($acl);
  162. }
  163. /**
  164. * {@inheritdoc}
  165. */
  166. public function deleteObjectSecurity(AdminInterface $admin, $object)
  167. {
  168. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  169. $this->deleteAcl($objectIdentity);
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function getObjectAcl(ObjectIdentityInterface $objectIdentity)
  175. {
  176. try {
  177. $acl = $this->aclProvider->findAcl($objectIdentity);
  178. } catch (AclNotFoundException $e) {
  179. return;
  180. }
  181. return $acl;
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function findObjectAcls(\Traversable $oids, array $sids = array())
  187. {
  188. try {
  189. $acls = $this->aclProvider->findAcls(iterator_to_array($oids), $sids);
  190. } catch (\Exception $e) {
  191. if ($e instanceof NotAllAclsFoundException) {
  192. $acls = $e->getPartialResult();
  193. } elseif ($e instanceof AclNotFoundException) {
  194. // if only one oid, this error is thrown
  195. $acls = new \SplObjectStorage();
  196. } else {
  197. throw $e;
  198. }
  199. }
  200. return $acls;
  201. }
  202. /**
  203. * {@inheritdoc}
  204. */
  205. public function addObjectOwner(AclInterface $acl, UserSecurityIdentity $securityIdentity = null)
  206. {
  207. if (false === $this->findClassAceIndexByUsername($acl, $securityIdentity->getUsername())) {
  208. // only add if not already exists
  209. $acl->insertObjectAce($securityIdentity, constant("$this->maskBuilderClass::MASK_OWNER"));
  210. }
  211. }
  212. /**
  213. * {@inheritdoc}
  214. */
  215. public function addObjectClassAces(AclInterface $acl, array $roleInformation = array())
  216. {
  217. $builder = new $this->maskBuilderClass();
  218. foreach ($roleInformation as $role => $permissions) {
  219. $aceIndex = $this->findClassAceIndexByRole($acl, $role);
  220. $hasRole = false;
  221. foreach ($permissions as $permission) {
  222. // add only the object permissions
  223. if (in_array($permission, $this->getObjectPermissions())) {
  224. $builder->add($permission);
  225. $hasRole = true;
  226. }
  227. }
  228. if ($hasRole) {
  229. if ($aceIndex === false) {
  230. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  231. } else {
  232. $acl->updateClassAce($aceIndex, $builder->get());
  233. }
  234. $builder->reset();
  235. } elseif ($aceIndex !== false) {
  236. $acl->deleteClassAce($aceIndex);
  237. }
  238. }
  239. }
  240. /**
  241. * {@inheritdoc}
  242. */
  243. public function createAcl(ObjectIdentityInterface $objectIdentity)
  244. {
  245. return $this->aclProvider->createAcl($objectIdentity);
  246. }
  247. /**
  248. * {@inheritdoc}
  249. */
  250. public function updateAcl(AclInterface $acl)
  251. {
  252. $this->aclProvider->updateAcl($acl);
  253. }
  254. /**
  255. * {@inheritdoc}
  256. */
  257. public function deleteAcl(ObjectIdentityInterface $objectIdentity)
  258. {
  259. $this->aclProvider->deleteAcl($objectIdentity);
  260. }
  261. /**
  262. * {@inheritdoc}
  263. */
  264. public function findClassAceIndexByRole(AclInterface $acl, $role)
  265. {
  266. foreach ($acl->getClassAces() as $index => $entry) {
  267. if ($entry->getSecurityIdentity() instanceof RoleSecurityIdentity && $entry->getSecurityIdentity()->getRole() === $role) {
  268. return $index;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * {@inheritdoc}
  275. */
  276. public function findClassAceIndexByUsername(AclInterface $acl, $username)
  277. {
  278. foreach ($acl->getClassAces() as $index => $entry) {
  279. if ($entry->getSecurityIdentity() instanceof UserSecurityIdentity && $entry->getSecurityIdentity()->getUsername() === $username) {
  280. return $index;
  281. }
  282. }
  283. return false;
  284. }
  285. }