private function validateEmail(User $entity, ViolationsList $violations)
 {
     $email = $entity->getEmail();
     if ($this->isEmpty($email)) {
         //Empty is okay, non-required field.
         return;
     }
     if (!EmailValidator::isValid($email)) {
         $violations->add('email', 'devture_user.validation.email.invalid');
         return;
     }
     //Make sure it's unique, so it can potentially be used as an alternative user identifier.
     try {
         $user = $this->repository->findByEmail($email);
         if ($user->getId() !== $entity->getId()) {
             $violations->add('email', 'devture_user.validation.email.in_use', array('%username%' => $user->getUsername()));
         }
     } catch (NotFound $e) {
     }
 }
 /**
  * @param User $user
  * @param Request $request
  * @param Response $response
  */
 public function extendSessionIfNeeded(User $user, Request $request, Response $response)
 {
     if (!$request->attributes->has(self::REQUEST_ATTRIBUTE_EXTEND_SESSION)) {
         return;
     }
     $id = $request->attributes->get(self::REQUEST_ATTRIBUTE_EXTEND_SESSION);
     if ((string) $user->getId() !== $id) {
         //The user whose session we were about to extend is different than the one given.
         //Something weird is going on. Don't perform the extension on this request/response cycle.
         return;
     }
     $this->login($user, $response);
 }