Skip to content

Instantly share code, notes, and snippets.

@awartoft
Created April 15, 2013 12:06
Show Gist options
  • Save awartoft/5387605 to your computer and use it in GitHub Desktop.
Save awartoft/5387605 to your computer and use it in GitHub Desktop.
<?php
/**
* @author Antoine Hedgecock <[email protected]>
*
* @copyright PMG Media Group AB
*/
namespace User\Controller;
use MCNStdlib\Interfaces\UserServiceInterface;
use MCNUser\Service\Exception\TokenHasExpiredException;
use MCNUser\Service\Exception\TokenNotFoundException;
use User\Service\Exception\AccountAlreadyActivatedException;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use User\Entity\AbstractUser;
use User\Service\Account as AccountService;
/**
* Class AccountController
* @package User\Controller
*
* @method \Zend\Http\Response getResponse
* @method \Zend\Http\Request getRequest
*/
class AccountController extends AbstractActionController
{
/**
* @var \User\Service\User
*/
protected $userService;
/**
* @var \User\Service\Account
*/
protected $accountService;
/**
* @param \User\Service\Account $tokenService
* @param \MCNStdlib\Interfaces\UserServiceInterface $userService
*/
public function __construct(AccountService $tokenService, UserServiceInterface $userService)
{
$this->userService = $userService;
$this->tokenService = $tokenService;
}
/**
* Get the base user registration form
*
* @codeCoverageIgnore
* @see \User\Factory\Form\Register\BaseUser
* @return \Zend\Form\Form
*/
protected function getRegisterForm()
{
return $this->getServiceLocator()->get('user.form.registration.base-user');
}
/**
* Get the form for lost accounts
*
* @codeCoverageIgnore
* @see \User\Factory\Form\LostPassword
* @return \Zend\Form\Form
*/
protected function getLostPasswordForm()
{
return $this->getServiceLocator()->get('user.form.lost-password');
}
/**
* Confirm the users email account
*
* Validates the token and if valid confirms the users email
*
* @return \Zend\Http\Response|ViewModel
*/
public function confirmEmailAction()
{
$id = $this->params('id');
$token = $this->params('token');
$user = $this->userService->getById($id);
if (! $user) {
return $this->getResponse()->setStatusCode(404);
}
try {
$this->accountService->confirmEmail($user, $token);
} catch(AccountAlreadyActivatedException $e) {
return $this->message(
'Info',
'Your account has already been confirmed.'
);
} catch(TokenNotFoundException $e) {
return $this->message(
'Confirmation error',
'Invalid confirmation token specified.'
);
}
return $this->message(
'Account confirmed',
'Your account has now been confirmed.'
);
}
/**
* Registration
*
* todo: add validation on selecting entity class
*
* @return ViewModel
*/
public function registerAction()
{
/**
* @var $form Form
* @var $post \Zend\Stdlib\ParametersInterface
*/
$form = $this->getRegisterForm();
$post = $this->getRequest()->getPost();
$viewModel = new ViewModel();
$viewModel->setTemplate('user/register/form');
$viewModel->setVariable('form', $form);
if ($this->getRequest()->isPost()) {
$class = $post->get('type') == 'entrepreneur' ? '\\User\Entity\User\Entrepreneur'
: '\\User\Entity\User\Investor';
$user = new $class();
$form->bind($user);
$form->setData($post->toArray());
if ($form->isValid()) {
$user->setPassword($this->userService->generatePasswordHash($form->getValue('password')));
$this->userService->save($user);
$viewModel->setTemplate('user/account/register-success');
}
}
return $viewModel;
}
/**
* Send a new activation email
*
* Checks if a account is still unconfirmed and if so sends an activation email
*
* @return ViewModel
*/
public function sendEmailConfirmationAction()
{
$id = $this->params('id');
$user = $this->userService->getById($id);
if (! $user) {
return $this->getResponse()->setStatusCode(404);
}
try {
$this->accountService->sendActivationEmail($user);
} catch(AccountAlreadyActivatedException $e) {
return $this->message(
'Error',
'Your account has already been activated'
);
}
return $this->message(
'Request sent',
'An confirmation email has been sent to your email address.'
);
}
/**
* Request password to be reset
*
* @return \Zend\View\Model\ViewModel
*/
public function requestResetPasswordAction()
{
$form = $this->getLostPasswordForm();
$vm = new ViewModel();
$vm->setVariable('form', $form);
if ($this->getRequest()->isPost()) {
$form->setData($this->getRequest()->getPost()->toArray());
if ($form->isValid()) {
$this->accountService->sendResetPasswordRequest(
$this->userService->getByEmail($form->get('email')->getValue())
);
return $this->message(
'Confirmation sent',
'An email has been sent to your e-mail account to confirm your request to change password.'
);
}
}
return $vm;
}
/**
* Confirm password to be reset
*
* @return \Zend\View\Model\ViewModel
*/
public function confirmLostPasswordRequestAction()
{
$id = $this->params('id');
$token = $this->params('token');
$user = $this->userService->getById($id);
if (! $user) {
return $this->getResponse()->setStatusCode(404);
}
try {
$this->accountService->confirmAndResetPassword($user, $token);
} catch (TokenNotFoundException $e) {
return $this->message(
'Error',
'Invalid authentication token specified.'
);
} catch (TokenHasExpiredException $e) {
return $this->message(
'Error',
'The link was only valid for 48 hours and has already expired.'
);
}
return $this->message(
'Password reset',
'Your password has been reset and your new password has been sent to your email.'
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment