179 lines
4.8 KiB
PHP
179 lines
4.8 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: lucas
|
|
* Date: 27/02/18
|
|
* Time: 16:19
|
|
*/
|
|
|
|
namespace api\module;
|
|
|
|
|
|
use database\core\Repo;
|
|
use database\repo\professor;
|
|
use error\core\Error;
|
|
use error\core\Err;
|
|
|
|
class casController{
|
|
|
|
|
|
/* (1) Authentication callback
|
|
*
|
|
* @return professors<array> The professor(s) data
|
|
*
|
|
---------------------------------------------------------*/
|
|
public static function get($args){
|
|
|
|
// login: https://sso.univ-pau.fr/cas/login?service=http://ptut.com:8080/api/v/1.0/cas
|
|
|
|
|
|
// Communicate over popups
|
|
// ----------------------------
|
|
// window.pop = { closed: false };
|
|
//
|
|
// window.cas_callback = function(cas_login){
|
|
//
|
|
// setTimeout( function(){
|
|
//
|
|
// if( window.pop.closed )
|
|
// console.log('CAS login (null means error): '+cas_login);
|
|
//
|
|
// }, 1);
|
|
//
|
|
// };
|
|
|
|
// Launch PopUp
|
|
// window.pop = window.open('https://sso.univ-pau.fr/cas/login?service=http://ptut.com:8080/api/v/1.0/cas', '_blank', 'location=no,height=1024,width=1024,scrollbars=yes,status=no');
|
|
|
|
/* (0) Initialize
|
|
---------------------------------------------------------*/
|
|
/* (1) Global DOWNLOAD data */
|
|
$headers = ['Content-Type' => 'text/html; charset=UTF-8' ];
|
|
$body_start = "Veuillez patienter...<br>Vous allez être redirigés<script type='text/javascript'>( typeof window.opener.cas_callback === 'function' ) && window.opener.cas_callback(";
|
|
$body_end = ");window.close();</script>";
|
|
|
|
/* (2) Reset SESSION */
|
|
$_SESSION['CAS'] = [];
|
|
|
|
|
|
|
|
/* (1) Check if already connected
|
|
---------------------------------------------------------*/
|
|
/* (1) If already -> return @cas_login */
|
|
if( in_array('cas_user', $_SESSION['AUTH']) ){
|
|
|
|
return [
|
|
'headers' => $headers,
|
|
'body' => $body_start."'".$_SESSION['CAS']['login']."'".$body_end
|
|
];
|
|
|
|
}
|
|
|
|
/* (2) Fail if no ticket */
|
|
if( !isset($_GET['ticket']) || !is_string($_GET['ticket']) || strlen($_GET['ticket']) < 1 )
|
|
return [ 'headers' => $headers, 'body' => $body_start.'null'.$body_end ];
|
|
|
|
|
|
|
|
/* (2) Check ticket (validate)
|
|
---------------------------------------------------------*/
|
|
/* (1) Build useful variables */
|
|
$service = ($_SERVER['SERVER_NAME'] == 'ptut.com' ) ? 'http' : 'https';
|
|
$service .= '://'.$_SERVER['HTTP_HOST'].'/api/v/1.0/cas';
|
|
$ticket = urlencode($_GET['ticket']);
|
|
$validate_url = "https://sso.univ-pau.fr/cas/serviceValidate?ticket=$ticket&service=$service";
|
|
|
|
/* (2) Configure & Prepare CURL */
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $validate_url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
|
|
/* (3) Execute CURL & Close it */
|
|
$output = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
/* (4) Fail if not validated */
|
|
if( strpos($output, 'user') === false )
|
|
return [ 'headers' => $headers, 'body' => $body_start.'null'.$body_end ];
|
|
|
|
/* (5) Extract cas_login */
|
|
$cas_login = trim(strip_tags($output));
|
|
|
|
/* (6) Check empty */
|
|
if( strlen($cas_login) < 1 )
|
|
return [ 'headers' => $headers, 'body' => $body_start.'null'.$body_end ];
|
|
|
|
|
|
|
|
/* (3) Meta database: check if @cas_login referenced
|
|
---------------------------------------------------------*/
|
|
/** @var professor $prof_repo */
|
|
$prof_repo = Repo::getRepo('professor');
|
|
|
|
/* (1) Get the list of linked departments for this @cas_login */
|
|
$departments = $prof_repo->getLinkedDepartments($cas_login);
|
|
|
|
/* (2) Failure: if no department for @cas_login */
|
|
if( count($departments) === 0 )
|
|
return [ 'headers' => $headers, 'body' => $body_start.'null'.$body_end ];
|
|
|
|
/* (3) Set departments data */
|
|
$_SESSION['AvailableDepartment'] = $departments;
|
|
|
|
/* (4) Choose first department by default */
|
|
$_SESSION['CurrentDatabase'] = $departments[0]['dbName'];
|
|
$_SESSION['CurrentDepartmentId'] = $departments[0]['idDep'];
|
|
|
|
/* (5) Use this department's database */
|
|
Repo::switchDatabase($_SESSION['CurrentDatabase']);
|
|
|
|
|
|
|
|
/* (4) Fetch @cas_login professor data
|
|
---------------------------------------------------------*/
|
|
/* (1) Try to fetch professor */
|
|
$by_login = $prof_repo->getByLogin($cas_login);
|
|
|
|
/* (2) If not found -> reset SESSION */
|
|
if( !is_array($by_login) || !isset($by_login['idProfesseur']) || !isset($by_login['admin']) )
|
|
return [ 'headers' => $headers, 'body' => $body_start.'null'.$body_end ];
|
|
|
|
|
|
|
|
/* (5) Store data in session
|
|
---------------------------------------------------------*/
|
|
/* (1) Security */
|
|
\session_regenerate_id();
|
|
|
|
/* (2) Store CAS user data in SESSION */
|
|
$_SESSION['CAS'] = [
|
|
'login' => $cas_login,
|
|
'ticket' => $ticket,
|
|
'id' => (int) $by_login['idProfesseur'],
|
|
'admin' => (bool) $by_login['admin']
|
|
];
|
|
|
|
/* (3) Success CAS login */
|
|
return [
|
|
'headers' => $headers,
|
|
'body' => $body_start."'".$_SESSION['CAS']['login']."'".$body_end
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function delete(){
|
|
|
|
\session_destroy();
|
|
|
|
/* (3) Return if logged out */
|
|
return ['logged_out' => true];
|
|
|
|
|
|
}
|
|
|
|
} |