class Oauth2TokenSettingsForm in Simple OAuth (OAuth2) & OpenID Connect 8.3
Same name and namespace in other branches
- 8.4 src/Entity/Form/Oauth2TokenSettingsForm.php \Drupal\simple_oauth\Entity\Form\Oauth2TokenSettingsForm
- 8.2 src/Entity/Form/Oauth2TokenSettingsForm.php \Drupal\simple_oauth\Entity\Form\Oauth2TokenSettingsForm
- 5.x src/Entity/Form/Oauth2TokenSettingsForm.php \Drupal\simple_oauth\Entity\Form\Oauth2TokenSettingsForm
The settings form.
@internal
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\simple_oauth\Entity\Form\Oauth2TokenSettingsForm
- class \Drupal\Core\Form\ConfigFormBase uses ConfigFormBaseTrait
Expanded class hierarchy of Oauth2TokenSettingsForm
1 string reference to 'Oauth2TokenSettingsForm'
File
- src/
Entity/ Form/ Oauth2TokenSettingsForm.php, line 18
Namespace
Drupal\simple_oauth\Entity\FormView source
class Oauth2TokenSettingsForm extends ConfigFormBase {
/**
* The file system checker.
*
* @var \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker
*/
protected $fileSystemChecker;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected $messenger;
/**
* Oauth2TokenSettingsForm constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The factory for configuration objects.
* @param \Drupal\simple_oauth\Service\Filesystem\FileSystemChecker $file_system_checker
* The simple_oauth.filesystem service.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger service.
*/
public function __construct(ConfigFactoryInterface $configFactory, FileSystemChecker $file_system_checker, MessengerInterface $messenger) {
parent::__construct($configFactory);
$this->fileSystemChecker = $file_system_checker;
$this->messenger = $messenger;
}
/**
* Creates the form.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The container.
*
* @return \Drupal\simple_oauth\Entity\Form\Oauth2TokenSettingsForm
* The form.
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('simple_oauth.filesystem_checker'), $container
->get('messenger'));
}
/**
* Returns a unique string identifying the form.
*
* @return string
* The unique string identifying the form.
*/
public function getFormId() {
return 'oauth2_token_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'simple_oauth.settings',
];
}
/**
* Form submission handler.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$settings = $this
->config('simple_oauth.settings');
$settings
->set('access_token_expiration', $form_state
->getValue('access_token_expiration'));
$settings
->set('refresh_token_expiration', $form_state
->getValue('refresh_token_expiration'));
$settings
->set('token_cron_batch_size', $form_state
->getValue('token_cron_batch_size'));
$settings
->set('public_key', $form_state
->getValue('public_key'));
$settings
->set('private_key', $form_state
->getValue('private_key'));
$settings
->set('remember_clients', $form_state
->getValue('remember_clients'));
$settings
->save();
parent::submitForm($form, $form_state);
}
/**
* Defines the settings form for Access Token entities.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return array
* Form definition array.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('simple_oauth.settings');
$form['access_token_expiration'] = [
'#type' => 'number',
'#title' => $this
->t('Access token expiration time'),
'#description' => $this
->t('The default value, in seconds, to be used as expiration time when creating new tokens.'),
'#default_value' => $config
->get('access_token_expiration'),
];
$form['refresh_token_expiration'] = [
'#type' => 'number',
'#title' => $this
->t('Refresh token expiration time'),
'#description' => $this
->t('The default value, in seconds, to be used as expiration time when creating new tokens.'),
'#default_value' => $config
->get('refresh_token_expiration'),
];
$form['token_cron_batch_size'] = [
'#type' => 'number',
'#title' => $this
->t('Token batch size.'),
'#description' => $this
->t('The number of expired token to delete per batch during cron cron.'),
'#default_value' => $config
->get('token_cron_batch_size') ?: 0,
];
$form['public_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Public Key'),
'#description' => $this
->t('The path to the public key file.'),
'#default_value' => $config
->get('public_key'),
'#element_validate' => [
'::validateExistingFile',
],
'#required' => TRUE,
'#attributes' => [
'id' => 'pubk',
],
];
$form['private_key'] = [
'#type' => 'textfield',
'#title' => $this
->t('Private Key'),
'#description' => $this
->t('The path to the private key file.'),
'#default_value' => $config
->get('private_key'),
'#element_validate' => [
'::validateExistingFile',
],
'#required' => TRUE,
'#attributes' => [
'id' => 'pk',
],
];
$form['remember_clients'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Remember previously approved clients'),
'#description' => $this
->t('When enabled, autorized clients will be stored and a authorization requests for the same client with previously accepted scopes will automatically be accepted.'),
'#default_value' => $config
->get('remember_clients'),
];
$form['actions'] = [
'actions' => [
'#cache' => [
'max-age' => 0,
],
'#weight' => 20,
],
];
// Generate Key Modal Button if openssl extension is enabled.
if ($this->fileSystemChecker
->isExtensionEnabled('openssl')) {
// Generate Modal Button.
$form['actions']['generate']['keys'] = [
'#type' => 'link',
'#title' => $this
->t('Generate keys'),
'#url' => Url::fromRoute('oauth2_token.settings.generate_key', [], [
'query' => [
'pubk_id' => 'pubk',
'pk_id' => 'pk',
],
]),
'#attributes' => [
'class' => [
'use-ajax',
'button',
],
],
];
// Attach Drupal Modal Dialog library.
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
}
else {
// Generate Notice Info Message about enabling openssl extension.
$this->messenger
->addMessage($this
->t('Enabling the PHP OpenSSL Extension will permit you generate the keys from this form.'), 'warning');
}
return parent::buildForm($form, $form_state);
}
/**
* Validates if the file exists.
*
* @param array $element
* The element being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*/
public function validateExistingFile(array &$element, FormStateInterface $form_state, array &$complete_form) {
if (!empty($element['#value'])) {
$path = $element['#value'];
// Does the file exist?
if (!$this->fileSystemChecker
->fileExist($path)) {
$form_state
->setError($element, $this
->t('The %field file does not exist.', [
'%field' => $element['#title'],
]));
}
// Is the file readable?
if (!$this->fileSystemChecker
->isReadable($path)) {
$form_state
->setError($element, $this
->t('The %field file at the specified location is not readable.', [
'%field' => $element['#title'],
]));
}
}
}
}
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:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
Oauth2TokenSettingsForm:: |
protected | property | The file system checker. | |
Oauth2TokenSettingsForm:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
Oauth2TokenSettingsForm:: |
public | function |
Defines the settings form for Access Token entities. Overrides ConfigFormBase:: |
|
Oauth2TokenSettingsForm:: |
public static | function |
Creates the form. Overrides ConfigFormBase:: |
|
Oauth2TokenSettingsForm:: |
protected | function |
Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait:: |
|
Oauth2TokenSettingsForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
Oauth2TokenSettingsForm:: |
public | function |
Form submission handler. Overrides ConfigFormBase:: |
|
Oauth2TokenSettingsForm:: |
public | function | Validates if the file exists. | |
Oauth2TokenSettingsForm:: |
public | function |
Oauth2TokenSettingsForm 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. |