Commit cf136167 authored by Open Source Developer's avatar Open Source Developer

Merge branch 'develop' of https://git.minedu.gov.gr/itminedu/e-epal into stylesandmenus

parents 45a639ee 3881f9c4
......@@ -5,6 +5,8 @@ namespace Drupal\epal;
use Symfony\Component\HttpFoundation\Response;
use Exception;
use Drupal\epal\Crypt;
/**
* Description of Client
*
......@@ -30,23 +32,11 @@ class Client
$this->_settings = array_merge($this->_settings, $settings);
$this->_settings['ws_endpoint_token'] = "{$this->_settings['ws_endpoint']}/oauth2/token";
$this->_settings['ws_endpoint_token_granttype'] = 'password';
$this->_settings['ws_endpoint_studentepalinfo'] = "{$this->_settings['ws_endpoint']}/api/epal/GetStudentEpalInfo";
$this->_settings['ws_endpoint_studentepalcertification'] = "{$this->_settings['ws_endpoint']}/api/epal/GetStudentEpalCertification";
$this->_settings['ws_endpoint_studentepalpromotion'] = "{$this->_settings['ws_endpoint']}/api/epal/GetStudentEpalPromotion";
$this->_settings['ws_endpoint_alldidactiyear'] = "{$this->_settings['ws_endpoint']}/api/general/GetAllDidactiYear";
}
/**
* Επιστρέφει πίνακα με κλειδιά τα property names των πεδίων που επιστρέφει η GetStudentEpalInfo
* και τιμές λεκτικά - ετικέτες τους.
*
* @return array
*/
public function getStudentInfoFields()
{
return $this->studentInfoFields;
}
/**
* Λαμβάνει το authentication token
*
......@@ -183,30 +173,32 @@ class Client
}, '');
$result = $this->get($endpoint, [], $headers); // data as path params...
try {
$crypt = new Crypt();
$val = 'call:' . print_r($endpoint, true) . ':rcv:' . print_r($result, true);
$val_enc = $crypt->encrypt($val);
$this->log(__METHOD__ . $val_enc, 'info');
} catch (\Exception $e) {
$this->log(__METHOD__ . " cannot log encrypted", 'info');
}
if ($result['success'] === false) {
$this->log(__METHOD__ . " Error while calling ws. Diagnostic: {$result['response']}. Response code: {$result['http_status']}", "error");
throw new Exception("Προέκυψε λάθος κατά την άντληση των στοιχείων.");
}
// now return true/false/null ?
return $result['response'];
// if (($response = json_decode($result['response'], true)) !== null) {
// return $response;
// } else {
// throw new Exception("Προέκυψε λάθος κατά την λήψη των στοιχείων. Αδυναμία άντλησης στοιχείων από το response {$result['response']}");
// }
}
public function getStudentEpalPromotion($didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name)
{
$this->log(__METHOD__); // " $didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name");
$this->log(__METHOD__);
return $this->getStudentEpalPromotionOrCertification($this->_settings['ws_endpoint_studentepalpromotion'], $didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name);
}
public function getStudentEpalCertification($didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name)
{
$this->log(__METHOD__); // " $didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name");
$this->log(__METHOD__);
return $this->getStudentEpalPromotionOrCertification($this->_settings['ws_endpoint_studentepalcertification'], $didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name);
}
......@@ -240,6 +232,14 @@ class Client
$result = curl_exec($ch);
// log url as appropriate
try {
$crypt = new Crypt();
$uri = $crypt->encrypt($uri);
} catch (\Exception $e) {
$uri = '-cannot encrypt-';
}
if (curl_errno($ch)) {
$this->log(__METHOD__ . " Error calling {$uri}. Curl error: " . curl_error($ch) . " Curl info: " . var_export(curl_getinfo($ch), true), "error");
throw new Exception("Λάθος κατά την κλήση της υπηρεσίας.");
......@@ -275,6 +275,14 @@ class Client
$result = curl_exec($ch);
// log url as appropriate
try {
$crypt = new Crypt();
$uri = $crypt->encrypt($uri);
} catch (\Exception $e) {
$uri = '-cannot encrypt-';
}
if (curl_errno($ch)) {
$this->log(__METHOD__ . " Error calling {$uri}. Curl error: " . curl_error($ch) . " Curl info: " . var_export(curl_getinfo($ch), true), "error");
throw new Exception("Λάθος κατά την κλήση της υπηρεσίας.");
......
<?php
namespace Drupal\epal;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\epal\Client;
class ClientConsumer
{
protected $entityTypeManager;
protected $logger;
protected $client;
protected $settings;
protected $cached_didactic_years = [
"1" => "2008 - 2009",
"2" => "2011 - 2012",
"3" => "1999 - 2000",
"4" => "2000 - 2001",
"5" => "2009 - 2010",
"6" => "2010 - 2011",
"7" => "2001 - 2002",
"8" => "2002 - 2003",
"9" => "2003 - 2004",
"10" => "2004 - 2005",
"11" => "2005 - 2006",
"12" => "2006 - 2007",
"13" => "2007 - 2008",
"17" => "2012 - 2013",
"18" => "2013 - 2014",
"22" => "2014 - 2015",
"23" => "2015 - 2016",
"24" => "2016 - 2017"
];
protected $cached_level_names = [
"1" => "Α",
"2" => "Β",
"3" => "Γ",
"4" => "Δ"
];
public function __construct($settings, EntityTypeManagerInterface $entityTypeManager, LoggerChannelFactoryInterface $loggerChannel)
{
$this->settings = $settings;
$this->entityTypeManager = $entityTypeManager;
$this->logger = $loggerChannel->get('epal-school');
$this->client = new Client($this->settings, $this->logger);
}
public function getAllDidactiYear()
{
$ts_start = microtime(true);
// try {
// $catalog = $this->client->getAllDidactiYear();
// } catch (\Exception $e) {
// $catalog = [];
// }
$catalog = $this->cached_didactic_years;
$duration = microtime(true) - $ts_start;
$this->logger->info(__METHOD__ . " :: timed [{$duration}]");
return $catalog;
}
public function getStudentEpalPromotion($didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name)
{
$ts_start = microtime(true);
try {
$result = $this->client->getStudentEpalPromotion($didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name);
} catch (\Exception $e) {
$result = -1;
}
$duration = microtime(true) - $ts_start;
$this->logger->info(__METHOD__ . " :: timed [{$duration}]");
return $result;
}
public function getStudentEpalCertification($didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name)
{
$ts_start = microtime(true);
try {
$result = $this->client->getStudentEpalCertification($didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name);
} catch (\Exception $e) {
$result = -1;
}
$duration = microtime(true) - $ts_start;
$this->logger->info(__METHOD__ . " :: timed [{$duration}]");
return $result;
}
/**
* If $ending is provided
* it is assumed as the second part of the academic-year (i.e. 2017 for 2016-2017),
* the function returns the corresponding id to match first;
* If $id is provided, return the corresponding label.
* $id has priority over $ending, if both are supplied.
*
* @return null|string null if no input or no info located
*/
public function getDidacticYear($ending = null, $id = null)
{
$value = null;
if ($id !== null) {
if (array_key_exists($id, $this->cached_didactic_years)) {
$value = $this->cached_didactic_years[$id];
}
} elseif ($ending !== null) {
$remain = array_filter($this->cached_didactic_years, function ($v) use ($ending) {
$pos = strpos($v, "$ending");
return ($pos !== false && $pos > 4);
});
if (count($remain) > 0) {
$values = array_keys($remain);
$value = $values[0];
}
}
return $value;
}
/**
* Get the level name of the denoted class level
*
* @return string|mixed The level name of the provided failsafe value if not found
*/
public function getLevelName($id, $failsafe_value = 'X')
{
$value = $failsafe_value;
if (array_key_exists($id, $this->cached_level_names)) {
$value = $this->cached_level_names["$id"];
}
return $value;
}
private function generateRandomString($length)
{
$characters = ['Α','Β','Γ','Δ','Ε','Ζ','Η','Θ','Ι','Κ','Λ','Μ','Ν','Ξ','Ο','Π','Ρ','Σ','Τ','Υ','Φ','Χ','Ψ','Ω'];
$charactersLength = count($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
}
......@@ -24,10 +24,10 @@ use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;
use Drupal\Core\Language\LanguageManagerInterface;
define("ERROR_DB", -1);
define("NO_CLASS_LIMIT_DOWN", -2);
define("SMALL_CLASS", 1);
define("NON_SMALL_CLASS", 2);
define("ERR_DB", -1);
define("NO_CLASS_LIM_DOWN", -2);
define("SMALL_CLS", 1);
define("NON_SMALL_CLS", 2);
class ReportsCreator extends ControllerBase {
......@@ -603,7 +603,7 @@ class ReportsCreator extends ControllerBase {
for ($j = 0; $j < sizeof($schoolNameColumn); $j++) {
//αν έγινε αίτημα για εμφάνιση ολιγομελών και είναι το τρέχον τμήμα ολιγομελές
if ( ($finalized === "1") || ($finalized === "0" && $smallClass[$j] === SMALL_CLASS
if ( ($finalized === "1") || ($finalized === "0" && $smallClass[$j] === SMALL_CLS
&& $schoolSectionColumn[$j] !== "Β τάξη" && $schoolSectionColumn[$j] !== "Γ τάξη" && $schoolSectionColumn[$j] !== "Δ τάξη" ) )
array_push($list,(object) array(
......@@ -644,16 +644,16 @@ class ReportsCreator extends ControllerBase {
$limitDown = $this->retrieveLimitDown($classId, $regionId);
if ($limitDown === NO_CLASS_LIMIT_DOWN)
return NO_CLASS_LIMIT_DOWN;
else if ($limitDown === ERROR_DB)
return ERROR_DB;
if ($limitDown === NO_CLASS_LIM_DOWN)
return NO_CLASS_LIM_DOWN;
else if ($limitDown === ERR_DB)
return ERR_DB;
$numStudents = (int) $numStud;
if ( ($numStudents < $limitDown) /*&& ($numStudents > 0)*/ )
return SMALL_CLASS;
return SMALL_CLS;
else
return NON_SMALL_CLASS;
return NON_SMALL_CLS;
}
......@@ -671,12 +671,12 @@ class ReportsCreator extends ControllerBase {
return $classLimit->limit_down;
}
else {
return NO_CLASS_LIMIT_DOWN;
return NO_CLASS_LIM_DOWN;
}
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return ERROR_DB;
return ERR_DB;
}
} //end function
......
......@@ -9,19 +9,36 @@ use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\epal\Crypt;
class SubmitedApplications extends ControllerBase
{
protected $entityTypeManager;
protected $logger;
/*
public function __construct(EntityTypeManagerInterface $entityTypeManager)
{
$this->entityTypeManager = $entityTypeManager;
}
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
LoggerChannelFactoryInterface $loggerChannel)
{
$this->entityTypeManager = $entityTypeManager;
$this->logger = $loggerChannel->get('epal');
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('entity_type.manager')
$container->get('entity_type.manager'),
$container->get('logger.factory')
);
}
......@@ -38,17 +55,37 @@ class SubmitedApplications extends ControllerBase
$epalStudents = $this->entityTypeManager->getStorage('epal_student')->loadByProperties(array('epaluser_id' => $userid));
$i = 0;
if ($epalStudents) {
$crypt = new Crypt();
$list = array();
foreach ($epalStudents as $object) {
$indexid = intval($object -> id())-1;
try {
$name_decoded = $crypt->decrypt($object->name->value);
$studentsurname_decoded = $crypt->decrypt($object->studentsurname->value);
}
catch (\Exception $e) {
unset($crypt);
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected error occured during DECODING data in getSubmittedApplications Method ")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
$list[] = array(
'id' => $indexid,
'name' => $object -> name ->value,
'studentsurname' => $object -> studentsurname ->value);
//'name' => $object -> name ->value,
'name' => $name_decoded,
//'studentsurname' => $object -> studentsurname ->value);
'studentsurname' => $studentsurname_decoded );
$i++;
}
unset($crypt);
return $this->respondWithStatus(
$list
, Response::HTTP_OK);
......@@ -105,31 +142,74 @@ class SubmitedApplications extends ControllerBase
if ($course)
$courseName = $this->entityTypeManager->getStorage('eepal_specialty')->load($course->coursefield_id->target_id)->name->value;
}
$crypt = new Crypt();
try {
$name_decoded = $crypt->decrypt($object->name->value);
$studentsurname_decoded = $crypt->decrypt($object->studentsurname->value);
$fatherfirstname_decoded = $crypt->decrypt($object->fatherfirstname->value);
$motherfirstname_decoded = $crypt->decrypt($object->motherfirstname->value);
$regionaddress_decoded = $crypt->decrypt($object->regionaddress->value);
$regiontk_decoded = $crypt->decrypt($object->regiontk->value);
$regionarea_decoded = $crypt->decrypt($object->regionarea->value);
$certificatetype_decoded = $crypt->decrypt($object->certificatetype->value);
$relationtostudent_decoded = $crypt->decrypt($object->relationtostudent->value);
$telnum_decoded = $crypt->decrypt($object->telnum->value);
$guardian_name_decoded = $crypt->decrypt($object->guardian_name->value);
$guardian_surname_decoded = $crypt->decrypt($object->guardian_surname->value);
$guardian_fathername_decoded = $crypt->decrypt($object->guardian_fathername->value);
$guardian_mothername_decoded = $crypt->decrypt($object->guardian_mothername->value);
}
catch (\Exception $e) {
//print_r($e->getMessage());
unset($crypt);
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected error occured during DECODING data in getStudentApplications Method ")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
unset($crypt);
$list[] = array(
'applicationId' => $object->id(),
'name' => $object -> name ->value,
'studentsurname' => $object -> studentsurname ->value,
'fatherfirstname' => $object -> fatherfirstname ->value,
//'name' => $object -> name ->value,
'name' => $name_decoded,
//'studentsurname' => $object -> studentsurname ->value,
'studentsurname' => $studentsurname_decoded,
//'fatherfirstname' => $object -> fatherfirstname ->value,
'fatherfirstname' => $fatherfirstname_decoded,
'fathersurname' =>$object -> fathersurname ->value,
'motherfirstname' => $object -> motherfirstname ->value,
//'motherfirstname' => $object -> motherfirstname ->value,
'motherfirstname' => $motherfirstname_decoded,
'mothersurname' =>$object -> mothersurname ->value,
'guardian_name' =>$object -> guardian_name ->value,
'guardian_surname' =>$object -> guardian_surname ->value,
'guardian_fathername' =>$object -> guardian_fathername ->value,
'guardian_mothername' =>$object -> guardian_mothername ->value,
//'guardian_name' =>$object -> guardian_name ->value,
'guardian_name' =>$guardian_name_decoded,
//'guardian_surname' =>$object -> guardian_surname ->value,
'guardian_surname' => $guardian_surname_decoded,
//'guardian_fathername' =>$object -> guardian_fathername ->value,
'guardian_fathername' =>$guardian_fathername_decoded,
//'guardian_mothername' =>$object -> guardian_mothername ->value,
'guardian_mothername' =>$guardian_mothername_decoded,
'lastschool_schoolname' =>$object -> lastschool_schoolname ->value,
'lastschool_schoolyear' =>$object -> lastschool_schoolyear ->value,
'lastschool_class' =>$object -> lastschool_class ->value,
'currentclass' =>$object -> currentclass ->value,
'currentsector' =>$sectorName,
'currentcourse' =>$courseName,
'regionaddress' =>$object -> regionaddress ->value,
'regiontk' =>$object -> regiontk ->value,
'regionarea' =>$object -> regionarea ->value,
'certificatetype' =>$object -> certificatetype ->value,
//'regionaddress' =>$object -> regionaddress ->value,
'regionaddress' =>$regionaddress_decoded,
//'regiontk' =>$object -> regiontk ->value,
'regiontk' =>$regiontk_decoded,
//'regionarea' =>$object -> regionarea ->value,
'regionarea' =>$regionarea_decoded,
//'certificatetype' =>$object -> certificatetype ->value,
'certificatetype' => $certificatetype_decoded,
'graduation_year' =>$object -> graduation_year ->value,
'telnum' =>$object -> telnum ->value,
'relationtostudent' =>$object -> relationtostudent ->value,
//'telnum' =>$object -> telnum ->value,
'telnum' =>$telnum_decoded,
//'relationtostudent' =>$object -> relationtostudent ->value,
'relationtostudent' => $relationtostudent_decoded,
'birthdate' => substr($object->birthdate->value, 8, 2) . '/' . substr($object->birthdate->value, 6, 2) . '/' . substr($object->birthdate->value, 0, 4),
'created' => date('d/m/Y H:i', $object -> created ->value),
......
......@@ -118,10 +118,4 @@ class WSConsumer extends ControllerBase
return $randomString;
}
// private function respondWithStatus($arr, $s)
// {
// $res = new JsonResponse($arr);
// $res->setStatusCode($s);
// return $res;
// }
}
......@@ -11,7 +11,7 @@ class Crypt
private $fname; // store
public function __construct($fname)
public function __construct($fname = null)
{
if (is_string($fname)) {
$this->fname = $fname;
......
CREATE UNIQUE INDEX uidx_region_regno ON eepal_region_field_data(registry_no);
CREATE UNIQUE INDEX uidx_adminarea_regno ON eepal_admin_area_field_data(registry_no);
CREATE UNIQUE INDEX uidx_taxis_userid ON epal_users(taxis_userid);
CREATE INDEX uidx_authtoken ON epal_users(authtoken(150));
......@@ -9,6 +9,7 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Controller\ControllerBase;
use OAuth;
use DOMDocument;
use OAuthException;
use Drupal\user\Entity\User;
use Drupal\Core\Database\Connection;
......@@ -128,21 +129,25 @@ class CBController extends ControllerBase
public function authenticatePhase2($request, $authToken, $authVerifier)
{
try {
$taxis_userid = null;
$trx = $this->connection->startTransaction();
try {
$oauth = new OAuth($this->consumer_key, $this->consumer_secret, OAUTH_SIG_METHOD_PLAINTEXT, OAUTH_AUTH_TYPE_URI);
$oauth->enableDebug();
// $oauth->enableDebug();
$oauth->setToken($authToken, $this->requestTokenSecret);
$accessToken = $oauth->getAccessToken($this->access_token_url, '', $authVerifier);
$oauth->setToken($accessToken['oauth_token'], $accessToken['oauth_token_secret']);
$oauth->fetch($this->api_url);
$this->logger->warning($oauth->getLastResponse());
$taxis_userid = $this->xmlParse($oauth->getLastResponse(), 'messageText');
$dom = $this->loadXML($oauth->getLastResponse());
$taxis_userData = $this->getXMLElements($dom);
if (!$taxis_userData || sizeof($taxis_userData) === 0) {
return false;
}
$currentTime = time();
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('taxis_userid' => $taxis_userid));
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('taxis_userid' => $taxis_userData['tin']));
$epalUser = reset($epalUsers);
$epalToken = md5(uniqid(mt_rand(), true));
......@@ -163,7 +168,6 @@ class CBController extends ControllerBase
$epalUser->save();
}
}
if ($epalUser === null || !$epalUser) {
//Create a User
......@@ -190,17 +194,15 @@ class CBController extends ControllerBase
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('mail' => $unique_id));
$user = reset($users);
if ($user) {
$this->logger->warning('userid 190='.$user->id().'*** name='.$user->name->value);
$epalUser = $this->entityTypeManager()->getStorage('epal_users')->create(array(
'langcode' => 'el',
'user_id' => $user->id(),
'drupaluser_id' => $user->id(),
'taxis_userid' => $taxis_userid,
'taxis_taxid' => $unique_id,
'name' => $unique_id,
'surname' => $unique_id,
'fathername' => $unique_id,
'taxis_userid' => $taxis_userData['tin'],
'taxis_taxid' => $taxis_userData['tin'],
'name' => $taxis_userData['firstName'],
'surname' => $taxis_userData['surname'],
'fathername' => $taxis_userData['fathersName'],
'mothername' => $unique_id,
'accesstoken' => $accessToken['oauth_token'],
'accesstoken_secret' => $accessToken['oauth_token_secret'],
......@@ -221,7 +223,6 @@ class CBController extends ControllerBase
}
$this->oauthostSession->set('authtoken', $epalToken);
$this->oauthostSession->save();
// $this->oauthostSession->delete();
return $epalToken;
} catch (OAuthException $e) {
......@@ -237,7 +238,66 @@ class CBController extends ControllerBase
return false;
}
public function xmlParse($xmlText, $token){
return '12345';
public function loadXML($text_response){
$dom = new DOMDocument();
// Fix possible whitespace problems
$dom->preserveWhiteSpace = false;
if (!($dom->loadXML($text_response))) {
$this->logger->warning('dom->loadXML() failed');
return false;
}
if (!($tree_response = $dom->documentElement)) {
$this->logger->warning('documentElement() failed');
return false;
}
return $dom;
}