mirror of
https://github.com/pierre42100/ComunicAPI
synced 2025-06-19 00:25:18 +00:00
Updated API tables structure
This commit is contained in:
@ -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();
|
||||
|
@ -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"];
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user