public function registerAction() { $request = $this->getRequest(); $form = new UserForm(); $userNameConflict = false; if ($request->isPost()) { // check if the form is valid $form->setData($request->getPost()); $form->setInputFilter(new UserInputFilter()); if ($form->isValid()) { $data = $form->getData(); $userRepo = $this->getObjectManager()->getRepository(User::class); $userNameConflict = $userRepo->findOneBy(['userName' => $data['username']]) instanceof User; if ($userNameConflict) { $form->get('username')->setValue(''); } else { // if the requested username is not taken yet, create the password and redirect the user to the login $user = new User(); $user->setEmail($data['email']); $user->setUserName($data['username']); $bcrypt = new Bcrypt(); $password = $bcrypt->create($data['password']); $user->setPassword($password); $this->getObjectManager()->persist($user); $this->getObjectManager()->flush(); return $this->redirect()->toRoute('application/user', ['action' => 'login']); } } } return new ViewModel(['form' => $form, 'userNameConflict' => $userNameConflict]); }
public function indexAction() { $this->getServiceLocator()->get('ViewHelperManager')->get('HeadTitle')->set('Encontre Barato: Tenha chances de ganhar um Iphone todo mês'); $form = new UserForm(); $request = $this->getRequest(); if ($request->isPost()) { $data = $request->getPost(); $form->setData($data); if ($form->isValid()) { try { $user = new User(); $user->exchangeArray($data); $this->getUserTable()->saveUser($user); return new ViewModel(['form' => $form, 'success' => 'Cadastro efetuado com sucesso']); } catch (Exception $ex) { return new ViewModel(['form' => $form, 'error' => $ex->getMessage()]); } } } return new ViewModel(['form' => $form]); }
public function addAction() { $form = new UserForm(); $request = $this->getRequest(); $userDAO = UserDAO::getInstance($this->getServiceLocator()); if ($request->isPost()) { $post = $request->getPost()->toArray(); $form->setData($post); if ($form->isValid()) { $data = $form->getData(); $userData = new User(); $userData->setDisplayName($data['displayName']); $userData->setEmail($data['email']); $userData->setPassword(md5($data['password'])); $userData->setRole(RoleDAO::getInstance($this->getServiceLocator())->findOneById($data['role'])); $userDAO->save($userData); return $this->redirect()->toRoute('users'); } else { $form->getMessages(); } } return array('form' => $form); }
public function editAction() { $id = (int) $this->params()->fromRoute('id', 0); // id that we editing, defaults to zero $form = new UserForm(); // form used for the edit $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); $user = $em->getRepository('Application\\Entity\\User')->find($id); // if we do not have an entry for the user, i.e id not found or not defined // send them to add if (!$user) { return $this->redirect()->toRoute('admin/user', array('action' => 'add')); } // setup form // (validation, data and button) $form->setInputFilter($user->getInputFilter())->setData($user->toArray())->get('submit')->setAttribute('value', 'Edit'); // remove the original password from being displayed $form->get('password')->setLabel('Password (Leave blank to keep the old password)')->setValue(''); // process a submission if ($this->getRequest()->isPost()) { /* * The original password would have been removed from the form above to prevent it being displayed on the screen * * It will now fail validation so we need to put it back in, * * if the post submission does not contain a new password reinstate the password from the user object to password property * * if the post submission does contain a new password, encyrpt the password submission and assign it the password property * * whatever is stored in the password property will be saved each time along with the rest of the object */ $this->getRequest()->getPost()->password = $this->getRequest()->getPost()->password == '' ? $this->getRequest()->getPost()->password = $user->password : md5($this->getRequest()->getPost()->password); $form->setData($this->getRequest()->getPost()); // set the form with the submitted values // is valid? if ($form->isValid()) { $user->setOptions($form->getData()); // set the data $user->id = $id; $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager'); // entity manager $em->persist($user); // set data $em->flush(); // save $this->flashMessenger()->addMessage(array('alert-success' => 'Updated!')); // Redirect to list of users return $this->redirect()->toRoute('admin/user'); } else { //$this->flashMessenger()->addMessage(array('alert-error'=>'Form error')); } } return array('id' => $id, 'form' => $form); }