user_login_finalize example

$flood_config = $this->config('user.flood');
    if ($flood_config->get('uid_only')) {
      $identifier = $user->id();
    }
    else {
      $identifier = $user->id() . '-' . $request->getClientIP();
    }

    $this->flood->clear('user.failed_login_user', $identifier);
    $this->flood->clear('user.http_login', $identifier);

    user_login_finalize($user);
    $this->logger->info('User %name used one-time login link at time %timestamp.', ['%name' => $user->getDisplayName(), '%timestamp' => $timestamp]);
    $this->messenger()->addStatus($this->t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please set your password.'));
    // Let the user's password be changed without the current password     // check.     $token = Crypt::randomBytesBase64(55);
    $request->getSession()->set('pass_reset_' . $user->id()$token);
    // Clear any flood events for this user.     $this->flood->clear('user.password_request_user', $uid);
    return $this->redirect(
      'entity.user.edit_form',
      ['user' => $user->id()],
      [
// A destination was set, probably on an exception controller.     if (!$this->getRequest()->request->has('destination')) {
      $form_state->setRedirect(
        'entity.user.canonical',
        ['user' => $account->id()]
      );
    }
    else {
      $this->getRequest()->query->set('destination', $this->getRequest()->request->get('destination'));
    }

    user_login_finalize($account);
  }

  /** * Sets an error if supplied username has been blocked. */
  public function validateName(array &$form, FormStateInterface $form_state) {
    if (!$form_state->isValueEmpty('name') && user_is_blocked($form_state->getValue('name'))) {
      // Blocked in user administration.       $form_state->setErrorByName('name', $this->t('The username %name has not been activated or is blocked.', ['%name' => $form_state->getValue('name')]));
    }
  }

  
protected function userIsBlocked($name) {
    return user_is_blocked($name);
  }

  /** * Finalizes the user login. * * @param \Drupal\user\UserInterface $user * The user. */
  protected function userLoginFinalize(UserInterface $user) {
    user_login_finalize($user);
  }

  /** * Logs out a user. * * @return \Symfony\Component\HttpFoundation\Response * The response object. */
  public function logout() {
    $this->userLogout();
    return new Response(NULL, 204);
  }
// Add plain text password into user account to generate mail tokens.     $account->password = $pass;

    // New administrative account without notification.     if ($admin && !$notify) {
      $this->messenger()->addStatus($this->t('Created a new user account for <a href=":url">%name</a>. No email has been sent.', [':url' => $account->toUrl()->toString(), '%name' => $account->getAccountName()]));
    }
    // No email verification required; log in user immediately.     elseif (!$admin && !\Drupal::config('user.settings')->get('verify_mail') && $account->isActive()) {
      _user_mail_notify('register_no_approval_required', $account);
      user_login_finalize($account);
      $this->messenger()->addStatus($this->t('Registration successful. You are now logged in.'));
      $form_state->setRedirect('<front>');
    }
    // No administrator approval required.     elseif ($account->isActive() || $notify) {
      if (!$account->getEmail() && $notify) {
        $this->messenger()->addStatus($this->t('The new user <a href=":url">%name</a> was created without an email address, so no welcome message was sent.', [':url' => $account->toUrl()->toString(), '%name' => $account->getAccountName()]));
      }
      else {
        $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
        if (_user_mail_notify($op$account)) {
          
Home | Imprint | This part of the site doesn't use cookies.