Adapt Lemonldap module to single API

e_collectivites-version
Romain_L 2 years ago
parent 40647446ee
commit 3b3d0a9a86
  1. 3
      api_lemon_pleiade/api_lemon_pleiade.module
  2. 2
      api_lemon_pleiade/css/api_lemon_pleiade.css
  3. 142
      api_lemon_pleiade/js/api_lemon_pleiade_desktop_blocks.js
  4. 11
      api_lemon_pleiade/js/api_lemon_pleiade_history.js
  5. 6
      api_lemon_pleiade/src/Controller/PleiadeAjaxController.php
  6. 157
      api_lemon_pleiade/src/LemonDataApiManager.php

@ -19,6 +19,9 @@ function api_lemon_pleiade_preprocess_page(&$variables){
$store = $tempstore->get('api_lemon_pleiade');
$groups = $store->get('groups');
// Add the CSS library
$variables['#attached']['library'][] = 'api_lemon_pleiade/api_lemon_pleiade_css';
// Add the JS library
$variables['#attached']['library'][] = 'api_lemon_pleiade/api_lemon_pleiade_js';

@ -22,4 +22,4 @@
display: flex;
align-items: center;
white-space: nowrap;
}
}

@ -23,18 +23,14 @@
var id = "row-" + i;
blocLemon +=
`<div class="col-lg-12"><div class="mb-2 shadow-lg"><div class="card my-2"><div class="card-header rounded-top" style="background-color: #1f3889"><h4 class="card-title text-light">` +
`<div class="col-lg-12"><div class="shadow-lg"><div class="card mb-2">\
<div class="card-header rounded-top bg-white border-bottom rounded-top">\
<h4 class="card-title text-dark py-2">` +
donnees.myapplications[i].Category +
`<span></span></h4></div><div class="card-body"><div class="row" id="${id}">`; // ajout de le l'id dans le html avec le numéro de boucle
for (
var f = 0;
f < donnees.myapplications[i].Applications.length;
f++
) {
const temp = Object.values(
donnees.myapplications[i].Applications[f]
);
for ( var f = 0; f < donnees.myapplications[i].Applications.length; f++ )
{
const temp = Object.values( donnees.myapplications[i].Applications[f]);
blocLemon +=
'<div class="col-md-4 my-3"><a target="_blank" class="border-dark text-center" title="' +
temp[0].AppDesc +
@ -64,72 +60,72 @@
xhr.onloadend = function () {
//Sortable management inside the Lemon apps blocs
const htmlDoc = document.getElementById("lemon_block_id");
const nbObjinBlocLemon = htmlDoc.children.length;
for (var f = 0; f < nbObjinBlocLemon; f++) {
// récup des éléments du bloc dont l'id contient row-n
var nbBloc = document.getElementById("row-" + f);
Sortable.create(nbBloc, {
animation: 150,
store: {
// ajout de la sauvegarde des emplacements de chaque blocs au rafraichissement
/**
* Get the order of elements. Called once during initialization.
* @param {Sortable} sortable
* @returns {Array}
*/
get: function (sortable) {
var order = localStorage.getItem(
sortable.options.group
);
return order ? order.split("|") : [];
},
// const htmlDoc = document.getElementById("areaSortable");
// const nbObjinBlocLemon = htmlDoc.children.length;
// for (var f = 0; f < nbObjinBlocLemon; f++) {
// // récup des éléments du bloc dont l'id contient row-n
// var nbBloc = document.getElementById("row-" + f);
// Sortable.create(nbBloc, {
// animation: 150,
// store: {
// // ajout de la sauvegarde des emplacements de chaque blocs au rafraichissement
// /**
// * Get the order of elements. Called once during initialization.
// * @param {Sortable} sortable
// * @returns {Array}
// */
// get: function (sortable) {
// var order = localStorage.getItem(
// sortable.options.group
// );
// return order ? order.split("|") : [];
// },
/**
* Save the order of elements. Called onEnd (when the item is dropped).
* @param {Sortable} sortable
*/
set: function (sortable) {
var order = sortable.toArray();
localStorage.setItem(
sortable.options.group,
order.join("|")
);
},
},
});
var recupBlocForDragAndDrop = document.getElementById("lemon_block_id");
// /**
// * Save the order of elements. Called onEnd (when the item is dropped).
// * @param {Sortable} sortable
// */
// set: function (sortable) {
// var order = sortable.toArray();
// localStorage.setItem(
// sortable.options.group,
// order.join("|")
// );
// },
// },
// });
// var recupBlocForDragAndDrop = document.getElementById("zimbra_block_id");
new Sortable.create(recupBlocForDragAndDrop, {
animation: 150,
store: {
// ajout de la sauvegarde des emplacement de chaque blocs au rafraichissement
/**
* Get the order of elements. Called once during initialization.
* @param {Sortable} sortable
* @returns {Array}
*/
get: function (sortable) {
var order = localStorage.getItem(
sortable.options.group
);
return order ? order.split("|") : [];
},
// new Sortable.create(recupBlocForDragAndDrop, {
// animation: 150,
// store: {
// // ajout de la sauvegarde des emplacement de chaque blocs au rafraichissement
// /**
// * Get the order of elements. Called once during initialization.
// * @param {Sortable} sortable
// * @returns {Array}
// */
// get: function (sortable) {
// var order = localStorage.getItem(
// sortable.options.group
// );
// return order ? order.split("|") : [];
// },
/**
* Save the order of elements. Called onEnd (when the item is dropped).
* @param {Sortable} sortable
*/
set: function (sortable) {
var order = sortable.toArray();
localStorage.setItem(
sortable.options.group,
order.join("|")
);
},
},
});
}
// /**
// * Save the order of elements. Called onEnd (when the item is dropped).
// * @param {Sortable} sortable
// */
// set: function (sortable) {
// var order = sortable.toArray();
// localStorage.setItem(
// sortable.options.group,
// order.join("|")
// );
// },
// },
// });
// }
};
xhr.send();

@ -20,17 +20,14 @@
// Si on est sur la page historique des connexions
if (drupalSettings.path.currentPath === "history") {
console.log("We are on page : history!!");
// console.log("We are on page : history!!");
var history_table =
'<h4 class="card-header rounded-top card-title text-light" style="background-color: #1f3889">Dernières connexions</h4><table class="table"><thead><tr><th scope="col">Date</th><th scope="col">Adresse IP</th></tr></thead><tbody>';
if(response._loginHistory.successLogin) {
for (
var i = 0;
i < response._loginHistory.successLogin.length;
i++
) {
var objectDate = new Date(
for ( var i = 0; i < response._loginHistory.successLogin.length; i++)
{
var objectDate = new Date(
response._loginHistory.successLogin[i]._utime * 1000
);
history_table +=

@ -8,7 +8,7 @@ use Drupal\Component\Serialization\JSON;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Drupal\api_lemon_pleiade\LemonDataApiManager;
use Drupal\module_api_pleiade\ApiPleiadeManager;
use Drupal\user\PrivateTempStoreFactory;
@ -21,7 +21,7 @@ class PleiadeAjaxController extends ControllerBase {
// Debug what's in request
\Drupal::logger('lemon_myapps_query')->info('Request $request: @request', ['@request' => $request ]);
$lemondataApi = new LemonDataApiManager();
$lemondataApi = new ApiPleiadeManager();
$return = $lemondataApi->searchMyApps();
return new JsonResponse(json_encode($return), 200, [], true);
}
@ -32,7 +32,7 @@ class PleiadeAjaxController extends ControllerBase {
// Debug what's in request
\Drupal::logger('lemon_session_query')->info('Request $request: @request', ['@request' => $request ]);
$lemondataApi = new LemonDataApiManager();
$lemondataApi = new ApiPleiadeManager();
$return = $lemondataApi->searchMySession();
// Store groups in Drupal private tempstore to serve to other modules later

@ -1,157 +0,0 @@
<?php
namespace Drupal\api_lemon_pleiade;
use Drupal\Component\Serialization\Json;
use GuzzleHttp\Exception\RequestException;
/**
* Basic manager of module.
*/
class LemonDataApiManager {
/**
* Drupal's settings manager.
*/
protected $settings;
public $client;
/**
* Constructor.
*/
public function __construct() {
if (!isset($_COOKIE['lemonldap'])) {
$msg = 'Pas authentifié dans le SSO Lemon';
\Drupal::logger('api_lemon_pleiade')->error($msg);
return;
}
$this->client = \Drupal::httpClient();
// get our custom module settings
$this->settings = \Drupal::config('api_lemon_pleiade.settings');
}
/**
* Do CURL request with authorization.
*
* @param string $endpoint
* A request action of api.
* @param string $method
* A method of curl request.
* @param Array $inputs
* A data of curl request.
*
* @return array
* An associate array with respond data.
*/
private function executeCurl($endpoint, $method, $inputs, $api) {
if (!isset($_COOKIE['lemonldap'])) {
$msg = 'Pas authentifié dans le SSO Lemon';
\Drupal::logger('api_lemon_pleiade')->error($msg);
return NULL;
}
// Si on a le cookie Lemon on le loggue dans Drupal pour test - TODO : supprimer
// \Drupal::logger('api_lemon_pleiade')->info($_COOKIE['lemonldap']);
\Drupal::logger('api_lemon_pleiade')->info('Cookie Lemon: @cookie', ['@cookie' => $_COOKIE['lemonldap']]);
$LEMON_API_URL = $api . "/" . $endpoint;
\Drupal::logger('api_lemon_pleiade')->info('LEMON_API_URL: @api', ['@api' => $LEMON_API_URL ]);
$options = [
'headers' => [
'Content-Type' => 'application/json',
'Cookie'=> 'llnglanguage=fr; lemonldap=' . $_COOKIE['lemonldap']
],
];
if (!empty($inputs)) {
\Drupal::logger('api_lemon_pleiade')->info('Inputs dans la requête: @inp', ['@inp' => $inputs ]);
if($method == 'GET'){
$LEMON_API_URL.= '?' . self::arrayKeyfirst($inputs) . '=' . array_shift($inputs);
foreach($inputs as $param => $value){
$LEMON_API_URL.= '&' . $param . '=' . $value;
}
}else{
//POST request send data in array index form_params.
$options['body'] = $inputs;
}
}
try {
$clientRequest = $this->client->request($method, $LEMON_API_URL, $options);
$body = $clientRequest->getBody();
} catch (RequestException $e) {
\Drupal::logger('api_lemon_pleiade')->error('Curl error: @error', ['@error' => $e->getMessage()]);
}
return Json::decode($body);
}
/**
* Get Request of API.
*
* @param string $endpoint
* A request action.
* @param string $input
* A data of curl request.
*
* @return array
* A respond data.
*/
public function curlGet($endpoint, $inputs, $api) {
// \Drupal::logger('api_lemon_pleiade')->info('Ressource, inputs, api: @res @inputs @api', ['@res' => $endpoint, '@inputs' => $inputs, '@api' => $api]);
return $this->executeCurl($endpoint, "GET", $inputs, $api);
}
/**
* Post Request of API.
*
* @param string $endpoint
* A request action.
* @param string $inputs
* A data of curl request.
*
* @return array
* A respond data.
*/
public function curlPost($endpoint, $inputs, $api) {
return $this->executeCurl($endpoint, "POST", $inputs, $api);
}
public function searchMyApps() {
$endpoints = $this->settings->get('field_lemon_myapps_url'); // Endpoint myapplications de Lemon qui renvoie toutes nos apps
\Drupal::logger('api_lemon_pleiade')->info('function searchMyApps triggered !');
return $this->curlGet($endpoints, [], $this->settings->get('field_lemon_url'));
}
public function searchMySession() {
$endpoints = $this->settings->get('field_lemon_sessioninfo_url'); // Endpoint myapplications de Lemon qui renvoie toutes nos apps
\Drupal::logger('api_lemon_pleiade')->info('function searchMySession triggered !');
return $this->curlGet($endpoints, [], $this->settings->get('field_lemon_url'));
}
/**
* Function to return first element of the array, compatability with PHP 5, note that array_key_first is only available for PHP > 7.3.
*
* @param array $array
* Associative array.
*
* @return string
* The first key data.
*/
public static function arrayKeyfirst($array){
if (!function_exists('array_key_first')) {
foreach($array as $key => $unused) {
return $key;
}
return NULL;
}else{
return array_key_first($array);
}
}
}
Loading…
Cancel
Save