/**
  * Edits user profile
  * @return \Framework\Response\ResponseRedirect
  */
 public function updateAction()
 {
     $errors = array();
     if ($this->getRequest()->isPost()) {
         try {
             $user = User::find($this->getRequest()->post('id'));
             $user->role = $this->getRequest()->post('role');
             $user->email = $this->getRequest()->post('email');
             $user->save();
             Service::get('security')->setUser($user);
         } catch (DatabaseException $e) {
             $errors = array($e->getMessage());
         }
     }
     return $this->redirect($this->generateRoute('profile'));
 }
 /**
  * @return \Framework\Response\Response|\Framework\Response\ResponseRedirect
  * @throws \Framework\Exception\DatabaseException
  * @throws \Framework\Exception\ServiceException
  */
 public function updateAction()
 {
     $isEmailUpdated = false;
     $isPasswordUpdated = false;
     $updateMessage = '';
     try {
         $user = Service::get('security')->getUser();
         $oldUser = User::find($user->id);
         $newPassword = $this->getRequest()->post('newPassword');
         $newEmail = $this->getRequest()->post('newEmail');
         if (!empty($newEmail) && $newEmail != $oldUser->email) {
             $user->email = $newEmail;
             $isEmailUpdated = true;
             $updateMessage .= 'email ';
         }
         if (!empty($newPassword) && !Service::get('security')->isPasswordMatch($newPassword, $oldUser)) {
             $soplPass = Service::get('security')->getSoltedPassword($newPassword);
             $user->password = $soplPass['soltedPassword'];
             $user->solt = $soplPass['solt'];
             $isPasswordUpdated = true;
             if (!empty($updateMessage)) {
                 $updateMessage .= 'and ';
             }
             $updateMessage .= 'password ';
         }
         if (!$isEmailUpdated && !$isPasswordUpdated) {
             return $this->redirect($this->generateRoute('home'), 'You don\' change data.');
         } else {
             if (!$isEmailUpdated) {
                 unset($user->email);
             } else {
                 if (!$isPasswordUpdated) {
                     unset($user->password);
                     unset($user->solt);
                 }
             }
         }
         Service::get('security')->setUser($oldUser);
         $user->save();
         return $this->redirect($this->generateRoute('logout'), 'Data: ' . $updateMessage . 'has been changet succesfully. Please login again.');
     } catch (DatabaseException $e) {
         $error = $e->getMessage();
         return $this->render('update.html', array('user' => $oldUser, 'errors' => isset($error) ? $error : null, 'action' => $this->generateRoute('update_profile'), 'src' => array('src' => 'Blog', 'controller' => 'Security')));
     }
 }
Esempio n. 3
0
 public function updateAction()
 {
     if (!Service::get('request')->isPost()) {
         throw new \Exception('Hack attempt');
     }
     if (!Service::get('security')->isAuthenticated()) {
         return $this->redirect('login', 'Please Login');
     }
     $errors = [];
     $userId = (int) $this->getRequest()->post('id');
     try {
         User::where(['id' => $userId])->update(['email' => $this->getRequest()->post('email'), 'password' => $this->getRequest()->post('password')]);
     } catch (DatabaseException $e) {
         $errors[] = $e->getMessage();
     }
     $userId = Service::get('security')->getUser()->id;
     $user = User::find((int) $userId);
     Service::get('security')->setUser($user);
     return $this->render('profile.html', ['user' => $user, 'action' => $this->generateRoute('update_profile'), 'errors' => isset($errors) ? $errors : null]);
 }