public function addAction()
 {
     $form = new UserForm();
     $form->get('submit')->setValue('Add');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $user = new User();
         $form->setInputFilter($user->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $user->exchangeArray($form->getData());
             $this->getUserTable()->saveUser($user);
             return $this->redirect()->toRoute('user');
         }
     }
     return array('form' => $form);
 }
 public function loginAction()
 {
     $authStorage = new AuthenticationStorage(self::NAMESPACE_ZENDSTORE_FRONT);
     $authService = new AuthenticationService($authStorage);
     if ($authService->hasIdentity()) {
         echo 'You have logined';
         exit;
     }
     $form = new UserForm();
     $request = $this->getRequest();
     if ($request->isPost()) {
         $user = new User();
         $form->setInputFilter($user->getInputFilter());
         $form->setData($request->post());
         if ($form->isValid()) {
             $data = $form->getData();
             // Authentication
             $sm = $this->getServiceLocator();
             $db = $sm->get('db-adapter');
             //$authAdapter = new AuthenticationAdapter($db, 'user', 'email', 'password', 'MD5(?)');
             $authAdapter = new AuthenticationAdapter($db, 'user', 'email', 'password', 'MD5(CONCAT(?, password_salt))');
             $authAdapter->setIdentity($data['email']);
             $authAdapter->setCredential($data['password']);
             $result = $authService->authenticate($authAdapter);
             if ($result->isValid()) {
                 return $this->redirect()->toRoute('user-front-user');
             } else {
                 var_dump($result->getMessages());
                 exit;
             }
         } else {
             echo '<h1>ERROR: Form data is invalid.</h1>';
             echo '<pre>';
             print_r($form->getMessages());
             exit;
         }
     }
     $viewVars = array('form' => $form);
     $viewModel = $this->getViewModel();
     $viewModel->setVariables($viewVars);
     return $viewModel;
 }
 public function addAction()
 {
     //        $user_session = new \Zend\Session\Container('user');
     //        if ($user_session->email == null) {
     //            return $this->redirect()->toRoute('signin', array(
     //                        'action' => 'signin'
     //            ));
     //        }
     //        if ($user_session->role == 'Operator') {
     //            return array('error' => 'You don\'t have permission to do this action');
     //        }
     $auth = new \Zend\Authentication\AuthenticationService();
     if ($auth->hasIdentity()) {
         $user = $auth->getIdentity();
     } else {
         return $this->redirect()->toRoute('signin', array('action' => 'index'));
     }
     if ($user->role == 'Operator') {
         //$this->flashMessenger()->addMessage('You don\'t have permission to do this action');
         //$this->redirect()->toRoute('add');
         return array('error' => 'You don\'t have permission to do this action');
     }
     $form = new UserForm(null, $user->role);
     $form->get('submit')->setValue('Add');
     $request = $this->getRequest();
     if ($request->isPost()) {
         $user = new User();
         $form->setInputFilter($user->getInputFilter());
         $form->setData($request->getPost());
         if ($form->isValid()) {
             $user->exchangeArray($form->getData());
             $this->getUserTable()->saveUser($user);
             return $this->redirect()->toRoute('user');
         } else {
             echo "Fail to add new user";
         }
     }
     return array('form' => $form, 'flashMessages' => $this->flashMessenger()->getMessages());
 }