Updated API tables structure

This commit is contained in:
Pierre HUBERT 2018-08-27 17:36:58 +02:00
parent b698118a47
commit 2749dbcb3f
3 changed files with 38 additions and 44 deletions

View File

@ -7,6 +7,12 @@
class APIClients { class APIClients {
/**
* Tables name
*/
const SERVICES_TOKENS_TABLE = DBprefix."api_services_tokens";
const USERS_TOKENS_TABLE = DBprefix."api_users_tokens";
/** /**
* Check request client tokens * Check request client tokens
* *
@ -21,7 +27,7 @@ class APIClients {
return false; return false;
//Save service ID in a constant //Save service ID in a constant
define("APIServiceID", $serviceInfos["ID"]); define("APIServiceID", $serviceInfos["id"]);
//Save service domain in a constant (if any) //Save service domain in a constant (if any)
if($serviceInfos["clientDomain"] != "") if($serviceInfos["clientDomain"] != "")
@ -40,8 +46,8 @@ class APIClients {
*/ */
private function validateClientTokens(string $serviceName, string $token) { private function validateClientTokens(string $serviceName, string $token) {
//Prepare DataBase request //Prepare DataBase request
$tableName = CS::get()->config->get("dbprefix")."API_ServicesToken"; $tableName = self::SERVICES_TOKENS_TABLE;
$conditions = "WHERE serviceName = ? AND token = ?"; $conditions = "WHERE service_name = ? AND token = ?";
$values = array( $values = array(
$serviceName, $serviceName,
$token $token
@ -58,7 +64,7 @@ class APIClients {
//The API is correctly identified //The API is correctly identified
//Generate client informations //Generate client informations
$clientInformations = array( $clientInformations = array(
"ID" => $requestResult[0]['ID'], "id" => $requestResult[0]['id'],
"clientDomain" => ($requestResult[0]["client_domain"] == "" ? false : $requestResult[0]["client_domain"]) "clientDomain" => ($requestResult[0]["client_domain"] == "" ? false : $requestResult[0]["client_domain"])
); );
@ -80,7 +86,7 @@ class APIClients {
$entry = self::APIClientsToDb($client); $entry = self::APIClientsToDb($client);
//Insert the entry in the database //Insert the entry in the database
$tableName = CS::get()->config->get("dbprefix")."API_ServicesToken"; $tableName = self::SERVICES_TOKENS_TABLE;
return CS::get()->db->addLine($tableName, $entry); return CS::get()->db->addLine($tableName, $entry);
} }
@ -95,7 +101,7 @@ class APIClients {
$data = array(); $data = array();
$data["time_insert"] = $client->get_time_insert(); $data["time_insert"] = $client->get_time_insert();
$data["serviceName"] = $client->get_name(); $data["service_name"] = $client->get_name();
$data["token"] = $client->get_token(); $data["token"] = $client->get_token();
if($client->has_client_domain()) if($client->has_client_domain())
$data["client_domain"] = $client->get_client_domain(); $data["client_domain"] = $client->get_client_domain();

View File

@ -12,18 +12,6 @@ class AccountComponent {
*/ */
const USER_TABLE = "utilisateurs"; const USER_TABLE = "utilisateurs";
/**
* @var String $userLoginAPItable The name of the table that contains logins performed on the API
*/
private $userLoginAPItable = "";
/**
* Public constructor
*/
public function __construct(){
$this->userLoginAPItable = CS::get()->config->get("dbprefix")."API_userLoginToken";
}
/** /**
* Try to login user with returning a service token * Try to login user with returning a service token
* *
@ -61,10 +49,10 @@ class AccountComponent {
$token2 = random_str(75); $token2 = random_str(75);
//Insert token in the database //Insert token in the database
$tableName = $this->userLoginAPItable; $tableName = APIClients::USERS_TOKENS_TABLE;
$insertValues = array( $insertValues = array(
"ID_utilisateurs" => $userID, "user_id" => $userID,
"ID_".CS::get()->config->get("dbprefix")."API_ServicesToken" => $serviceID, "service_id" => $serviceID,
"token1" => $token1, "token1" => $token1,
"token2" => $token2 "token2" => $token2
); );
@ -84,12 +72,12 @@ class AccountComponent {
*/ */
private function getUserLoginTokenByIDs(int $userID, int $serviceID) { private function getUserLoginTokenByIDs(int $userID, int $serviceID) {
//Prepare database request //Prepare database request
$conditions = "WHERE ID_utilisateurs = ? AND ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ?"; $conditions = "WHERE user_id = ? AND service_id = ?";
$values = array( $values = array(
$userID, $userID,
$serviceID $serviceID
); );
$tokenInfos = CS::get()->db->select($this->userLoginAPItable, $conditions, $values); $tokenInfos = CS::get()->db->select(APIClients::USERS_TOKENS_TABLE, $conditions, $values);
if(count($tokenInfos) == 0) if(count($tokenInfos) == 0)
return false; //There is nobody at this address return false; //There is nobody at this address
@ -111,14 +99,14 @@ class AccountComponent {
public function deleteUserLoginToken(int $userID, string $serviceID) : bool { public function deleteUserLoginToken(int $userID, string $serviceID) : bool {
//Prepare database request //Prepare database request
$condition = "ID_utilisateurs = ? AND ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ?"; $condition = "user_id = ? AND service_id = ?";
$values = array( $values = array(
$userID, $userID,
$serviceID $serviceID
); );
//Try to perform request //Try to perform request
if(!CS::get()->db->deleteEntry($this->userLoginAPItable, $condition, $values)) if(!CS::get()->db->deleteEntry(APIClients::USERS_TOKENS_TABLE, $condition, $values))
return false; //Something went wrong during the request return false; //Something went wrong during the request
//Everything is ok //Everything is ok
@ -135,13 +123,13 @@ class AccountComponent {
public function deleteAllUserLoginTokens(int $userID) : bool { public function deleteAllUserLoginTokens(int $userID) : bool {
//Prepare database request //Prepare database request
$condition = "ID_utilisateurs = ?"; $condition = "user_id = ?";
$values = array( $values = array(
$userID $userID
); );
//Try to perform request //Try to perform request
if(!CS::get()->db->deleteEntry($this->userLoginAPItable, $condition, $values)) if(!CS::get()->db->deleteEntry(APIClients::USERS_TOKENS_TABLE, $condition, $values))
return false; //Something went wrong during the request return false; //Something went wrong during the request
//Everything is ok //Everything is ok
@ -162,8 +150,8 @@ class AccountComponent {
return 0; return 0;
//Prepare database request //Prepare database request
$tablesName = $this->userLoginAPItable; $tablesName = APIClients::USERS_TOKENS_TABLE;
$conditions = "WHERE ".$this->userLoginAPItable.".ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ? AND ".$this->userLoginAPItable.".token1 = ? AND ".$this->userLoginAPItable.".token2 = ?"; $conditions = "WHERE ".APIClients::USERS_TOKENS_TABLE.".service_id = ? AND ".APIClients::USERS_TOKENS_TABLE.".token1 = ? AND ".APIClients::USERS_TOKENS_TABLE.".token2 = ?";
$conditionsValues = array( $conditionsValues = array(
$serviceID, $serviceID,
$tokens[0], $tokens[0],
@ -178,7 +166,7 @@ class AccountComponent {
return 0; //No result return 0; //No result
//Return ID //Return ID
return $userInfos[0]["ID_utilisateurs"]; return $userInfos[0]["user_id"];
} }
/** /**

View File

@ -81,25 +81,25 @@ CREATE TABLE `comunic_api_limit_count` (
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_API_ServicesToken`; DROP TABLE IF EXISTS `comunic_api_services_tokens`;
CREATE TABLE `comunic_API_ServicesToken` ( CREATE TABLE `comunic_api_services_tokens` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`time_insert` int(11) DEFAULT NULL, `time_insert` int(11) DEFAULT NULL,
`serviceName` varchar(255) NOT NULL, `service_name` varchar(255) NOT NULL,
`token` varchar(255) NOT NULL, `token` varchar(255) NOT NULL,
`client_domain` varchar(45) DEFAULT NULL, `client_domain` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_API_userLoginToken`; DROP TABLE IF EXISTS `comunic_api_users_tokens`;
CREATE TABLE `comunic_API_userLoginToken` ( CREATE TABLE `comunic_api_users_tokens` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_utilisateurs` int(11) NOT NULL, `user_id` int(11) NOT NULL,
`ID_comunic_API_ServicesToken` int(11) NOT NULL, `service_id` int(11) NOT NULL,
`token1` varchar(255) NOT NULL, `token1` varchar(255) NOT NULL,
`token2` varchar(255) NOT NULL, `token2` varchar(255) NOT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
@ -110,7 +110,7 @@ CREATE TABLE `comunic_conversations_list` (
`name` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL,
`last_active` int(11) DEFAULT NULL, `last_active` int(11) DEFAULT NULL,
`creation_time` int(11) DEFAULT NULL, `creation_time` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
@ -122,7 +122,7 @@ CREATE TABLE `comunic_conversations_messages` (
`time_insert` int(11) DEFAULT NULL, `time_insert` int(11) DEFAULT NULL,
`message` varchar(200) DEFAULT NULL, `message` varchar(200) DEFAULT NULL,
`image_path` varchar(100) DEFAULT NULL, `image_path` varchar(100) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
@ -134,7 +134,7 @@ CREATE TABLE `comunic_conversations_users` (
`time_add` int(11) DEFAULT NULL, `time_add` int(11) DEFAULT NULL,
`following` int(1) DEFAULT '0', `following` int(1) DEFAULT '0',
`saw_last_message` int(1) DEFAULT NULL, `saw_last_message` int(1) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_groups`; DROP TABLE IF EXISTS `comunic_groups`;