Authorize.php in DRD Agent 8.3
File
src/Form/Authorize.php
View source
<?php
namespace Drupal\drd_agent\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\drd_agent\Setup;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Authorize extends FormBase {
protected $setupService;
public function __construct(Setup $setup_service) {
$this->setupService = $setup_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('drd_agent.setup'));
}
public function getFormId() : string {
return 'drd_agent_authorize_form';
}
protected function buildFormToken(array $form) : array {
$form['token'] = [
'#type' => 'textarea',
'#title' => t('Authentication token'),
'#description' => t('Paste the token for this domain from the DRD dashboard, which you want to authorize.'),
'#default_value' => '',
'#required' => TRUE,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Validate'),
];
return $form;
}
protected function buildFormConfirmation(array $form) : array {
$form['attention'] = [
'#markup' => t('You are about to grant admin access to the Drupal Remote Dashboard on the following domain:'),
'#prefix' => '<div>',
'#suffix' => '</div>',
];
$form['domain'] = [
'#markup' => $this->setupService
->getDomain(),
'#prefix' => '<div class="domain">',
'#suffix' => '</div>',
];
$form['cancel'] = [
'#type' => 'submit',
'#value' => t('Cancel'),
];
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Grant admin access'),
];
return $form;
}
public function buildForm(array $form, FormStateInterface $form_state) : array {
$form = empty($_SESSION['drd_agent_authorization_values']) ? $this
->buildFormToken($form) : $this
->buildFormConfirmation($form);
$form['#attributes'] = [
'class' => [
'drd-agent-auth',
],
];
$form['#attached']['library'][] = 'drd_agent/general';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if (empty($_SESSION['drd_agent_authorization_values'])) {
$_SESSION['drd_agent_authorization_values'] = $form_state
->getValue('token');
}
else {
if ($form_state
->getValue('op') === $form['submit']['#value']) {
$values = $this->setupService
->execute();
$form_state
->setResponse(TrustedRedirectResponse::create($values['redirect']));
}
unset($_SESSION['drd_agent_authorization_values']);
}
}
}
Classes
Name |
Description |
Authorize |
Authorize a new dashboard for this drd-agent. |