Skip to content

Commit

Permalink
system added to add a mail after user import if no valid mail given
Browse files Browse the repository at this point in the history
  • Loading branch information
kokspflanze committed Nov 7, 2015
1 parent 2fa42af commit a70f25b
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
'email/tpl/country' => __DIR__ . '/../view/email/tpl/country.phtml',
'email/tpl/secretLogin' => __DIR__ . '/../view/email/tpl/secret_login.phtml',
'email/tpl/ticketAnswer' => __DIR__ . '/../view/email/tpl/ticket_answer.phtml',
'email/tpl/addEmail' => __DIR__ . '/../view/email/tpl/add_email.phtml',
'helper/sidebarWidget' => __DIR__ . '/../view/helper/sidebar.phtml',
'helper/sidebarLoggedInWidget' => __DIR__ . '/../view/helper/logged-in.phtml',
'helper/sidebarServerInfoWidget' => __DIR__ . '/../view/helper/server-info.phtml',
Expand Down Expand Up @@ -394,6 +395,7 @@
'register' => null,
'password' => null,
'country' => null,
'add_email' => null,
'secret_login' => 60
]
],
Expand Down
5 changes: 5 additions & 0 deletions src/PServerCMS/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function indexAction()

public function addEmailAction()
{
$this->getAddEmailService()->addEmail(
$this->params()->fromPost(),
$this->getUserService()->getAuthService()->getIdentity()
);

return $this->redirect()->toRoute('PServerCMS/user', ['action' => 'index']);
}

Expand Down
14 changes: 14 additions & 0 deletions src/PServerCMS/Controller/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,20 @@ public function wrongCodeAction()
return [];
}

public function addEmailAction()
{
$code = $this->params()->fromRoute('code');

$codeEntity = $this->getCode4Data($code, UserCodes::TYPE_ADD_EMAIL);
if (!$codeEntity) {
return $this->forward()->dispatch('PServerCMS\Controller\Auth', ['action' => 'wrong-code']);
}
$user = $this->getAddEmailService()->changeMail($codeEntity->getUser());
$this->getUserCodesService()->deleteCode($codeEntity);

$this->getUserService()->doAuthentication($user);
}

protected function getCode4Data($code, $type)
{
$entityManager = $this->getEntityManager();
Expand Down
1 change: 1 addition & 0 deletions src/PServerCMS/Entity/UserCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class UserCodes
const TYPE_LOST_PASSWORD = 'password';
const TYPE_CONFIRM_COUNTRY = 'country';
const TYPE_SECRET_LOGIN = 'secret_login';
const TYPE_ADD_EMAIL = 'add_email';
const EXPIRE_DEFAULT = 86400;

/**
Expand Down
2 changes: 2 additions & 0 deletions src/PServerCMS/Entity/UserExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
class UserExtension
{
const KEY_ADD_EMAIL = 'add_email';

/**
* @var integer
* @ORM\Column(name="id", type="integer", nullable=false)
Expand Down
8 changes: 8 additions & 0 deletions src/PServerCMS/Helper/HelperService.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ protected function getIpService()
return $this->getService('payment_api_ip_service');
}

/**
* @return \PServerCMS\Service\AddEmail
*/
protected function getAddEmailService()
{
return $this->getService('pserver_add_email_service');
}

/**
* @param $serviceName
*
Expand Down
80 changes: 78 additions & 2 deletions src/PServerCMS/Service/AddEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,103 @@
namespace PServerCMS\Service;


use PServerCMS\Entity\UserCodes;
use PServerCMS\Entity\UserInterface;

class AddEmail extends InvokableBase
{
const ERROR_NAMESPACE = 'pserver-user-account-error';
const ERROR_NAMESPACE = 'pserver-user-account-errorAddEmail';
const SUCCESS_NAMESPACE = 'pserver-user-account-successAddEmail';

/**
* @param $data
* @param UserInterface $user
* @return bool
*/
public function addEmail($data, UserInterface $user)
{
$form = $this->getAddEmailForm();
$form->setData($data);
if (!$form->isValid()) {
foreach ($form->getElements() as $messages) {
/** @var \Zend\Form\ElementInterface $messages */
foreach ($messages->getMessages() as $message) {
$this->getFlashMessenger()
->setNamespace(self::ERROR_NAMESPACE)
->addMessage($message);
}
}

return false;
}

$user = $this->getUser4Id($user->getId());
if ($user->getEmail()) {
$this->getFlashMessenger()->setNamespace(self::ERROR_NAMESPACE)->addMessage('Email already exists in your Account');
$this->getFlashMessenger()
->setNamespace(self::ERROR_NAMESPACE)
->addMessage('Email already exists in your Account');
return false;
}

$data = $form->getData();
$entityManager = $this->getEntityManager();

if ($this->getRegisterOptions()->isMailConfirmation()) {
$userExtensionName = $this->getEntityOptions()->getUserExtension();
/** @var \PServerCMS\Entity\UserExtension $userExtension */
$userExtension = new $userExtensionName;

/** @var \PServerCMS\Entity\Repository\UserExtension $extensionRepository */
$extensionRepository = $entityManager->getRepository($userExtensionName);
$extensionRepository->deleteExtension($user, $userExtension::KEY_ADD_EMAIL);

$userExtension->setKey($userExtension::KEY_ADD_EMAIL)
->setUser($user)
->setValue($data['email']);

$entityManager->persist($userExtension);
$entityManager->flush();

$code = $this->getUserCodesService()->setCode4User($user, UserCodes::TYPE_ADD_EMAIL);
$user->setEmail($data['email']);

$this->getMailService()->addEmail($user, $code);

$this->getFlashMessenger()
->setNamespace(self::SUCCESS_NAMESPACE)
->addMessage('Success, please confirm your mail.');
} else {
$user->setEmail($data['email']);
$entityManager->persist($user);
$entityManager->flush();

$this->getAuthService()->getStorage()->write($user);
}

return true;
}

/**
* @param UserInterface $user
* @return UserInterface
*/
public function changeMail(UserInterface $user)
{
$entityManager = $this->getEntityManager();
$userExtensionName = $this->getEntityOptions()->getUserExtension();
/** @var \PServerCMS\Entity\UserExtension $userExtension */
$userExtension = new $userExtensionName;
/** @var \PServerCMS\Entity\Repository\UserExtension $extensionRepository */
$extensionRepository = $entityManager->getRepository($userExtensionName);
$userExtension = $extensionRepository->findOneBy(['key' => $userExtension::KEY_ADD_EMAIL]);

$user->setEmail($userExtension->getValue());
$entityManager->persist($user);
$entityManager->flush();

$extensionRepository->deleteExtension($user, $userExtension::KEY_ADD_EMAIL);

return $user;
}

}
15 changes: 15 additions & 0 deletions src/PServerCMS/Service/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Mail extends InvokableBase
const SUBJECT_KEY_CONFIRM_COUNTRY = 'country';
const SUBJECT_KEY_SECRET_LOGIN = 'secretLogin';
const SUBJECT_KEY_TICKET_ANSWER = 'ticketAnswer';
const SUBJECT_KEY_ADD_EMAIL = 'addEmail';

/**
* @var SmtpOptions
Expand Down Expand Up @@ -100,6 +101,20 @@ public function ticketAnswer(UserInterface $user, TicketSubject $ticketSubject,
$this->send(self::SUBJECT_KEY_TICKET_ANSWER, $user, $params);
}

/**
* @param UserInterface $user
* @param $code
*/
public function addEmail(UserInterface $user, $code)
{
$params = [
'user' => $user,
'code' => $code
];

$this->send(self::SUBJECT_KEY_ADD_EMAIL, $user, $params);
}

/**
* @param $subjectKey
* @param UserInterface $user
Expand Down
6 changes: 6 additions & 0 deletions view/email/tpl/add_email.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php include '_salutation.phtml' ?>
Please confirm your email by clicking on the following link.<br />
<br />
<a href="<?= $this->url('small-user-auth', array('action' => 'add-email', 'code' => $this->code), array('force_canonical' => true)); ?>">
<?= $this->url('small-user-auth', array('action' => 'add-email', 'code' => $this->code), array('force_canonical' => true)); ?>
</a>
4 changes: 3 additions & 1 deletion view/p-server-cms/account/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
{% endfor %}
</div>
{% endif %}
{% set a = addEmailForm.changeWebPwdForm('action', url('PServerCMS/user', {'action' : 'index'})) %}
{{ formWidget(changeWebPwdForm, 'helper/formNoLabelWidget') }}
</div>
{% endif %}
Expand All @@ -42,6 +43,7 @@
{% endfor %}
</div>
{% endif %}
{% set a = addEmailForm.changeIngamePwdForm('action', url('PServerCMS/user', {'action' : 'index'})) %}
{{ formWidget(changeIngamePwdForm, 'helper/formNoLabelWidget') }}
</div>

Expand All @@ -55,7 +57,7 @@
{% if errorsAddEmail %}
<div class="alert alert-danger" role="alert">
{% for error in errorsAddEmail %}
{{ error }}
{{ error }}<br />
{% endfor %}
</div>
{% elseif messagesAddEmail %}
Expand Down
11 changes: 11 additions & 0 deletions view/p-server-cms/auth/add-email.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'layout/layout' %}

{% block title %}
{{ translate('Add-Email') }}
{% endblock %}

{% block content %}

{{ translate('success, your mail is changed') }}

{% endblock content %}

0 comments on commit a70f25b

Please sign in to comment.