/** * This method does not flush ORM. You'll need to manually call it. * * @param RZ\Roadiz\Core\Entities\Group $newGroup * * @throws \RuntimeException If newGroup param is null */ public function diff(Group $newGroup) { if (null !== $newGroup) { if ("" != $newGroup->getName()) { $this->group->setName($newGroup->getName()); } $existingRolesNames = $this->group->getRoles(); foreach ($newGroup->getRolesEntities() as $newRole) { if (false === in_array($newRole->getName(), $existingRolesNames)) { $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($newRole->getName()); $this->group->addRole($role); } } } else { throw new \RuntimeException("New group is null", 1); } }
/** * Deserializes a Json into readable datas * @param string $string * * @return ArrayCollection */ public static function deserialize($string) { if ($string == "") { throw new \Exception('File is empty.'); } $collection = new ArrayCollection(); $array = json_decode($string, true); foreach ($array as $groupAssoc) { if (!empty($groupAssoc["roles"]) && !empty($groupAssoc["name"])) { $group = new Group(); $group->setName($groupAssoc['name']); foreach ($groupAssoc["roles"] as $role) { $role = Kernel::getService('em')->getRepository('RZ\\Roadiz\\Core\\Entities\\Role')->findOneByName($role['name']); $group->addRole($role); } $collection[] = $group; } } return $collection; }
/** * @param RZ\Roadiz\Core\Entities\User $user * @param RZ\Roadiz\Core\Entities\Group $group * * @return \Symfony\Component\Form\Form */ private function buildRemoveGroupForm(User $user, Group $group) { $builder = $this->createFormBuilder()->add('userId', 'hidden', ['data' => $user->getId(), 'constraints' => [new NotBlank()]])->add('groupId', 'hidden', ['data' => $group->getId(), 'constraints' => [new NotBlank()]]); return $builder->getForm(); }
/** * @param array $data * @param RZ\Roadiz\Core\Entities\Group $group * @param RZ\Roadiz\Core\EntitiesUser $user * * @return RZ\Roadiz\Core\Entities\User */ private function removeUser($data, Group $group, User $user) { if ($data['groupId'] == $group->getId() && $data['userId'] == $user->getId()) { if ($user !== null) { $user->removeGroup($group); $this->getService('em')->flush(); } return $user; } }