class OAuthKeyForm in Lightning API 8
Same name and namespace in other branches
- 8.4 src/Form/OAuthKeyForm.php \Drupal\lightning_api\Form\OAuthKeyForm
- 8.2 src/Form/OAuthKeyForm.php \Drupal\lightning_api\Form\OAuthKeyForm
- 8.3 src/Form/OAuthKeyForm.php \Drupal\lightning_api\Form\OAuthKeyForm
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
- class \Drupal\lightning_api\Form\OAuthKeyForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of OAuthKeyForm
1 file declares its use of OAuthKeyForm
- OAuthKeyFormTest.php in tests/
src/ Kernel/ OAuthKeyFormTest.php
1 string reference to 'OAuthKeyForm'
File
- src/
Form/ OAuthKeyForm.php, line 13
Namespace
Drupal\lightning_api\FormView source
class OAuthKeyForm extends ConfigFormBase {
/**
* The OAuth key service.
*
* @var \Drupal\lightning_api\OAuthKey
*/
protected $key;
/**
* OAuthKeyForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\lightning_api\OAuthKey $key
* The OAuth keys service.
* @param TranslationInterface $translation
* The string translation service.
*/
public function __construct(ConfigFactoryInterface $config_factory, OAuthKey $key, TranslationInterface $translation) {
parent::__construct($config_factory);
$this->key = $key;
$this
->setStringTranslation($translation);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('lightning_api.oauth_key'), $container
->get('string_translation'));
}
/**
* {@inheritdoc}
*/
public function getEditableConfigNames() {
return [
'simple_oauth.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'oauth_key_form';
}
/**
* Handles exceptions caught during form submission.
*
* @param \Exception $e
* The caught exception.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current form state.
*/
private function onException(\Exception $e, FormStateInterface $form_state) {
drupal_set_message($e
->getMessage(), 'error');
$form_state
->setRebuild();
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
if (extension_loaded('openssl') == FALSE) {
drupal_set_message($this
->t('The OpenSSL extension is unavailable. Please enable it to generate OAuth keys.'), 'error');
return $form;
}
if ($this->key
->exists() && $form_state
->isSubmitted() == FALSE && $form_state
->isRebuilding() == FALSE) {
drupal_set_message($this
->t('A key pair already exists and will be overwritten if you generate new keys.'), 'warning');
}
$form['dir'] = [
'#type' => 'textfield',
'#title' => $this
->t('Destination'),
'#description' => $this
->t('Path to the directory in which to store the generated keys.'),
'#required' => TRUE,
'#element_validate' => [
'::validateDestinationExists',
],
];
$form['advanced'] = [
'#type' => 'details',
'#title' => $this
->t('Advanced'),
];
$form['advanced']['private_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Private key name'),
'#description' => $this
->t('File name of the generated private key. Will be automatically generated if left empty.'),
'#element_validate' => [
'::validateKeyFileName',
],
];
$form['advanced']['public_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Public key name'),
'#description' => $this
->t('File name of the generated public key. Will be automatically generated if left empty.'),
'#element_validate' => [
'::validateKeyFileName',
],
];
$form['advanced']['conf'] = [
'#type' => 'textfield',
'#title' => $this
->t('OpenSSL configuration file'),
'#description' => $this
->t('Path to the openssl.cnf configuration file. PHP will attempt to auto-detect this if not specified.'),
];
$form['generate'] = [
'#type' => 'submit',
'#value' => $this
->t('Generate keys'),
];
return $form;
}
/**
* Ensures that the destination directory exists.
*
* @param array $element
* The form element being validated.
* @param FormStateInterface $form_state
* The current form state.
*/
public function validateDestinationExists(array &$element, FormStateInterface $form_state) {
$dir = $element['#value'];
if (is_dir($dir) == FALSE) {
$form_state
->setError($element, $this
->t('%dir does not exist.', [
'%dir' => $dir,
]));
}
}
/**
* Ensures that a requested file name contains no illegal characters.
*
* @param array $element
* The form element being validated.
* @param FormStateInterface $form_state
* The current form state.
*/
public function validateKeyFileName(array &$element, FormStateInterface $form_state) {
$value = $element['#value'];
if (strpos($value, '/') !== FALSE) {
$form_state
->setError($element, $this
->t('%value is not a valid name for a key file.', [
'%value' => $value,
]));
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$conf = [];
// Gather OpenSSL configuration values specified by the user.
$config = $form_state
->getValue('conf');
if ($config) {
$conf['config'] = $config;
}
try {
list($private_key, $public_key) = OAuthKey::generate($conf);
} catch (KeyGenerationException $e) {
return $this
->onException($e, $form_state);
}
$dir = rtrim($form_state
->getValue('dir'), '/');
$config = $this
->config('simple_oauth.settings');
try {
$destination = $dir . '/' . trim($form_state
->getValue('private_key'));
$destination = $this->key
->write($destination, $private_key);
$config
->set('private_key', $destination);
$destination = $dir . '/' . trim($form_state
->getValue('public_key'));
$destination = $this->key
->write($destination, $public_key);
$config
->set('public_key', $destination);
$config
->save();
} catch (\RuntimeException $e) {
return $this
->onException($e, $form_state);
}
drupal_set_message($this
->t('A key pair was generated successfully.'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfigFormBaseTrait:: |
protected | function | Retrieves a configuration object. | |
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OAuthKeyForm:: |
protected | property | The OAuth key service. | |
OAuthKeyForm:: |
public | function |
Form constructor. Overrides ConfigFormBase:: |
|
OAuthKeyForm:: |
public static | function |
Instantiates a new instance of this class. Overrides ConfigFormBase:: |
|
OAuthKeyForm:: |
public | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
OAuthKeyForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
OAuthKeyForm:: |
private | function | Handles exceptions caught during form submission. | |
OAuthKeyForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
OAuthKeyForm:: |
public | function | Ensures that the destination directory exists. | |
OAuthKeyForm:: |
public | function | Ensures that a requested file name contains no illegal characters. | |
OAuthKeyForm:: |
public | function |
OAuthKeyForm constructor. Overrides ConfigFormBase:: |
|
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |