private function mergeHydrationResult(array $currentResult, array $hydratorResult, HydrationProfile $profile, $groupName) { if ($profile->isGroupingNeeded()) { $currentResult[$groupName] = $hydratorResult; } else { if (is_callable($hydratorResult)) { $currentResult = $hydratorResult($currentResult); } elseif (is_array($hydratorResult)) { $currentResult = array_merge($currentResult, $hydratorResult); } else { throw new \RuntimeException(); } } return $currentResult; }
public function testGetGroupProfileWhenProfileNameMatchesGroup() { /* @var HydrationProfile $result */ $result = $this->config->getProfileDefinition('list'); $this->assertInstanceOf(HydrationProfile::clazz(), $result); $this->assertFalse($result->isGroupingNeeded()); $this->assertSame(array('list'), $result->getGroups()); }
public function getConfig() { return array('entity' => DummyArticle::clazz(), 'hydration' => array('groups' => array('form' => array('id', 'title', 'body'), 'list' => function (DummyArticle $e) { if (DummyArticle::$suicideEngaged) { $e->suicide(); } return array('id' => $e->getId(), 'title' => substr($e->title, 0, 10), 'body' => substr($e->body, 0, 10)); }, 'suicide' => function () { throw new \Exception(); }), 'profiles' => array('new_record' => HydrationProfile::create()->useGroups(array('form')), 'get_record' => HydrationProfile::create()->useGroups(array('form')), 'list' => HydrationProfile::create(false)->useGroups(array('list')), 'rotten_profile' => HydrationProfile::create()->useGroups(array('suicide'))))); }
public function setUp() { $this->container = $this->createMock('Symfony\\Component\\DependencyInjection\\ContainerInterface'); $this->service = new HydrationService($this->container); $this->config = array('groups' => array('tags' => function () { }, 'comments' => function (Article $e) { $result = array(); foreach ($e->comments as $comment) { $result[] = array('body' => $comment->body); } return $result; }, 'form' => array('title', 'body'), 'author' => array('firstname' => 'author.firstname', 'lastname' => 'author.lastname'), 'list' => function (Article $e) { return array('title' => substr($e->title, 0, 10), 'body' => substr($e->body, 0, 10)); }), 'profiles' => array('list' => HydrationProfile::create(false)->useGroups(array('list')), 'form' => HydrationProfile::create()->useGroups(array('form', 'comments', 'author')), 'author')); $author = new Author(); $author->firstname = 'Vassily'; $author->lastname = 'Pupkin'; $article = new Article(); $article->author = $author; $article->title = 'Foo title'; $article->body = 'Bar body'; $article->comments = array(new ArticleComment($author, 'Comment1')); $this->article = $article; }
/** * @return array */ public function getConfig() { $self = $this; return array('entity' => User::clazz(), 'create_default_data_mapper' => function (ContainerInterface $container) { return $this->container->get('modera_backend_security.data_mapper.user_data_mapper'); }, 'security' => array('actions' => array('create' => ModeraBackendSecurityBundle::ROLE_MANAGE_USER_PROFILES, 'update' => function (AuthorizationCheckerInterface $ac, array $params) use($self) { /* @var TokenStorageInterface $ts */ $ts = $self->get('security.token_storage'); /* @var User $user */ $user = $ts->getToken()->getUser(); if ($ac->isGranted(ModeraBackendSecurityBundle::ROLE_MANAGE_USER_PROFILES)) { return true; } else { // irrespectively of what privileges user has we will always allow him to edit his // own profile data return $user instanceof User && isset($params['record']['id']) && $user->getId() == $params['record']['id']; } }, 'remove' => ModeraBackendSecurityBundle::ROLE_MANAGE_USER_PROFILES, 'list' => ModeraBackendSecurityBundle::ROLE_ACCESS_BACKEND_TOOLS_SECURITY_SECTION)), 'hydration' => array('groups' => array('main-form' => ['id', 'username', 'email', 'firstName', 'lastName', 'middleName', 'meta'], 'list' => function (User $user) { $groups = array(); foreach ($user->getGroups() as $group) { $groups[] = $group->getName(); } return array('id' => $user->getId(), 'username' => $user->getUsername(), 'email' => $user->getEmail(), 'firstName' => $user->getFirstName(), 'lastName' => $user->getLastName(), 'middleName' => $user->getMiddleName(), 'state' => $user->getState(), 'groups' => $groups, 'meta' => $user->getMeta()); }, 'compact-list' => ['id', 'username', 'fullname'], 'delete-user' => ['username']), 'profiles' => array('list', 'delete-user', 'main-form', 'compact-list', 'modera-backend-security-group-groupusers' => HydrationProfile::create(false)->useGroups(array('compact-list')))), 'map_data_on_create' => function (array $params, User $entity, DataMapperInterface $defaultMapper, ContainerInterface $container) use($self) { $defaultMapper->mapData($params, $entity); if (isset($params['plainPassword']) && $params['plainPassword']) { $plainPassword = $params['plainPassword']; } else { $plainPassword = $self->generatePassword(); } $self->setPassword($entity, $plainPassword); if (isset($params['sendPassword']) && $params['sendPassword'] != '') { /* @var MailService $mailService */ $mailService = $container->get('modera_backend_security.service.mail_service'); $mailService->sendPassword($entity, $plainPassword); } }, 'map_data_on_update' => function (array $params, User $entity, DataMapperInterface $defaultMapper, ContainerInterface $container) use($self) { $defaultMapper->mapData($params, $entity); /* @var LoggerInterface $activityMgr */ $activityMgr = $container->get('modera_activity_logger.manager.activity_manager'); /* @var TokenStorageInterface $ts */ $ts = $container->get('security.token_storage'); if (isset($params['plainPassword']) && $params['plainPassword']) { $self->setPassword($entity, $params['plainPassword']); if (isset($params['sendPassword']) && $params['sendPassword'] != '') { /* @var MailService $mailService */ $mailService = $container->get('modera_backend_security.service.mail_service'); $mailService->sendPassword($entity, $params['plainPassword']); } $activityMsg = T::trans('Password has been changed for user "%user%".', array('%user%' => $entity->getUsername())); $activityContext = array('type' => 'user.password_changed', 'author' => $ts->getToken()->getUser()->getId()); $activityMgr->info($activityMsg, $activityContext); } else { $activityMsg = T::trans('Profile data is changed for user "%user%".', array('%user%' => $entity->getUsername())); $activityContext = array('type' => 'user.profile_updated', 'author' => $ts->getToken()->getUser()->getId()); $activityMgr->info($activityMsg, $activityContext); } }, 'remove_entities_handler' => function ($entities, $params, $defaultHandler, ContainerInterface $container) { /* @var UserService $userService */ $userService = $container->get('modera_security.service.user_service'); $operationResult = new OperationResult(); foreach ($entities as $entity) { /* @var User $entity*/ $userService->remove($entity); $operationResult->reportEntity(User::clazz(), $entity->getId(), OperationResult::TYPE_ENTITY_REMOVED); } return $operationResult; }); }