SamlauthAuthmapDeleteForm.php in SAML Authentication 4.x
File
src/Form/SamlauthAuthmapDeleteForm.php
View source
<?php
namespace Drupal\samlauth\Form;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SamlauthAuthmapDeleteForm extends ConfirmFormBase {
protected $connection;
protected $entityTypeManager;
protected $authmapEntry;
public static function create(ContainerInterface $container) {
return new static($container
->get('database'), $container
->get('entity_type.manager'));
}
public function __construct(Connection $connection, EntityTypeManagerInterface $entity_type_manager) {
$this->connection = $connection;
$this->entityTypeManager = $entity_type_manager;
}
public function getFormId() {
return 'samlauth_authmap_delete';
}
public function getQuestion() {
if (!empty($this->authmapEntry['uid'])) {
$user = $this->entityTypeManager
->getStorage('user')
->load($this->authmapEntry['uid']);
}
return $this
->t('Are you sure you want to delete the link between login ID %id and Drupal user %user?', [
'%id' => $this->authmapEntry['authname'],
'%user' => isset($user) ? $user
->getAccountName() : "<unknown> ({$this->authmapEntry['uid']})",
]);
}
public function getCancelUrl() {
return new Url('view.samlauth_map.page');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$authname = FALSE;
$uid = $this
->getRouteMatch()
->getParameter('uid');
if ($uid && filter_var($uid, FILTER_VALIDATE_INT)) {
$authname = $this->connection
->select('authmap', 'm')
->fields('m', [
'authname',
])
->condition('m.uid', (int) $uid)
->condition('m.provider', 'samlauth')
->execute()
->fetchField();
}
if ($authname === FALSE) {
$this
->messenger()
->addError(t('No authmap record found for uid @uid.', [
'@uid' => $uid,
]));
return [];
}
$this->authmapEntry = [
'uid' => $uid,
'authname' => $authname,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$uid = $this
->getRouteMatch()
->getParameter('uid');
if (!$uid || filter_var($uid, FILTER_VALIDATE_INT) === FALSE) {
throw new \LogicException('It should be impossible to submit this form without valid uid parameter.');
}
$this->connection
->delete('authmap')
->condition('uid', (int) $uid)
->condition('provider', 'samlauth')
->execute();
$this
->messenger()
->addStatus($this
->t('The link has been deleted.'));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}