AclSecurityHandler.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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\Acl\Model\ObjectIdentityInterface;
  12. use Symfony\Component\Security\Core\SecurityContextInterface;
  13. use Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException;
  14. use Symfony\Component\Security\Acl\Model\AclProviderInterface;
  15. use Symfony\Component\Security\Acl\Model\AclInterface;
  16. use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
  17. use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
  18. use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
  19. use Sonata\AdminBundle\Admin\AdminInterface;
  20. class AclSecurityHandler implements SecurityHandlerInterface
  21. {
  22. protected $securityContext;
  23. protected $aclProvider;
  24. protected $superAdminRoles;
  25. protected $adminPermissions;
  26. protected $objectPermissions;
  27. protected $maskBuilderClass;
  28. /**
  29. * @param SecurityContextInterface $securityContext
  30. * @param AclProviderInterface $aclProvider
  31. * @param string $maskBuilderClass
  32. * @param array $superAdminRoles
  33. */
  34. public function __construct(SecurityContextInterface $securityContext, AclProviderInterface $aclProvider, $maskBuilderClass, array $superAdminRoles)
  35. {
  36. $this->securityContext = $securityContext;
  37. $this->aclProvider = $aclProvider;
  38. $this->maskBuilderClass = $maskBuilderClass;
  39. $this->superAdminRoles = $superAdminRoles;
  40. }
  41. /**
  42. * Set the permissions not related to an object instance and also to be available when objects do not exist
  43. *
  44. * @param array $permissions
  45. */
  46. public function setAdminPermissions(array $permissions)
  47. {
  48. $this->adminPermissions = $permissions;
  49. }
  50. /**
  51. * Return the permissions not related to an object instance and also to be available when objects do not exist
  52. *
  53. * @return array
  54. */
  55. public function getAdminPermissions()
  56. {
  57. return $this->adminPermissions;
  58. }
  59. /**
  60. * Set the permissions related to an object instance
  61. *
  62. * @param array $permissions
  63. */
  64. public function setObjectPermissions(array $permissions)
  65. {
  66. $this->objectPermissions = $permissions;
  67. }
  68. /**
  69. * Return the permissions related to an object instance
  70. *
  71. * @return array
  72. */
  73. public function getObjectPermissions()
  74. {
  75. return $this->objectPermissions;
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. public function isGranted(AdminInterface $admin, $attributes, $object = null)
  81. {
  82. if (!is_array($attributes)) {
  83. $attributes = array($attributes);
  84. }
  85. try {
  86. return $this->securityContext->isGranted($this->superAdminRoles) || $this->securityContext->isGranted($attributes, $object);
  87. } catch (AuthenticationCredentialsNotFoundException $e) {
  88. return false;
  89. } catch (\Exception $e) {
  90. throw $e;
  91. }
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. public function getBaseRole(AdminInterface $admin)
  97. {
  98. return 'ROLE_'.str_replace('.', '_', strtoupper($admin->getCode())).'_%s';
  99. }
  100. /**
  101. * {@inheritDoc}
  102. */
  103. public function buildSecurityInformation(AdminInterface $admin)
  104. {
  105. $baseRole = $this->getBaseRole($admin);
  106. $results = array();
  107. foreach ($admin->getSecurityInformation() as $role => $permissions) {
  108. $results[sprintf($baseRole, $role)] = $permissions;
  109. }
  110. return $results;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. */
  115. public function createObjectSecurity(AdminInterface $admin, $object)
  116. {
  117. $acl = $this->getNewObjectOwnerAcl($object);
  118. $this->addObjectClassAces($acl, $this->buildSecurityInformation($admin));
  119. $this->updateAcl($acl);
  120. }
  121. /**
  122. * {@inheritDoc}
  123. */
  124. public function deleteObjectSecurity(AdminInterface $admin, $object)
  125. {
  126. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  127. if (!is_null($acl = $this->getObjectAcl($objectIdentity)))
  128. {
  129. $this->deleteAcl($acl);
  130. }
  131. }
  132. /**
  133. * Get the ACL for the object
  134. *
  135. * @param ObjectIdentityInterface $objectIdentity
  136. * @return \Symfony\Component\Security\Acl\Model\AclInterface
  137. */
  138. public function getObjectAcl(ObjectIdentityInterface $objectIdentity)
  139. {
  140. try {
  141. $acl = $aclProvider->findAcl($objectIdentity);
  142. } catch(AclNotFoundException $e) {
  143. return null;
  144. }
  145. return $acl;
  146. }
  147. /**
  148. * Get a new ACL with an object ACE where the currently logged in user is set as owner
  149. *
  150. * @param object $object
  151. * @return Symfony\Component\Security\Acl\Model\AclInterface
  152. */
  153. public function getNewObjectOwnerAcl($object)
  154. {
  155. // creating the object ACL, fe. Comment 1 ACL
  156. $objectIdentity = ObjectIdentity::fromDomainObject($object);
  157. $acl = $this->aclProvider->createAcl($objectIdentity);
  158. // retrieving the security identity of the currently logged-in user
  159. $user = $this->securityContext->getToken()->getUser();
  160. $securityIdentity = UserSecurityIdentity::fromAccount($user);
  161. // grant owner access
  162. $this->addObjectOwnwer($acl, $securityIdentity);
  163. return $acl;
  164. }
  165. /**
  166. * Add an object owner ACE to the object ACL
  167. *
  168. * @param AclInterface $acl
  169. * @param UserSecurityIdentity $securityIdentity
  170. */
  171. public function addObjectOwnwer(AclInterface $acl, UserSecurityIdentity $securityIdentity = null)
  172. {
  173. if (false === $this->findClassAceIndexByUsername($acl, $securityIdentity->getUsername())) {
  174. // only add if not already exists
  175. $acl->insertObjectAce($securityIdentity, constant("$this->maskBuilderClass::MASK_OWNER"));
  176. }
  177. }
  178. /**
  179. * Add the object class ACE's to the object ACL
  180. *
  181. * @param AclInterface $acl
  182. * @param array $roleInformation
  183. * @return void
  184. */
  185. public function addObjectClassAces(AclInterface $acl, array $roleInformation = array())
  186. {
  187. $builder = new $this->maskBuilderClass();
  188. foreach ($roleInformation as $role => $permissions) {
  189. $aceIndex = $this->findClassAceIndexByRole($acl, $role);
  190. $hasRole = false;
  191. foreach ($permissions as $permission) {
  192. // add only the object permissions
  193. if (in_array($permission, $this->getObjectPermissions())) {
  194. $builder->add($permission);
  195. $hasRole = true;
  196. }
  197. }
  198. if ($hasRole) {
  199. if ($aceIndex === false) {
  200. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  201. } else {
  202. $acl->updateClassAce($aceIndex, $builder->get());
  203. }
  204. $builder->reset();
  205. } elseif ($aceIndex !== false) {
  206. $acl->deleteClassAce($aceIndex);
  207. }
  208. }
  209. }
  210. /**
  211. * Add the class ACE's to the admin ACL
  212. *
  213. * @param AclInterface $acl
  214. * @param array $roleInformation
  215. * @param Symfony\Component\Console\Output\OutputInterface $output
  216. * @return boolean TRUE if admin class ACEs are added, FALSE if not
  217. */
  218. public function addAdminClassAces(AclInterface $acl, array $roleInformation = array(), \Symfony\Component\Console\Output\OutputInterface $output = null)
  219. {
  220. if (count($this->getAdminPermissions()) > 0 ) {
  221. $builder = new $this->maskBuilderClass();
  222. foreach ($roleInformation as $role => $permissions) {
  223. $aceIndex = $this->findClassAceIndexByRole($acl, $role);
  224. $roleAdminPermissions = array();
  225. foreach ($permissions as $permission) {
  226. // add only the admin permissions
  227. if (in_array($permission, $this->getAdminPermissions())) {
  228. $builder->add($permission);
  229. $roleAdminPermissions[] = $permission;
  230. }
  231. }
  232. if (count($roleAdminPermissions) > 0) {
  233. if ($aceIndex === false) {
  234. $acl->insertClassAce(new RoleSecurityIdentity($role), $builder->get());
  235. $action = 'add';
  236. } else {
  237. $acl->updateClassAce($aceIndex, $builder->get());
  238. $action = 'update';
  239. }
  240. if (!is_null($output)) {
  241. $output->writeln(sprintf(' - %s role: %s, permissions: %s', $action, $role, json_encode($roleAdminPermissions)));
  242. }
  243. $builder->reset();
  244. } elseif ($aceIndex !== false) {
  245. $acl->deleteClassAce($aceIndex);
  246. if (!is_null($output)) {
  247. $output->writeln(sprintf(' - remove role: %s', $action, $role));
  248. }
  249. }
  250. }
  251. return true;
  252. } else {
  253. return false;
  254. }
  255. }
  256. /**
  257. * Update the ACL
  258. *
  259. * @param AclInterface $acl
  260. * @return void
  261. */
  262. public function updateAcl(AclInterface $acl)
  263. {
  264. $this->aclProvider->updateAcl($acl);
  265. }
  266. /**
  267. * Delete the ACL
  268. *
  269. * @param AclInterface $acl
  270. * @return void
  271. */
  272. public function deleteAcl(AclInterface $acl)
  273. {
  274. $this->aclProvider->deleteAcl($acl);
  275. }
  276. /**
  277. * Helper method to find the index of a class ACE for a role
  278. *
  279. * @param AclInterface $acl
  280. * @param string $role
  281. */
  282. protected function findClassAceIndexByRole(AclInterface $acl, $role)
  283. {
  284. foreach ($acl->getClassAces() as $index => $entry) {
  285. if ($entry->getSecurityIdentity() instanceof RoleSecurityIdentity && $entry->getSecurityIdentity()->getRole() === $role) {
  286. return $index;
  287. }
  288. }
  289. return false;
  290. }
  291. /**
  292. * Helper method to find the index of a class ACE for a username
  293. *
  294. * @param AclInterface $acl
  295. * @param string $username
  296. */
  297. protected function findClassAceIndexByUsername(AclInterface $acl, $username)
  298. {
  299. foreach ($acl->getClassAces() as $index => $entry) {
  300. if ($entry->getSecurityIdentity() instanceof UserSecurityIdentity && $entry->getSecurityIdentity()->getUsername() === $username) {
  301. return $index;
  302. }
  303. }
  304. return false;
  305. }
  306. }