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

Capacity cahsnges

parents 26cd365b efb927c5
......@@ -173,7 +173,7 @@ class CASLogin extends ControllerBase
};
$umdobject = $filterAttribute("umdobject");
$physicaldeliveryofficename = $filterAttribute("physicaldeliveryofficename");
// $physicaldeliveryofficename = $filterAttribute("physicaldeliveryofficename");
/****** the following is for production : Χρήση μόνο από ΕΠΙΣΗΜΟΥΣ ΛΟΓΑΡΙΑΣΜΟΥΣ ***************************/
......@@ -186,12 +186,27 @@ class CASLogin extends ControllerBase
}
*/
phpCAS::trace($umdobject);
phpCAS::trace($physicaldeliveryofficename);
$gsnunitcodedn = $filterAttribute('edupersonorgunitdn:gsnunitcode:extended');
$gsnunitcode = substr($gsnunitcodedn, strpos($gsnunitcodedn, ";") + 1);
phpCAS::trace($gsnunitcode);
/* check if myschool account */
if (!$umdobject || $umdobject !== "ISaccount") {
return $this->redirectForbidden($configRowName, '5002');
}
if (!$gsnunitcode || $gsnunitcode !== $CASUser) {
return $this->redirectForbidden($configRowName, '5003');
}
/* end of checking myschool account */
$userAssigned = $this->assignRoleToUser($gsnunitcode);
if (sizeof($userAssigned) === 0) {
return $this->redirectForbidden($configRowName, '5004');
}
......
ws_endpoint: 'https://the.url'
ws_username: 'username'
ws_password: 'password'
verbose: true
NO_SAFE_CURL: false
......@@ -182,6 +182,14 @@ epal.ministry.massive_mail_unallocated:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToUnallocatedStudents'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.massive_mail_unallocated_smallclass:
path: '/ministry/send-unallocated-sc-massive-mail'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\InformUnlocatedStudents::sendMailToUnallocatedStudentsSC'
requirements:
_user_is_logged_in: 'TRUE'
epal.ministry.massive_mail_located:
path: '/ministry/send-located-massive-mail'
options:
......@@ -278,3 +286,27 @@ epal.applicant.creationpdf:
_controller: '\Drupal\epal\Controller\PdfCreator::createApplicantPDF'
requirements:
_user_is_logged_in: 'TRUE'
epal.svc.getdidacticyears:
path: '/epal/get-didactic-years'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\WSConsumer::getAllDidactiYear'
requirements:
_user_is_logged_in: 'TRUE'
epal.svc.getstudentpromotion:
path: '/epal/get-student-promotion/{didactic_year_id}/{lastname}/{firstname}/{father_firstname}/{mother_firstname}/{birthdate}/{registry_no}/{level_name}'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\WSConsumer::getStudentEpalPromotion'
requirements:
_user_is_logged_in: 'TRUE'
epal.svc.getstudentcertification:
path: '/epal/get-student-certification/{didactic_year_id}/{lastname}/{firstname}/{father_firstname}/{mother_firstname}/{birthdate}/{registry_no}/{level_name}'
options:
_auth: [ 'basic_auth' ]
defaults:
_controller: '\Drupal\epal\Controller\WSConsumer::getStudentEpalCertification'
requirements:
_user_is_logged_in: 'TRUE'
<?php
namespace Drupal\epal;
use Symfony\Component\HttpFoundation\Response;
use Exception;
/**
* Description of Client
*
*/
class Client
{
private $_settings = [
'verbose' => false,
'ws_endpoint' => '',
'ws_username' => '',
'ws_password' => '',
'NO_SAFE_CURL' => false
];
private $logger; // if this is set and settings sets verbose mode, it will be used for logging
private $_token = null; // cache JWT
private $_tokenExpirationTS = null; // try to calculate token expiration time
public function __construct($settings = [], $logger = null)
{
$this->logger = $logger;
$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
*
* @return string To authentication header (Bearer <token>) έτοιμο για να μπει στο authorization
* @throws \Exception
*/
public function getTokenBearer()
{
if ($this->_token !== null && $this->_tokenExpirationTS !== null && intval($this->_tokenExpirationTS) >= time()) {
$this->log(__METHOD__ . " reusing token");
return $this->_token;
}
$this->_token = null;
$this->_tokenExpirationTS = null;
$this->log(__METHOD__ . " new token");
$headers = [
'Accept: application/json',
'Accept-Language: en-gb',
'Audience: Any',
// 'Content-Type: application/x-www-form-urlencoded' // should be set by post
'User-Agent: OSTEAM Client/v1.1 osteam'
];
$payload = http_build_query([
'username' => $this->_settings['ws_username'],
'password' => $this->_settings['ws_password'],
'grant_type' => $this->_settings['ws_endpoint_token_granttype']
]);
$result = $this->post($this->_settings['ws_endpoint_token'], $payload, $headers);
if ($result['success'] === false) {
$this->log(__METHOD__ . " Error while calling ws. Diagnostic: {$result['response']}. Response code: {$result['http_status']}", "error");
throw new Exception("Προέκυψε λάθος κατά την άντληση των στοιχείων.");
}
if (($response = json_decode($result['response'], true)) !== null) {
$this->_tokenExpirationTS = time() + intval($response['expires_in']) - 15; // skip 15 seconds... just in case
$this->_token = ucfirst($response['token_type']) . " {$response['access_token']}";
return $this->_token;
} else {
$this->log(__METHOD__ . " Error while getting token from response {$result['response']}.", "error");
throw new Exception("Προέκυψε λάθος κατά την λήψη του token. Αδυναμία άντλησης του token από το response.");
}
}
/**
* Επιστρέφει λίστα των διδακτικών ετών με κλειδί το id και τιμή το λεκτικό.
*
* @return array Associative array με κλειδί το id και τιμή το λεκτικό. Π.χ.
* Array (
* [1] => 2008 - 2009
* [2] => 2011 - 2012
* [18] => 2013 - 2014
* [24] => 2016 - 2017
* )
*
* @throws \Exception Σε περίπτωση οποιουδήποτε λάθους
*/
public function getAllDidactiYear()
{
$this->log(__METHOD__);
$headers = [
'Accept: application/json',
'Accept-Language: en-gb',
'Audience: Any',
'Authorization: ' . $this->getTokenBearer(),
'User-Agent: OSTEAM Client/v1.1 osteam'
];
$result = $this->get($this->_settings['ws_endpoint_alldidactiyear'], [], $headers); // data as path params...
if ($result['success'] === false) {
$this->log(__METHOD__ . " Error while calling ws. Diagnostic: {$result['response']}. Response code: {$result['http_status']}", "error");
throw new Exception("Προέκυψε λάθος κατά την άντληση των στοιχείων.");
}
if (($response = json_decode($result['response'], true)) !== null) {
return $response;
} else {
$this->log(__METHOD__ . " Error while getting data from response {$result['response']}.", "error");
throw new Exception("Προέκυψε λάθος κατά την λήψη των στοιχείων. Αδυναμία άντλησης στοιχείων από την απάντηση.");
}
}
/**
*
* @param string $endpoint_base_url web service url
* @param int $didactic_year_id {@see getAllDidactiYear()}
* @param string $lastname μόνο χαρακτήρες, κενά και μεσαίες παύλες
* @param string $firstname μόνο χαρακτήρες, κενά και μεσαίες παύλες
* @param string $father_firstname μόνο χαρακτήρες, κενά και μεσαίες παύλες
* @param string $mother_firstname μόνο χαρακτήρες, κενά και μεσαίες παύλες
* @param string $birthdate ημερομηνίες στη μορφή 4-1-1997 (d-M-yyyy)
*
* @return boolean|null
* @throws \Exception Σε περίπτωση οποιουδήποτε λάθους
*/
public function getStudentEpalPromotionOrCertification($endpoint_base_url, $didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name)
{
$parts = explode('-', $birthdate, 3);
if (($parts === false) || count($parts) != 3 || checkdate(intval($parts[1]), intval($parts[0]), intval($parts[2])) === false) {
$this->log(__METHOD__ . " Mallformed birthdate", "error");
throw new Exception('Η ημερομηνία γέννησης πρέπει να είναι της μορφής Η/Μ/Ε', Response::HTTP_BAD_REQUEST);
}
if (mb_strlen($lastname) == 0 || mb_strlen($firstname) == 0 || mb_strlen($father_firstname) == 0 || mb_strlen($mother_firstname) == 0) {
$this->log(__METHOD__ . " Missing parameters", "error");
throw new Exception('Όλες οι παράμετροι είναι υποχρεωτικοί', Response::HTTP_BAD_REQUEST);
}
$data = [
'DidacticYearId' => intval($didactic_year_id),
'LastName' => $lastname,
'FirstName' => $firstname,
'FatherFirstName' => $father_firstname,
'MotherFirstname' => $mother_firstname,
'BirthDate' => $birthdate,
'RegistryNo' => $registry_no,
'LevelName' => $level_name
];
$headers = [
'Accept: application/json',
// 'Accept-Language: en-gb',
'Accept-Language: {"Accept-Language":"en-gb"}', // as per spec provided...
'Audience: Any',
'Authorization: ' . $this->getTokenBearer(),
'User-Agent: OSTEAM Client/v1.1 osteam'
];
$endpoint = $endpoint_base_url . array_reduce($data, function ($c, $v) {
$c .= "/" . urlencode($v);
return $c;
}, '');
$result = $this->get($endpoint, [], $headers); // data as path params...
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");
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");
return $this->getStudentEpalPromotionOrCertification($this->_settings['ws_endpoint_studentepalcertification'], $didactic_year_id, $lastname, $firstname, $father_firstname, $mother_firstname, $birthdate, $registry_no, $level_name);
}
protected function setCommonCurlOptions($ch, $uri, $headers)
{
curl_setopt($ch, CURLOPT_URL, $uri);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_USERAGENT, "OSTEAM Client/v1.1 osteam");
if (isset($this->_settings['NO_SAFE_CURL']) && $this->_settings['NO_SAFE_CURL'] === true) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
if ($this->_settings['verbose']) {
curl_setopt($ch, CURLOPT_VERBOSE, true);
}
}
public function post($uri, $payload, $headers = [])
{
$ch = curl_init();
$this->setCommonCurlOptions($ch, $uri, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
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("Λάθος κατά την κλήση της υπηρεσίας.");
}
if (intval(($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) / 100) != 2) {
return [
'success' => false,
'http_status' => $http_code,
'response' => $result
];
}
curl_close($ch);
return [
'success' => true,
'http_status' => $http_code,
'response' => $result
];
}
public function get($uri, $params = [], $headers = [])
{
$ch = curl_init();
if (is_array($params) && count($params) > 0) {
$qs = '?' . http_build_query($params);
} else {
$qs = '';
}
$this->setCommonCurlOptions($ch, "{$uri}{$qs}", $headers);
// curl_setopt($ch, CURLOPT_HTTPGET, true); // default
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
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("Λάθος κατά την κλήση της υπηρεσίας.");
}
if (intval(($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) / 100) != 2) {
return [
'success' => false,
'http_status' => $http_code,
'response' => $result
];
}
curl_close($ch);
return [
'success' => true,
'http_status' => $http_code,
'response' => $result
];
}
protected function log($msg, $level = 'info')
{
if ($this->logger !== null && $this->_settings['verbose']) {
switch ($level) {
case 'info':
case 'warning':
case 'error':
$this->logger->$level($msg);
break;
default:
$this->logger->error($msg);
break;
}
}
return;
}
}
......@@ -8,7 +8,6 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Database\Connection;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
class DirectorView extends ControllerBase
......@@ -32,597 +31,527 @@ class DirectorView extends ControllerBase
);
}
public function getSectorsPerSchool(Request $request)
public function getSectorsPerSchool(Request $request)
{
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if ($user) {
$epalId = $user -> init -> value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->
$user = reset($users);
if ($user) {
$epalId = $user->init->value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->
loadByProperties(array('id' => $epalId));
$school = reset($schools);
if (!$school) {
$this->logger->warning("no access to this school=" . $user->id());
$response = new Response();
$response->setContent('No access to this school');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$userRoles = $user->getRoles();
$userRole = '';
foreach ($userRoles as $tmpRole) {
if ($tmpRole === 'epal') {
$userRole = $tmpRole;
}
$school = reset($schools);
if (!$school) {
$this->logger->warning('no access to this school='.$user->id());
$response = new Response();
$response->setContent('No access to this school');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$userRoles = $user->getRoles();
$userRole = '';
foreach ($userRoles as $tmpRole) {
if ($tmpRole === 'epal') {
$userRole = $tmpRole;
}
if ($userRole === '') {
return $this->respondWithStatus([
}
if ($userRole === '') {
return $this->respondWithStatus([
'error_code' => 4003,
], Response::HTTP_FORBIDDEN);
}
else if ($userRole === 'epal')
{
$sectorPerSchool = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id'=> $epalId));
if ($sectorPerSchool) {
$list = array();
foreach ($sectorPerSchool as $object) {
$list[] = array(
'sector_id' => $object -> sector_id ->entity->get('name')->value ,
'id' => $object -> sector_id -> entity -> id()
} elseif ($userRole === 'epal') {
$sectorPerSchool = $this->entityTypeManager->getStorage('eepal_sectors_in_epal')->loadByProperties(array('epal_id' => $epalId));
if ($sectorPerSchool) {
$list = array();
foreach ($sectorPerSchool as $object) {
$list[] = array(
'sector_id' => $object->sector_id->entity->get('name')->value,
'id' => $object->sector_id->entity->id(),
);
}
}
return $this->respondWithStatus(
$list
, Response::HTTP_OK);
return $this->respondWithStatus(
$list, Response::HTTP_OK);
}
}
else {
return $this->respondWithStatus([
'message' => t("School not found!!!"),
} else {
return $this->respondWithStatus([
'message' => t('School not found!!!'),
], Response::HTTP_OK);
}
}
} else {
return $this->respondWithStatus([
'message' => t("User not found!"),
'message' => t('User not found!'),
], Response::HTTP_FORBIDDEN);
}
}
public function getSpecialPerSchool(Request $request , $sectorId)
public function getSpecialPerSchool(Request $request, $sectorId)
{
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if ($user) {
$epalId = $user -> init -> value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array( 'id' => $epalId));
$school = reset($schools);
if (!$school) {
$this->logger->warning("no access to this school=" . $user->id());
$response = new Response();
$response->setContent('No access to this school');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$userRoles = $user->getRoles();
$userRole = '';
foreach ($userRoles as $tmpRole) {
if ($tmpRole === 'epal') {
$userRole = $tmpRole;
}
$user = reset($users);
if ($user) {
$epalId = $user->init->value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $epalId));
$school = reset($schools);
if (!$school) {
$this->logger->warning('no access to this school='.$user->id());
$response = new Response();
$response->setContent('No access to this school');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$userRoles = $user->getRoles();
$userRole = '';
foreach ($userRoles as $tmpRole) {
if ($tmpRole === 'epal') {
$userRole = $tmpRole;
}
if ($userRole === '') {
return $this->respondWithStatus([
}
if ($userRole === '') {
return $this->respondWithStatus([
'error_code' => 4003,
], Response::HTTP_FORBIDDEN);
}
else if ($userRole === 'epal')
{
$specialityPerSchool = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id'=> $epalId));
} elseif ($userRole === 'epal') {
$specialityPerSchool = $this->entityTypeManager->getStorage('eepal_specialties_in_epal')->loadByProperties(array('epal_id' => $epalId));
$i = 0;
if ($specialityPerSchool) {
$list = array();
$SectorIdNew = intval($sectorId);
foreach ($specialityPerSchool as $object) {
$idSpecial = $object -> specialty_id -> entity -> id() ;
$specialityPerSector = $this->entityTypeManager->getStorage('eepal_specialty')->loadByProperties(array('id'=> $idSpecial,'sector_id' => $SectorIdNew ));
$specialPerSec = reset($specialityPerSector);
if ($specialPerSec)
{ $list[] = array(
'specialty_id' => $object -> specialty_id ->entity->get('name')->value ,
'id' => $object -> specialty_id -> entity -> id() );
$i++;
}
if ($specialityPerSchool) {
$list = array();
$SectorIdNew = intval($sectorId);
foreach ($specialityPerSchool as $object) {
$idSpecial = $object->specialty_id->entity->id();
}
$specialityPerSector = $this->entityTypeManager->getStorage('eepal_specialty')->loadByProperties(array('id' => $idSpecial, 'sector_id' => $SectorIdNew));
$specialPerSec = reset($specialityPerSector);
if ($specialPerSec) {
$list[] = array(
'specialty_id' => $object->specialty_id->entity->get('name')->value,
'id' => $object->specialty_id->entity->id(), );
++$i;
}
}
return $this->respondWithStatus(
$list
, Response::HTTP_OK);
return $this->respondWithStatus(
$list, Response::HTTP_OK);
}
}
else {
return $this->respondWithStatus([
'message' => t("School not found!!!"),
} else {
return $this->respondWithStatus([
'message' => t('School not found!!!'),
], Response::HTTP_OK);
}
}
} else {
return $this->respondWithStatus([
'message' => t("User not found!"),
'message' => t('User not found!'),
], Response::HTTP_FORBIDDEN);
}
}
public function getStudentPerSchool(Request $request , $selectId, $classId, $limitdown, $limitup)
public function getStudentPerSchool(Request $request, $selectId, $classId, $limitdown, $limitup)
{
try {
$authToken = $request->headers->get('PHP_AUTH_USER');
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('name' => $authToken));
$user = reset($users);
if ($user) {
$epalId = $user -> init -> value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array( 'id' => $epalId));
$epalId = $user->init->value;
$schools = $this->entityTypeManager->getStorage('eepal_school')->loadByProperties(array('id' => $epalId));
$school = reset($schools);
if (!$school) {
$this->logger->warning("no access to this school=" . $user->id());
$this->logger->warning('no access to this school='.$user->id());
$response = new Response();
$response->setContent('No access to this school');
$response->setStatusCode(Response::HTTP_FORBIDDEN);
$response->headers->set('Content-Type', 'application/json');
return $response;
}
$userRoles = $user->getRoles();
$userRole = '';
foreach ($userRoles as $tmpRole) {
if ($tmpRole === 'epal') {
$userRole = $tmpRole;
}
$userRoles = $user->getRoles();
$userRole = ''