public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); if (!$id) { return $this->redirect()->toRoute('user', array('action' => 'add')); } try { $user = $this->getUserTable()->getUser($id); } catch (\Exception $ex) { return $this->redirect()->toRoute('user', array('action' => 'index')); } $form = new UserForm(); $form->bind($user); $form->get('submit')->setAttribute('value', 'Edit'); $request = $this->getRequest(); if ($request->isPost()) { $form->setInputFilter($user->getInputFilter()); $form->setData($request->getPost()); if ($form->isValid()) { $this->getUserTable()->saveUser($user); return $this->redirect()->toRoute('user'); } } return array('id' => $id, '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() { $form = new UserForm(); $request = $this->getRequest(); if ($request->isPost()) { $User = new User(); $form->setInputFilter($User->getInputFilter()); $form->setData($request->getPost()); $User->user_nom = $request->getPost('User_nom'); $User->user_prenom = $request->getPost('User_prenom'); $User->user_password = $request->getPost('Passwd'); $bcrypt = new Bcrypt(); $securePass = $bcrypt->create($User->user_password); $User->user_password = $securePass; // Sauvegarde $user_id = $this->getUserTable()->saveUser($User); return $this->redirect()->toRoute('User', array('user_id' => $user_id, 'action' => 'index')); } return array('form' => $form, 'erreur' => @$erreur); }
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()); }