remove db on drupal site and gibe data handling to daemon
This commit is contained in:
parent
d7ae7d3338
commit
43c865e658
3 changed files with 60 additions and 110 deletions
|
|
@ -2,7 +2,6 @@
|
||||||
|
|
||||||
namespace Drupal\wisski_cloud_account_manager\Form;
|
namespace Drupal\wisski_cloud_account_manager\Form;
|
||||||
|
|
||||||
use Drupal\Core\Database\Database;
|
|
||||||
use Drupal\Core\Form\FormBase;
|
use Drupal\Core\Form\FormBase;
|
||||||
use Drupal\Core\Form\FormStateInterface;
|
use Drupal\Core\Form\FormStateInterface;
|
||||||
use Drupal\wisski_cloud_account_manager\WisskiCloudAccountManagerDaemonApiActions;
|
use Drupal\wisski_cloud_account_manager\WisskiCloudAccountManagerDaemonApiActions;
|
||||||
|
|
@ -15,7 +14,7 @@ class WisskiCloudAccountManagerCreateForm extends FormBase {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Drupal\wisski_cloud_account_manager\WisskiCloudAccountManagerDaemonApiActions
|
* @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;
|
protected WisskiCloudAccountManagerDaemonApiActions $wisskiCloudAccountManagerDaemonApiActions;
|
||||||
|
|
||||||
|
|
@ -119,48 +118,30 @@ class WisskiCloudAccountManagerCreateForm extends FormBase {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
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?.
|
// @todo Check if username is WissKI Cloud accounts, i.e add direct by admin?.
|
||||||
$username = $form_state->getValue('username');
|
$dataToCheck['username'] = $form_state->getValue('username');
|
||||||
$conn = Database::getConnection();
|
$dataToCheck['email'] = $form_state->getValue('email');
|
||||||
$accountWithUsername = $conn
|
$dataToCheck['subdomain'] = $form_state->getValue('subdomain');
|
||||||
->select('wisski_cloud_accounts', 'wca')
|
|
||||||
->fields('wca', ['username'])
|
$response = $this->wisskiCloudAccountManagerDaemonApiActions->checkAccountData($dataToCheck);
|
||||||
->condition('username', $username)
|
|
||||||
->execute()
|
if ($response['accountData']['userWithUsername']) {
|
||||||
->fetchCol();
|
$form_state->setErrorByName('username', $this->t('The username "@username" is already in use.', ['@username' => $dataToCheck['username']]));
|
||||||
if (!empty($accountWithUsername)) {
|
}
|
||||||
$form_state->setErrorByName('username', $this->t('The username @username is already in use.', ['@username' => $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.
|
// Check if email is in valid form.
|
||||||
$email = $form_state->getValue('email');
|
if (!\Drupal::service('email.validator')->isValid($dataToCheck['email'])) {
|
||||||
if (!\Drupal::service('email.validator')->isValid($email)) {
|
|
||||||
$form_state->setErrorByName('email', $this->t('Email not in valid form, i.e. "name@example.com".'));
|
$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 {
|
public function submitForm(array &$form, FormStateInterface $form_state): void {
|
||||||
try {
|
try {
|
||||||
$conn = Database::getConnection();
|
|
||||||
|
|
||||||
$field = $form_state->getValues();
|
$field = $form_state->getValues();
|
||||||
|
|
||||||
$account["personname"] = $field['personname'];
|
$account["personname"] = $field['personname'];
|
||||||
|
|
@ -179,19 +158,10 @@ class WisskiCloudAccountManagerCreateForm extends FormBase {
|
||||||
$account["password"] = $field['password'];
|
$account["password"] = $field['password'];
|
||||||
$account["subdomain"] = $field['subdomain'];
|
$account["subdomain"] = $field['subdomain'];
|
||||||
|
|
||||||
$daemonResponse = $this->wisskiCloudAccountManagerDaemonApiActions->addAccount($account);
|
$this->wisskiCloudAccountManagerDaemonApiActions->addAccount($account);
|
||||||
dpm($daemonResponse, 'Daemon response');
|
|
||||||
|
|
||||||
unset($account["password"]);
|
|
||||||
|
|
||||||
dpm($account);
|
|
||||||
|
|
||||||
/*
|
|
||||||
$conn->insert('wisski_cloud_accounts')
|
|
||||||
->fields($account)->execute();
|
|
||||||
\Drupal::messenger()
|
\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) {
|
catch (\Exception $ex) {
|
||||||
\Drupal::logger('wisski_cloud_account_manager')->error($ex->getMessage());
|
\Drupal::logger('wisski_cloud_account_manager')->error($ex->getMessage());
|
||||||
|
|
|
||||||
|
|
@ -12,8 +12,11 @@ use GuzzleHttp\ClientInterface;
|
||||||
* Handles the communication with the WissKI Cloud account manager daemon.
|
* Handles the communication with the WissKI Cloud account manager daemon.
|
||||||
*/
|
*/
|
||||||
class WisskiCloudAccountManagerDaemonApiActions {
|
class WisskiCloudAccountManagerDaemonApiActions {
|
||||||
|
|
||||||
const DAEMON_URL = 'http://wisski_cloud_api_daemon:3000/wisski-cloud-daemon/api/v1/user/';
|
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.
|
* The string translation service.
|
||||||
*
|
*
|
||||||
|
|
@ -75,4 +78,40 @@ class WisskiCloudAccountManagerDaemonApiActions {
|
||||||
return json_decode($response->getBody()->getContents(), TRUE);
|
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" => [],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,59 +0,0 @@
|
||||||
<?php
|
|
||||||
use Drupal\Core\Database\Database;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_schema().
|
|
||||||
*/
|
|
||||||
function wisski_cloud_account_manager_schema(): array {
|
|
||||||
$schema['wisski_cloud_accounts'] = [
|
|
||||||
'description' => '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;
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue