Esempio n. 1
0
 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 editAction()
 {
     $form = new UserForm();
     $userId = (int) $this->params()->fromRoute('id', '');
     $request = $this->getRequest();
     $view = $request->getQuery()->view;
     $userDAO = UserDAO::getInstance($this->getServiceLocator());
     if (!empty($userId)) {
         $editableUser = $userDAO->findOneById($userId);
         if ($editableUser !== null) {
             if (!$request->isPost()) {
                 $userData = array('displayName' => $editableUser->getDisplayName(), 'email' => $editableUser->getEmail(), 'password' => $editableUser->getPassword(), 'role' => $editableUser->getRole()->getId());
                 $form->setData($userData);
                 $form->setAttribute('action', '/users/edit/' . $editableUser->getId());
             }
         } else {
             return $this->redirect()->toRoute('users');
         }
     }
     if ($request->isPost()) {
         $post = $request->getPost()->toArray();
         $post['password'] = $editableUser !== null ? $editableUser->getPassword() : md5($post['password']);
         $form->setData($post);
         if ($form->isValid()) {
             $data = $form->getData();
             $userData = $editableUser !== null ? $editableUser : new User();
             $userData->setDisplayName($data['displayName']);
             $userData->getEmail($data['email']);
             $userData->setPassword($data['password']);
             $userData->setRole(RoleDAO::getInstance($this->getServiceLocator())->findOneById($data['role']));
             $userDAO->save($userData);
             return $this->redirect()->toRoute('users');
         } else {
             $form->getMessages();
         }
     }
     if ($editableUser) {
         $form->get('password')->setAttribute('disabled', 'disabled');
         $form->get('password')->setAttribute('type', 'password');
     }
     return array('form' => $form, 'view' => $view);
 }
Esempio n. 3
0
 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);
 }