vendor/sulu/sulu/src/Sulu/Bundle/SecurityBundle/Entity/Role.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Bundle\SecurityBundle\Entity;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use JMS\Serializer\Annotation\Exclude;
  14. use JMS\Serializer\Annotation\Groups;
  15. use Sulu\Component\Persistence\Model\AuditableTrait;
  16. use Sulu\Component\Security\Authentication\RoleInterface;
  17. use Sulu\Component\Security\Authentication\RoleSettingInterface;
  18. /**
  19. * Role.
  20. */
  21. class Role implements RoleInterface
  22. {
  23. use AuditableTrait;
  24. /**
  25. * @var int
  26. */
  27. private $id;
  28. /**
  29. * @var string
  30. */
  31. private $name;
  32. /**
  33. * @var string|null
  34. */
  35. private $key;
  36. /**
  37. * @var string
  38. */
  39. private $system;
  40. /**
  41. * @var SecurityType|null
  42. */
  43. private $securityType;
  44. /**
  45. * @var Collection<int, Permission>
  46. */
  47. #[Groups(['fullRole'])]
  48. private $permissions;
  49. /**
  50. * @var Collection<int, UserRole>
  51. */
  52. #[Exclude]
  53. private $userRoles;
  54. /**
  55. * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  56. *
  57. * @var Collection<int, Group>
  58. */
  59. #[Exclude]
  60. private $groups;
  61. /**
  62. * @var Collection<string, RoleSettingInterface>
  63. */
  64. private $settings;
  65. /**
  66. * @var bool
  67. */
  68. private $anonymous = false;
  69. /**
  70. * Constructor.
  71. */
  72. public function __construct()
  73. {
  74. $this->permissions = new ArrayCollection();
  75. $this->userRoles = new ArrayCollection();
  76. $this->groups = new ArrayCollection();
  77. $this->settings = new ArrayCollection();
  78. }
  79. /**
  80. * Get id.
  81. *
  82. * @return int
  83. */
  84. public function getId()
  85. {
  86. return $this->id;
  87. }
  88. /**
  89. * @deprecated since 2.1 and will be removed in 3.0. Use "getIdentifier" instead.
  90. *
  91. * @return string
  92. */
  93. public function getRole()
  94. {
  95. @trigger_deprecation('sulu/sulu', '2.1', 'The "%s" method is deprecated, use "%s" instead.', __METHOD__, 'getIdentifier');
  96. return $this->getIdentifier();
  97. }
  98. public function getIdentifier()
  99. {
  100. if ($this->anonymous) {
  101. return RoleInterface::IS_SULU_ANONYMOUS;
  102. }
  103. $key = $this->getKey();
  104. // keep backwards compatibility as name was used for generating identifier before key was introduced
  105. if (!$key) {
  106. $key = $this->getName();
  107. }
  108. return 'ROLE_SULU_' . \strtoupper($key);
  109. }
  110. public function setName($name)
  111. {
  112. $this->name = $name;
  113. return $this;
  114. }
  115. public function getName()
  116. {
  117. return $this->name;
  118. }
  119. public function getKey()
  120. {
  121. return $this->key;
  122. }
  123. public function setKey($key)
  124. {
  125. $this->key = $key;
  126. return $this;
  127. }
  128. public function setSystem($system)
  129. {
  130. $this->system = $system;
  131. return $this;
  132. }
  133. public function getSystem()
  134. {
  135. return $this->system;
  136. }
  137. public function setSecurityType(?SecurityType $securityType = null)
  138. {
  139. $this->securityType = $securityType;
  140. return $this;
  141. }
  142. public function getSecurityType()
  143. {
  144. return $this->securityType;
  145. }
  146. public function addPermission(Permission $permissions)
  147. {
  148. $this->permissions[] = $permissions;
  149. return $this;
  150. }
  151. public function removePermission(Permission $permissions)
  152. {
  153. $this->permissions->removeElement($permissions);
  154. }
  155. public function getPermissions()
  156. {
  157. return $this->permissions;
  158. }
  159. public function addUserRole(UserRole $userRoles)
  160. {
  161. $this->userRoles[] = $userRoles;
  162. return $this;
  163. }
  164. public function removeUserRole(UserRole $userRoles)
  165. {
  166. $this->userRoles->removeElement($userRoles);
  167. }
  168. public function getUserRoles()
  169. {
  170. return $this->userRoles;
  171. }
  172. public function addGroup(Group $groups)
  173. {
  174. $this->groups[] = $groups;
  175. return $this;
  176. }
  177. public function removeGroup(Group $groups)
  178. {
  179. $this->groups->removeElement($groups);
  180. }
  181. public function getGroups()
  182. {
  183. return $this->groups;
  184. }
  185. /**
  186. * @return $this
  187. */
  188. public function addSetting(RoleSettingInterface $setting)
  189. {
  190. $this->settings->set($setting->getKey(), $setting);
  191. return $this;
  192. }
  193. /**
  194. * @return void
  195. */
  196. public function removeSetting(RoleSettingInterface $setting)
  197. {
  198. $this->settings->remove($setting->getKey());
  199. }
  200. /**
  201. * Get settings.
  202. *
  203. * @return Collection<string, RoleSettingInterface>
  204. */
  205. public function getSettings()
  206. {
  207. return $this->settings;
  208. }
  209. public function getSetting($key)
  210. {
  211. return $this->settings->get($key);
  212. }
  213. public function getAnonymous(): bool
  214. {
  215. return $this->anonymous;
  216. }
  217. public function setAnonymous(bool $anonymous)
  218. {
  219. $this->anonymous = $anonymous;
  220. }
  221. }