You are here

class AuthmapDeleteForm in External Authentication 2.0.x

Confirm the user wants to delete an authmap entry.

Hierarchy

Expanded class hierarchy of AuthmapDeleteForm

1 string reference to 'AuthmapDeleteForm'
externalauth.routing.yml in ./externalauth.routing.yml
externalauth.routing.yml

File

src/Form/AuthmapDeleteForm.php, line 15

Namespace

Drupal\externalauth\Form
View source
class AuthmapDeleteForm extends ConfirmFormBase {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The EntityTypeManager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Temporary storage for the current authmap entry.
   *
   * @var array
   */
  protected $authmapEntry;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('entity_type.manager'));
  }

  /**
   * Constructs a router for Drupal with access check and upcasting.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection to get authmap entries.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The EntityTypeManager service.
   */
  public function __construct(Connection $connection, EntityTypeManagerInterface $entity_type_manager) {
    $this->connection = $connection;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'authmap_delete';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    if (!empty($this->authmapEntry['uid'])) {

      /** @var \Drupal\user\Entity\User $user */
      $user = $this->entityTypeManager
        ->getStorage('user')
        ->load($this->authmapEntry['uid']);
    }

    // We don't display the provider name; in most use cases it's implicit.
    return $this
      ->t('Are you sure you want to delete the link between authentication name %id and Drupal user %user?', [
      '%id' => $this->authmapEntry['authname'],
      '%user' => isset($user) ? $user
        ->getAccountName() : "<unknown> ({$this->authmapEntry['uid']})",
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {

    // We want to return a URL object pointing to admin/people/authmap/PROVIDER.
    // Url('view.authmap.page', ['provider' => PROVIDER]) instead returns
    // admin/people/authmap?provider=PROVIDER, which is not recognized as
    // contextual filter value.
    return Url::fromUri('internal:/admin/people/authmap/' . $this->authmapEntry['provider']);
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // This form has uid + provider in its URL, not authname + provider, to not
    // expose authnames externally in e.g. HTTP referrer logs.
    $authname = FALSE;
    $provider = $this
      ->getRouteMatch()
      ->getParameter('provider');
    $uid = $this
      ->getRouteMatch()
      ->getParameter('uid');
    if ($provider && $uid && filter_var($uid, FILTER_VALIDATE_INT)) {
      $authname = $this->connection
        ->select('authmap', 'm')
        ->fields('m', [
        'authname',
      ])
        ->condition('m.uid', (int) $uid)
        ->condition('m.provider', $provider)
        ->execute()
        ->fetchField();
    }
    if ($authname === FALSE) {

      // Display same error for either illegal UID or no record.
      $this
        ->messenger()
        ->addError($this
        ->t('No authmap record found for provider @provider / uid @uid.', [
        '@provider' => $provider,
        '@uid' => $uid,
      ]));
      return [];
    }
    $this->authmapEntry = [
      'provider' => $provider,
      'authname' => $authname,
      'uid' => $uid,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $provider = $this
      ->getRouteMatch()
      ->getParameter('provider');
    $uid = $this
      ->getRouteMatch()
      ->getParameter('uid');
    if (!$provider || !$uid || filter_var($uid, FILTER_VALIDATE_INT) === FALSE) {
      throw new \LogicException('It should be impossible to submit this form without valid provider/uid parameters.');
    }
    $this->connection
      ->delete('authmap')
      ->condition('uid', (int) $uid)
      ->condition('provider', $provider)
      ->execute();
    $this
      ->messenger()
      ->addStatus($this
      ->t('The link has been deleted.'));
    $form_state
      ->setRedirectUrl($this
      ->getCancelUrl());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthmapDeleteForm::$authmapEntry protected property Temporary storage for the current authmap entry.
AuthmapDeleteForm::$connection protected property The database connection.
AuthmapDeleteForm::$entityTypeManager protected property The EntityTypeManager service.
AuthmapDeleteForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
AuthmapDeleteForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
AuthmapDeleteForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
AuthmapDeleteForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
AuthmapDeleteForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
AuthmapDeleteForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
AuthmapDeleteForm::__construct public function Constructs a router for Drupal with access check and upcasting.
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 2
ConfirmFormBase::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormInterface::getConfirmText 22
ConfirmFormBase::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormInterface::getDescription 14
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 72
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.