mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-25 00:24:35 +00:00
Merge branch 'master' into feature-13011/installation_on_multiple_servers
This commit is contained in:
@@ -12,6 +12,7 @@ class AnrechnungLib
|
||||
|
||||
const ANRECHNUNG_NOTIZTITEL_NOTIZ_BY_LEKTOR = 'AnrechnungNotizLektor';
|
||||
const ANRECHNUNG_NOTIZTITEL_NOTIZ_BY_STGL = 'AnrechnungNotizSTGL';
|
||||
const ANRECHNUNG_NOTIZTITEL_EMPFEHLUNGSNOTIZ_BY_STGL = 'AnrechnungEmpfehlungsnotizSTGL';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
@@ -23,6 +24,10 @@ class AnrechnungLib
|
||||
$this->ci->load->model('organisation/Studiengang_model', 'StudiengangModel');
|
||||
$this->ci->load->model('crm/Student_model', 'StudentModel');
|
||||
$this->ci->load->model('content/DmsVersion_model', 'DmsVersionModel');
|
||||
$this->ci->load->model('crm/Prestudent_model', 'PrestudentModel');
|
||||
$this->ci->load->model('person/Notiz_model', 'NotizModel');
|
||||
|
||||
$this->ci->load->library('DmsLib');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,16 +37,19 @@ class AnrechnungLib
|
||||
* @param $lv_id
|
||||
* @return StdClass
|
||||
*/
|
||||
public function getAntragData($uid, $studiensemester_kurzbz, $lv_id)
|
||||
public function getAntragData($prestudent_id, $studiensemester_kurzbz, $lv_id)
|
||||
{
|
||||
$antrag_data = new StdClass();
|
||||
|
||||
|
||||
// Get students UID.
|
||||
$uid = $this->ci->StudentModel->getUID($prestudent_id);
|
||||
|
||||
// Get lehrveranstaltung data. Break, if course is not assigned to student.
|
||||
if(!$lv = getData($this->ci->LehrveranstaltungModel->getLvByStudent($uid, $studiensemester_kurzbz, $lv_id))[0])
|
||||
{
|
||||
show_error('You are not assigned to this course yet.');
|
||||
}
|
||||
|
||||
|
||||
// Get the students personal data
|
||||
if (!$person = getData($this->ci->PersonModel->getByUid($uid))[0])
|
||||
{
|
||||
@@ -61,12 +69,18 @@ class AnrechnungLib
|
||||
}
|
||||
|
||||
// Get lectors of lehrveranstaltung
|
||||
$antrag_data->lektoren = array();
|
||||
if (!$lv_lektoren = getData($this->ci->LehrveranstaltungModel->getLecturersByLv($studiensemester_kurzbz, $lv_id)))
|
||||
$result = $this->ci->LehrveranstaltungModel->getLecturersByLv($studiensemester_kurzbz, $lv_id);
|
||||
if (isError($result))
|
||||
{
|
||||
show_error('Failed loading course lectors.');
|
||||
}
|
||||
|
||||
|
||||
$lv_lektoren_arr = hasData($result) ? getData($result) : array();
|
||||
|
||||
// Get latest ZGV
|
||||
$result = $this->ci->PrestudentModel->getLatestZGVBezeichnung($prestudent_id);
|
||||
$latest_zgv_bezeichnung = hasData($result) ? getData($result)[0]->bezeichnung : '';
|
||||
|
||||
// Set the given studiensemester
|
||||
$antrag_data->lv_id = $lv_id;
|
||||
$antrag_data->lv_bezeichnung = $lv->bezeichnung;
|
||||
@@ -76,7 +90,8 @@ class AnrechnungLib
|
||||
$antrag_data->nachname = $person->nachname;
|
||||
$antrag_data->matrikelnr = $student->matrikelnr;
|
||||
$antrag_data->stg_bezeichnung = $studiengang->bezeichnung;
|
||||
$antrag_data->lektoren = $lv_lektoren;
|
||||
$antrag_data->lektoren = $lv_lektoren_arr;
|
||||
$antrag_data->zgv = $latest_zgv_bezeichnung;
|
||||
|
||||
return $antrag_data;
|
||||
}
|
||||
@@ -107,8 +122,12 @@ class AnrechnungLib
|
||||
{
|
||||
$anrechnung_data = $this->_setAnrechnungDataObject($anrechnung);
|
||||
}
|
||||
else
|
||||
{
|
||||
show_error('No Anrechnung with this anrechnung_id.');
|
||||
}
|
||||
|
||||
return success($anrechnung_data);
|
||||
return $anrechnung_data;
|
||||
|
||||
}
|
||||
|
||||
@@ -153,7 +172,7 @@ class AnrechnungLib
|
||||
$anrechnung_data = $this->_setAnrechnungDataObject($anrechnung);
|
||||
}
|
||||
|
||||
return success($anrechnung_data);
|
||||
return $anrechnung_data;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -171,8 +190,8 @@ class AnrechnungLib
|
||||
$this->ci->AnrechnungModel->addSelect('tbl_benutzer.uid, tbl_prestudent.prestudent_id, tbl_person.person_id, tbl_anrechnung.studiensemester_kurzbz, vorname, nachname, geschlecht, tbl_lehrveranstaltung.bezeichnung AS "lv_bezeichnung"');
|
||||
$this->ci->AnrechnungModel->addJoin('public.tbl_prestudent', 'prestudent_id');
|
||||
$this->ci->AnrechnungModel->addJoin('public.tbl_student', 'prestudent_id');
|
||||
$this->ci->AnrechnungModel->addJoin('public.tbl_benutzer', 'uid=student_uid');
|
||||
$this->ci->AnrechnungModel->addJoin('public.tbl_person', 'tbl_benutzer.person_id=tbl_person.person_id');
|
||||
$this->ci->AnrechnungModel->addJoin('public.tbl_benutzer', 'uid = student_uid');
|
||||
$this->ci->AnrechnungModel->addJoin('public.tbl_person', 'tbl_benutzer.person_id = tbl_person.person_id');
|
||||
$this->ci->AnrechnungModel->addJoin('lehre.tbl_lehrveranstaltung', 'lehrveranstaltung_id');
|
||||
|
||||
$result = $this->ci->AnrechnungModel->load($anrechnung_id);
|
||||
@@ -207,28 +226,53 @@ class AnrechnungLib
|
||||
$empfehlung_data->empfehlung = null;
|
||||
$empfehlung_data->empfehlung_von = '-';
|
||||
$empfehlung_data->empfehlung_am = '-';
|
||||
$empfehlung_data->empfehlung_angefordert_am = '-';
|
||||
$empfehlung_data->notiz = ''; // Begruendung, if not recommended
|
||||
|
||||
$empfehlung_data->empfehlungsanfrageAm = '-';
|
||||
$empfehlung_data->empfehlungsanfrageAn = '-';
|
||||
$empfehlung_data->begruendung = '-'; // Begruendung, if not recommended
|
||||
$empfehlung_data->notiz_id = ''; // Empfehlungsnotiz from STGL
|
||||
$empfehlung_data->notiz = ''; // Empfehlungsnotiz from STGL
|
||||
|
||||
|
||||
if(!$anrechnung = getData($this->ci->AnrechnungModel->load($anrechnung_id))[0])
|
||||
{
|
||||
show_error('Failed loading Anrechnung');
|
||||
}
|
||||
|
||||
// Get Empfehlungsnotiz
|
||||
$result = $this->ci->NotizModel->getNotizByAnrechnung(
|
||||
$anrechnung_id,
|
||||
self::ANRECHNUNG_NOTIZTITEL_EMPFEHLUNGSNOTIZ_BY_STGL
|
||||
);
|
||||
|
||||
if ($notiz = getData($result)[0])
|
||||
{
|
||||
$empfehlung_data->notiz_id = $notiz->notiz_id;
|
||||
$empfehlung_data->notiz = $notiz->text;
|
||||
}
|
||||
|
||||
// Get date, where recommendation was last requested
|
||||
$result = $this->ci->AnrechnungModel->getLastAnrechnungstatus(
|
||||
$anrechnung_id,
|
||||
self::ANRECHNUNGSTATUS_PROGRESSED_BY_LEKTOR // when STLG asks for recommendation, status is set to in progress lektor
|
||||
);
|
||||
if ($result = getData($result)[0])
|
||||
|
||||
// If request for recommendation exists
|
||||
if (hasData($result))
|
||||
{
|
||||
$empfehlung_data->empfehlung_angefordert_am = (new DateTime($result->insertamum))->format('d.m.Y');
|
||||
$empfehlung_data->empfehlungsanfrageAm = (new DateTime($result->retval[0]->insertamum))->format('d.m.Y');
|
||||
|
||||
// Get lectors who received request for recommendation
|
||||
$lector_arr = self::getLectors($anrechnung_id);
|
||||
|
||||
if (!isEmptyArray($lector_arr))
|
||||
{
|
||||
$empfehlung_data->empfehlungsanfrageAn = implode(', ', array_column($lector_arr, 'fullname'));
|
||||
}
|
||||
}
|
||||
|
||||
if (is_null($anrechnung->empfehlung_anrechnung))
|
||||
{
|
||||
return success($empfehlung_data);
|
||||
return $empfehlung_data;
|
||||
}
|
||||
|
||||
// If Empfehlung is true or false
|
||||
@@ -260,15 +304,14 @@ class AnrechnungLib
|
||||
if (!$anrechnung->empfehlung_anrechnung)
|
||||
{
|
||||
// Get Ablehnungsbegruendung (only set, if Anrechnung was not recommended yet)
|
||||
$this->ci->load->model('person/Notiz_model', 'NotizModel');
|
||||
$result = $this->ci->NotizModel->getNotizByAnrechnung($anrechnung_id, self::ANRECHNUNG_NOTIZTITEL_NOTIZ_BY_LEKTOR);
|
||||
if ($notiz = getData($result)[0])
|
||||
{
|
||||
$empfehlung_data->notiz = $notiz->text;
|
||||
$empfehlung_data->begruendung = $notiz->text;
|
||||
}
|
||||
}
|
||||
|
||||
return success($empfehlung_data);
|
||||
return $empfehlung_data;
|
||||
|
||||
}
|
||||
|
||||
@@ -300,9 +343,10 @@ class AnrechnungLib
|
||||
// Get date of approvement or rejection
|
||||
$result = $this->ci->AnrechnungModel->getApprovedOrRejected($anrechnung_id);
|
||||
|
||||
// If no approved or rejected Anrechnung exist, return basic genehmigung data object
|
||||
if (!$result = getData($result)[0])
|
||||
{
|
||||
return success($genehmigung_data);
|
||||
return $genehmigung_data;
|
||||
}
|
||||
|
||||
|
||||
@@ -331,9 +375,30 @@ class AnrechnungLib
|
||||
}
|
||||
}
|
||||
|
||||
return success($genehmigung_data);
|
||||
return $genehmigung_data;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Anrechnungstatusbezeichnung of given status_kurzbz in users language.
|
||||
*
|
||||
* @param $status_kurzbz
|
||||
* @return mixed
|
||||
*/
|
||||
public function getStatusbezeichnung ($status_kurzbz)
|
||||
{
|
||||
$this->ci->AnrechnungstatusModel->addSelect('bezeichnung_mehrsprachig');
|
||||
$result = $this->ci->AnrechnungstatusModel->load($status_kurzbz);
|
||||
|
||||
if (!hasData($result))
|
||||
{
|
||||
show_error('Failed retrieving Anrechnungstatusbezeichung');
|
||||
}
|
||||
|
||||
return getUserLanguage() == 'German'
|
||||
? $result->retval[0]->bezeichnung_mehrsprachig[0]
|
||||
: $result->retval[0]->bezeichnung_mehrsprachig[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get last Anrechnungstatusbezeichnung in users language.
|
||||
@@ -370,7 +435,7 @@ class AnrechnungLib
|
||||
// Exit if already approved or rejected
|
||||
if ($status_kurzbz == self::ANRECHNUNGSTATUS_APPROVED || $status_kurzbz == self::ANRECHNUNGSTATUS_REJECTED)
|
||||
{
|
||||
return success(false); // dont approve
|
||||
return false; // dont approve
|
||||
}
|
||||
|
||||
// Start DB transaction
|
||||
@@ -395,10 +460,10 @@ class AnrechnungLib
|
||||
if ($this->ci->db->trans_status() === false)
|
||||
{
|
||||
$this->ci->db->trans_rollback();
|
||||
return error($result->msg, EXIT_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
return success(true); // approved
|
||||
return true; // approved
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -419,16 +484,14 @@ class AnrechnungLib
|
||||
// Exit if already approved or rejected
|
||||
if ($status_kurzbz == self::ANRECHNUNGSTATUS_APPROVED || $status_kurzbz == self::ANRECHNUNGSTATUS_REJECTED)
|
||||
{
|
||||
return success(false); // dont reject
|
||||
return false; // dont reject
|
||||
}
|
||||
|
||||
// Start DB transaction
|
||||
$this->ci->db->trans_start(false);
|
||||
|
||||
// Insert new status rejected
|
||||
$result = $this->ci->AnrechnungModel->saveAnrechnungstatus($anrechnung_id, self::ANRECHNUNGSTATUS_REJECTED);
|
||||
|
||||
if (isError($result))
|
||||
{
|
||||
show_error(getError($result));
|
||||
}
|
||||
$this->ci->AnrechnungModel->saveAnrechnungstatus($anrechnung_id, self::ANRECHNUNGSTATUS_REJECTED);
|
||||
|
||||
// Add begruendung as notiz
|
||||
$this->ci->load->model('person/Notiz_model', 'NotizModel');
|
||||
@@ -438,8 +501,17 @@ class AnrechnungLib
|
||||
$begruendung,
|
||||
getAuthUID()
|
||||
);
|
||||
|
||||
// Transaction complete
|
||||
$this->ci->db->trans_complete();
|
||||
|
||||
if ($this->ci->db->trans_status() === false)
|
||||
{
|
||||
$this->ci->db->trans_rollback();
|
||||
return false;
|
||||
}
|
||||
|
||||
return success(true); // rejected
|
||||
return true; // rejected
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -462,7 +534,7 @@ class AnrechnungLib
|
||||
|| $status_kurzbz == self::ANRECHNUNGSTATUS_REJECTED
|
||||
|| $status_kurzbz == self::ANRECHNUNGSTATUS_PROGRESSED_BY_LEKTOR)
|
||||
{
|
||||
return success(false); // dont ask for recommendation
|
||||
return false; // dont ask for recommendation
|
||||
}
|
||||
|
||||
// Start DB transaction
|
||||
@@ -492,7 +564,7 @@ class AnrechnungLib
|
||||
return error($result->msg, EXIT_ERROR);
|
||||
}
|
||||
|
||||
return success(true); // recommended
|
||||
return true; // recommended
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -537,10 +609,10 @@ class AnrechnungLib
|
||||
if ($this->ci->db->trans_status() === false)
|
||||
{
|
||||
$this->ci->db->trans_rollback();
|
||||
return error($result->msg, EXIT_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
return success(true); // recommended
|
||||
return true; // recommended
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -562,7 +634,7 @@ class AnrechnungLib
|
||||
// Exit if already approved or rejected
|
||||
if ($status_kurzbz == self::ANRECHNUNGSTATUS_APPROVED || $status_kurzbz == self::ANRECHNUNGSTATUS_REJECTED)
|
||||
{
|
||||
return success(false); // dont approve
|
||||
return false; // dont approve
|
||||
}
|
||||
|
||||
// Start DB transaction
|
||||
@@ -596,10 +668,125 @@ class AnrechnungLib
|
||||
if ($this->ci->db->trans_status() === false)
|
||||
{
|
||||
$this->ci->db->trans_rollback();
|
||||
return error($result->msg, EXIT_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
return success(true); // recommended
|
||||
return true; // recommended
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Filename that should be used on download
|
||||
* @param $dms_id
|
||||
* @return string|null
|
||||
*/
|
||||
public function setFilenameOnDownload($dms_id)
|
||||
{
|
||||
// Load Anrechnung
|
||||
$result = $this->ci->AnrechnungModel->loadWhere(array('dms_id' => $dms_id));
|
||||
|
||||
// Return null if no data found
|
||||
if (!hasData($result))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
$prestudent_id = $result->retval[0]->prestudent_id;
|
||||
$lehrveranstaltung_id = $result->retval[0]->lehrveranstaltung_id;
|
||||
|
||||
// Get LV OrgForm
|
||||
$this->ci->LehrveranstaltungModel->addSelect('stg.orgform_kurzbz');
|
||||
$this->ci->LehrveranstaltungModel->addJoin('public.tbl_studiengang AS stg', 'studiengang_kz');
|
||||
$result = $this->ci->LehrveranstaltungModel->load($lehrveranstaltung_id);
|
||||
$orgform_kurzbz = hasData($result) ? '_'. $result->retval[0]->orgform_kurzbz : '';
|
||||
|
||||
// Get full name of student
|
||||
$this->ci->load->model('crm/Prestudent_model', 'PrestudentModel');
|
||||
$this->ci->PrestudentModel->addSelect('vorname, nachname');
|
||||
$this->ci->PrestudentModel->addJoin('public.tbl_person', 'person_id');
|
||||
$result = $this->ci->PrestudentModel->load($prestudent_id);
|
||||
$fullname = hasData($result) ? $result->retval[0]->vorname. $result->retval[0]->nachname : '';
|
||||
|
||||
// Return filename
|
||||
return 'Anrechnungsantrag'. $orgform_kurzbz .'_LV-'. $lehrveranstaltung_id. '_'. $fullname;
|
||||
}
|
||||
|
||||
public function LVhasLector($anrechnung_id)
|
||||
{
|
||||
$result = $this->ci->AnrechnungModel->load($anrechnung_id);
|
||||
if (!hasData($result))
|
||||
{
|
||||
showError('Anrechnung existiert nicht');
|
||||
}
|
||||
|
||||
// Get lectors of lehrveranstaltung
|
||||
$result = $this->ci->LehrveranstaltungModel->getLecturersByLv(
|
||||
$result->retval[0]->studiensemester_kurzbz,
|
||||
$result->retval[0]->lehrveranstaltung_id
|
||||
);
|
||||
|
||||
// Continue, if LV has no lector (there is no one to ask for recommendation)
|
||||
return hasData($result) ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get LV Leitung. If not present, get all LV lectors.
|
||||
*
|
||||
* @param $anrechnung_id
|
||||
* @return array|bool
|
||||
*/
|
||||
public function getLectors($anrechnung_id)
|
||||
{
|
||||
$this->ci->AnrechnungModel->addSelect('lehrveranstaltung_id, studiensemester_kurzbz');
|
||||
$result = $this->ci->AnrechnungModel->load($anrechnung_id);
|
||||
|
||||
if (!hasData($result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$lehrveranstaltung_id = getData($result)[0]->lehrveranstaltung_id;
|
||||
$studiensemester_kurzbz = getData($result)[0]->studiensemester_kurzbz;
|
||||
|
||||
// Get lectors
|
||||
$lector_arr = array();
|
||||
|
||||
$this->ci->load->model('education/Lehrveranstaltung_model', 'LehrveranstaltungModel');
|
||||
$result = $this->ci->LehrveranstaltungModel->getLecturersByLv($studiensemester_kurzbz, $lehrveranstaltung_id);
|
||||
|
||||
if (!$result = getData($result))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if lv has LV-Leitung
|
||||
$key = array_search(true, array_column($result, 'lvleiter'));
|
||||
|
||||
// If lv has LV-Leitung, keep only the one
|
||||
if ($key !== false)
|
||||
{
|
||||
$lector_arr[]= $result[$key];
|
||||
}
|
||||
// ...otherwise keep all lectors
|
||||
else
|
||||
{
|
||||
$lector_arr = array_merge($lector_arr, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOTE: This step is only done to make the array unique by uid, vorname and nachname in the following step
|
||||
* (e.g. if same lector is ones LV-Leitung and another time not, then array_unique would leave both.
|
||||
* But we wish to send only one email by to that one person)
|
||||
* **/
|
||||
foreach ($lector_arr as $lector)
|
||||
{
|
||||
unset($lector->lvleiter);
|
||||
$lector->fullname = $lector->vorname. ' '. $lector->nachname;
|
||||
}
|
||||
|
||||
// Now make the lector array aka mail receivers unique
|
||||
$lector_arr = array_unique($lector_arr, SORT_REGULAR);
|
||||
|
||||
return $lector_arr;
|
||||
}
|
||||
|
||||
// Return an object with Anrechnungdata
|
||||
@@ -631,12 +818,7 @@ class AnrechnungLib
|
||||
$this->ci->DmsVersionModel->addSelect('name');
|
||||
$result = $this->ci->DmsVersionModel->loadWhere(array('dms_id' => $anrechnung->dms_id));
|
||||
|
||||
if (isError($result))
|
||||
{
|
||||
show_error(getError($result));
|
||||
}
|
||||
|
||||
$anrechnung_data->dokumentname = $result->retval[0]->name;
|
||||
$anrechnung_data->dokumentname = hasData($result) ? getData($result)[0]->name : '';
|
||||
|
||||
return $anrechnung_data;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,309 @@
|
||||
<?php
|
||||
|
||||
class AuthLDAPLib
|
||||
{
|
||||
const LDAP_CONF_FILE = 'ldap'; // LDAP config file name
|
||||
|
||||
// LDAP codes
|
||||
const LDAP_NO_USER_DN = 10; // No users found
|
||||
const LDAP_TOO_MANY_USER_DN = 11; // More then one user found
|
||||
|
||||
// LDAP configuration array elements
|
||||
const SERVER = 'server';
|
||||
const PORT = 'port';
|
||||
const STARTTLS = 'starttls';
|
||||
const BASEDN = 'basedn';
|
||||
const USERNAME = 'username';
|
||||
const PASSWORD = 'password';
|
||||
const USF = 'usf';
|
||||
const TIMEOUT = 'timeout';
|
||||
|
||||
const DN = 'dn'; // LDAP dn name
|
||||
|
||||
const LDAP_PROTOCOL_VERSION = 3; // Specifies the LDAP protocol to be used (V2 or V3) V2 is deprecated
|
||||
const LDAP_REFERRALS = 0; // Specifies whether to automatically follow referrals returned by the LDAP server (0 = disabled)
|
||||
const LDAP_INVALID_CREDENTIALS = 49; // LDAP invalid credentials code
|
||||
const LDAP_DEFAULT_TIMEOUT = 1; // Default LDAP timeout in seconds
|
||||
|
||||
/**
|
||||
* Sets the properties and loads the LDAP configuration
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Gets CI instance
|
||||
$this->_ci =& get_instance();
|
||||
|
||||
// Loads the LogLib
|
||||
$this->_ci->load->library('LogLib');
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
* Checks if the given credentials are valid on one of the configured LDAP servers
|
||||
*/
|
||||
public function checkUsernamePassword($username, $password)
|
||||
{
|
||||
$authenticated = false;
|
||||
|
||||
if (isEmptyString($username) || isEmptyString($password)) return error('Wrong username and password');
|
||||
|
||||
$ldapConfigArrays = $this->_loadConfig(); // NOTE: always the last to be called!
|
||||
|
||||
// For each configured LDAP server
|
||||
foreach ($ldapConfigArrays as $ldapConfigs)
|
||||
{
|
||||
// Check if the LDAP server is up and running
|
||||
if (!$this->_servicePing($ldapConfigs))
|
||||
{
|
||||
// If not available log debug and skip to the next configured server
|
||||
$this->_ci->loglib->logError('This LDAP server is not available: '.$ldapConfigs[self::SERVER]);
|
||||
continue;
|
||||
}
|
||||
|
||||
// Connection without username and passoword _or_ with the configured username and password
|
||||
$noCredentialsConnectResult = $this->_connect($ldapConfigs);
|
||||
if (isError($noCredentialsConnectResult)) // If an error occurred
|
||||
{
|
||||
// If the error is due to invalid credentials or
|
||||
// the LDAP server does not support anonymous authentication
|
||||
if (getCode($noCredentialsConnectResult) == AUTH_INVALID_CREDENTIALS)
|
||||
{
|
||||
$this->_ci->loglib->logDebug(getError($noCredentialsConnectResult).' on server '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
else // otherwise if it was due to a fatal error
|
||||
{
|
||||
$this->_ci->loglib->logError(getError($noCredentialsConnectResult).' on server '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
|
||||
continue; // anyway skip to the next configured server
|
||||
}
|
||||
|
||||
// If it is a success
|
||||
$noCredentialsConnection = getData($noCredentialsConnectResult);
|
||||
|
||||
// Check if the user exists on this LDAP server
|
||||
$userDNResult = $this->_getUserDN(
|
||||
$noCredentialsConnection,
|
||||
$ldapConfigs[self::BASEDN],
|
||||
$ldapConfigs[self::USF],
|
||||
$username
|
||||
);
|
||||
// If an error occurred or the user was not found or many users were found
|
||||
if (isError($userDNResult))
|
||||
{
|
||||
// Log debug and skip to the next configured server
|
||||
// If the error is due to invalid credentials or
|
||||
// the LDAP server does not support anonymous authentication
|
||||
if (getCode($userDNResult) == self::LDAP_NO_USER_DN)
|
||||
{
|
||||
$this->_ci->loglib->logDebug(getError($userDNResult).' on server '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
elseif (getCode($userDNResult) == self::LDAP_TOO_MANY_USER_DN)
|
||||
{
|
||||
$this->_ci->loglib->logDebug(getError($userDNResult).' on server '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
else // otherwise if it was due to a fatal error
|
||||
{
|
||||
$this->_ci->loglib->logError(getError($userDNResult).' on server '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
|
||||
$this->_close($noCredentialsConnection); // Close the current LDAP connection
|
||||
continue; // anyway skip to the next configured server
|
||||
}
|
||||
|
||||
$this->_close($noCredentialsConnection); // Close the current LDAP connection
|
||||
|
||||
// Connect to LDAP with the userDN and password
|
||||
$credentialsConnectResult = $this->_connect($ldapConfigs, getData($userDNResult), $password);
|
||||
if (isError($credentialsConnectResult)) // If an error occurred
|
||||
{
|
||||
// Log debug and skip to the next configured server
|
||||
$this->_ci->loglib->logError(getError($credentialsConnectResult).' on server '.$ldapConfigs[self::SERVER]);
|
||||
continue;
|
||||
}
|
||||
else // otherwise the user is authenticated
|
||||
{
|
||||
$this->_close(getData($credentialsConnectResult));
|
||||
$authenticated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return $authenticated;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Private methods
|
||||
|
||||
/**
|
||||
* Loads the LDAP configuration file and returns the LDAP configuration array
|
||||
*/
|
||||
private function _loadConfig()
|
||||
{
|
||||
// Tries to require the LDAP configuration file...
|
||||
// ...first in the ENVIRONMENT subdirectory...
|
||||
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/'.self::LDAP_CONF_FILE.'.php'))
|
||||
{
|
||||
require_once(APPPATH.'config/'.ENVIRONMENT.'/'.self::LDAP_CONF_FILE.'.php');
|
||||
}
|
||||
else // ...then in the default config directory
|
||||
{
|
||||
require_once(APPPATH.'config/'.self::LDAP_CONF_FILE.'.php');
|
||||
}
|
||||
|
||||
return $ldap[$ldap_active_group];
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish a connection to LDAP with the given LDAP configuration array and eventually with
|
||||
* with a given username and password
|
||||
*/
|
||||
private function _connect($ldapConfigs, $username = null, $password = null)
|
||||
{
|
||||
// Checks if the LDAP configuraion is empty
|
||||
if (isEmptyArray($ldapConfigs)) return error('Wrong parameters given');
|
||||
|
||||
// LDAP connection
|
||||
$ldapConnection = @ldap_connect($ldapConfigs[self::SERVER].':'.$ldapConfigs[self::PORT]);
|
||||
if ($ldapConnection) // if success
|
||||
{
|
||||
// Sets the LDAP protocol version
|
||||
if (!@ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, self::LDAP_PROTOCOL_VERSION))
|
||||
{
|
||||
return error('Was not possible to set the protocol version using LDAP sever '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
|
||||
// Enable/disable the LDAP referrals
|
||||
if (!@ldap_set_option($ldapConnection, LDAP_OPT_REFERRALS, self::LDAP_REFERRALS))
|
||||
{
|
||||
return error('Was not possible to enable referrals using LDAP sever '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
|
||||
// Starts TLS if required
|
||||
if ($ldapConfigs[self::STARTTLS] === true && !@ldap_start_tls($ldapConnection))
|
||||
{
|
||||
return error('Was not possible to enable TLS using LDAP sever '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
|
||||
// If username or password are not provided...
|
||||
if (isEmptyString($username) || isEmptyString($password))
|
||||
{
|
||||
// ...use those provided by the configuration
|
||||
$username = $ldapConfigs[self::USERNAME];
|
||||
$password = $ldapConfigs[self::PASSWORD];
|
||||
}
|
||||
|
||||
// Binds to LDAP directory
|
||||
if (!@ldap_bind($ldapConnection, $username, $password))
|
||||
{
|
||||
// Wrong username and/or password
|
||||
if (ldap_errno($ldapConnection) == self::LDAP_INVALID_CREDENTIALS)
|
||||
{
|
||||
return error('Invalid credentials', AUTH_INVALID_CREDENTIALS);
|
||||
}
|
||||
else // Error
|
||||
{
|
||||
return error('Was not possible to bind to the LDAP sever '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
}
|
||||
|
||||
return success($ldapConnection); // connected!!!
|
||||
}
|
||||
else // Connection error
|
||||
{
|
||||
return error('An error occurred while connecting to the LDAP server '.$ldapConfigs[self::SERVER]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the network service is up and running
|
||||
*/
|
||||
private function _servicePing($ldapConfigs)
|
||||
{
|
||||
// Set the default timeout
|
||||
$timeout = self::LDAP_DEFAULT_TIMEOUT;
|
||||
|
||||
// If a timeout was configured for this server then use it
|
||||
if (isset($ldapConfigs[self::TIMEOUT])) $timeout = $ldapConfigs[self::TIMEOUT];
|
||||
|
||||
// The LDAP server name or URL
|
||||
$host = $ldapConfigs[self::SERVER];
|
||||
|
||||
// If it is a URL
|
||||
if (strpos($ldapConfigs[self::SERVER], 'ldap://') !== false
|
||||
|| strpos($ldapConfigs[self::SERVER], 'ldaps://') !== false)
|
||||
{
|
||||
// Get the host from the URL
|
||||
$host = parse_url($ldapConfigs[self::SERVER], PHP_URL_HOST);
|
||||
}
|
||||
|
||||
// Check if the given host answers on the given port using the given timeout
|
||||
if ($op = @fsockopen($host, $ldapConfigs[self::PORT], $errno, $errstr, $timeout))
|
||||
{
|
||||
// If it works then close the socket connection
|
||||
fclose($op);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false; // otherwise this server is not up or LDAP service is not running on the given port
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the current connection to LDAP if present
|
||||
*/
|
||||
private function _close($connection)
|
||||
{
|
||||
@ldap_unbind($connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user DN from LDAP using the given username
|
||||
*/
|
||||
private function _getUserDN($connection, $baseDN, $usf, $username)
|
||||
{
|
||||
$userDN = error('AuthLDAPLib->_getUserDN() failed');
|
||||
|
||||
// Tries to search for a user DN using the given username
|
||||
$searchResultIdentifier = @ldap_search(
|
||||
$connection,
|
||||
$baseDN,
|
||||
$usf.'='.$username
|
||||
);
|
||||
if (!$searchResultIdentifier) // Error
|
||||
{
|
||||
$userDN = error(ldap_error($connection));
|
||||
}
|
||||
|
||||
// Counts the number of found entries
|
||||
$countEntries = @ldap_count_entries($connection, $searchResultIdentifier);
|
||||
if ($countEntries === false) // Error
|
||||
{
|
||||
$userDN = error(ldap_error($connection));
|
||||
}
|
||||
elseif ($countEntries == 0)
|
||||
{
|
||||
$userDN = error('No user DN were found with username: '.$username, self::LDAP_NO_USER_DN);
|
||||
}
|
||||
elseif ($countEntries > 1)
|
||||
{
|
||||
$userDN = error('Too many users DN were found with username: '.$username, self::LDAP_TOO_MANY_USER_DN);
|
||||
}
|
||||
else // One entry was found
|
||||
{
|
||||
$entries = @ldap_get_entries($connection, $searchResultIdentifier);
|
||||
if (!$entries) // Error
|
||||
{
|
||||
$userDN = error(ldap_error($connection));
|
||||
}
|
||||
else
|
||||
{
|
||||
$userDN = success($entries[0][self::DN]);
|
||||
}
|
||||
}
|
||||
|
||||
return $userDN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,7 +385,7 @@ class AuthLib
|
||||
// Invalid credentials
|
||||
// NOTE: this is a corner case because of the HTTP basic authentication
|
||||
if (getCode($hta) == AUTH_NOT_AUTHENTICATED || getCode($hta) == AUTH_INVALID_CREDENTIALS
|
||||
|| getCode($hta) == LDAP_NO_USER_DN || getCode($hta) == LDAP_TOO_MANY_USER_DN)
|
||||
|| getCode($hta) == AuthLDAPLib::LDAP_NO_USER_DN || getCode($hta) == AuthLDAPLib::LDAP_TOO_MANY_USER_DN)
|
||||
{
|
||||
$this->_showInvalidAuthentication(); // this also stop the execution
|
||||
}
|
||||
@@ -404,37 +404,12 @@ class AuthLib
|
||||
{
|
||||
$ldap = error('Not authenticated', AUTH_NOT_AUTHENTICATED); // by default is NOT authenticated
|
||||
|
||||
$this->_ci->load->library('LDAPLib'); // Loads the LDAP library
|
||||
$this->_ci->load->library('AuthLDAPLib'); // Loads the LDAP library
|
||||
|
||||
$ldapConnection = $this->_ci->ldaplib->anonymousConnect(); // connect anonymously!
|
||||
if (isSuccess($ldapConnection)) // connected!!
|
||||
// If it is possible to authenticate on LDAP with the given username and password
|
||||
if ($this->_ci->authldaplib->checkUsernamePassword($username, $password) === true)
|
||||
{
|
||||
// Get the user DN from LDAP
|
||||
$userDN = $this->_ci->ldaplib->getUserDN($username);
|
||||
if (isSuccess($userDN)) // got it!
|
||||
{
|
||||
$this->_ci->ldaplib->close(); // close the previous LDAP anonymous connection
|
||||
|
||||
// Connects to LDAP using the last working configuration + the retrieved user DN + the provided password
|
||||
$ldapConnection = $this->_ci->ldaplib->connectUsernamePassword(getData($userDN), $password);
|
||||
if (isSuccess($ldapConnection)) // connected!
|
||||
{
|
||||
$this->_ci->ldaplib->close(); // close the previous connection
|
||||
$ldap = success('Authenticated', AUTH_SUCCESS); // authenticated!
|
||||
}
|
||||
else // blocking error
|
||||
{
|
||||
$ldap = $ldapConnection;
|
||||
}
|
||||
}
|
||||
else // blocking error
|
||||
{
|
||||
$ldap = $userDN;
|
||||
}
|
||||
}
|
||||
else // blocking error
|
||||
{
|
||||
$ldap = $ldapConnection;
|
||||
$ldap = success('Authenticated', AUTH_SUCCESS); // authenticated!
|
||||
}
|
||||
|
||||
return $ldap;
|
||||
|
||||
@@ -2,11 +2,10 @@
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
class DmsLib
|
||||
class DmsLib extends FHC_Controller
|
||||
{
|
||||
const FILE_CONTENT_PROPERTY = 'file_content';
|
||||
|
||||
const FILE_INPUT_NAME = 'uploadfile'; // name of the HTML input tag containing the uploaded file
|
||||
|
||||
private $UPLOAD_PATH = DMS_PATH; // temporary directory to store the upload file
|
||||
|
||||
/**
|
||||
@@ -21,6 +20,34 @@ class DmsLib
|
||||
$this->ci->load->model('content/DmsVersion_model', 'DmsVersionModel');
|
||||
$this->ci->load->model('content/DmsFS_model', 'DmsFSModel');
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a DMS Document.
|
||||
* If no version is particularly given, the latest version is loaded.
|
||||
*
|
||||
* @param $dms_id
|
||||
* @param integer $version
|
||||
* @return array
|
||||
*/
|
||||
public function load($dms_id, $version = null)
|
||||
{
|
||||
if (is_numeric($dms_id))
|
||||
{
|
||||
$this->ci->DmsModel->addJoin('campus.tbl_dms_version', 'dms_id');
|
||||
$this->ci->DmsModel->addOrder('version', 'DESC');
|
||||
$this->ci->DmsModel->addLimit(1);
|
||||
|
||||
if (!is_numeric($version))
|
||||
{
|
||||
return $this->ci->DmsModel->load($dms_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
return $this->ci->DmsModel->loadWhere(array('dms_id' => $dms_id, 'version' => $version));
|
||||
}
|
||||
}
|
||||
return error('The parameter DMS ID must be a number');
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a DMS Document from the Filesystem
|
||||
@@ -95,19 +122,20 @@ class DmsLib
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Uploads a document and saves it to DMS
|
||||
* @param $dms DMS assoc array
|
||||
* @param array $allowed_types Default: all. Param example: array(jpg, pdf)
|
||||
* @param $field_name Name of the HTML uploadfile input name attribute
|
||||
* @param array $allowed_types Default: all. Param example: array(jpg, pdf)
|
||||
* @return array
|
||||
*/
|
||||
public function upload($dms, $allowed_types = array('*'))
|
||||
public function upload($dms, $field_name, $allowed_types = array('*'))
|
||||
{
|
||||
// Init upload configs
|
||||
$this->_loadUploadLibrary($allowed_types);
|
||||
|
||||
if (!$this->ci->upload->do_upload(DmsLib::FILE_INPUT_NAME))
|
||||
if (!$this->ci->upload->do_upload($field_name))
|
||||
{
|
||||
return error($this->ci->upload->display_errors());
|
||||
}
|
||||
@@ -132,46 +160,80 @@ class DmsLib
|
||||
// return result of uploaded data
|
||||
return success($upload_data); // data about the uploaded file
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Download a document
|
||||
* Download a document.
|
||||
*
|
||||
* @param $dms_id
|
||||
* @param string $filename $filename If String is given, it will be used as filename on download
|
||||
* @param string $disposition [inline | attachment]
|
||||
* Inline opens doc in new tab. Attachment displays download dialog box.
|
||||
*/
|
||||
public function download($dms_id)
|
||||
public function download($dms_id, $filename = null, $disposition = 'inline')
|
||||
{
|
||||
if (!is_numeric($dms_id))
|
||||
{
|
||||
show_error('Wrong parameter');
|
||||
}
|
||||
|
||||
$this->ci->DmsVersionModel->addSelect('filename');
|
||||
$result = $this->ci->DmsVersionModel->loadWhere(array('dms_id' => $dms_id));
|
||||
|
||||
$result = $this->getFileInfo($dms_id);
|
||||
|
||||
if (isError($result))
|
||||
{
|
||||
show_error(getError($result));
|
||||
return error(getError($result));
|
||||
}
|
||||
|
||||
$filename = $result->retval[0]->filename;
|
||||
$file = DMS_PATH. $filename;
|
||||
|
||||
if (file_exists($file))
|
||||
$fileObj = getData($result);
|
||||
|
||||
// Change filename, if filename is provided
|
||||
if (is_string($filename))
|
||||
{
|
||||
$finfo = new finfo(FILEINFO_MIME);
|
||||
|
||||
header('Content-Description: File Transfer');
|
||||
header('Content-Type: '.$finfo->file($file));
|
||||
header('Expires: 0');
|
||||
header('Cache-Control: must-revalidate');
|
||||
header('Pragma: public');
|
||||
header('Content-Length: ' . filesize($file));
|
||||
readfile($file);
|
||||
exit;
|
||||
$fileObj->name = $filename;
|
||||
}
|
||||
|
||||
// Add file disposition
|
||||
if ($disposition == 'attachment')
|
||||
{
|
||||
$fileObj->disposition = 'attachment';
|
||||
}
|
||||
else
|
||||
{
|
||||
show_error('File does not exist');
|
||||
$fileObj->disposition = 'inline';
|
||||
}
|
||||
|
||||
// Output file
|
||||
if(!$this->outputFile($fileObj))
|
||||
{
|
||||
return error('Error on file output');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get file information.
|
||||
*
|
||||
* @param $dms_id
|
||||
* @param integer $version
|
||||
* @return array with File Object.
|
||||
*/
|
||||
public function getFileInfo($dms_id, $version = null)
|
||||
{
|
||||
if (!is_numeric($dms_id))
|
||||
{
|
||||
return error('Wrong parameter');
|
||||
}
|
||||
|
||||
// Load file
|
||||
$result = $this->load($dms_id, $version);
|
||||
|
||||
if (isError($result))
|
||||
{
|
||||
return error(getError($result));
|
||||
}
|
||||
|
||||
// Store file information in fileObj
|
||||
$fileObj = new StdClass();
|
||||
$fileObj->filename = getData($result)[0]->filename;
|
||||
$fileObj->file = DMS_PATH. getData($result)[0]->filename;
|
||||
$fileObj->name = DMS_PATH. getData($result)[0]->name; // original users filename
|
||||
$fileObj->mimetype = DMS_PATH. getData($result)[0]->mimetype;
|
||||
|
||||
return success($fileObj);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,13 +58,16 @@ class JobsQueueLib
|
||||
|
||||
/**
|
||||
* To get the oldest added jobs using the given job type
|
||||
* It is eventually specify the maximum amount of jobs to be retrieved
|
||||
*/
|
||||
public function getOldestJob($type)
|
||||
public function getOldestJobs($type, $maxAmount = null)
|
||||
{
|
||||
$this->_ci->JobsQueueModel->resetQuery();
|
||||
|
||||
$this->_ci->JobsQueueModel->addOrder('creationtime', 'ASC');
|
||||
$this->_ci->JobsQueueModel->addLimit('1');
|
||||
|
||||
// If the maximum amount of jobs to retrieve have been specified
|
||||
if (is_numeric($maxAmount)) $this->_ci->JobsQueueModel->addLimit($maxAmount);
|
||||
|
||||
return $this->_ci->JobsQueueModel->loadWhere(array('status' => self::STATUS_NEW, 'type' => $type));
|
||||
}
|
||||
@@ -221,6 +224,23 @@ class JobsQueueLib
|
||||
return success($results); // otherwise return results in a success object
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Public static methods
|
||||
|
||||
/**
|
||||
* Utility method to generate a job with the given parameters and return it inside an array
|
||||
* ready to be used by addNewJobsToQueue and updateJobsQueue
|
||||
*/
|
||||
public static function generateJobs($status, $input)
|
||||
{
|
||||
$job = new stdClass();
|
||||
|
||||
$job->{self::PROPERTY_STATUS} = $status;
|
||||
$job->{self::PROPERTY_INPUT} = $input;
|
||||
|
||||
return array($job);
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Private methods
|
||||
|
||||
|
||||
@@ -1,228 +0,0 @@
|
||||
<?php
|
||||
|
||||
class LDAPLib
|
||||
{
|
||||
const LDAP_CONF_FILE = 'ldap'; // LDAP config file name
|
||||
|
||||
// LDAP configuration array elements
|
||||
const SERVER = 'server';
|
||||
const PORT = 'port';
|
||||
const STARTTLS = 'starttls';
|
||||
const BASEDN = 'basedn';
|
||||
const USERNAME = 'username';
|
||||
const PASSWORD = 'password';
|
||||
const USF = 'usf';
|
||||
|
||||
const DN = 'dn'; // LDAP dn name
|
||||
|
||||
const LDAP_PROTOCOL_VERSION = 3; // Specifies the LDAP protocol to be used (V2 or V3) V2 is deprecated
|
||||
const LDAP_REFERRALS = 0; // Specifies whether to automatically follow referrals returned by the LDAP server (0 = disabled)
|
||||
const LDAP_INVALID_CREDENTIALS = 49; // LDAP invalid credentials code
|
||||
|
||||
// Private properties
|
||||
private $_connection;
|
||||
private $_workingConfigArray;
|
||||
private $_ldapConfigArray;
|
||||
|
||||
/**
|
||||
* Sets the properties and loads the LDAP configuration
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_connection = null;
|
||||
$this->_workingConfigArray = null;
|
||||
$this->_ldapConfigArray = null;
|
||||
|
||||
$this->_loadConfig(); // NOTE: always the last to be called!
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Public methods
|
||||
|
||||
/**
|
||||
* Tries to connect to LDAP using configurations in property _ldapConfigArray
|
||||
* The first that works is used and stored in property _workingConfigArray
|
||||
* The LDAP connection link is stored in _connection
|
||||
*/
|
||||
public function anonymousConnect()
|
||||
{
|
||||
$connect = error('Did not found a working LDAP configuration');
|
||||
|
||||
// Loops through LDAP configurations
|
||||
foreach ($this->_ldapConfigArray as $ldapConfigs)
|
||||
{
|
||||
// Tries to establish a connection
|
||||
$connect = $this->_connect($ldapConfigs);
|
||||
if (isSuccess($connect))
|
||||
{
|
||||
break; // found a working LDAP configuration and successfully connected!
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->close(); // close the eventually established connection
|
||||
}
|
||||
}
|
||||
|
||||
return $connect;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to connect using the given username and password and the last working configuration with anonymous connection
|
||||
*/
|
||||
public function connectUsernamePassword($username, $password)
|
||||
{
|
||||
if (isEmptyString($username) || isEmptyString($password)) return error('Wrong username and password');
|
||||
|
||||
return $this->_connect($this->_workingConfigArray, $username, $password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the current connection to LDAP if present
|
||||
*/
|
||||
public function close()
|
||||
{
|
||||
if ($this->_connection != null) @ldap_unbind($this->_connection);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user DN from LDAP using the given username
|
||||
*/
|
||||
public function getUserDN($username)
|
||||
{
|
||||
$userDN = error('No user DN were found', LDAP_NO_USER_DN);
|
||||
|
||||
// Tries to search for a user DN using the given username
|
||||
$searchResultIdentifier = @ldap_search(
|
||||
$this->_connection,
|
||||
$this->_workingConfigArray[self::BASEDN],
|
||||
$this->_workingConfigArray[self::USF].'='.$username
|
||||
);
|
||||
if (!$searchResultIdentifier) // Error
|
||||
{
|
||||
$userDN = error(ldap_error($this->_connection));
|
||||
}
|
||||
|
||||
// Counts the number of found entries
|
||||
$countEntries = @ldap_count_entries($this->_connection, $searchResultIdentifier);
|
||||
if ($countEntries === false) // Error
|
||||
{
|
||||
$userDN = error(ldap_error($this->_connection));
|
||||
}
|
||||
elseif ($countEntries == 0)
|
||||
{
|
||||
$userDN = error('No user DN were found', LDAP_NO_USER_DN);
|
||||
}
|
||||
elseif ($countEntries > 1)
|
||||
{
|
||||
$userDN = error('Too many users DN were found', LDAP_TOO_MANY_USER_DN);
|
||||
}
|
||||
else // One entry was found
|
||||
{
|
||||
$entries = @ldap_get_entries($this->_connection, $searchResultIdentifier);
|
||||
if (!$entries) // Error
|
||||
{
|
||||
$userDN = error(ldap_error($this->_connection));
|
||||
}
|
||||
else
|
||||
{
|
||||
$userDN = success($entries[0][self::DN]);
|
||||
}
|
||||
}
|
||||
|
||||
return $userDN;
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------------------------------------------
|
||||
// Private methods
|
||||
|
||||
/**
|
||||
* Loads the LDAP configuration file and store the LDAP configuration array into _ldapConfigArray property
|
||||
*/
|
||||
private function _loadConfig()
|
||||
{
|
||||
// Tries to require the LDAP configuration file...
|
||||
// ...first in the ENVIRONMENT subdirectory...
|
||||
if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/'.self::LDAP_CONF_FILE.'.php'))
|
||||
{
|
||||
require_once(APPPATH.'config/'.ENVIRONMENT.'/'.self::LDAP_CONF_FILE.'.php');
|
||||
}
|
||||
else // ...then in the default config directory
|
||||
{
|
||||
require_once(APPPATH.'config/'.self::LDAP_CONF_FILE.'.php');
|
||||
}
|
||||
|
||||
$this->_ldapConfigArray = $ldap[$ldap_active_group]; // store the active LDAP configuration array
|
||||
}
|
||||
|
||||
/**
|
||||
* Establish a connection to LDAP with the given LDAP configuration array and eventually with
|
||||
* with a given username and password
|
||||
*/
|
||||
private function _connect($ldapConfigs, $username = null, $password = null)
|
||||
{
|
||||
// Checks if the LDAP configuraion is empty
|
||||
if (isEmptyArray($ldapConfigs))
|
||||
{
|
||||
return error('Wrong parameters given');
|
||||
}
|
||||
|
||||
// LDAP connection
|
||||
$ldapConnection = @ldap_connect($ldapConfigs[self::SERVER].':'.$ldapConfigs[self::PORT]);
|
||||
if ($ldapConnection) // if success
|
||||
{
|
||||
// Sets the LDAP protocol version
|
||||
if (!@ldap_set_option($ldapConnection, LDAP_OPT_PROTOCOL_VERSION, self::LDAP_PROTOCOL_VERSION))
|
||||
{
|
||||
return error(ldap_error($ldapConnection));
|
||||
}
|
||||
|
||||
// Enable/disable the LDAP referrals
|
||||
if (!@ldap_set_option($ldapConnection, LDAP_OPT_REFERRALS, self::LDAP_REFERRALS))
|
||||
{
|
||||
return error(ldap_error($ldapConnection));
|
||||
}
|
||||
|
||||
// Starts TLS if required
|
||||
if ($ldapConfigs[self::STARTTLS] === true)
|
||||
{
|
||||
if (!@ldap_start_tls($ldapConnection))
|
||||
{
|
||||
return error(ldap_error($ldapConnection));
|
||||
}
|
||||
}
|
||||
|
||||
// If username and password are not provided...
|
||||
if ($username == null || $password == null)
|
||||
{
|
||||
// ...uses those provided by the configuration
|
||||
$username = $ldapConfigs[self::USERNAME];
|
||||
$password = $ldapConfigs[self::PASSWORD];
|
||||
}
|
||||
|
||||
// Binds to LDAP directory
|
||||
if (!@ldap_bind($ldapConnection, $username, $password))
|
||||
{
|
||||
// Wrong username and/or password
|
||||
if (ldap_errno($ldapConnection) == self::LDAP_INVALID_CREDENTIALS)
|
||||
{
|
||||
return error('Invalid credentials', AUTH_INVALID_CREDENTIALS);
|
||||
}
|
||||
else // Error
|
||||
{
|
||||
return error(ldap_error($ldapConnection));
|
||||
}
|
||||
}
|
||||
|
||||
$this->_connection = $ldapConnection; // save the connection into _connection property
|
||||
$this->_workingConfigArray = $ldapConfigs; // save the working LDAP configuration into _workingConfigArray property
|
||||
|
||||
return success('Connected'); // connected!!!
|
||||
}
|
||||
else // Connection error
|
||||
{
|
||||
return error(
|
||||
'An error occurred while connecting to the LDAP server: '.$ldapConfigs[self::SERVER].':'.$ldapConfigs[self::PORT]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user