From 43c865e6586eb15649a51dfa1c4688cbd5599dec Mon Sep 17 00:00:00 2001 From: Robert Nasarek Date: Thu, 3 Aug 2023 23:07:48 +0200 Subject: [PATCH] remove db on drupal site and gibe data handling to daemon --- .../WisskiCloudAccountManagerCreateForm.php | 72 ++++++------------- ...skiCloudAccountManagerDaemonApiActions.php | 39 ++++++++++ wisski_cloud_account_manager.install | 59 --------------- 3 files changed, 60 insertions(+), 110 deletions(-) delete mode 100644 wisski_cloud_account_manager.install diff --git a/src/Form/WisskiCloudAccountManagerCreateForm.php b/src/Form/WisskiCloudAccountManagerCreateForm.php index 38ee37a..f9abd75 100644 --- a/src/Form/WisskiCloudAccountManagerCreateForm.php +++ b/src/Form/WisskiCloudAccountManagerCreateForm.php @@ -2,7 +2,6 @@ namespace Drupal\wisski_cloud_account_manager\Form; -use Drupal\Core\Database\Database; use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormStateInterface; use Drupal\wisski_cloud_account_manager\WisskiCloudAccountManagerDaemonApiActions; @@ -15,7 +14,7 @@ class WisskiCloudAccountManagerCreateForm extends FormBase { /** * @var \Drupal\wisski_cloud_account_manager\WisskiCloudAccountManagerDaemonApiActions - * The WissKi Cloud account manager daemon API actions service. + * The WissKi Cloud account manager daemon API actions service. */ protected WisskiCloudAccountManagerDaemonApiActions $wisskiCloudAccountManagerDaemonApiActions; @@ -119,48 +118,30 @@ class WisskiCloudAccountManagerCreateForm extends FormBase { * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { - // Check if username is in database. + // Check if account data is already in use. // @todo Check if username is WissKI Cloud accounts, i.e add direct by admin?. - $username = $form_state->getValue('username'); - $conn = Database::getConnection(); - $accountWithUsername = $conn - ->select('wisski_cloud_accounts', 'wca') - ->fields('wca', ['username']) - ->condition('username', $username) - ->execute() - ->fetchCol(); - if (!empty($accountWithUsername)) { - $form_state->setErrorByName('username', $this->t('The username @username is already in use.', ['@username' => $username])); + $dataToCheck['username'] = $form_state->getValue('username'); + $dataToCheck['email'] = $form_state->getValue('email'); + $dataToCheck['subdomain'] = $form_state->getValue('subdomain'); + + $response = $this->wisskiCloudAccountManagerDaemonApiActions->checkAccountData($dataToCheck); + + if ($response['accountData']['userWithUsername']) { + $form_state->setErrorByName('username', $this->t('The username "@username" is already in use.', ['@username' => $dataToCheck['username']])); + } + + if ($response['accountData']['userWithEmail']) { + $form_state->setErrorByName('email', $this->t('The email "@email" is already in use.', ['@email' => $dataToCheck['email']])); + } + + if ($response['accountData']['userWithSubdomain']) { + $form_state->setErrorByName('subdomain', $this->t('The subdomain "@subdomain" is already in use.', ['@subdomain' => $dataToCheck['subdomain']])); } // Check if email is in valid form. - $email = $form_state->getValue('email'); - if (!\Drupal::service('email.validator')->isValid($email)) { + if (!\Drupal::service('email.validator')->isValid($dataToCheck['email'])) { $form_state->setErrorByName('email', $this->t('Email not in valid form, i.e. "name@example.com".')); } - - // Check if email is in database. - $accountWithEmail = $conn - ->select('wisski_cloud_accounts', 'wca') - ->fields('wca', ['email']) - ->condition('email', $email) - ->execute() - ->fetchCol(); - if (!empty($accountWithEmail)) { - $form_state->setErrorByName('email', $this->t('The email @email is already in use.', ['@email' => $email])); - } - - // Check if subdomain is in database. - $subdomain = $form_state->getValue('subdomain'); - $accountWithSubdomain = $conn - ->select('wisski_cloud_accounts', 'wca') - ->fields('wca', ['subdomain']) - ->condition('subdomain', $subdomain) - ->execute() - ->fetchCol(); - if (!empty($accountWithSubdomain)) { - $form_state->setErrorByName('subdomain', $this->t('The subdomain @subdomain is already in use.', ['@subdomain' => $subdomain])); - } } /** @@ -168,8 +149,6 @@ class WisskiCloudAccountManagerCreateForm extends FormBase { */ public function submitForm(array &$form, FormStateInterface $form_state): void { try { - $conn = Database::getConnection(); - $field = $form_state->getValues(); $account["personname"] = $field['personname']; @@ -179,19 +158,10 @@ class WisskiCloudAccountManagerCreateForm extends FormBase { $account["password"] = $field['password']; $account["subdomain"] = $field['subdomain']; - $daemonResponse = $this->wisskiCloudAccountManagerDaemonApiActions->addAccount($account); - dpm($daemonResponse, 'Daemon response'); + $this->wisskiCloudAccountManagerDaemonApiActions->addAccount($account); - unset($account["password"]); - - dpm($account); - - /* - $conn->insert('wisski_cloud_accounts') - ->fields($account)->execute(); \Drupal::messenger() - ->addMessage($this->t('The account data has been succesfully saved')); - */ + ->addMessage($this->t('The account data has been succesfully saved')); } catch (\Exception $ex) { \Drupal::logger('wisski_cloud_account_manager')->error($ex->getMessage()); diff --git a/src/WisskiCloudAccountManagerDaemonApiActions.php b/src/WisskiCloudAccountManagerDaemonApiActions.php index 7578f5f..ff243d0 100644 --- a/src/WisskiCloudAccountManagerDaemonApiActions.php +++ b/src/WisskiCloudAccountManagerDaemonApiActions.php @@ -12,8 +12,11 @@ use GuzzleHttp\ClientInterface; * Handles the communication with the WissKI Cloud account manager daemon. */ class WisskiCloudAccountManagerDaemonApiActions { + const DAEMON_URL = 'http://wisski_cloud_api_daemon:3000/wisski-cloud-daemon/api/v1/user/'; + const FILTER_BY_DATA_URL_PART = 'by_data'; + /** * The string translation service. * @@ -75,4 +78,40 @@ class WisskiCloudAccountManagerDaemonApiActions { return json_decode($response->getBody()->getContents(), TRUE); } + /** + * Check if an account with the given data already exists. + * + * @param array $dataToCheck + * The data to check. + * + * @return array + * The response from the daemon. + */ + public function checkAccountData($dataToCheck): array { + // Build the query string from the parameters. + $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; + + // Send the GET request using the `drupal_http_request()` function. + $response = $this->httpClient->get($request_url); + + // Check the response and handle the data accordingly. + if ($response->getStatusCode() == 200) { + // Request successful, handle the data in $response->data. + return [ + "message" => "Get account data", + "accountData" => json_decode($response->getBody()->getContents(), TRUE), + ]; + } + else { + // Request failed, handle the error. + return [ + "message" => 'Request failed with code: ' . $response->getStatusCode(), + "accountData" => [], + ]; + } + } + } diff --git a/wisski_cloud_account_manager.install b/wisski_cloud_account_manager.install deleted file mode 100644 index 18b5120..0000000 --- a/wisski_cloud_account_manager.install +++ /dev/null @@ -1,59 +0,0 @@ - 'The table for storing the WissKI Cloud users data.', - 'fields' => [ - 'id' => [ - 'description' => 'The primary identifier for user. Same as in mongo db.', - 'type' => 'char', - 'length' => 24, - 'not null' => TRUE, - ], - 'personname' => [ - 'description' => 'Real person name.', - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - ], - 'organisation' => [ - 'description' => 'Organisation.', - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - ], - 'email' => [ - 'description' => 'Email', - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - - ], - 'username' => [ - 'description' => 'User name.', - 'type' => 'varchar', - 'length' => 20, - 'not null' => TRUE, - ], - 'subdomain' => [ - 'description' => 'Subdomain.', - 'type' => 'varchar', - 'length' => 20, - 'not null' => TRUE, - ], - 'created' => [ - 'description' => 'Timestamp when the user was created.', - 'type' => 'int', - 'not null' => TRUE, - 'default' => 0, - ], - ], - 'primary key' => ['id'], - ]; - - return $schema; -}