add config page and validation
This commit is contained in:
parent
43c865e658
commit
79887ee7e6
9 changed files with 210 additions and 36 deletions
|
|
@ -51,7 +51,7 @@ class WisskiCloudAccountManagerCreateForm extends FormBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state) {
|
||||
$form['personname'] = [
|
||||
$form['personName'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Person name'),
|
||||
'#description' => $this->t('Your first and last name.'),
|
||||
|
|
@ -117,7 +117,7 @@ class WisskiCloudAccountManagerCreateForm extends FormBase {
|
|||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
public function validateForm(array &$form, FormStateInterface $form_state): void {
|
||||
// Check if account data is already in use.
|
||||
// @todo Check if username is WissKI Cloud accounts, i.e add direct by admin?.
|
||||
$dataToCheck['username'] = $form_state->getValue('username');
|
||||
|
|
@ -151,17 +151,19 @@ class WisskiCloudAccountManagerCreateForm extends FormBase {
|
|||
try {
|
||||
$field = $form_state->getValues();
|
||||
|
||||
$account["personname"] = $field['personname'];
|
||||
$account["personName"] = $field['personName'];
|
||||
$account["organisation"] = $field['organisation'];
|
||||
$account["email"] = $field['email'];
|
||||
$account["username"] = $field['username'];
|
||||
$account["password"] = $field['password'];
|
||||
$account["subdomain"] = $field['subdomain'];
|
||||
|
||||
$this->wisskiCloudAccountManagerDaemonApiActions->addAccount($account);
|
||||
$accountResponse = $this->wisskiCloudAccountManagerDaemonApiActions->addAccount($account);
|
||||
dpm($accountResponse, 'accountResponse');
|
||||
$this->wisskiCloudAccountManagerDaemonApiActions->sendValidationEmail($accountResponse['user']['email'], $accountResponse['user']['validationCode']);
|
||||
|
||||
\Drupal::messenger()
|
||||
->addMessage($this->t('The account data has been succesfully saved'));
|
||||
->addMessage($this->t('The account data has been successfully saved, please check your email for validation!'));
|
||||
}
|
||||
catch (\Exception $ex) {
|
||||
\Drupal::logger('wisski_cloud_account_manager')->error($ex->getMessage());
|
||||
|
|
|
|||
|
|
@ -35,6 +35,27 @@ class WisskiCloudAccountManagerSettingsForm extends ConfigFormBase {
|
|||
/** @var \Drupal\Core\Config\ImmutableConfig $config */
|
||||
$config = $this->config('wisski_cloud_account_manager.settings');
|
||||
|
||||
$form['daemonUrl'] = [
|
||||
'#type' => 'url',
|
||||
'#title' => $this->t('The WissKI Cloud API Daemon URL'),
|
||||
'#description' => $this->t('Provide the complete base URL with protocol, domain (resp. service name in docker), ports and API path, i. e. "http://wisski_cloud_api_daemon:3000/wisski-cloud-daemon/api/v1"'),
|
||||
'#default_value' => $config->get('daemonUrl'),
|
||||
];
|
||||
|
||||
$form['userPostUrlPath'] = [
|
||||
'#type' => 'url',
|
||||
'#title' => $this->t('POST URL path'),
|
||||
'#description' => $this->t('Provide the path to the POST endpoint, i. e. "/user"'),
|
||||
'#default_value' => $config->get('userPostUrlPath'),
|
||||
];
|
||||
|
||||
$form['userFilterByData'] = [
|
||||
'#type' => 'url',
|
||||
'#title' => $this->t('Filter by Data URL path'),
|
||||
'#description' => $this->t('Provide the path to the Get user by data endpoint, i. e. "/user/by_data"'),
|
||||
'#default_value' => $config->get('userFilterByData'),
|
||||
];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
|
@ -43,27 +64,13 @@ class WisskiCloudAccountManagerSettingsForm extends ConfigFormBase {
|
|||
*/
|
||||
public function submitForm(array &$form, FormStateInterface $form_state) {
|
||||
$config = $this->config('wisski_cloud_account_manager.settings');
|
||||
/*
|
||||
$config->set('', $form_state->getValue(''))
|
||||
|
||||
$config->set('daemonURL', $form_state->getValue('daemonURL'))
|
||||
->set('userPostUrlPath', $form_state->getValue('userPostUrlPath'))
|
||||
->set('userFilterByData', $form_state->getValue('userFilterByData'))
|
||||
->save();
|
||||
*/
|
||||
|
||||
parent::submitForm($form, $form_state);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
/*
|
||||
// Hash expiration validation.
|
||||
$hash_expiration = intval($form_state->getValue('hash_expiration'));
|
||||
if ($hash_expiration < 1) {
|
||||
$form_state->setErrorByName('hash_expiration', $this->t('The miminum hash expiration time is @min_value.', ['@min_value' => $this->t('one hour')]));
|
||||
}
|
||||
elseif ($hash_expiration > 48) {
|
||||
$form_state->setErrorByName('hash_expiration', $this->t('The maximum hash expiration time is @max_value.', ['@max_value' => $this->t('@count days', ['@count' => 2])]));
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,11 @@ namespace Drupal\wisski_cloud_account_manager;
|
|||
|
||||
use Drupal\Core\Config\Config;
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
|
||||
use Drupal\Core\Language\LanguageManagerInterface;
|
||||
use Drupal\Core\Mail\MailManagerInterface;
|
||||
use Drupal\Core\Messenger\MessengerInterface;
|
||||
use Drupal\Core\Render\Markup;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
|
|
@ -13,9 +17,28 @@ use GuzzleHttp\ClientInterface;
|
|||
*/
|
||||
class WisskiCloudAccountManagerDaemonApiActions {
|
||||
|
||||
const DAEMON_URL = 'http://wisski_cloud_api_daemon:3000/wisski-cloud-daemon/api/v1/user/';
|
||||
use DependencySerializationTrait;
|
||||
|
||||
const FILTER_BY_DATA_URL_PART = 'by_data';
|
||||
/**
|
||||
* The base URL of the WissKI Cloud account manager daemon.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private string $DAEMON_URL;
|
||||
|
||||
/**
|
||||
* The URL path to the POST endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private string $USER_POST_URL_PART = '/user';
|
||||
|
||||
/**
|
||||
* The URL path to the GET endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private string $FILTER_BY_DATA_URL_PART = '/user/by_data';
|
||||
|
||||
/**
|
||||
* The string translation service.
|
||||
|
|
@ -45,15 +68,49 @@ class WisskiCloudAccountManagerDaemonApiActions {
|
|||
*/
|
||||
protected Config $settings;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* The mail manager.
|
||||
*
|
||||
* @var \Drupal\Core\Mail\MailManagerInterface
|
||||
*/
|
||||
protected MailManagerInterface $mailManager;
|
||||
|
||||
/**
|
||||
* The language manager.
|
||||
*
|
||||
* @var \Drupal\Core\Language\LanguageManagerInterface
|
||||
*/
|
||||
protected LanguageManagerInterface $languageManager;
|
||||
|
||||
/**
|
||||
* Class constructor.
|
||||
*/
|
||||
public function __construct(TranslationInterface $stringTranslation, MessengerInterface $messenger, ClientInterface $httpClient, ConfigFactoryInterface $configFactory) {
|
||||
public function __construct(
|
||||
TranslationInterface $stringTranslation,
|
||||
MessengerInterface $messenger,
|
||||
ClientInterface $httpClient,
|
||||
ConfigFactoryInterface $configFactory,
|
||||
MailManagerInterface $mailManager,
|
||||
LanguageManagerInterface $languageManager) {
|
||||
// Services from container.
|
||||
$this->stringTranslation = $stringTranslation;
|
||||
$this->messenger = $messenger;
|
||||
$this->httpClient = $httpClient;
|
||||
$this->settings = $configFactory
|
||||
$this->mailManager = $mailManager;
|
||||
$this->languageManager = $languageManager;
|
||||
|
||||
// Settings.
|
||||
$settings = $configFactory
|
||||
->getEditable('wisski_cloud_account_manager.settings');
|
||||
$this->settings = $settings;
|
||||
|
||||
// Set the daemon URL and the URL parts class variables.
|
||||
$this->DAEMON_URL = $settings->get('daemonUrl') ?: 'http://wisski_cloud_api_daemon:3000/wisski-cloud-daemon/api/v1';
|
||||
$this->USER_POST_URL_PART = $settings->get('userPostUrlPath') ?: '/user';
|
||||
$this->FILTER_BY_DATA_URL_PART = $settings->get('userFilterByData') ?: '/user/by_data';
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -66,16 +123,15 @@ class WisskiCloudAccountManagerDaemonApiActions {
|
|||
* The response from the daemon (user id with validation code).
|
||||
*/
|
||||
public function addAccount(array $account): array {
|
||||
dpm($account, 'account');
|
||||
$request = [
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'body' => json_encode($account),
|
||||
];
|
||||
dpm($request, 'request');
|
||||
$response = $this->httpClient->post(self::DAEMON_URL, $request);
|
||||
return json_decode($response->getBody()->getContents(), TRUE);
|
||||
$userPostUrl = $this->DAEMON_URL . $this->USER_POST_URL_PART;
|
||||
$response = $this->httpClient->post($userPostUrl, $request);
|
||||
return array_merge(json_decode($response->getBody()->getContents(), TRUE), ['statusCode' => $response->getStatusCode()]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -92,7 +148,7 @@ class WisskiCloudAccountManagerDaemonApiActions {
|
|||
$query_string = http_build_query($dataToCheck);
|
||||
|
||||
// Combine the base URL and the query string.
|
||||
$request_url = self::DAEMON_URL . self::FILTER_BY_DATA_URL_PART . '?' . $query_string;
|
||||
$request_url = $this->DAEMON_URL . $this->FILTER_BY_DATA_URL_PART . '?' . $query_string;
|
||||
|
||||
// Send the GET request using the `drupal_http_request()` function.
|
||||
$response = $this->httpClient->get($request_url);
|
||||
|
|
@ -114,4 +170,35 @@ class WisskiCloudAccountManagerDaemonApiActions {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a validation email to the given email address.
|
||||
*
|
||||
* @param string $email
|
||||
* The email address to send the validation email to.
|
||||
* @param string $validationCode
|
||||
* The validation code to be used in the validation link.
|
||||
*/
|
||||
public function sendValidationEmail(string $email, string $validationCode): void {
|
||||
$module = 'wisski_cloud_account_manager';
|
||||
$key = 'wisski_cloud_account_validation';
|
||||
$langcode = $this->languageManager->getDefaultLanguage()->getId();
|
||||
$to = $email;
|
||||
|
||||
$validationLink = \Drupal::request()->getSchemeAndHttpHost() . '/wisski-cloud-account-manager/validate/' . $validationCode;
|
||||
|
||||
$params['message'] = Markup::create($this->stringTranslation->translate('<p>Please validate your account by clicking on this <a href="@validationLink" target="_blank">link</a> or copy this to the address bar of your browser: <p>@validationLink</p>.</p>', ['@validationLink' => $validationLink]));
|
||||
$params['subject'] = $this->stringTranslation->translate('WissKI Cloud account validation');
|
||||
|
||||
$result = $this->mailManager->mail($module, $key, $to, $langcode, $params, NULL, TRUE);
|
||||
dpm($result, 'result');
|
||||
if ($result['result'] === TRUE) {
|
||||
$this->messenger
|
||||
->addMessage($this->stringTranslation->translate('Email sent successfully.'));
|
||||
}
|
||||
else {
|
||||
$this->messenger
|
||||
->addMessage($this->stringTranslation->translate('There was an error sending the email.'), 'error');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue