From abfd816ab58e6b830e7cc13a105b1467ad861786 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 11:37:44 +0100 Subject: [PATCH 1/7] create and update correct email entity for backend edit --- Controller/AbstractController.php | 6 +++--- Manager/CommunityManager.php | 2 ++ Manager/UserManager.php | 35 ++++++++++++++++++++++++------- Manager/UserManagerInterface.php | 9 ++++++++ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/Controller/AbstractController.php b/Controller/AbstractController.php index 18c87477..84b3e9f9 100644 --- a/Controller/AbstractController.php +++ b/Controller/AbstractController.php @@ -15,7 +15,7 @@ use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface; use Sulu\Bundle\SecurityBundle\Entity\User; use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\Form\Form; +use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Response; /** @@ -69,11 +69,11 @@ protected function getWebspaceKey() * Set Password and Salt by a Symfony Form. * * @param User $user - * @param Form $form + * @param FormInterface $form * * @return User */ - protected function setUserPasswordAndSalt(User $user, Form $form) + protected function setUserPasswordAndSalt(User $user, FormInterface $form) { $plainPassword = $form->get('plainPassword')->getData(); if (null === $plainPassword) { diff --git a/Manager/CommunityManager.php b/Manager/CommunityManager.php index 17957b33..f38a3949 100644 --- a/Manager/CommunityManager.php +++ b/Manager/CommunityManager.php @@ -261,6 +261,8 @@ public function sendEmails($type, BaseUser $user) */ public function saveProfile(BaseUser $user) { + $this->userManager->updateUser($user); + // Event $event = new CommunityEvent($user, $this->config); $this->eventDispatcher->dispatch(self::EVENT_SAVE_PROFILE, $event); diff --git a/Manager/UserManager.php b/Manager/UserManager.php index 5f72f254..4cb3854c 100644 --- a/Manager/UserManager.php +++ b/Manager/UserManager.php @@ -114,13 +114,8 @@ public function createUser(User $user, $webspaceKey, $roleName) $contact->setLastName(''); } - $emailType = $this->entityManager->getReference(EmailType::class, 1); - - $contactEmail = new Email(); - $contactEmail->setEmail($user->getEmail()); - $contactEmail->setEmailType($emailType); - $contact->addEmail($contactEmail); $contact->setMainEmail($user->getEmail()); + $user = $this->updateUser($user); // Create and Add User Role $userRole = $this->createUserRole($user, $webspaceKey, $roleName); @@ -128,13 +123,39 @@ public function createUser(User $user, $webspaceKey, $roleName) // Save Entity $this->entityManager->persist($userRole); - $this->entityManager->persist($contactEmail); $this->entityManager->persist($contact); $this->entityManager->persist($user); return $user; } + /** + * {@inheritdoc} + */ + public function updateUser(User $user) + { + $contact = $user->getContact(); + + if (!$contact->getEmails()->isEmpty()) { + /** @var Email $email */ + $email = $contact->getEmails()->first(); + $email->setEmail($contact->getMainEmail()); + + return $user; + } + + /** @var EmailType $emailType */ + $emailType = $this->entityManager->getReference(EmailType::class, 1); + $contactEmail = new Email(); + $contactEmail->setEmail($contact->getMainEmail()); + $contactEmail->setEmailType($emailType); + $contact->addEmail($contactEmail); + + $this->entityManager->persist($contactEmail); + + return $user; + } + /** * {@inheritdoc} */ diff --git a/Manager/UserManagerInterface.php b/Manager/UserManagerInterface.php index a2463d5f..44c4e3e4 100644 --- a/Manager/UserManagerInterface.php +++ b/Manager/UserManagerInterface.php @@ -29,6 +29,15 @@ interface UserManagerInterface */ public function createUser(User $user, $webspaceKey, $roleName); + /** + * Update User entity. + * + * @param User $user + * + * @return User + */ + public function updateUser(User $user); + /** * Generates a unique token. * From 043c5d8b76ac38b7148b7b7fb1f42736ba86d2be Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 13:39:21 +0100 Subject: [PATCH 2/7] fix requirements --- composer.json | 1 - 1 file changed, 1 deletion(-) diff --git a/composer.json b/composer.json index 54f32a97..f1cd9502 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,6 @@ "jackalope/jackalope-doctrine-dbal": "^1.2.5", "symfony/monolog-bundle": "^2.8.7 || ^3.0", "massive/search-bundle": "@dev", - "massive/build-bundle": "^0.2", "zendframework/zend-stdlib": "~2.3", "zendframework/zendsearch": "@dev", "phpunit/phpunit": ">=4.8, <6.0" From 35420bfb2779137510491c31824f527214e1c3a8 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 14:47:47 +0100 Subject: [PATCH 3/7] fix profile controller tests --- .../Functional/Controller/ProfileControllerTest.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Tests/Functional/Controller/ProfileControllerTest.php b/Tests/Functional/Controller/ProfileControllerTest.php index b6a2ea53..70302660 100644 --- a/Tests/Functional/Controller/ProfileControllerTest.php +++ b/Tests/Functional/Controller/ProfileControllerTest.php @@ -15,6 +15,7 @@ use Doctrine\ORM\Mapping\ClassMetadata; use Sulu\Bundle\ContactBundle\Entity\AddressType; use Sulu\Bundle\ContactBundle\Entity\Country; +use Sulu\Bundle\ContactBundle\Entity\EmailType; use Sulu\Bundle\SecurityBundle\Entity\User; use Sulu\Bundle\SecurityBundle\Entity\UserRepository; use Sulu\Bundle\TestBundle\Testing\SuluTestCase; @@ -45,8 +46,16 @@ protected function setUp() $metadata = $entityManager->getClassMetadata(get_class($country)); $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE); + $emailType = new EmailType(); + $emailType->setName('work'); + $emailType->setId(1); + + $metadata = $entityManager->getClassMetadata(get_class($emailType)); + $metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE); + $entityManager->persist($addressType); $entityManager->persist($country); + $entityManager->persist($emailType); $entityManager->flush(); } @@ -66,8 +75,11 @@ public function testProfile() $crawler = $client->request('GET', '/profile'); $this->assertHttpStatusCode(200, $client->getResponse()); + $this->assertCount(1, $crawler->filter('#profile_contact_formOfAddress')); $this->assertCount(1, $crawler->filter('#profile_contact_firstName')); $this->assertCount(1, $crawler->filter('#profile_contact_lastName')); + $this->assertCount(1, $crawler->filter('#profile_contact_mainEmail')); + $this->assertCount(1, $crawler->filter('#profile_contact_contactAddresses_0_main')); $this->assertCount(1, $crawler->filter('#profile_contact_contactAddresses_0_address_street')); $this->assertCount(1, $crawler->filter('#profile_contact_contactAddresses_0_address_number')); $this->assertCount(1, $crawler->filter('#profile_contact_contactAddresses_0_address_zip')); @@ -80,6 +92,7 @@ public function testProfile() 'profile[contact][formOfAddress]' => 0, 'profile[contact][firstName]' => 'Hikaru', 'profile[contact][lastName]' => 'Sulu', + 'profile[contact][mainEmail]' => 'sulu@example.org', 'profile[contact][contactAddresses][0][address][street]' => 'Rathausstraße', 'profile[contact][contactAddresses][0][address][number]' => 16, 'profile[contact][contactAddresses][0][address][zip]' => 12351, From c320d84f4fbed898a73883657c52720cf769d9c3 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 15:24:09 +0100 Subject: [PATCH 4/7] allow version 0.2 and 0.3 of massive build bundle --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index f1cd9502..82876896 100644 --- a/composer.json +++ b/composer.json @@ -8,6 +8,7 @@ "symfony/symfony": "^2.8.7 || ^3.0", "symfony/security-acl": "~2.7|~3.0.0", "sulu/sulu": "^1.3", + "massive/build-bundle": "^0.2 || ^0.3", "symfony/swiftmailer-bundle": "~2.3", "beberlei/DoctrineExtensions": "^1.0" }, From 9624337b374178672ee014e67d86f7182e8388cf Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 15:35:46 +0100 Subject: [PATCH 5/7] decrease phpstan level for phpstan 0.9 --- .travis.yml | 2 +- Command/InitCommand.php | 17 ++++++++++++----- Entity/BlacklistUserRepository.php | 2 +- Entity/EmailConfirmationTokenRepository.php | 4 ++-- EventListener/EmailConfirmationListener.php | 2 +- Manager/CommunityManagerInterface.php | 2 +- Manager/UserManager.php | 2 ++ composer.json | 3 ++- 8 files changed, 22 insertions(+), 12 deletions(-) diff --git a/.travis.yml b/.travis.yml index e97b5194..8d776ef5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ install: script: - ./vendor/bin/phpunit --coverage-clover=coverage.clover - - if [[ $PHPSTAN == 'true' ]]; then ./vendor/bin/phpstan analyse ./ --level 4 -c phpstan.neon ; fi + - if [[ $PHPSTAN == 'true' ]]; then ./vendor/bin/phpstan analyse ./ --level 2 -c phpstan.neon ; fi after_script: - if [[ $CODE_COVERAGE == 'true' ]]; then wget https://scrutinizer-ci.com/ocular.phar ; fi diff --git a/Command/InitCommand.php b/Command/InitCommand.php index b19db78c..33756c78 100644 --- a/Command/InitCommand.php +++ b/Command/InitCommand.php @@ -11,10 +11,12 @@ namespace Sulu\Bundle\CommunityBundle\Command; +use Doctrine\ORM\EntityManagerInterface; use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration; use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface; use Sulu\Bundle\SecurityBundle\Entity\Role; use Sulu\Bundle\SecurityBundle\Entity\RoleRepository; +use Sulu\Component\Webspace\Manager\WebspaceManagerInterface; use Sulu\Component\Webspace\Webspace; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputInterface; @@ -42,13 +44,16 @@ public function configure() */ protected function execute(InputInterface $input, OutputInterface $output) { + /** @var WebspaceManagerInterface $webspaceManager */ $webspaceManager = $this->getContainer()->get('sulu_core.webspace.webspace_manager'); + /** @var EntityManagerInterface $entityManager */ + $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); $webspaceKey = $input->getArgument('webspace'); if (null !== $webspaceKey) { $this->initWebspace($webspaceManager->findWebspaceByKey($webspaceKey), $output); - $this->getContainer()->get('doctrine.orm.entity_manager')->flush(); + $entityManager->flush(); return; } @@ -58,7 +63,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->initWebspace($webspace, $output); } - $this->getContainer()->get('doctrine.orm.entity_manager')->flush(); + $entityManager->flush(); } /** @@ -97,8 +102,8 @@ protected function initWebspace($webspace, OutputInterface $output) /** * Create a role for a specific system if not exists. * - * @param $roleName - * @param $system + * @param string $roleName + * @param string $system * * @return string */ @@ -123,7 +128,9 @@ protected function createRoleIfNotExists($roleName, $system) $role->setSystem($system); $role->setName($roleName); - $this->getContainer()->get('doctrine.orm.entity_manager')->persist($role); + /** @var EntityManagerInterface $entityManager */ + $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); + $entityManager->persist($role); return $outputMessage; } diff --git a/Entity/BlacklistUserRepository.php b/Entity/BlacklistUserRepository.php index 785ab6f8..e2b5eeb7 100644 --- a/Entity/BlacklistUserRepository.php +++ b/Entity/BlacklistUserRepository.php @@ -22,7 +22,7 @@ class BlacklistUserRepository extends EntityRepository * * @param string $token * - * @return BlacklistUser|null + * @return BlacklistUser|object|null */ public function findByToken($token) { diff --git a/Entity/EmailConfirmationTokenRepository.php b/Entity/EmailConfirmationTokenRepository.php index 00623411..88034d52 100644 --- a/Entity/EmailConfirmationTokenRepository.php +++ b/Entity/EmailConfirmationTokenRepository.php @@ -26,7 +26,7 @@ class EmailConfirmationTokenRepository extends EntityRepository * * @param string $token * - * @return EmailConfirmationToken|null + * @return EmailConfirmationToken|object|null */ public function findByToken($token) { @@ -44,7 +44,7 @@ public function findByToken($token) * * @param UserInterface $user * - * @return EmailConfirmationToken|null + * @return EmailConfirmationToken|object|null */ public function findByUser($user) { diff --git a/EventListener/EmailConfirmationListener.php b/EventListener/EmailConfirmationListener.php index feb26b41..0faacf9e 100644 --- a/EventListener/EmailConfirmationListener.php +++ b/EventListener/EmailConfirmationListener.php @@ -84,7 +84,7 @@ public function sendConfirmationOnEmailChange(CommunityEvent $event) $entity = $this->emailConformationRepository->findByUser($user); $token = $this->tokenGenerator->generateToken(); - if (null === $entity) { + if (!$entity instanceof EmailConfirmationToken) { $entity = new EmailConfirmationToken($user); $this->entityManager->persist($entity); } diff --git a/Manager/CommunityManagerInterface.php b/Manager/CommunityManagerInterface.php index eddd7d18..b98624d5 100644 --- a/Manager/CommunityManagerInterface.php +++ b/Manager/CommunityManagerInterface.php @@ -116,7 +116,7 @@ public function getConfigTypeProperty($type, $property); /** * Send email to user and admin by type. * - * @param $type + * @param string $type * @param BaseUser $user */ public function sendEmails($type, BaseUser $user); diff --git a/Manager/UserManager.php b/Manager/UserManager.php index 4cb3854c..e311334f 100644 --- a/Manager/UserManager.php +++ b/Manager/UserManager.php @@ -15,6 +15,7 @@ use Sulu\Bundle\ContactBundle\Contact\ContactManagerInterface; use Sulu\Bundle\ContactBundle\Entity\Email; use Sulu\Bundle\ContactBundle\Entity\EmailType; +use Sulu\Bundle\SecurityBundle\Entity\BaseUser; use Sulu\Bundle\SecurityBundle\Entity\User; use Sulu\Bundle\SecurityBundle\Entity\UserRole; use Sulu\Bundle\SecurityBundle\Util\TokenGeneratorInterface; @@ -204,6 +205,7 @@ protected function createUserRole(User $user, $webspaceKey, $roleName) */ public function findByPasswordResetToken($token) { + /** @var BaseUser $user */ $user = $this->userRepository->findOneBy(['passwordResetToken' => $token]); if (!$user || $user->getPasswordResetTokenExpiresAt() < new \DateTime()) { diff --git a/composer.json b/composer.json index 82876896..6866d786 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "massive/search-bundle": "@dev", "zendframework/zend-stdlib": "~2.3", "zendframework/zendsearch": "@dev", - "phpunit/phpunit": ">=4.8, <6.0" + "phpunit/phpunit": ">=4.8, <6.0", + "phpstan/phpstan": "^0.9.1" }, "keywords": [ "registration", From 3dcb18a8f692e647b6a74e5ad5f57516ba2e2190 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 15:56:33 +0100 Subject: [PATCH 6/7] set phpstan flag to false --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8d776ef5..2cfa3d95 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,7 +10,9 @@ matrix: include: - php: 5.5 env: - - COMPOSER_FLAGS="--prefer-dist --no-interaction" # --prefer-lowest not possible in sulu 1.3 + - COMPOSER_FLAGS="--prefer-dist --no-interaction" # --prefer-lowest not possible in sulu 1.3 + - CODE_COVERAGE=false + - PHPSTAN=false - php: 7.0 env: - COMPOSER_FLAGS="--prefer-dist --no-interaction" From bf59282e835996259cf4ad0815a2eee0e6e7de0c Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Mon, 4 Dec 2017 15:57:49 +0100 Subject: [PATCH 7/7] avoid phpstan for php55 --- .travis.yml | 2 -- composer.json | 3 +-- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2cfa3d95..2309f39f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,8 +11,6 @@ matrix: - php: 5.5 env: - COMPOSER_FLAGS="--prefer-dist --no-interaction" # --prefer-lowest not possible in sulu 1.3 - - CODE_COVERAGE=false - - PHPSTAN=false - php: 7.0 env: - COMPOSER_FLAGS="--prefer-dist --no-interaction" diff --git a/composer.json b/composer.json index 6866d786..82876896 100644 --- a/composer.json +++ b/composer.json @@ -18,8 +18,7 @@ "massive/search-bundle": "@dev", "zendframework/zend-stdlib": "~2.3", "zendframework/zendsearch": "@dev", - "phpunit/phpunit": ">=4.8, <6.0", - "phpstan/phpstan": "^0.9.1" + "phpunit/phpunit": ">=4.8, <6.0" }, "keywords": [ "registration",