RoleSecurityIdentity.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Security\Acl\Domain;
  11. use Symfony\Component\Security\Acl\Model\SecurityIdentityInterface;
  12. use Symfony\Component\Security\Core\Role\Role;
  13. /**
  14. * A SecurityIdentity implementation for roles.
  15. *
  16. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  17. */
  18. final class RoleSecurityIdentity implements SecurityIdentityInterface
  19. {
  20. private $role;
  21. /**
  22. * Constructor.
  23. *
  24. * @param mixed $role a Role instance, or its string representation
  25. */
  26. public function __construct($role)
  27. {
  28. if ($role instanceof Role) {
  29. $role = $role->getRole();
  30. }
  31. $this->role = $role;
  32. }
  33. /**
  34. * Returns the role name.
  35. *
  36. * @return string
  37. */
  38. public function getRole()
  39. {
  40. return $this->role;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function equals(SecurityIdentityInterface $sid)
  46. {
  47. if (!$sid instanceof self) {
  48. return false;
  49. }
  50. return $this->role === $sid->getRole();
  51. }
  52. /**
  53. * Returns a textual representation of this security identity.
  54. *
  55. * This is solely used for debugging purposes, not to make an equality decision.
  56. *
  57. * @return string
  58. */
  59. public function __toString()
  60. {
  61. return sprintf('RoleSecurityIdentity(%s)', $this->role);
  62. }
  63. }