From ff233e3f169b6104273e9659b7b093857f2d87b3 Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 25 Sep 2017 17:16:52 +0200 Subject: [PATCH] Added AuthLib --- application/libraries/AuthLib.php | 190 ++++++++++++++++++++++++++++++ 1 file changed, 190 insertions(+) create mode 100644 application/libraries/AuthLib.php diff --git a/application/libraries/AuthLib.php b/application/libraries/AuthLib.php new file mode 100644 index 000000000..80351cd45 --- /dev/null +++ b/application/libraries/AuthLib.php @@ -0,0 +1,190 @@ +ci =& get_instance(); + + // Loads helper message to manage returning messages + $this->ci->load->helper('Message'); + } + + /** + * Checks the authentication of an addon + * returns TRUE if valid, otherwise FALSE + */ + public function basicAuthentication($username, $password) + { + return $this->checkpassword($username, $password); + } + + /** + * Checks if the given username and password of a final user are valid + */ + public function CheckUserAuthByUsernamePassword($username, $password, $keys = false) + { + $result = error(false); + + if (isset($username) && isset($password)) + { + if ($this->checkpassword($username, $password) === true) + { + if ($keys === true) + { + $result = $this->_getFinalUserBasicDataByUID($username); + } + else + { + $result = success(true); + } + } + } + + return $result; + } + + /** + * Checks if the given code of a final user is valid + */ + public function CheckUserAuthByCode($code) + { + $result = error(false); + + // Load model PersonModel + $this->ci->load->model('person/person_model', 'PersonModel'); + + $person = $this->ci->PersonModel->loadWhere(array('zugangscode' => $code)); + + if (hasData($person)) + { + $result = $this->_getFinalUserBasicDataByPersonID($person->retval[0]->person_id); + } + + return $result; + } + + /** + * Checks if the given code and email of a final user are valid + */ + public function CheckUserAuthByCodeEmail($code, $email) + { + $result = error(false); + + // Load model PersonModel + $this->ci->load->model('person/person_model', 'PersonModel'); + + $person = $this->ci->PersonModel->getPersonKontaktByZugangscode($code, $email); + + if (hasData($person)) + { + $result = $this->_getFinalUserBasicDataByPersonID($person->retval[0]->person_id); + } + + return $result; + } + + /** + * Returns all the keys with which is possible to obtain personal data about a final user + * using the given username (uid) + */ + private function _getFinalUserBasicDataByUID($uid) + { + $finalUserBasicDataByUID = null; + + // Load model BenutzerModel + $this->ci->load->model('person/Benutzer_model', 'BenutzerModel'); + + $benutzer = $this->ci->BenutzerModel->load($uid); + + if (hasData($benutzer)) + { + $finalUserBasicDataByUID = $this->_getFinalUserBasicDataByPersonID($benutzer->retval[0]->person_id, $uid); + } + + return $finalUserBasicDataByUID; + } + + /** + * Returns all the keys with which is possible to obtain personal data about a final user + * using the given person_id. The uid is optional + */ + private function _getFinalUserBasicDataByPersonID($person_id, $uid = null) + { + $finalUserBasicDataByPersonID = new stdClass(); // returned object + + // Store the person_id and eventually all the uids and prestudent_ids related to this final user + $finalUserBasicDataByPersonID->person_id = $person_id; + $finalUserBasicDataByPersonID->uids = array(); + $finalUserBasicDataByPersonID->prestudent_ids = array(); + + // If the UID has not been given as a parameter + if ($uid != null) + { + $finalUserBasicDataByPersonID->uid[0] = $uid; + } + else + { + // Load model BenutzerModel + $this->ci->load->model('person/Benutzer_model', 'BenutzerModel'); + + // Loads all the benutzer whith that person_id + $benutzer = $this->ci->BenutzerModel->loadWhere(array('person_id' => $person_id)); + + if (hasData($benutzer)) // store all the uids + { + foreach ($benutzer->retval as $rownum => $rowdata) + { + $finalUserBasicDataByPersonID->uids[$rownum] = $rowdata->uid; + } + } + } + + // Load model PrestudentModel + $this->ci->load->model('crm/Prestudent_model', 'PrestudentModel'); + + // Loads all the prestudent whith that person_id + $prestudent = $this->ci->PrestudentModel->loadWhere(array('person_id' => $person_id)); + + if (hasData($prestudent)) // store all the prestudent_ids + { + foreach ($prestudent->retval as $rownum => $rowdata) + { + $finalUserBasicDataByPersonID->prestudent_ids[$rownum] = $rowdata->prestudent_id; + } + } + + return success($finalUserBasicDataByPersonID); + } +}