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. */
  49. private $permissions;
  50. /**
  51. * @var Collection<int, UserRole>
  52. *
  53. * @Exclude
  54. */
  55. private $userRoles;
  56. /**
  57. * @deprecated The group functionality was deprecated in Sulu 2.1 and will be removed in Sulu 3.0
  58. *
  59. * @var Collection<int, Group>
  60. *
  61. * @Exclude
  62. */
  63. private $groups;
  64. /**
  65. * @var Collection<string, RoleSettingInterface>
  66. */
  67. private $settings;
  68. /**
  69. * @var bool
  70. */
  71. private $anonymous = false;
  72. /**
  73. * Constructor.
  74. */
  75. public function __construct()
  76. {
  77. $this->permissions = new ArrayCollection();
  78. $this->userRoles = new ArrayCollection();
  79. $this->groups = new ArrayCollection();
  80. $this->settings = new ArrayCollection();
  81. }
  82. /**
  83. * Get id.
  84. *
  85. * @return int
  86. */
  87. public function getId()
  88. {
  89. return $this->id;
  90. }
  91. /**
  92. * @deprecated since 2.1 and will be removed in 3.0. Use "getIdentifier" instead.
  93. *
  94. * @return string
  95. */
  96. public function getRole()
  97. {
  98. @trigger_deprecation('sulu/sulu', '2.1', 'The "%s" method is deprecated, use "%s" instead.', __METHOD__, 'getIdentifier');
  99. return $this->getIdentifier();
  100. }
  101. public function getIdentifier()
  102. {
  103. if ($this->anonymous) {
  104. return RoleInterface::IS_SULU_ANONYMOUS;
  105. }
  106. $key = $this->getKey();
  107. // keep backwards compatibility as name was used for generating identifier before key was introduced
  108. if (!$key) {
  109. $key = $this->getName();
  110. }
  111. return 'ROLE_SULU_' . \strtoupper($key);
  112. }
  113. public function setName($name)
  114. {
  115. $this->name = $name;
  116. return $this;
  117. }
  118. public function getName()
  119. {
  120. return $this->name;
  121. }
  122. public function getKey()
  123. {
  124. return $this->key;
  125. }
  126. public function setKey($key)
  127. {
  128. $this->key = $key;
  129. return $this;
  130. }
  131. public function setSystem($system)
  132. {
  133. $this->system = $system;
  134. return $this;
  135. }
  136. public function getSystem()
  137. {
  138. return $this->system;
  139. }
  140. public function setSecurityType(?SecurityType $securityType = null)
  141. {
  142. $this->securityType = $securityType;
  143. return $this;
  144. }
  145. public function getSecurityType()
  146. {
  147. return $this->securityType;
  148. }
  149. public function addPermission(Permission $permissions)
  150. {
  151. $this->permissions[] = $permissions;
  152. return $this;
  153. }
  154. public function removePermission(Permission $permissions)
  155. {
  156. $this->permissions->removeElement($permissions);
  157. }
  158. public function getPermissions()
  159. {
  160. return $this->permissions;
  161. }
  162. public function addUserRole(UserRole $userRoles)
  163. {
  164. $this->userRoles[] = $userRoles;
  165. return $this;
  166. }
  167. public function removeUserRole(UserRole $userRoles)
  168. {
  169. $this->userRoles->removeElement($userRoles);
  170. }
  171. public function getUserRoles()
  172. {
  173. return $this->userRoles;
  174. }
  175. public function addGroup(Group $groups)
  176. {
  177. $this->groups[] = $groups;
  178. return $this;
  179. }
  180. public function removeGroup(Group $groups)
  181. {
  182. $this->groups->removeElement($groups);
  183. }
  184. public function getGroups()
  185. {
  186. return $this->groups;
  187. }
  188. /**
  189. * @return $this
  190. */
  191. public function addSetting(RoleSettingInterface $setting)
  192. {
  193. $this->settings->set($setting->getKey(), $setting);
  194. return $this;
  195. }
  196. /**
  197. * @return void
  198. */
  199. public function removeSetting(RoleSettingInterface $setting)
  200. {
  201. $this->settings->remove($setting->getKey());
  202. }
  203. /**
  204. * Get settings.
  205. *
  206. * @return Collection<string, RoleSettingInterface>
  207. */
  208. public function getSettings()
  209. {
  210. return $this->settings;
  211. }
  212. public function getSetting($key)
  213. {
  214. return $this->settings->get($key);
  215. }
  216. public function getAnonymous(): bool
  217. {
  218. return $this->anonymous;
  219. }
  220. public function setAnonymous(bool $anonymous)
  221. {
  222. $this->anonymous = $anonymous;
  223. }
  224. }