Update database creation to support multi-user paradigm

This commit is contained in:
osaajani 2019-11-08 18:06:18 +01:00
parent 922e6c1c3b
commit 493859a688
2 changed files with 50 additions and 159 deletions

View File

@ -1,138 +0,0 @@
<?php
namespace controllers\publics;
/**
* Page des smsapis
*/
class SmsAPI extends \descartes\Controller
{
//On défini les constantes qui servent pour les retours d'API
const API_ERROR_NO = 0;
const API_ERROR_BAD_ID = 1;
const API_ERROR_CREATION_FAILED = 2;
const API_ERROR_MISSING_FIELD = 3;
/**
* Cette fonction est appelée avant toute les autres :
* Elle vérifie que l'utilisateur est bien connecté
* @return void;
*/
public function _before()
{
global $bdd;
global $model;
$this->bdd = $bdd;
$this->model = $model;
$this->internal_user = new \controllers\internals\User($this->bdd);
$this->internalContact = new \controllers\internals\Contact($this->bdd);
\controllers\internals\Tool::verifyconnect();
}
/**
* Cette fonction permet d'envoyer un Sms, en passant simplement des arguments à l'URL (ou pas $_GET)
* @param string text = Le contenu du Sms
* @param mixed numbers = Les numéros auxquels envoyer les Sms. Soit un seul numéro, et il s'agit d'un string. Soit plusieurs numéros, et il s'agit d'un tableau
* @param mixed contacts = Les noms des contacts auxquels envoyer les Sms. Soit un seul et il s'agit d'un string. Soit plusieurs, et il s'agit d'un tableau
* @param mixed groupes = Les noms des groupes auxquels envoyer les Sms. Soit un seul et il s'agit d'un string. Soit plusieurs, et il s'agit d'un tableau
* @param optionnal string date = La date à laquelle doit être envoyé le Sms. Au format 'Y-m-d H:i'. Si non fourni, le Sms sera envoyé dans 2 minutes
*/
public function api()
{
//On récupère l'email et le password
$email = isset($_GET['email']) ? $_GET['email'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : $email;
$password = isset($_GET['password']) ? $_GET['password'] : null;
$password = isset($_POST['password']) ? $_POST['password'] : $password;
//Si les identifiants sont incorrect on retourne une erreur
$user = $internal_user->check_credentials($email, $password);
if (!$user) {
echo json_encode(array(
'error' => self::API_ERROR_BAD_ID,
));
return true;
}
//On map les variables $_GET
$get_numbers = isset($_GET['numbers']) ? $_GET['numbers'] : array();
$get_contacts = isset($_GET['contacts']) ? $_GET['contacts'] : array();
$get_groupes = isset($_GET['groupes']) ? $_GET['groupes'] : array();
//On map les variables POST
$post_numbers = isset($_POST['numbers']) ? $_POST['numbers'] : array();
$post_contacts = isset($_POST['contacts']) ? $_POST['contacts'] : array();
$post_groupes = isset($_POST['groupes']) ? $_POST['groupes'] : array();
//On map le texte et la date à part car c'est les seuls arguments qui ne sera jamais un tableau
$text = isset($_GET['text']) ? $_GET['text'] : null;
$text = isset($_POST['text']) ? $_POST['text'] : $text;
$date = isset($_GET['date']) ? $_GET['date'] : null;
$date = isset($_POST['date']) ? $_POST['date'] : $date;
//On passe tous les paramètres GET en tableau
$get_numbers = is_array($get_numbers) ? $get_numbers : ($get_numbers ? array($get_numbers) : array());
$get_contacts = is_array($get_contacts) ? $get_contacts : array($get_contacts);
$get_groupes = is_array($get_groupes) ? $get_groupes : array($get_groupes);
//On passe tous les paramètres POST en tableau
$post_numbers = is_array($post_numbers) ? $post_numbers : array($post_numbers);
$post_contacts = is_array($post_contacts) ? $post_contacts : array($post_contacts);
$post_groupes = is_array($post_groupes) ? $post_groupes : array($post_groupes);
//On merge les données reçus en GET, et celles en POST
$numbers = array_merge($get_numbers, $post_numbers);
$contacts = array_merge($get_contacts, $post_contacts);
$groupes = array_merge($get_groupes, $post_groupes);
//Pour chaque contact, on récupère l'id du contact
foreach ($contacts as $key => $contact) {
if (!$contact = $internalContact->get_by_name($contact)) {
unset($contacts[$key]);
continue;
}
$contacts[$key] = $contact['id'];
}
//Pour chaque groupe, on récupère l'id du groupe
foreach ($groupes as $key => $name) {
if (!$groupe = $internalContact->get_by_name($groupe)) {
unset($groupes[$key]);
continue;
}
$groupes[$key] = $groupe['id'];
}
//Si la date n'est pas définie, on la met à la date du jour
if (!$date) {
$now = new \DateTime();
$date = $now->format('Y-m-d H:i');
}
//Si il manque des champs essentiels, on leve une erreur
if (!$text || (!$numbers && !$contacts && !$groupes)) {
echo json_encode(array(
'error' => self::API_ERROR_MISSING_FIELD,
));
return false;
}
//On assigne les variable POST (après avoir vidé $_POST) en prévision de la création du Sms
if (!$this->internalScheduled->create(['at' => $date, 'content' => $text], $numbers, $contacts, $groupes)) {
echo json_encode(array(
'error' => self::API_ERROR_CREATION_FAILED,
));
return false;
}
echo json_encode(array(
'error' => self::API_ERROR_NO,
));
return true;
}
}

View File

@ -6,18 +6,23 @@ USE raspisms;
CREATE TABLE IF NOT EXISTS setting CREATE TABLE IF NOT EXISTS setting
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
name VARCHAR(50) NOT NULL, name VARCHAR(50) NOT NULL,
value VARCHAR(1000) NOT NULL, value VARCHAR(1000) NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (name) UNIQUE (name)
); );
CREATE TABLE IF NOT EXISTS scheduled CREATE TABLE IF NOT EXISTS scheduled
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
send_by VARCHAR(25) DEFAULT NULL,
at DATETIME NOT NULL, at DATETIME NOT NULL,
text VARCHAR(1000) NOT NULL, text VARCHAR(1000) NOT NULL,
flash BOOLEAN NOT NULL DEFAULT 0, flash BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id) PRIMARY KEY (id)
); );
@ -32,7 +37,7 @@ CREATE TABLE IF NOT EXISTS received
PRIMARY KEY (id) PRIMARY KEY (id)
); );
CREATE TABLE IF NOT EXISTS sent CREATE TABLE IF NOT EXISTS sended
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
at DATETIME NOT NULL, at DATETIME NOT NULL,
@ -46,29 +51,32 @@ CREATE TABLE IF NOT EXISTS sent
CREATE TABLE IF NOT EXISTS contact CREATE TABLE IF NOT EXISTS contact
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
name VARCHAR(100) NOT NULL, name VARCHAR(100) NOT NULL,
number VARCHAR(20) NOT NULL, number VARCHAR(20) NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (name) UNIQUE (name)
); );
CREATE TABLE IF NOT EXISTS groupe CREATE TABLE IF NOT EXISTS group
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
name VARCHAR(100) NOT NULL, name VARCHAR(100) NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (name) UNIQUE (name)
); );
CREATE TABLE IF NOT EXISTS groupe_contact CREATE TABLE IF NOT EXISTS group_contact
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_groupe INT NOT NULL, id_group INT NOT NULL,
id_contact INT NOT NULL, id_contact INT NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_groupe) REFERENCES groupe (id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (id_group) REFERENCES group (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (id_contact) REFERENCES contact (id) ON DELETE CASCADE ON UPDATE CASCADE FOREIGN KEY (id_contact) REFERENCES contact (id) ON DELETE CASCADE ON UPDATE CASCADE
); );
@ -82,14 +90,14 @@ CREATE TABLE IF NOT EXISTS scheduled_contact
FOREIGN KEY (id_contact) REFERENCES contact (id) ON DELETE CASCADE ON UPDATE CASCADE FOREIGN KEY (id_contact) REFERENCES contact (id) ON DELETE CASCADE ON UPDATE CASCADE
); );
CREATE TABLE IF NOT EXISTS scheduled_groupe CREATE TABLE IF NOT EXISTS scheduled_group
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_scheduled INT NOT NULL, id_scheduled INT NOT NULL,
id_groupe INT NOT NULL, id_group INT NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_scheduled) REFERENCES scheduled (id) ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY (id_scheduled) REFERENCES scheduled (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (id_groupe) REFERENCES groupe (id) ON DELETE CASCADE ON UPDATE CASCADE FOREIGN KEY (id_group) REFERENCES group (id) ON DELETE CASCADE ON UPDATE CASCADE
); );
CREATE TABLE IF NOT EXISTS scheduled_number CREATE TABLE IF NOT EXISTS scheduled_number
@ -104,19 +112,23 @@ CREATE TABLE IF NOT EXISTS scheduled_number
CREATE TABLE IF NOT EXISTS command CREATE TABLE IF NOT EXISTS command
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
name VARCHAR(25) NOT NULL, name VARCHAR(25) NOT NULL,
script VARCHAR(100) NOT NULL, script VARCHAR(100) NOT NULL,
admin BOOLEAN NOT NULL, admin BOOLEAN NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (name) UNIQUE (name)
); );
CREATE TABLE IF NOT EXISTS event CREATE TABLE IF NOT EXISTS event
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
type VARCHAR(25) NOT NULL, type VARCHAR(25) NOT NULL,
at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
text VARCHAR(255) NOT NULL, text VARCHAR(255) NOT NULL,
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id) PRIMARY KEY (id)
); );
@ -131,11 +143,35 @@ CREATE TABLE IF NOT EXISTS user
UNIQUE (email) UNIQUE (email)
); );
CREATE TABLE IF NOT EXISTS user_number
(
id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
phone_number VARCHAR(25) NOT NULL,
platform VARCHAR(100) NOT NULL,
platform_datas JSON NOT NULL,
CHECK (JSON_VALID(platform_datas)),
PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE
);
#Table to ensure external validation process by mailing or other
CREATE TABLE IF NOT EXISTS validation
(
id INT NOT NULL AUTO_INCREMENT,
token VARCHAR(200) NOT NULL,
random VARCHAR(32) NOT NULL,
action VARCHAR(200) NOT NULL,
datas JSON NOT NULL,
CHECK (JSON_VALID(datas)),
PRIMARY KEY (id),
UNIQUE(token)
);
CREATE TABLE IF NOT EXISTS transfer CREATE TABLE IF NOT EXISTS transfer
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_received INT NOT NULL, id_received INT NOT NULL,
progress BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_received) REFERENCES received (id) ON DELETE CASCADE ON UPDATE CASCADE FOREIGN KEY (id_received) REFERENCES received (id) ON DELETE CASCADE ON UPDATE CASCADE
); );
@ -143,16 +179,20 @@ CREATE TABLE IF NOT EXISTS transfer
CREATE TABLE IF NOT EXISTS smsstop CREATE TABLE IF NOT EXISTS smsstop
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
number VARCHAR(20) NOT NULL, number VARCHAR(20) NOT NULL,
PRIMARY KEY (id), PRIMARY KEY (id),
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
UNIQUE (number) UNIQUE (number)
); );
CREATE TABLE IF NOT EXISTS webhook CREATE TABLE IF NOT EXISTS webhook
( (
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
id_user INT NOT NULL,
url VARCHAR(250) NOT NULL, url VARCHAR(250) NOT NULL,
type INT NOT NULL, type INT NOT NULL,
FOREIGN KEY (id_user) REFERENCES user (id) ON DELETE CASCADE ON UPDATE CASCADE,
PRIMARY KEY (id) PRIMARY KEY (id)
); );
@ -161,16 +201,5 @@ CREATE TABLE IF NOT EXISTS webhook_querie
id INT NOT NULL AUTO_INCREMENT, id INT NOT NULL AUTO_INCREMENT,
url VARCHAR(250) NOT NULL, url VARCHAR(250) NOT NULL,
datas VARCHAR(10000) NOT NULL, datas VARCHAR(10000) NOT NULL,
progress BOOLEAN NOT NULL DEFAULT FALSE,
PRIMARY KEY (id) PRIMARY KEY (id)
); );
#On insert les données par défaut dans les settings
INSERT INTO setting (name, value)
VALUES ('transfer', '1'),
('smsstops', '1'),
('detect_url', '1'),
('default_phone_country', 'fr'),
('preferred_phone_country', 'fr,be,ca'),
('sms_flash', '0'),
('sms_reception_sound', '1');