Permalink
Browse files

Merge remote-tracking branch 'bjyoungblood/feature/refactor-usercontr…

…oller'

PR #116
  • Loading branch information...
2 parents cb19738 + c01c06c commit 92d78929fb3df5e3ee7a962f221d4777b0cb7a4d @EvanDotPro EvanDotPro committed Aug 15, 2012
View
@@ -110,13 +110,26 @@ public function getServiceConfig()
return $form;
},
- 'zfcuser_change_password_form' => function($sm) {
+ 'zfcuser_change_password_form' => function($sm) {
$options = $sm->get('zfcuser_module_options');
$form = new Form\ChangePassword(null, $sm->get('zfcuser_module_options'));
$form->setInputFilter(new Form\ChangePasswordFilter($options));
return $form;
},
+ 'zfcuser_change_email_form' => function($sm) {
+ $options = $sm->get('zfcuser_module_options');
+ $form = new Form\ChangeEmail(null, $sm->get('zfcuser_module_options'));
+ $form->setInputFilter(new Form\ChangeEmailFilter(
+ $options,
+ new Validator\NoRecordExists(array(
+ 'mapper' => $sm->get('zfcuser_user_mapper'),
+ 'key' => 'email'
+ ))
+ ));
+ return $form;
+ },
+
'zfcuser_user_hydrator' => function ($sm) {
$hydrator = new \Zend\Stdlib\Hydrator\ClassMethods();
return $hydrator;
View
@@ -77,12 +77,34 @@
'changepassword' => array(
'type' => 'Literal',
'options' => array(
- 'route' => '/changepassword',
+ 'route' => '/change-password',
'defaults' => array(
'controller' => 'zfcuser',
'action' => 'changepassword',
),
),
+ 'may_terminate' => true,
+ 'child_routes' => array(
+ 'query' => array(
+ 'type' => 'Query',
+ ),
+ ),
+ ),
+ 'changeemail' => array(
+ 'type' => 'Literal',
+ 'options' => array(
+ 'route' => '/change-email',
+ 'defaults' => array(
+ 'controller' => 'zfcuser',
+ 'action' => 'changeemail',
+ ),
+ ),
+ 'may_terminate' => true,
+ 'child_routes' => array(
+ 'query' => array(
+ 'type' => 'Query',
+ ),
+ ),
),
),
),
@@ -33,6 +33,11 @@ class UserController extends AbstractActionController
protected $changePasswordForm;
/**
+ * @var Form
+ */
+ protected $changeEmailForm;
+
+ /**
* @todo Make this dynamic / translation-friendly
* @var string
*/
@@ -143,7 +148,9 @@ public function authenticateAction()
*/
public function registerAction()
{
+ // if the user is logged in, we don't need to register
if ($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {
+ // redirect to the login redirect route
return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
}
@@ -157,23 +164,20 @@ public function registerAction()
$redirect = false;
}
- if ($request->isPost()) {
- $this->flashMessenger()->setNamespace('zfcuser-register-form')->addMessage($request->getPost()->toArray());
- // See http://en.wikipedia.org/wiki/Post/Redirect/Get
- return $this->redirect()->toUrl($this->url()->fromRoute('zfcuser/register') . ($redirect ? '?redirect='.$redirect : ''));
- }
+ $redirectUrl = $this->url()->fromRoute('zfcuser/register') . ($redirect ? '?redirect=' . $redirect : '');
+ $prg = $this->prg($redirectUrl, true);
- $post = $this->flashMessenger()->setNamespace('zfcuser-register-form')->getMessages();
-
- if (!isset($post[0]) || !$service->getOptions()->getEnableRegistration()) {
+ if ($prg instanceof Response) {
+ return $prg;
+ } else if ($prg === false) {
return array(
'registerForm' => $form,
'enableRegistration' => $this->getOptions()->getEnableRegistration(),
'redirect' => $redirect,
);
}
- $post = $post[0];
+ $post = $prg;
$user = $service->register($post);
if (!$user) {
@@ -204,53 +208,68 @@ public function registerAction()
* Change the users password
*/
public function changepasswordAction() {
- $this->getServiceLocator()->get('zfcuser_user_mapper');
- $request = $this->getRequest();
$form = $this->getChangePasswordForm();
- if ($this->getOptions()->getUseRedirectParameterIfPresent() && $request->getQuery()->get('redirect')) {
- $redirect = $request->getQuery()->get('redirect');
- } else {
- $redirect = false;
- }
-
- if (!$request->isPost()) {
+ $prg = $this->prg('zfcuser/changepassword');
+ if ($prg instanceof Response) {
+ return $prg;
+ } else if ($prg === false) {
return array(
'changePasswordForm' => $form,
- 'redirect' => $redirect,
);
}
- $form->setData($request->getPost());
+ $form->setData($prg);
if (!$form->isValid()) {
- //TODO implement real error messages
- $this->flashMessenger()->setNamespace('zfcuser-change-password-form')->addMessage("ERROR");
- return $this->redirect()->toUrl($this->url()->fromRoute('zfcuser/changepassword'));
+ return array(
+ 'changePasswordForm' => $form,
+ );
}
- //check current user against supplied credential
- $adapter = $this->zfcUserAuthentication()->getAuthAdapter();
- $redirect = $request->getPost()->get('redirect') ? $request->getPost()->get('redirect') : false;
- $result = $adapter->prepareForAuthentication($request);
+ if (!$this->getUserService()->changePassword($form->getData())) {
+ $form->setMessages(array('credential' => array('Invalid password')));
+ return array(
+ 'changePasswordForm' => $form,
+ );
+ }
- // Return early if an adapter returned a response
- if ($result instanceof Response) {
- return $result;
+ return $this->redirect()->toRoute('zfcuser/changepassword/query', array('success' => 1));
+ }
+
+ public function changeEmailAction()
+ {
+ $form = $this->getChangeEmailForm();
+ $request = $this->getRequest();
+ $request->getPost()->set('identity', $this->getUserService()->getAuthService()->getIdentity()->getEmail());
+
+ $prg = $this->prg('zfcuser/changeemail');
+ if ($prg instanceof Response) {
+ return $prg;
+ } else if ($prg === false) {
+ return array(
+ 'changeEmailForm' => $form,
+ );
}
- $auth = $this->zfcUserAuthentication()->getAuthService()->authenticate($adapter);
+ $form->setData($prg);
- if (!$auth->isValid()) {
- $this->flashMessenger()->setNamespace('zfcuser-change-password-form')->addMessage("Wrong Credentials");
- $adapter->resetAdapters();
- return $this->redirect()->toUrl($this->url()->fromRoute('zfcuser/changepassword'));
+ if (!$form->isValid()) {
+ return array(
+ 'changeEmailForm' => $form,
+ );
}
- $this->getUserService()->changePassword($form->getData());
+ $change = $this->getUserService()->changeEmail($prg);
- // clear adapters
- return $this->forward()->dispatch('zfcuser', array('action' => 'authenticate'));
+ if (!$change) {
+ $form->setMessages(array('credential' => array('Invalid password')));
+ return array(
+ 'changeEmailForm' => $form,
+ );
+ }
+
+ return $this->redirect()->toRoute('zfcuser/changeemail/query', array('success' => 1));
}
/**
@@ -304,7 +323,7 @@ public function setLoginForm(Form $loginForm)
return $this;
}
- public function getChangePasswordForm() {
+ public function getChangePasswordForm() {
if (!$this->changePasswordForm) {
$this->setChangePasswordForm($this->getServiceLocator()->get('zfcuser_change_password_form'));
}
@@ -313,12 +332,6 @@ public function getChangePasswordForm() {
public function setChangePasswordForm(Form $changePasswordForm) {
$this->changePasswordForm = $changePasswordForm;
- $fm = $this->flashMessenger()->setNamespace('zfcuser-change-password-form')->getMessages();
- if (isset($fm[0])) {
- $this->changePasswordForm->setMessages(
- array('identity' => array($fm[0]))
- );
- }
return $this;
}
@@ -346,4 +359,28 @@ public function getOptions()
}
return $this->options;
}
+
+ /**
+ * Get changeEmailForm.
+ *
+ * @return changeEmailForm.
+ */
+ public function getChangeEmailForm()
+ {
+ if (!$this->changePasswordForm) {
+ $this->setChangeEmailForm($this->getServiceLocator()->get('zfcuser_change_email_form'));
+ }
+ return $this->changeEmailForm;
+ }
+
+ /**
+ * Set changeEmailForm.
+ *
+ * @param changeEmailForm the value to set.
+ */
+ public function setChangeEmailForm($changeEmailForm)
+ {
+ $this->changeEmailForm = $changeEmailForm;
+ return $this;
+ }
}
@@ -0,0 +1,80 @@
+<?php
+
+namespace ZfcUser\Form;
+
+use ZfcBase\Form\ProvidesEventsForm;
+use ZfcUser\Options\RegistrationOptionsInterface;
+use ZfcUser\Options\AuthenticationOptionsInterface;
+
+class ChangeEmail extends ProvidesEventsForm
+{
+ public function __construct($name = null, AuthenticationOptionsInterface $options)
+ {
+ $this->setAuthenticationOptions($options);
+ parent::__construct($name);
+
+ $this->add(array(
+ 'name' => 'identity',
+ 'options' => array(
+ 'label' => '',
+ ),
+ 'attributes' => array(
+ 'type' => 'hidden',
+ ),
+ ));
+
+ $this->add(array(
+ 'name' => 'newIdentity',
+ 'options' => array(
+ 'label' => 'New Email',
+ ),
+ 'attributes' => array(
+ 'type' => 'text',
+ ),
+ ));
+
+ $this->add(array(
+ 'name' => 'newIdentityVerify',
+ 'options' => array(
+ 'label' => 'Verify New Email',
+ ),
+ 'attributes' => array(
+ 'type' => 'text',
+ ),
+ ));
+
+ $this->add(array(
+ 'name' => 'credential',
+ 'options' => array(
+ 'label' => 'Password',
+ ),
+ 'attributes' => array(
+ 'type' => 'password',
+ ),
+ ));
+
+ $this->getEventManager()->trigger('init', $this);
+ }
+
+ /**
+ * Set Authentication-related Options
+ *
+ * @param AuthenticationOptionsInterface $authOptions
+ * @return Login
+ */
+ public function setAuthenticationOptions(AuthenticationOptionsInterface $authOptions)
+ {
+ $this->authOptions = $authOptions;
+ return $this;
+ }
+
+ /**
+ * Get Authentication-related Options
+ *
+ * @return AuthenticationOptionsInterface
+ */
+ public function getAuthenticationOptions()
+ {
+ return $this->authOptions;
+ }
+}
Oops, something went wrong.

0 comments on commit 92d7892

Please sign in to comment.