-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f8da13c
commit 60c4c37
Showing
21 changed files
with
725 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\CommunityBundle\Controller; | ||
|
||
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Handle email confirmation. | ||
*/ | ||
class EmailConfirmationController extends AbstractController | ||
{ | ||
const TYPE = Configuration::TYPE_EMAIL_CONFIRMATION; | ||
|
||
/** | ||
* Overwrite user email with contact email. | ||
* | ||
* @param Request $request | ||
* | ||
* @return Response | ||
*/ | ||
public function indexAction(Request $request) | ||
{ | ||
$repository = $this->get('sulu_community.email_confirmation.repository'); | ||
|
||
$success = false; | ||
$token = $repository->findByToken($request->get('token')); | ||
|
||
if ($token !== null) { | ||
$user = $token->getUser(); | ||
$user->setEmail($user->getContact()->getMainEmail()); | ||
$this->get('doctrine.orm.entity_manager')->remove($token); | ||
$this->saveEntities(); | ||
|
||
$success = true; | ||
} | ||
|
||
return $this->renderTemplate(self::TYPE, ['success' => $success]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\CommunityBundle\Entity; | ||
|
||
use Sulu\Component\Security\Authentication\UserInterface; | ||
|
||
/** | ||
* Represents a email-confirmation request. | ||
*/ | ||
class EmailConfirmationToken | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $token; | ||
|
||
/** | ||
* @var UserInterface | ||
*/ | ||
private $user; | ||
|
||
/** | ||
* @param UserInterface $user | ||
*/ | ||
public function __construct(UserInterface $user) | ||
{ | ||
$this->user = $user; | ||
} | ||
|
||
/** | ||
* Returns id. | ||
* | ||
* @return int | ||
*/ | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
/** | ||
* Returns token. | ||
* | ||
* @return string | ||
*/ | ||
public function getToken() | ||
{ | ||
return $this->token; | ||
} | ||
|
||
/** | ||
* Set token. | ||
* | ||
* @param string $token | ||
* | ||
* @return $this | ||
*/ | ||
public function setToken($token) | ||
{ | ||
$this->token = $token; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns user. | ||
* | ||
* @return UserInterface | ||
*/ | ||
public function getUser() | ||
{ | ||
return $this->user; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\CommunityBundle\Entity; | ||
|
||
use Doctrine\ORM\NonUniqueResultException; | ||
use Doctrine\ORM\NoResultException; | ||
use Sulu\Component\Persistence\Repository\ORM\EntityRepository; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
/** | ||
* Provides special queries for email-confirmation-tokens. | ||
*/ | ||
class EmailConfirmationTokenRepository extends EntityRepository | ||
{ | ||
/** | ||
* Return email-confirmation for given token. | ||
* | ||
* @param string $token | ||
* | ||
* @return EmailConfirmationToken | ||
*/ | ||
public function findByToken($token) | ||
{ | ||
try { | ||
return $this->findOneBy(['token' => $token]); | ||
} catch (NonUniqueResultException $e) { | ||
return; | ||
} catch (NoResultException $e) { | ||
return; | ||
} | ||
} | ||
|
||
/** | ||
* Return email-confirmation for given token. | ||
* | ||
* @param UserInterface $user | ||
* | ||
* @return EmailConfirmationToken | ||
*/ | ||
public function findByUser($user) | ||
{ | ||
try { | ||
return $this->findOneBy(['user' => $user]); | ||
} catch (NonUniqueResultException $e) { | ||
return; | ||
} catch (NoResultException $e) { | ||
return; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\CommunityBundle\EventListener; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration; | ||
use Sulu\Bundle\CommunityBundle\Entity\EmailConfirmationToken; | ||
use Sulu\Bundle\CommunityBundle\Entity\EmailConfirmationTokenRepository; | ||
use Sulu\Bundle\CommunityBundle\Event\CommunityEvent; | ||
use Sulu\Bundle\CommunityBundle\Mail\Mail; | ||
use Sulu\Bundle\CommunityBundle\Mail\MailFactoryInterface; | ||
use Sulu\Bundle\SecurityBundle\Util\TokenGeneratorInterface; | ||
|
||
/** | ||
* Compares user-email and contact main-email. | ||
* If they are different a confirmation link will be send. | ||
*/ | ||
class EmailConfirmationListener | ||
{ | ||
/** | ||
* @var MailFactoryInterface | ||
*/ | ||
private $mailFactory; | ||
|
||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $entityManager; | ||
|
||
/** | ||
* @var TokenGeneratorInterface | ||
*/ | ||
private $tokenGenerator; | ||
|
||
/** | ||
* @var EmailConfirmationTokenRepository | ||
*/ | ||
private $emailConformationRepository; | ||
|
||
/** | ||
* @param MailFactoryInterface $mailFactory | ||
* @param EntityManagerInterface $entityManager | ||
* @param EmailConfirmationTokenRepository $emailConformationRepository | ||
* @param TokenGeneratorInterface $tokenGenerator | ||
*/ | ||
public function __construct( | ||
MailFactoryInterface $mailFactory, | ||
EntityManagerInterface $entityManager, | ||
EmailConfirmationTokenRepository $emailConformationRepository, | ||
TokenGeneratorInterface $tokenGenerator | ||
) { | ||
$this->mailFactory = $mailFactory; | ||
$this->entityManager = $entityManager; | ||
$this->emailConformationRepository = $emailConformationRepository; | ||
$this->tokenGenerator = $tokenGenerator; | ||
} | ||
|
||
/** | ||
* Send confirmation-email if email-address has changed. | ||
* | ||
* @param CommunityEvent $event | ||
*/ | ||
public function sendConfirmationOnEmailChange(CommunityEvent $event) | ||
{ | ||
$user = $event->getUser(); | ||
if ($user->getEmail() === $user->getContact()->getMainEmail()) { | ||
return; | ||
} | ||
|
||
$entity = $this->emailConformationRepository->findByUser($user); | ||
$token = $this->tokenGenerator->generateToken(); | ||
if (null === $entity) { | ||
$entity = new EmailConfirmationToken($user); | ||
$this->entityManager->persist($entity); | ||
} | ||
|
||
$entity->setToken($token); | ||
$this->entityManager->flush(); | ||
|
||
$this->mailFactory->sendEmails( | ||
Mail::create( | ||
$event->getConfigProperty(Configuration::EMAIL_FROM), | ||
$event->getConfigProperty(Configuration::EMAIL_TO), | ||
$event->getConfigTypeProperty(Configuration::TYPE_EMAIL_CONFIRMATION, Configuration::EMAIL) | ||
)->setUserEmail($user->getContact()->getMainEmail()), | ||
$user, | ||
['token' => $entity->getToken()] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.