Skip to content

Commit

Permalink
Added duplicate email check and reduced nesting on user create
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjroberts committed Sep 16, 2013
1 parent a6c8318 commit 9a1cbf9
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/Message/Mothership/User/Controller/User/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,34 @@ public function newUserFormProcess()
{
$form = $this->newUserForm();

if ($form->isValid() && $data = $form->getFilteredData()) {
$user = $this->get('user');
$user->title = $data['title'];
$user->forename = $data['forename'];
$user->surname = $data['surname'];
$user->email = $data['email'];
$user->password = $data['password'];

if($user = $this->get('user.create')->save($user)) {

$this->addFlash('success', 'Successfully added new account');

return $this->redirectToRoute('ms.cp.user.admin.detail.edit', array('userID' => $user->id));
// Check if the form is valid and attempt to get the data
if (false === $form->isValid() || false == $data = $form->getFilteredData()) {
return $this->redirectToRoute('ms.cp.user.admin.create');
}

} else {
$this->addFlash('error', 'Account could not be added');
}
// Check if the user email already exists
if (null !== $this->get('user.loader')->getByEmail($data['email'])) {
$this->addFlash('error', 'A user already exists with the email address "%s"', $data['email']);
return $this->redirectToRoute('ms.cp.user.admin.create');
}

// Create the user
$user = $this->get('user');
$user->title = $data['title'];
$user->forename = $data['forename'];
$user->surname = $data['surname'];
$user->email = $data['email'];
$user->password = $data['password'];

// Attempt to save the user
if (false === $user = $this->get('user.create')->save($user)) {
$this->addFlash('error', 'Account could not be added');
return $this->redirectToRoute('ms.cp.user.admin.create');
}

return $this->render('Message:Mothership:User::User:create', array(
'newuser' => $form,
));
$this->addFlash('success', 'Successfully added new account');

return $this->redirectToRoute('ms.cp.user.admin.detail.edit', array('userID' => $user->id));
}

}

0 comments on commit 9a1cbf9

Please sign in to comment.