* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\MainBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Chill\MainBundle\Entity\RoleScope; use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * @ORM\Entity() * @ORM\Table(name="permission_groups") * @ORM\Cache(usage="NONSTRICT_READ_WRITE", region="acl_cache_region") * * @author Julien Fastré */ class PermissionsGroup { /** * @var integer * * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(type="string", length=255) */ private $name; /** * @var string[] * * @ORM\Column(type="json") */ private $flags = []; /** * @var Collection * * @ORM\ManyToMany( * targetEntity="Chill\MainBundle\Entity\RoleScope", * inversedBy="permissionsGroups", * cascade={ "persist" }) * @ORM\Cache(usage="NONSTRICT_READ_WRITE") */ private $roleScopes; /** * @var Collection * * @ORM\OneToMany( * targetEntity="Chill\MainBundle\Entity\GroupCenter", * mappedBy="permissionsGroup" * ) */ private $groupCenters; /** * PermissionsGroup constructor. */ public function __construct() { $this->roleScopes = new \Doctrine\Common\Collections\ArrayCollection(); $this->groupCenters = new \Doctrine\Common\Collections\ArrayCollection(); } /** * @return int */ public function getId() { return $this->id; } /** * @return string */ public function getName() { return $this->name; } /** * @return ArrayCollection|Collection */ public function getRoleScopes() { return $this->roleScopes; } /** * @param $name * @return $this */ public function setName($name) { $this->name = $name; return $this; } /** * @param RoleScope $roleScope */ public function addRoleScope(RoleScope $roleScope) { $this->roleScopes->add($roleScope); } /** * @param RoleScope $roleScope * @throws \RuntimeException if the roleScope could not be removed. */ public function removeRoleScope(RoleScope $roleScope) { $result = $this->roleScopes->removeElement($roleScope); if ($result === FALSE) { throw new \RuntimeException(sprintf("The roleScope '%s' could not be removed, " . "aborting.", spl_object_hash($roleScope))); } } /** * @return string[] */ public function getFlags() { return $this->flags; } /** * @param array $flags * @return $this */ public function setFlags(array $flags) { $this->flags = $flags; return $this; } /** * Test that a role scope is associated only once * with the permission group * * @param ExecutionContextInterface $context */ public function isRoleScopePresentOnce(ExecutionContextInterface $context) { $roleScopesId = array_map(function(RoleScope $roleScope) { return $roleScope->getId(); }, $this->getRoleScopes()->toArray()); $countedIds = array_count_values($roleScopesId); foreach ($countedIds as $id => $nb) { if ($nb > 1) { $context->buildViolation("A permission is already present " . "for the same role and scope") ->addViolation(); } } } }