/**
  * Create new user
  *
  * @return \Zend\Http\Response|ViewModel
  */
 public function registerAction()
 {
     $form = new FormUser();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $service = $this->getServiceLocator()->get('User\\Service\\User');
             $validator = new ObjectExists(array('object_repository' => $this->getEm()->getRepository('User\\Entity\\User'), 'fields' => array('email')));
             if ($validator->isValid(array('email' => $request->getPost()->toArray()))) {
                 $this->flashMessenger()->addErrorMessage('Email Já cadastrado em nosso sistemas!');
                 if ($this->flashMessenger()->hasErrorMessages()) {
                     return new ViewModel(['form' => $form, 'error' => $this->flashMessenger()->getErrorMessages()]);
                 }
                 return new ViewModel(['form' => $form, 'error' => $this->flashMessenger()->getErrorMessages()]);
             }
             if ($service->insert($request->getPost()->toArray())) {
                 $this->flashMessenger()->addSuccessMessage('Usuário cadastrado com sucesso! Eviamos uma ativação para seu email!!!');
             } else {
                 $this->flashMessenger()->addErrorMessage('Erro ao fazer o cadastro!!!');
             }
             return $this->redirect()->toRoute('user-register', ['controller' => 'User\\Controller\\Index']);
         }
     }
     if ($this->flashMessenger()->hasSuccessMessages()) {
         return new ViewModel(['form' => $form, 'success' => $this->flashMessenger()->getSuccessMessages()]);
     }
     if ($this->flashMessenger()->hasErrorMessages()) {
         return new ViewModel(['form' => $form, 'error' => $this->flashMessenger()->getErrorMessages()]);
     }
     return new ViewModel(['form' => $form]);
 }
 public function loginAction()
 {
     $userForm = new UserForm();
     $userForm->get('submit')->setValue('Sign In');
     $userForm->setAttribute('action', '/user')->prepare();
     $request = $this->getRequest();
     if (!$request->isPost()) {
         return new ViewModel(array('userForm' => $userForm));
     }
     $userFormData = $request->getPost();
     $userForm->setData($userFormData);
     if (!$userForm->isValid()) {
         return new ViewModel(array('userForm' => $userForm));
     }
     $authAdapter = $this->getAuthManager()->getAdapter();
     $authAdapter->setIdentityValue($userFormData['user']['email']);
     $authAdapter->setCredentialValue($userFormData['user']['password']);
     $this->getAuthManager()->authenticate();
     if (!$this->identity()) {
         $this->flashmessenger()->addMessage('Invalid credential.');
     }
     return new ViewModel(array('userForm' => $userForm, 'messages' => $this->flashmessenger()->getMessages()));
 }