Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and update correct email entity for backend edit #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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
- php: 7.0
env:
- COMPOSER_FLAGS="--prefer-dist --no-interaction"
Expand All @@ -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
Expand Down
17 changes: 12 additions & 5 deletions Command/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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
*/
Expand All @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions Controller/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Entity/BlacklistUserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BlacklistUserRepository extends EntityRepository
*
* @param string $token
*
* @return BlacklistUser|null
* @return BlacklistUser|object|null
*/
public function findByToken($token)
{
Expand Down
4 changes: 2 additions & 2 deletions Entity/EmailConfirmationTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class EmailConfirmationTokenRepository extends EntityRepository
*
* @param string $token
*
* @return EmailConfirmationToken|null
* @return EmailConfirmationToken|object|null
*/
public function findByToken($token)
{
Expand All @@ -44,7 +44,7 @@ public function findByToken($token)
*
* @param UserInterface $user
*
* @return EmailConfirmationToken|null
* @return EmailConfirmationToken|object|null
*/
public function findByUser($user)
{
Expand Down
2 changes: 1 addition & 1 deletion EventListener/EmailConfirmationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions Manager/CommunityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Manager/CommunityManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
37 changes: 30 additions & 7 deletions Manager/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,27 +115,48 @@ 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);
$user->addUserRole($userRole);

// 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}
*/
Expand Down Expand Up @@ -183,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()) {
Expand Down
9 changes: 9 additions & 0 deletions Manager/UserManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions Tests/Functional/Controller/ProfileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand All @@ -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'));
Expand All @@ -80,6 +92,7 @@ public function testProfile()
'profile[contact][formOfAddress]' => 0,
'profile[contact][firstName]' => 'Hikaru',
'profile[contact][lastName]' => 'Sulu',
'profile[contact][mainEmail]' => '[email protected]',
'profile[contact][contactAddresses][0][address][street]' => 'Rathausstraße',
'profile[contact][contactAddresses][0][address][number]' => 16,
'profile[contact][contactAddresses][0][address][zip]' => 12351,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
"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"
},
"require-dev": {
"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"
Expand Down