diff --git a/application/config/autoload.php b/application/config/autoload.php index 97eac47b8..76835381d 100755 --- a/application/config/autoload.php +++ b/application/config/autoload.php @@ -58,7 +58,7 @@ $autoload['packages'] = array(); | | $autoload['libraries'] = array('user_agent' => 'ua'); */ -$autoload['libraries'] = array('Session', 'FHC_Auth'); +$autoload['libraries'] = array('Session', 'AuthLib'); /* | ------------------------------------------------------------------- diff --git a/application/config/rest.php b/application/config/rest.php index ec2d6f5ad..4f3a78972 100644 --- a/application/config/rest.php +++ b/application/config/rest.php @@ -143,7 +143,7 @@ $config['auth_source'] = 'library'; | password for that username, even if it is hashed | */ -$config['auth_library_class'] = 'FHC_Auth'; +$config['auth_library_class'] = 'AuthLib'; // rest_auth is basic $config['auth_library_function'] = 'basicAuthentication'; diff --git a/application/controllers/api/v1/CheckUserAuth.php b/application/controllers/api/v1/CheckUserAuth.php index a793845a7..ac4195808 100644 --- a/application/controllers/api/v1/CheckUserAuth.php +++ b/application/controllers/api/v1/CheckUserAuth.php @@ -22,18 +22,23 @@ class CheckUserAuth extends APIv1_Controller public function __construct() { parent::__construct(); - $this->load->model('CheckUserAuth_model', 'CheckUserAuthModel'); + + // Loads the authentication library + $this->load->library('AuthLib'); } - + + /** + * Checks if username and password of a final user are valid + */ public function getCheckByUsernamePassword() { - $username = $this->get("username"); - $password = $this->get("password"); - + $username = $this->get('username'); + $password = $this->get('password'); + if (isset($username) && isset($password)) { - $result = $this->CheckUserAuthModel->checkByUsernamePassword($username, $password); - + $result = $this->authlib->CheckUserAuthByUsernamePassword($username, $password); + $this->response($result, REST_Controller::HTTP_OK); } else @@ -41,4 +46,45 @@ class CheckUserAuth extends APIv1_Controller $this->response(); } } -} \ No newline at end of file + + /** + * Checks if username and password of a final user are valid + * Returns all the keys with which is possible to obtain all the personal data about a final user + */ + public function getCheckUserAuth() + { + $code = $this->get('code'); + $email = $this->get('email'); + $username = $this->get('username'); + $password = $this->get('password'); + + $result = null; + $httpCode = null; + + // If username and password are given then check authentication using them + if (isset($username) && isset($password)) + { + $result = $this->authlib->CheckUserAuthByUsernamePassword($username, $password, true); + } + elseif (isset($code) || isset($email)) + { + // If code and email are given then check authentication using them + if (isset($code) && isset($email)) + { + $result = $this->authlib->CheckUserAuthByCodeEmail($code, $email); + } + else // otherwise check authentication using only code + { + $result = $this->authlib->CheckUserAuthByCode($code); + } + } + + // If is a success and contains data + if (hasData($result)) + { + $httpCode = REST_Controller::HTTP_OK; + } + + $this->response($result, $httpCode); + } +} 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); + } +} diff --git a/application/libraries/FHC_Auth.php b/application/libraries/FHC_Auth.php deleted file mode 100644 index dd7bb09e2..000000000 --- a/application/libraries/FHC_Auth.php +++ /dev/null @@ -1,69 +0,0 @@ -checkpassword($username, $password)) - { - return true; - } - else - { - return false; - } - } - - /** - * - * TO BE UPDATED - * - * Get the md5 hashed password by the addon username - * - * @param string $username addon username - * @return string md5 hashed string - */ - public function digestAuthentication($username) - { - $aam = new AddonAuthentication(); - - return md5($aam->getPasswordByUsername($username)); - } -} diff --git a/application/models/CheckUserAuth_model.php b/application/models/CheckUserAuth_model.php deleted file mode 100644 index 503251268..000000000 --- a/application/models/CheckUserAuth_model.php +++ /dev/null @@ -1,18 +0,0 @@ -load->library('fhc_auth'); - } - - public function checkByUsernamePassword($username, $password) - { - return success($this->fhc_auth->checkpassword($username, $password)); - } -} \ No newline at end of file diff --git a/content/student/aufnahmetermine.xul.php b/content/student/aufnahmetermine.xul.php index ef5fcd387..62e273da2 100644 --- a/content/student/aufnahmetermine.xul.php +++ b/content/student/aufnahmetermine.xul.php @@ -139,7 +139,7 @@ echo ']>