View source
<?php
namespace Drupal\key_auth\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\key_auth\KeyAuth;
use Drupal\key_auth\KeyAuthInterface;
use Drupal\user\UserInterface;
use Drupal\user\Entity\User;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactory;
class UserKeyAuthForm extends FormBase {
protected $keyAuth;
protected $config;
public function __construct(KeyAuthInterface $key_auth, ConfigFactory $config_factory) {
$this->keyAuth = $key_auth;
$this->config = $config_factory
->get('key_auth.settings');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('key_auth'), $container
->get('config.factory'));
}
public function getFormId() {
return 'user_key_auth_form';
}
public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
$key = $user->api_key->value;
$form['#uid'] = $user
->id();
$form['key'] = [
'label' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $this
->t('Key'),
],
'key' => [
'#type' => 'item',
'#markup' => $key ? $key : $this
->t('You currently do not have a key'),
],
];
$form['auth'] = [
'label' => [
'#type' => 'html_tag',
'#tag' => 'h3',
'#value' => $this
->t('Authentication options'),
],
'#access' => (bool) $key,
];
if (in_array(KeyAuth::DETECTION_METHOD_HEADER, $this->config
->get('detection_methods'))) {
$form['auth']['header'] = [
'label' => [
'#type' => 'html_tag',
'#tag' => 'h5',
'#value' => $this
->t('Header'),
],
'instructions' => [
'#type' => 'item',
'#markup' => $this
->t('Include the following header in your API requests.'),
],
'example' => [
'#type' => 'html_tag',
'#tag' => 'pre',
'#value' => $this->config
->get('param_name') . ': ' . $key,
],
];
}
if (in_array(KeyAuth::DETECTION_METHOD_QUERY, $this->config
->get('detection_methods'))) {
$form['auth']['query'] = [
'label' => [
'#type' => 'html_tag',
'#tag' => 'h5',
'#value' => $this
->t('Query'),
],
'instructions' => [
'#type' => 'item',
'#markup' => $this
->t('Include the following query in the URL of your API requests.'),
],
'example' => [
'#type' => 'html_tag',
'#tag' => 'pre',
'#value' => '?' . $this->config
->get('param_name') . '=' . $key,
],
];
}
$form['actions'] = [
'new' => [
'#type' => 'submit',
'#value' => $this
->t('Generate new key'),
],
'delete' => [
'#type' => 'submit',
'#value' => $this
->t('Delete current key'),
'#access' => (bool) $key,
'#submit' => [
'::deleteKey',
],
],
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
User::load($form['#uid'])
->set('api_key', $this->keyAuth
->generateKey())
->save();
$this
->messenger()
->addMessage($this
->t('A new key has been generated.'));
}
public function deleteKey(array &$form, FormStateInterface $form_state) {
User::load($form['#uid'])
->set('api_key', NULL)
->save();
$this
->messenger()
->addMessage($this
->t('Your key has been deleted.'));
}
public function checkAccess(UserInterface $user) {
$current_user = User::load($this
->currentUser()
->id());
if ($user
->id() != $current_user
->id()) {
$access = AccessResult::allowedIfHasPermission($current_user, 'administer users');
}
else {
$access = AccessResult::allowedIf($this->keyAuth
->access($current_user));
}
$access
->addCacheContexts([
'user.permissions',
]);
$access
->addCacheableDependency($user);
return $access;
}
}