UsersKeyDeleteForm.php in JSON Web Token Authentication (JWT) 8
File
modules/users_jwt/src/Form/UsersKeyDeleteForm.php
View source
<?php
namespace Drupal\users_jwt\Form;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Drupal\users_jwt\UsersJwtKeyRepositoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class UsersKeyDeleteForm extends ConfirmFormBase {
protected $keyRepository;
protected $key;
public function __construct(UsersJwtKeyRepositoryInterface $key_repository) {
$this->keyRepository = $key_repository;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('users_jwt.key_repository'));
}
public function getFormId() {
return 'users_jwt_key_delete_form';
}
public function buildForm(array $form, FormStateInterface $form_state, $key_id = NULL, UserInterface $user = NULL) {
if (!$user) {
return $form;
}
$key = $this->keyRepository
->getKey($key_id);
if (!$key || $key->uid != $user
->id()) {
return $form;
}
$this->key = $key;
$form['key'] = [
'#type' => 'value',
'#value' => $key,
];
$header = [
$this
->t('Key ID'),
$this
->t('Key Type'),
$this
->t('Key'),
];
$options = $this->keyRepository
->algorithmOptions();
$row = [
'id' => $key->id,
'alg' => $options[$key->alg] ?? $key->alg,
'pubkey' => Unicode::truncate($key->pubkey, 40, FALSE, TRUE),
];
$rows[] = $row;
$form['key_display'] = [
'#type' => 'table',
'#header' => $header,
'#rows' => $rows,
];
return parent::buildForm($form, $form_state);
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete this key?');
}
public function getDescription() {
return t('This operation cannot be undone.');
}
public function getCancelUrl() {
return Url::fromRoute('users_jwt.key_list', [
'user' => $this->key->uid,
]);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$key = $form_state
->getValue('key');
$this->keyRepository
->deleteKey($key->id);
$this
->messenger()
->addMessage($this
->t('They key %key_id has been deleted', [
'%key_id' => $key->id,
]));
$form_state
->setRedirect('users_jwt.key_list', [
'user' => $key->uid,
]);
}
}