public function onAuthenticationFailure($event)
 {
     $app = $this->app;
     $authenticationToken = $event->getAuthenticationToken();
     $user = $app['user.provider']->loadUserByUsername($authenticationToken->getUser(), false);
     $userActionEntity = new UserActionEntity();
     $userActionEntity->setUser($user)->setKey('user.login.fail')->setMessage('An user has tried to log in!')->setData(array('username' => $authenticationToken->getUser()))->setIp($app['request']->getClientIp())->setUserAgent($app['request']->headers->get('User-Agent'));
     if (!$user) {
         $userActionEntity->setData(array('username' => $app['request']->request->get('username')));
     }
     $app['orm.em']->persist($userActionEntity);
     $app['orm.em']->flush();
 }
 public function onSwitchUser($event)
 {
     $app = $this->app;
     $user = $app['security']->getToken()->getUser();
     $targetUser = $event->getTargetUser();
     if ($app['security']->isGranted('ROLE_PREVIOUS_ADMIN')) {
         $targetUser = $app['orm.em']->find('Application\\Entity\\UserEntity', $targetUser->getId());
         $userActionEntity = new UserActionEntity();
         $userActionEntity->setUser($targetUser)->setKey('user.switch.back')->setMessage('User has switched back to own user (from user with ID "' . $user->getId() . '")!')->setData(array('user_id' => $targetUser->getId(), 'from_user_id' => $user->getId()))->setIp($app['request']->getClientIp())->setUserAgent($app['request']->headers->get('User-Agent'));
         $app['orm.em']->persist($userActionEntity);
         $app['orm.em']->flush();
     } else {
         $userActionEntity = new UserActionEntity();
         $userActionEntity->setUser($user)->setKey('user.switch')->setMessage('User has switched to user with ID "' . $targetUser->getId() . '"!')->setData(array('user_id' => $user->getId(), 'to_user_id' => $targetUser->getId()))->setIp($app['request']->getClientIp())->setUserAgent($app['request']->headers->get('User-Agent'));
         $app['orm.em']->persist($userActionEntity);
         $app['orm.em']->flush();
     }
 }
 /**
  * @param Request     $request
  * @param Application $app
  *
  * @return Response
  */
 public function passwordAction(Request $request, Application $app)
 {
     $form = $app['form.factory']->create(new PasswordType(), $app['user']);
     if ($request->getMethod() == 'POST') {
         $form->handleRequest($request);
         if ($form->isValid()) {
             $userEntity = $form->getData();
             if ($userEntity->getPlainPassword()) {
                 $userEntity->setPlainPassword($userEntity->getPlainPassword(), $app['security.encoder_factory']);
                 $app['orm.em']->persist($userEntity);
                 $userActionEntity = new UserActionEntity();
                 $userActionEntity->setUser($userEntity)->setKey('user.password.change')->setMessage('User has changed his password!')->setIp($app['request']->getClientIp())->setUserAgent($app['request']->headers->get('User-Agent'));
                 $app['orm.em']->persist($userActionEntity);
                 $app['orm.em']->flush();
                 $app['flashbag']->add('success', $app['translator']->trans('Your password was successfully changed!'));
             }
         }
     }
     return new Response($app['twig']->render('contents/members-area/my/password.html.twig', array('form' => $form->createView())));
 }
 /**
  * @param Request     $request
  * @param Application $app
  *
  * @return Response
  */
 public function resetPasswordAction(Request $request, Application $app)
 {
     if ($app['security.authorization_checker']->isGranted('ROLE_USER')) {
         return $app->redirect($app['url_generator']->generate('members-area'));
     }
     $code = $request->query->has('code') ? $request->query->get('code') : false;
     $action = $code ? 'reset' : 'request';
     $alert = false;
     $alertMessage = '';
     $form = $app['form.factory']->create(new ResetPasswordType($action), new UserEntity());
     if ($action == 'reset') {
         $userEntity = $app['orm.em']->getRepository('Application\\Entity\\UserEntity')->findOneByResetPasswordCode($code);
         if ($userEntity) {
             if ($request->getMethod() == 'POST') {
                 $form->handleRequest($request);
                 if ($form->isValid()) {
                     $temporaryUserEntity = $form->getData();
                     $userEntity->setResetPasswordCode(null)->setPlainPassword($temporaryUserEntity->getPlainPassword(), $app['security.encoder_factory']);
                     $app['orm.em']->persist($userEntity);
                     $userActionEntity = new UserActionEntity();
                     $userActionEntity->setUser($userEntity)->setKey('user.password.reset')->setMessage('User has reset his password!')->setIp($app['request']->getClientIp())->setUserAgent($app['request']->headers->get('User-Agent'));
                     $app['orm.em']->persist($userActionEntity);
                     $app['orm.em']->flush();
                     $app['application.mailer']->swiftMessageInitializeAndSend(array('subject' => $app['name'] . ' - ' . $app['translator']->trans('Reset Password Confirmation'), 'to' => array($userEntity->getEmail() => $userEntity->getProfile()->getFullName()), 'body' => 'emails/users/reset-password-confirmation.html.twig', 'templateData' => array('user' => $userEntity)));
                     $alert = 'success';
                     $alertMessage = 'You password has been reset successfully.';
                 }
             }
         } else {
             $alert = 'danger';
             $alertMessage = 'This reset code was not found.';
         }
     } else {
         if ($request->getMethod() == 'POST') {
             $form->handleRequest($request);
             if ($form->isValid()) {
                 $temporaryUserEntity = $form->getData();
                 $userEntity = $app['orm.em']->getRepository('Application\\Entity\\UserEntity')->findOneByEmail($temporaryUserEntity->getEmail());
                 if ($userEntity) {
                     $userEntity->setResetPasswordCode(md5(uniqid(null, true)));
                     $app['orm.em']->persist($userEntity);
                     $userActionEntity = new UserActionEntity();
                     $userActionEntity->setUser($userEntity)->setKey('user.password.request')->setMessage('User has requested a password reset!')->setIp($app['request']->getClientIp())->setUserAgent($app['request']->headers->get('User-Agent'));
                     $app['orm.em']->persist($userActionEntity);
                     // In the REALLY unlikely case that the reset password code wouldn't be unique
                     try {
                         $app['orm.em']->flush();
                         $app['application.mailer']->swiftMessageInitializeAndSend(array('subject' => $app['name'] . ' - ' . $app['translator']->trans('Reset password'), 'to' => array($userEntity->getEmail()), 'body' => 'emails/users/reset-password.html.twig', 'templateData' => array('user' => $userEntity)));
                         $alert = 'success';
                         $alertMessage = 'We have sent you an email. The link inside the email will lead you to a reset page.';
                     } catch (\Exception $e) {
                         $alert = 'danger';
                         $alertMessage = 'Whops. Something went wrong. Please try again.';
                     }
                 } else {
                     $alert = 'danger';
                     $alertMessage = 'This email was not found in our database.';
                 }
             }
         }
     }
     return new Response($app['twig']->render('contents/members-area/reset-password.html.twig', array('code' => $code, 'action' => $action, 'form' => $form->createView(), 'alert' => $alert, 'alertMessage' => $alertMessage)));
 }