From 2749dbcb3fed5a33025d3888c9eee24a2db8924a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 27 Aug 2018 17:36:58 +0200 Subject: [PATCH] Updated API tables structure --- classes/APIClients.php | 18 ++++++++----- classes/components/AccountComponent.php | 36 +++++++++---------------- db_struct.sql | 28 +++++++++---------- 3 files changed, 38 insertions(+), 44 deletions(-) diff --git a/classes/APIClients.php b/classes/APIClients.php index 5b0f6af..ceb7f1d 100644 --- a/classes/APIClients.php +++ b/classes/APIClients.php @@ -7,6 +7,12 @@ class APIClients { + /** + * Tables name + */ + const SERVICES_TOKENS_TABLE = DBprefix."api_services_tokens"; + const USERS_TOKENS_TABLE = DBprefix."api_users_tokens"; + /** * Check request client tokens * @@ -21,7 +27,7 @@ class APIClients { return false; //Save service ID in a constant - define("APIServiceID", $serviceInfos["ID"]); + define("APIServiceID", $serviceInfos["id"]); //Save service domain in a constant (if any) if($serviceInfos["clientDomain"] != "") @@ -40,8 +46,8 @@ class APIClients { */ private function validateClientTokens(string $serviceName, string $token) { //Prepare DataBase request - $tableName = CS::get()->config->get("dbprefix")."API_ServicesToken"; - $conditions = "WHERE serviceName = ? AND token = ?"; + $tableName = self::SERVICES_TOKENS_TABLE; + $conditions = "WHERE service_name = ? AND token = ?"; $values = array( $serviceName, $token @@ -58,7 +64,7 @@ class APIClients { //The API is correctly identified //Generate client informations $clientInformations = array( - "ID" => $requestResult[0]['ID'], + "id" => $requestResult[0]['id'], "clientDomain" => ($requestResult[0]["client_domain"] == "" ? false : $requestResult[0]["client_domain"]) ); @@ -80,7 +86,7 @@ class APIClients { $entry = self::APIClientsToDb($client); //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); } @@ -95,7 +101,7 @@ class APIClients { $data = array(); $data["time_insert"] = $client->get_time_insert(); - $data["serviceName"] = $client->get_name(); + $data["service_name"] = $client->get_name(); $data["token"] = $client->get_token(); if($client->has_client_domain()) $data["client_domain"] = $client->get_client_domain(); diff --git a/classes/components/AccountComponent.php b/classes/components/AccountComponent.php index 460c3b3..5752e3e 100644 --- a/classes/components/AccountComponent.php +++ b/classes/components/AccountComponent.php @@ -12,18 +12,6 @@ class AccountComponent { */ 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 * @@ -61,10 +49,10 @@ class AccountComponent { $token2 = random_str(75); //Insert token in the database - $tableName = $this->userLoginAPItable; + $tableName = APIClients::USERS_TOKENS_TABLE; $insertValues = array( - "ID_utilisateurs" => $userID, - "ID_".CS::get()->config->get("dbprefix")."API_ServicesToken" => $serviceID, + "user_id" => $userID, + "service_id" => $serviceID, "token1" => $token1, "token2" => $token2 ); @@ -84,12 +72,12 @@ class AccountComponent { */ private function getUserLoginTokenByIDs(int $userID, int $serviceID) { //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( $userID, $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) return false; //There is nobody at this address @@ -111,14 +99,14 @@ class AccountComponent { public function deleteUserLoginToken(int $userID, string $serviceID) : bool { //Prepare database request - $condition = "ID_utilisateurs = ? AND ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ?"; + $condition = "user_id = ? AND service_id = ?"; $values = array( $userID, $serviceID ); //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 //Everything is ok @@ -135,13 +123,13 @@ class AccountComponent { public function deleteAllUserLoginTokens(int $userID) : bool { //Prepare database request - $condition = "ID_utilisateurs = ?"; + $condition = "user_id = ?"; $values = array( $userID ); //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 //Everything is ok @@ -162,8 +150,8 @@ class AccountComponent { return 0; //Prepare database request - $tablesName = $this->userLoginAPItable; - $conditions = "WHERE ".$this->userLoginAPItable.".ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ? AND ".$this->userLoginAPItable.".token1 = ? AND ".$this->userLoginAPItable.".token2 = ?"; + $tablesName = APIClients::USERS_TOKENS_TABLE; + $conditions = "WHERE ".APIClients::USERS_TOKENS_TABLE.".service_id = ? AND ".APIClients::USERS_TOKENS_TABLE.".token1 = ? AND ".APIClients::USERS_TOKENS_TABLE.".token2 = ?"; $conditionsValues = array( $serviceID, $tokens[0], @@ -178,7 +166,7 @@ class AccountComponent { return 0; //No result //Return ID - return $userInfos[0]["ID_utilisateurs"]; + return $userInfos[0]["user_id"]; } /** diff --git a/db_struct.sql b/db_struct.sql index f2a398d..e0bc50d 100644 --- a/db_struct.sql +++ b/db_struct.sql @@ -81,25 +81,25 @@ CREATE TABLE `comunic_api_limit_count` ( ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -DROP TABLE IF EXISTS `comunic_API_ServicesToken`; -CREATE TABLE `comunic_API_ServicesToken` ( - `ID` int(11) NOT NULL AUTO_INCREMENT, +DROP TABLE IF EXISTS `comunic_api_services_tokens`; +CREATE TABLE `comunic_api_services_tokens` ( + `id` int(11) NOT NULL AUTO_INCREMENT, `time_insert` int(11) DEFAULT NULL, - `serviceName` varchar(255) NOT NULL, + `service_name` varchar(255) NOT NULL, `token` varchar(255) NOT NULL, `client_domain` varchar(45) DEFAULT NULL, - PRIMARY KEY (`ID`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -DROP TABLE IF EXISTS `comunic_API_userLoginToken`; -CREATE TABLE `comunic_API_userLoginToken` ( - `ID` int(11) NOT NULL AUTO_INCREMENT, - `ID_utilisateurs` int(11) NOT NULL, - `ID_comunic_API_ServicesToken` int(11) NOT NULL, +DROP TABLE IF EXISTS `comunic_api_users_tokens`; +CREATE TABLE `comunic_api_users_tokens` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `user_id` int(11) NOT NULL, + `service_id` int(11) NOT NULL, `token1` varchar(255) NOT NULL, `token2` varchar(255) NOT NULL, - PRIMARY KEY (`ID`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; @@ -110,7 +110,7 @@ CREATE TABLE `comunic_conversations_list` ( `name` varchar(50) DEFAULT NULL, `last_active` int(11) DEFAULT NULL, `creation_time` int(11) DEFAULT NULL, - PRIMARY KEY (`ID`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; @@ -122,7 +122,7 @@ CREATE TABLE `comunic_conversations_messages` ( `time_insert` int(11) DEFAULT NULL, `message` varchar(200) DEFAULT NULL, `image_path` varchar(100) DEFAULT NULL, - PRIMARY KEY (`ID`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; @@ -134,7 +134,7 @@ CREATE TABLE `comunic_conversations_users` ( `time_add` int(11) DEFAULT NULL, `following` int(1) DEFAULT '0', `saw_last_message` int(1) DEFAULT NULL, - PRIMARY KEY (`ID`) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; DROP TABLE IF EXISTS `comunic_groups`;