Can update general settings

This commit is contained in:
Pierre 2018-04-18 12:26:40 +02:00
parent e22205e91e
commit c5c3076c67
4 changed files with 164 additions and 7 deletions

View File

@ -28,6 +28,55 @@ class SettingsController {
}
/**
* Set (update) the general account settings
*
* @url POST /settings/set_general
*/
public function setGeneral(){
user_login_required(); //Login needed
//Check the existence of the fields
//if(!check_post_parametres(array("firstName", "lastName", "isPublic", "isOpen",
// "allowComments", "allowPostsFromFriends", "publicFriendsList", "personnalWebsite",
// "virtualDirectory", "allow_comunic_mails")))
// Rest_fatal_error(400, "Please specify all the parametres for this request!");
//Get and check virtual directory
$virtualDirectory = postString("virtualDirectory", 0);
if($virtualDirectory != ""){
$virtualDirectory = getPostUserDirectory("virtualDirectory");
//Check if the directory is available
if(!components()->settings->checkUserDirectoryAvailability($virtualDirectory, userID))
Rest_fatal_error(401, "The specified directory is not available!");
}
//Create and fill a GeneralSettings object with the new values
$settings = new GeneralSettings();
$settings->set_id(userID);
$settings->set_firstName(postString("firstName", 3));
$settings->set_lastName(postString("lastName", 3));
$settings->set_publicPage(postBool("isPublic"));
$settings->set_openPage(postBool("isOpen"));
$settings->rationalizePublicOpenStatus();
$settings->set_allowComments(postBool("allowComments"));
$settings->set_allowPostsFriends(postBool("allowPostsFromFriends"));
$settings->set_friendsListPublic(postBool("publicFriendsList"));
$settings->set_personnalWebsite(postString("personnalWebsite", 0));
$settings->set_virtualDirectory($virtualDirectory);
$settings->set_allowComunicMails(postBool("allow_comunic_mails"));
//Try to update settings
if(!components()->settings->save_general($settings))
Rest_fatal_error(500, "Coud not save user settings!");
//Success
return array("success" => "The general settings of the user have been successfully saved !");
}
/**
* Check the availability of a user directory
*
@ -43,10 +92,10 @@ class SettingsController {
//Check if the directory is available
if(!components()->settings->checkUserDirectoryAvailability($userDirectory, userID))
Rest_fatal_error(401, "The specified domain is not available!");
Rest_fatal_error(401, "The specified directory is not available!");
//Else the domain is available
return array("success" => "The domain is available!");
//Else the directory is available
return array("success" => "The directory is available!");
}
/**

View File

@ -27,6 +27,21 @@ class SettingsComponents {
return $this->dbToGeneralSettings($entry);
}
/**
* Save new version of the general settings of a user
*
* @param GeneralSettings $settings The settings to save in the database
* @return bool TRUE in case of success / FALSE else
*/
public function save_general(GeneralSettings $settings) : bool {
//Convert GeneralSettings object into database entry
$entry = $this->generalSettingsToDb($settings);
//Save information in the database
return $this->saveDBUserInfo($settings->get_id(), $entry);
}
/**
* Check whether a directory is already linked to a user or not. If yes,
* check if it linked to a specified user ID.
@ -74,6 +89,24 @@ class SettingsComponents {
return($userInfos[0]);
}
/**
* Save new user information in the database
*
* @param int $userID The ID of the user to update
* @param array $values The new values to update in the database
* @return bool TRUE in case of success / FALSE else
*/
private function saveDBUserInfo(int $userID, array $info) : bool {
//Prepare the request
$table = AccountComponent::USER_TABLE;
$conditions = "ID = ?";
$conditionsValues = array($userID);
//Perform the request
return CS::get()->db->updateDB($table, $conditions, $info, $conditionsValues);
}
/**
* Parse a user information entry into GeneralSettings object
*
@ -101,6 +134,30 @@ class SettingsComponents {
}
/**
* Turn GeneralSettings object into database entry
*
* @param GeneralSettings $settings Settings entry to turn into database entry
* @return array Generated entry
*/
private function generalSettingsToDb(GeneralSettings $settings) : array {
$data = array();
$data["prenom"] = $settings->get_firstName();
$data["nom"] = $settings->get_lastName();
$data["public"] = $settings->is_publicPage() ? 1 : 0;
$data["pageouverte"] = $settings->is_openPage() ? 1 : 0;
$data["bloquecommentaire"] = $settings->is_allowComments() ? 0 : 1;
$data["autoriser_post_amis"] = $settings->is_allowPostsFriends() ? 1 : 0;
$data["autorise_mail"] = $settings->is_allowComunicMails() ? 1 : 0;
$data["liste_amis_publique"] = $settings->is_friendsListPublic() ? 1 : 0;
$data["sous_repertoire"] = $settings->has_virtualDirectory() ? $settings->get_virtualDirectory() : "";
$data["site_web"] = $settings->has_personnalWebsite() ? $settings->get_personnalWebsite() : "";
return $data;
}
}
//Register component

View File

@ -78,6 +78,16 @@ class GeneralSettings {
return $this->openPage;
}
/**
* Make sure the public and the open status of the page
* are coherent
*/
public function rationalizePublicOpenStatus(){
//Make sure the page is not open if it is not public
if(!$this->is_publicPage())
$this->set_openPage(false);
}
//Set and get the comments status on user page
public function set_allowComments(bool $allowComments){
$this->allowComments = $allowComments;

View File

@ -8,10 +8,10 @@
/**
* Check $_POST parametres associated to a request
*
* @param Array $varList The list of variables to check
* @return Boolean True or false depending of the success of the operation
* @param array $varList The list of variables to check
* @return bool True or false depending of the success of the operation
*/
function check_post_parametres(array $varList){
function check_post_parametres(array $varList) : bool {
//Check each fields
foreach($varList as $process){
@ -36,7 +36,7 @@ function check_post_parametres(array $varList){
* @param String $list The input list
* @return Array The list of user / an empty list in case of errors
*/
function numbers_list_to_array($list) : array{
function numbers_list_to_array($list) : array {
//Split the list into an array
$array = explode(",", $list);
$usersList = array();
@ -56,6 +56,47 @@ function numbers_list_to_array($list) : array{
return $usersList;
}
/**
* Check a string included in a $_POST request safely.
* This function make a REST_Error if an error occur while
* processing the value
*
* @param string $name The name of the $_POST field
* @param int $minLength The minimal length for the string (default 1)
* @return string The string
*/
function postString(string $name, int $minLength = 1) : string {
//Check variable existence
if(!isset($_POST[$name]))
Rest_fatal_error(400, "Please add a POST string named '".$name."' in the request !");
$value = (string) $_POST[$name];
//Check variable length
if(strlen($value) < $minLength)
Rest_fatal_error(400, "Specified string in '".$name."' is too short!");
return $value;
}
/**
* Get a boolean given in a $_POST request safely.
* This function make a REST_Error if an error occur while
* processing the value
*
* @param string $name The name of the $_POST field
* @return bool The boolean
*/
function postBool(string $name) : bool {
//Check variable existence
if(!isset($_POST[$name]))
Rest_fatal_error(400, "Please add a POST boolean named '".$name."' in the request !");
return $_POST[$name] == "true";
}
/**
* Securely transform user given number (mixed) to integer (int)
*