Can return security settings

This commit is contained in:
Pierre 2018-04-19 09:48:25 +02:00
parent 2bd38270a1
commit 2ccb3de2f0
2 changed files with 86 additions and 0 deletions

View File

@ -98,6 +98,33 @@ class SettingsController {
return array("success" => "The directory is available!");
}
/**
* Get security settings
*
* Warning !!! This method is really sensitive, please double check any
* user input data !
*
* @url POST /settings/get_security
*/
public function getSecurity(){
//User login required
user_login_required();
//Make sure the password is valid
check_post_password(userID, "password");
//Fetch user security settings
$settings = components()->settings->get_security(userID);
//Check settings validity
if(!$settings->isValid())
Rest_fatal_error(500, "Could not get user security settings!");
//Parse and return settings entry
return $this->SecuritySettingsToAPI($settings);
}
/**
* Turn a GeneralSettings object into a valid API object
*
@ -124,4 +151,23 @@ class SettingsController {
return $data;
}
/**
* Turn a SecuritySettings object into a valid API object
*
* @param SecuritySettings $settings The object to convert
* @return array Generated API object
*/
private function SecuritySettingsToAPI(SecuritySettings $settings) : array {
$data = array();
$data["id"] = $settings->get_id();
$data["security_question_1"] = $settings->has_security_question_1() ? $settings->get_security_question_1() : "";
$data["security_answer_1"] = $settings->has_security_answer_1() ? $settings->get_security_answer_1() : "";
$data["security_question_2"] = $settings->has_security_question_2() ? $settings->get_security_question_2() : "";
$data["security_answer_2"] = $settings->has_security_answer_2() ? $settings->get_security_answer_2() : "";
return $data;
}
}

View File

@ -64,6 +64,26 @@ class SettingsComponents {
return $folderUserID == $userID;
}
/**
* Get and return security settings of a user
*
* @param int $userID Target user ID
* @return SecuritySettings An object containing the value / invalid object in
* case of failure
*/
public function get_security(int $userID) : SecuritySettings {
//Get user database entry
$entry = $this->getDBUserInfo($userID);
//Check for error
if(count($entry) == 0)
return new SecuritySettings(); //Return invalid object
//Parse database entry into SecuritySettings entry
return $this->dbToSecuritySettings($entry);
}
/**
* Get Single User Infos from database and return its information as an array
*
@ -158,6 +178,26 @@ class SettingsComponents {
return $data;
}
/**
* Parse a user information entry into SecuritySettings object
*
* @param array $entry The database entry to process
* @return SecuritySettings Generated SecuritySettings entry
*/
private function dbToSecuritySettings(array $entry) : SecuritySettings {
$obj = new SecuritySettings();
$obj->set_id($entry['ID']);
$obj->set_security_question_1($entry["question1"]);
$obj->set_security_answer_1($entry["reponse1"]);
$obj->set_security_question_2($entry["question2"]);
$obj->set_security_answer_2($entry["reponse2"]);
return $obj;
}
}
//Register component