From bf35609b6434796b62006b79130f4d316d9b67ce Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 14:51:14 +0100 Subject: [PATCH 01/44] drone --- .drone.yml | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..e150d4d --- /dev/null +++ b/.drone.yml @@ -0,0 +1,57 @@ +kind: pipeline +type: docker +name: smartfit + +trigger: + event: + - push + +steps: + - name: build_api_image + image: plugins/docker + settings: + dockerfile: Dockerfile + context: . + registry: hub.codefirst.iut.uca.fr + repo: hub.codefirst.iut.uca.fr/remi.arnal/smartfit_api + username: + from_secret: cf_username + password: + from_secret: cf_password + + - name: deploy_api_image + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest + environment: + IMAGENAME: hub.codefirst.iut.uca.fr/remi.arnal/smartfit_api:latest + CONTAINERNAME: smartfit_api + COMMAND: create + OVERWRITE: true + CODEFIRST_CLIENTDRONE_ENV_HOST: SmartFit-smartfit_db + CODEFIRST_CLIENTDRONE_ENV_DATABASE: + from_secret: db_database + CODEFIRST_CLIENTDRONE_ENV_USER: + from_secret: db_user + CODEFIRST_CLIENTDRONE_ENV_PASSWORD: + from_secret: db_password + CODEFIRST_CLIENTDRONE_ENV_ROOT_PASSWORD: + from_secret: db_root_password + ADMINS: remiarnal,enzojolys,othmanebenjelloun + depends_on: [ build_api_image ] + + - name: deploy_database_image + image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest + environment: + IMAGENAME: mariadb:10.5 + CONTAINERNAME: smartfit_db + PRIVATE : true + COMMAND: create + CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: + from_secret: db_root_password + CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE: + from_secret: db_database + CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER: + from_secret: db_user + CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: + from_secret: db_password + ADMINS: remiarnal,enzojolys,othmanebenjelloun + depends_on: [ deploy_api_image ] \ No newline at end of file -- 2.36.3 From 8c418a7383f61be91d9d5460293c03a6581e3709 Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 15:03:42 +0100 Subject: [PATCH 02/44] add dockerfile --- Dockerfile | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8466c9b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM php:8.1-apache +RUN apt-get update && apt-get install -y git zip +RUN docker-php-ext-install pdo pdo_mysql +COPY ./public /var/www/html +COPY ./src ./app /var/www/ +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer +RUN composer update && composer install +RUN a2enmod rewrite +RUN a2enmod actions +RUN service restart apache2 \ No newline at end of file -- 2.36.3 From 736740a87dc1a8d1a39b48d89ff2522a2f77c325 Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 15:05:56 +0100 Subject: [PATCH 03/44] update Dockerfile --- Dockerfile | 3 ++- SMDB_ENV.sh | 6 ++++++ init.sql | 35 +++++++++++++++++++++++++++++++++++ sym_keyfile.key | 1 + 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100755 SMDB_ENV.sh create mode 100644 init.sql create mode 100644 sym_keyfile.key diff --git a/Dockerfile b/Dockerfile index 8466c9b..d68297f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,7 +2,8 @@ FROM php:8.1-apache RUN apt-get update && apt-get install -y git zip RUN docker-php-ext-install pdo pdo_mysql COPY ./public /var/www/html -COPY ./src ./app /var/www/ +COPY ./src ./app composer.json /var/www/ +WORKDIR /var/www RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install RUN a2enmod rewrite diff --git a/SMDB_ENV.sh b/SMDB_ENV.sh new file mode 100755 index 0000000..ab71d9f --- /dev/null +++ b/SMDB_ENV.sh @@ -0,0 +1,6 @@ +#!/bin/sh + +export SMDB_HOST=localhost +export SMDB_DATABASE=smartfit +export SMDB_USER=manager +export SMDB_PASSWORD=manager diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..ab4fcd5 --- /dev/null +++ b/init.sql @@ -0,0 +1,35 @@ +CREATE TABLE user ( + `id` UUID PRIMARY KEY, + email VARCHAR(100) UNIQUE, + hash VARCHAR(255), + username VARCHAR(20) DEFAULT 'Change Me!', + creation_date DATE +); + +CREATE TABLE file ( + id UUID PRIMARY KEY, + `user_id` UUID REFERENCES `user`(`id`) ON DELETE CASCADE, + filename VARCHAR(100) DEFAULT CURDATE(), + import_date DATE, +); + +-- CREATE USER +INSERT INTO user VALUES (UUID(), MAIL, HASH, USERNAME, CURDATE()); + +-- DELETE USER +DELETE FROM user WHERE id=USER_ID; + +-- GET FILE LIST +SELECT id, import_date, title FROM file WHERE user_id=USER_ID; + +-- UPLOAD FILE +INSERT INTO file VALUES (UUID(), USER_ID, TITLE, CURDATE(), DATA); + +-- DELETE FILE +DELETE FROM file WHERE id=ID and USER_ID=USER_ID; + +-- UPDATE MAIL +UPDATE user SET mail=MAIL WHERE id=ID; + +-- UPDATE USERNAME +UPDATE user SET username=USERNAME WHERE id=ID; \ No newline at end of file diff --git a/sym_keyfile.key b/sym_keyfile.key new file mode 100644 index 0000000..b5163f4 --- /dev/null +++ b/sym_keyfile.key @@ -0,0 +1 @@ +k,'wȏUv:uvT"z^& Date: Thu, 9 Nov 2023 15:08:34 +0100 Subject: [PATCH 04/44] update Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d68297f..0c08cea 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions -RUN service restart apache2 \ No newline at end of file +RUN service apache2 restart \ No newline at end of file -- 2.36.3 From eed0e387afa6f22fa62558083f5f8b7543655d15 Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 15:11:54 +0100 Subject: [PATCH 05/44] update Dockerfile --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 0c08cea..7600f2c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,11 @@ FROM php:8.1-apache RUN apt-get update && apt-get install -y git zip RUN docker-php-ext-install pdo pdo_mysql -COPY ./public /var/www/html +COPY ./public/index.php /var/www/html COPY ./src ./app composer.json /var/www/ WORKDIR /var/www RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions -RUN service apache2 restart \ No newline at end of file +RUN service restart apache2 \ No newline at end of file -- 2.36.3 From 3a181e3c375ec4f5b195c0fc4bcc86321b8fe6b2 Mon Sep 17 00:00:00 2001 From: rem Date: Thu, 9 Nov 2023 15:13:25 +0100 Subject: [PATCH 06/44] Update 'Dockerfile' --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 7600f2c..f41f4a8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions -RUN service restart apache2 \ No newline at end of file +RUN service apache2 restart \ No newline at end of file -- 2.36.3 From acd877f5688baf8a98e2ce03a9f7b78337eac9ee Mon Sep 17 00:00:00 2001 From: rem Date: Thu, 9 Nov 2023 15:16:55 +0100 Subject: [PATCH 07/44] Update 'Dockerfile' --- Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index f41f4a8..724e561 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,7 @@ FROM php:8.1-apache RUN apt-get update && apt-get install -y git zip RUN docker-php-ext-install pdo pdo_mysql -COPY ./public/index.php /var/www/html -COPY ./src ./app composer.json /var/www/ +COPY . /var/www/html WORKDIR /var/www RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install -- 2.36.3 From 13f3f09535c16956661087c2020c63ad371faec6 Mon Sep 17 00:00:00 2001 From: rem Date: Thu, 9 Nov 2023 15:20:59 +0100 Subject: [PATCH 08/44] Update 'Dockerfile' --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 724e561..d986628 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM php:8.1-apache RUN apt-get update && apt-get install -y git zip RUN docker-php-ext-install pdo pdo_mysql -COPY . /var/www/html -WORKDIR /var/www +COPY . /var/www/ +WORKDIR /var/www/ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install RUN a2enmod rewrite -- 2.36.3 From 5880a5563e6acf908998e894006653775714c7d3 Mon Sep 17 00:00:00 2001 From: rem Date: Thu, 9 Nov 2023 15:22:29 +0100 Subject: [PATCH 09/44] Update 'Dockerfile' --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d986628..d4dd83a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,8 @@ FROM php:8.1-apache RUN apt-get update && apt-get install -y git zip RUN docker-php-ext-install pdo pdo_mysql -COPY . /var/www/ -WORKDIR /var/www/ +COPY . /var/www/html/ +WORKDIR /var/www/html/ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install RUN a2enmod rewrite -- 2.36.3 From 4e2bb084e2963c349045b56321a6e7dc47acda4c Mon Sep 17 00:00:00 2001 From: rem Date: Thu, 9 Nov 2023 15:29:33 +0100 Subject: [PATCH 10/44] Update '.drone.yml' --- .drone.yml | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/.drone.yml b/.drone.yml index e150d4d..b45f552 100644 --- a/.drone.yml +++ b/.drone.yml @@ -26,12 +26,12 @@ steps: CONTAINERNAME: smartfit_api COMMAND: create OVERWRITE: true - CODEFIRST_CLIENTDRONE_ENV_HOST: SmartFit-smartfit_db - CODEFIRST_CLIENTDRONE_ENV_DATABASE: + CODEFIRST_CLIENTDRONE_ENV_SMDB_HOST: SmartFit-smartfit_db + CODEFIRST_CLIENTDRONE_ENV_SMDB_DATABASE: from_secret: db_database - CODEFIRST_CLIENTDRONE_ENV_USER: + CODEFIRST_CLIENTDRONE_ENV_SMDB_USER: from_secret: db_user - CODEFIRST_CLIENTDRONE_ENV_PASSWORD: + CODEFIRST_CLIENTDRONE_ENV_SMDB_PASSWORD: from_secret: db_password CODEFIRST_CLIENTDRONE_ENV_ROOT_PASSWORD: from_secret: db_root_password @@ -47,11 +47,11 @@ steps: COMMAND: create CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: from_secret: db_root_password - CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE: + CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_DATABASE: from_secret: db_database - CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER: + CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_USER: from_secret: db_user - CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: + CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_PASSWORD: from_secret: db_password ADMINS: remiarnal,enzojolys,othmanebenjelloun depends_on: [ deploy_api_image ] \ No newline at end of file -- 2.36.3 From 7254025426f32ec2a6906464d3c8dbef01145386 Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 16:21:11 +0100 Subject: [PATCH 11/44] update status code + db init --- app/database_init.php | 42 ++++++++++++++++++++++++++++++++++++++++++ app/routes.php | 2 +- public/index.php | 7 +++++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 app/database_init.php diff --git a/app/database_init.php b/app/database_init.php new file mode 100644 index 0000000..b2437e2 --- /dev/null +++ b/app/database_init.php @@ -0,0 +1,42 @@ +con = (new DatabaseCon)->connect(); + } catch(PDOException $e) { + throw new PDOException($e->getMessage(), $e->getCode(), $e); + $this->createUserTable(); + $this->createFileTable(); + putenv("IS_DB_INIT=true"); + } + } + + private function createUserTable() { + $query = 'CREATE TABLE user ( + `id` UUID PRIMARY KEY, + email VARCHAR(100) UNIQUE, + hash VARCHAR(255), + username VARCHAR(20) DEFAULT \'Change Me!\', + creation_date DATE);'; + + $this->con->executeQuery($query); + } + + private function createFileTable() { + $query = 'CREATE TABLE file ( + id UUID PRIMARY KEY, + `user_id` UUID REFERENCES `user`(`id`) ON DELETE CASCADE, + filename VARCHAR(100) DEFAULT CURDATE(), + import_date DATE);'; + + $this->con->executeQuery($query); + } +} diff --git a/app/routes.php b/app/routes.php index d43f3f0..ffdcc49 100644 --- a/app/routes.php +++ b/app/routes.php @@ -27,7 +27,7 @@ return function (App $app) { return $res->withStatus(400); } $code = (new UserGateway)->createUser($req_body['email'], $req_body['hash'], $req_body['username']); - if($code === -1) return $res->withStatus(409); + if($code === -1) return $res->withStatus(500); $res->getBody()->write(json_encode($code)); return $res; diff --git a/public/index.php b/public/index.php index bc583c8..c789dc8 100644 --- a/public/index.php +++ b/public/index.php @@ -9,6 +9,10 @@ use App\Application\Settings\SettingsInterface; use DI\ContainerBuilder; use Slim\Factory\AppFactory; use Slim\Factory\ServerRequestCreatorFactory; +use Config\DatabaseInit; +require '../app/connection.php'; +require '../app/database_con.php'; +require '../app/database_init.php'; require __DIR__ . '/../vendor/autoload.php'; @@ -76,6 +80,9 @@ $app->addBodyParsingMiddleware(); $errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, $logError, $logErrorDetails); $errorMiddleware->setDefaultErrorHandler($errorHandler); +// Create DB +(new DatabaseInit); + // Run App & Emit Response $response = $app->handle($request); $responseEmitter = new ResponseEmitter(); -- 2.36.3 From 7f8ab101fd67eb16b6f2877ae2f5d140fdac9c28 Mon Sep 17 00:00:00 2001 From: RemRem Date: Thu, 9 Nov 2023 16:24:42 +0100 Subject: [PATCH 12/44] update --- public/index.php | 1 - 1 file changed, 1 deletion(-) diff --git a/public/index.php b/public/index.php index c789dc8..e385a8a 100644 --- a/public/index.php +++ b/public/index.php @@ -11,7 +11,6 @@ use Slim\Factory\AppFactory; use Slim\Factory\ServerRequestCreatorFactory; use Config\DatabaseInit; require '../app/connection.php'; -require '../app/database_con.php'; require '../app/database_init.php'; require __DIR__ . '/../vendor/autoload.php'; -- 2.36.3 From 6b3109c5d3b822fa117c1c06cb42e57f67852619 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 08:16:33 +0100 Subject: [PATCH 13/44] update --- app/database_init.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/database_init.php b/app/database_init.php index b2437e2..6f65202 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -9,10 +9,10 @@ class DatabaseInit { public function __construct() { if(getenv("IS_DB_INIT") != true) {} - try { + #try { $this->con = (new DatabaseCon)->connect(); - } catch(PDOException $e) { - throw new PDOException($e->getMessage(), $e->getCode(), $e); + #} catch(PDOException $e) { + # throw new PDOException($e->getMessage(), $e->getCode(), $e); $this->createUserTable(); $this->createFileTable(); putenv("IS_DB_INIT=true"); -- 2.36.3 From 4923d205d18c2e32e7f25ecbdc0293ee42994d02 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 08:18:41 +0100 Subject: [PATCH 14/44] upd --- app/database_init.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/database_init.php b/app/database_init.php index 6f65202..c88449a 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -8,7 +8,7 @@ class DatabaseInit { private Connection $con; public function __construct() { - if(getenv("IS_DB_INIT") != true) {} + if(getenv("IS_DB_INIT") != true) { #try { $this->con = (new DatabaseCon)->connect(); #} catch(PDOException $e) { -- 2.36.3 From a42e635ead3c569bdcdb2bb9cbf4c5acd74ba2fa Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 08:22:26 +0100 Subject: [PATCH 15/44] upd --- app/database_init.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/database_init.php b/app/database_init.php index c88449a..0ded011 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -21,7 +21,7 @@ class DatabaseInit { private function createUserTable() { $query = 'CREATE TABLE user ( - `id` UUID PRIMARY KEY, + id UUID PRIMARY KEY, email VARCHAR(100) UNIQUE, hash VARCHAR(255), username VARCHAR(20) DEFAULT \'Change Me!\', @@ -33,7 +33,7 @@ class DatabaseInit { private function createFileTable() { $query = 'CREATE TABLE file ( id UUID PRIMARY KEY, - `user_id` UUID REFERENCES `user`(`id`) ON DELETE CASCADE, + user_id UUID REFERENCES `user`(`id`) ON DELETE CASCADE, filename VARCHAR(100) DEFAULT CURDATE(), import_date DATE);'; -- 2.36.3 From 11a2bc6dfb99d767747baac4593be592311718af Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 08:30:19 +0100 Subject: [PATCH 16/44] upd --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index b45f552..d910dbd 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,7 +41,7 @@ steps: - name: deploy_database_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: - IMAGENAME: mariadb:10.5 + IMAGENAME: mariadb:11.1.2 CONTAINERNAME: smartfit_db PRIVATE : true COMMAND: create -- 2.36.3 From ddb55316e4b94e14a08024e72937af25189fb9d5 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 08:32:11 +0100 Subject: [PATCH 17/44] upd --- .drone.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.drone.yml b/.drone.yml index d910dbd..2738a3e 100644 --- a/.drone.yml +++ b/.drone.yml @@ -45,6 +45,7 @@ steps: CONTAINERNAME: smartfit_db PRIVATE : true COMMAND: create + OVERWRITE: true CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: from_secret: db_root_password CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_DATABASE: -- 2.36.3 From f594d64d2b9836b5fdacaa39302dd076737e90d4 Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 10 Nov 2023 08:40:39 +0100 Subject: [PATCH 18/44] Update '.drone.yml' --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 2738a3e..ea82c9c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,7 +41,7 @@ steps: - name: deploy_database_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: - IMAGENAME: mariadb:11.1.2 + IMAGENAME: mariadb:10.9 CONTAINERNAME: smartfit_db PRIVATE : true COMMAND: create -- 2.36.3 From a37f65d79f83fa624e817eb876d62b4de5d90e9d Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 10 Nov 2023 08:47:33 +0100 Subject: [PATCH 19/44] Update '.drone.yml' --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index ea82c9c..eb8af6c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,7 +41,7 @@ steps: - name: deploy_database_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: - IMAGENAME: mariadb:10.9 + IMAGENAME: mariadb:10.7 CONTAINERNAME: smartfit_db PRIVATE : true COMMAND: create -- 2.36.3 From 4927ec3605fbc82b3c0d236ff0a4c7253250892c Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 10 Nov 2023 08:50:27 +0100 Subject: [PATCH 20/44] Update '.drone.yml' --- .drone.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.drone.yml b/.drone.yml index eb8af6c..294cd6c 100644 --- a/.drone.yml +++ b/.drone.yml @@ -41,18 +41,18 @@ steps: - name: deploy_database_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: - IMAGENAME: mariadb:10.7 + IMAGENAME: mariadb:11.1.2 CONTAINERNAME: smartfit_db PRIVATE : true COMMAND: create OVERWRITE: true CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: from_secret: db_root_password - CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_DATABASE: + CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE: from_secret: db_database - CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_USER: + CODEFIRST_CLIENTDRONE_ENV_MARIADB_USER: from_secret: db_user - CODEFIRST_CLIENTDRONE_ENV_MARIADB_SMDB_PASSWORD: + CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: from_secret: db_password ADMINS: remiarnal,enzojolys,othmanebenjelloun depends_on: [ deploy_api_image ] \ No newline at end of file -- 2.36.3 From e1740a4051ec9f69a7d6ef0fe1157455a0aa9eb0 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 08:57:37 +0100 Subject: [PATCH 21/44] upd --- app/database_init.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/database_init.php b/app/database_init.php index 0ded011..d0deded 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -8,7 +8,7 @@ class DatabaseInit { private Connection $con; public function __construct() { - if(getenv("IS_DB_INIT") != true) { + if(getenv("IS_DB_INIT") === false) { #try { $this->con = (new DatabaseCon)->connect(); #} catch(PDOException $e) { -- 2.36.3 From c62523d5f0baa11293935d36fce6c3ca4ef099e4 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 09:03:25 +0100 Subject: [PATCH 22/44] upd --- app/database_init.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/database_init.php b/app/database_init.php index d0deded..b3e152e 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -20,7 +20,7 @@ class DatabaseInit { } private function createUserTable() { - $query = 'CREATE TABLE user ( + $query = 'CREATE TABLE IF NOT EXISTS user ( id UUID PRIMARY KEY, email VARCHAR(100) UNIQUE, hash VARCHAR(255), @@ -31,7 +31,7 @@ class DatabaseInit { } private function createFileTable() { - $query = 'CREATE TABLE file ( + $query = 'CREATE TABLE IF NOT EXISTS file ( id UUID PRIMARY KEY, user_id UUID REFERENCES `user`(`id`) ON DELETE CASCADE, filename VARCHAR(100) DEFAULT CURDATE(), -- 2.36.3 From b3ea383379b1028c50cb9177e957de0905c052dc Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 09:07:10 +0100 Subject: [PATCH 23/44] upd --- app/routes.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/routes.php b/app/routes.php index ffdcc49..3c46c40 100644 --- a/app/routes.php +++ b/app/routes.php @@ -5,6 +5,8 @@ require "gateway/file_gateway.php"; require "database_con.php"; require "token.php"; +header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); + use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\App; -- 2.36.3 From 64786e316d99df3bad2d1fa4f41ceebfc72b66c7 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 09:28:54 +0100 Subject: [PATCH 24/44] statusCode + Dockerfile --- Dockerfile | 4 +++- app/routes.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index d4dd83a..42324a5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,6 @@ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions -RUN service apache2 restart \ No newline at end of file +RUN service apache2 restart +RUN mkdir /home/hel/smartfit_hdd +RUN chmod 755 /home/hel/smartfit_hdd \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 3c46c40..56cd894 100644 --- a/app/routes.php +++ b/app/routes.php @@ -29,7 +29,7 @@ return function (App $app) { return $res->withStatus(400); } $code = (new UserGateway)->createUser($req_body['email'], $req_body['hash'], $req_body['username']); - if($code === -1) return $res->withStatus(500); + if($code === -1) return $res->withStatus(409); $res->getBody()->write(json_encode($code)); return $res; -- 2.36.3 From c471ad31983cfa345ca89f2d9dce3ac58abc8ccc Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 09:30:53 +0100 Subject: [PATCH 25/44] docker upd --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 42324a5..e1fc567 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,5 @@ RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions RUN service apache2 restart -RUN mkdir /home/hel/smartfit_hdd +RUN mkdir -p /home/hel/smartfit_hdd RUN chmod 755 /home/hel/smartfit_hdd \ No newline at end of file -- 2.36.3 From 4b58c122119f4094bd8c544bb18d31e0b8c98dae Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 09:36:09 +0100 Subject: [PATCH 26/44] upd --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index e1fc567..5657bfe 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,5 @@ RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions RUN service apache2 restart -RUN mkdir -p /home/hel/smartfit_hdd -RUN chmod 755 /home/hel/smartfit_hdd \ No newline at end of file +RUN mkdir /home/hel/smartfit_hdd +RUN chmod -R 755 /home/hel/smartfit_hdd \ No newline at end of file -- 2.36.3 From ffbf2a9262b8d66d196c56383d37a34e17565fad Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 09:37:50 +0100 Subject: [PATCH 27/44] upd --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 5657bfe..8747799 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,5 +8,5 @@ RUN composer update && composer install RUN a2enmod rewrite RUN a2enmod actions RUN service apache2 restart -RUN mkdir /home/hel/smartfit_hdd +RUN mkdir -p /home/hel/smartfit_hdd RUN chmod -R 755 /home/hel/smartfit_hdd \ No newline at end of file -- 2.36.3 From a4099d65ec47080f13d5fa5e37802d34052d42fa Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 10 Nov 2023 09:55:40 +0100 Subject: [PATCH 28/44] Update 'Dockerfile' --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 8747799..3114405 100644 --- a/Dockerfile +++ b/Dockerfile @@ -9,4 +9,4 @@ RUN a2enmod rewrite RUN a2enmod actions RUN service apache2 restart RUN mkdir -p /home/hel/smartfit_hdd -RUN chmod -R 755 /home/hel/smartfit_hdd \ No newline at end of file +RUN chmod -R 777 /home/hel/smartfit_hdd \ No newline at end of file -- 2.36.3 From 0198ba37969b87a928c8af7785725f16fb8fcba0 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 10:55:00 +0100 Subject: [PATCH 29/44] update dockerfile php.ini conf --- Dockerfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Dockerfile b/Dockerfile index 8747799..fbc7f80 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,8 @@ COPY . /var/www/html/ WORKDIR /var/www/html/ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install +RUN sed -i '/upload_max_filesize/s/.*/upload_max_filesize\ = 64M/g' /etc/php/php.ini +RUN sed -i '/post_max_size/s/.*/post_max_size \= 64M/g' /etc/php/php.ini RUN a2enmod rewrite RUN a2enmod actions RUN service apache2 restart -- 2.36.3 From 0ba01e2400254bd28067d6b6402b9ae685da1fd6 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 11:03:18 +0100 Subject: [PATCH 30/44] upd php.ini --- Dockerfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Dockerfile b/Dockerfile index 4183d36..5e0e389 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,6 +5,7 @@ COPY . /var/www/html/ WORKDIR /var/www/html/ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install +RUN php --ini RUN sed -i '/upload_max_filesize/s/.*/upload_max_filesize\ = 64M/g' /etc/php/php.ini RUN sed -i '/post_max_size/s/.*/post_max_size \= 64M/g' /etc/php/php.ini RUN a2enmod rewrite -- 2.36.3 From 1024741b247c629d607ad9d7ac7463e2d724d38e Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 11:08:35 +0100 Subject: [PATCH 31/44] add uploads.ini in dockerfile --- Dockerfile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 5e0e389..a08d4ed 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,9 +5,7 @@ COPY . /var/www/html/ WORKDIR /var/www/html/ RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer RUN composer update && composer install -RUN php --ini -RUN sed -i '/upload_max_filesize/s/.*/upload_max_filesize\ = 64M/g' /etc/php/php.ini -RUN sed -i '/post_max_size/s/.*/post_max_size \= 64M/g' /etc/php/php.ini +RUN echo "file_uploads = On\nmemory_limit = 64M\nupload_max_filesize = 64M\npost_max_size = 64M\nmax_execution_time = 600\n" > /usr/local/etc/php/conf.d/uploads.ini RUN a2enmod rewrite RUN a2enmod actions RUN service apache2 restart -- 2.36.3 From 13e897e8aecfd06bacbdc8492ec6fc981dabcd62 Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 10 Nov 2023 11:15:28 +0100 Subject: [PATCH 32/44] Update '.drone.yml' --- .drone.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index 294cd6c..801c14f 100644 --- a/.drone.yml +++ b/.drone.yml @@ -45,7 +45,6 @@ steps: CONTAINERNAME: smartfit_db PRIVATE : true COMMAND: create - OVERWRITE: true CODEFIRST_CLIENTDRONE_ENV_MARIADB_ROOT_PASSWORD: from_secret: db_root_password CODEFIRST_CLIENTDRONE_ENV_MARIADB_DATABASE: -- 2.36.3 From 1680f84a4ea26a9611ba81c9fe398dfffb20b8d0 Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 12:01:19 +0100 Subject: [PATCH 33/44] bug delete user + /user/info --- app/gateway/user_gateway.php | 21 +++++++++++++++++++-- app/routes.php | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/app/gateway/user_gateway.php b/app/gateway/user_gateway.php index 807e8e5..f8d4303 100644 --- a/app/gateway/user_gateway.php +++ b/app/gateway/user_gateway.php @@ -39,14 +39,16 @@ class UserGateway { // Delete User: (1:OK, 2:Unauthorize, 3:No User) public function deleteUser(string $uuid) : int { - $query = "DELETE FROM user WHERE id=:uuid;"; + $query = "DELETE FROM user WHERE id=:uuid RETURNING row_count();"; try { $this->con->executeQuery($query, array( ':uuid' => array($uuid, PDO::PARAM_STR) )); + $results = $this->con->getResults(); } catch (PDOException $e) { - return -1; + return -2; } + if(count($results) === 0) return -1; return 0; } @@ -69,6 +71,21 @@ class UserGateway { return json_encode($this->token->getNewJsonToken($results[0]['id'])); } + public function getInfo(string $uuid) { + $query = "SELECT email, username FROM user WHERE id=:uuid;"; + try { + $this->con->executeQuery($query,array( + ':uuid' => array($uuid, PDO::PARAM_STR) + )); + $results = $this->con->getResults(); + } catch(PDOException $e) { + return -2; + } + if(count($results) === 0) return -1; + + return ["email" => $results[0]['email'], "username" => $results[0]['username']]; + } + public function updateMail(string $uuid, string $new_email) { $query = "UPDATE user SET email=:new_email WHERE id=:uuid;"; try { diff --git a/app/routes.php b/app/routes.php index 56cd894..8a20fe4 100644 --- a/app/routes.php +++ b/app/routes.php @@ -50,6 +50,8 @@ return function (App $app) { return $res->withStatus(200); case -1: return $res->withStatus(404); + case -2: + return $res->withStatus(500); } return $res->withStatus(500); }); @@ -73,6 +75,25 @@ return function (App $app) { return $res; }); + $app->get('/user/info', function(Request $req, Response $res) { + $token = $req->getHeader('Authorization')[0]; + if(!(new Token)->verifyToken($token)) { + return $res->withStatus(401); + } + + $uuid = (new Token)->getUuidFromToken($token); + $code = (new UserGateway)->getInfo($uuid); + switch($code) { + case -1: + return $res->withStatus(404); + case -2: + return $res->withStatus(500); + } + + $res->getBody()->write(json_encode($code)); + return $res; + }); + // Update Mail $app->put('/user/email', function(Request $req, Response $res) { $token = $req->getHeader('Authorization')[0]; -- 2.36.3 From da80531edd0a0853122adecb2ed950fd8ee8ddd1 Mon Sep 17 00:00:00 2001 From: rem Date: Fri, 10 Nov 2023 15:10:34 +0100 Subject: [PATCH 34/44] Update '.drone.yml' --- .drone.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.drone.yml b/.drone.yml index 801c14f..3b21f69 100644 --- a/.drone.yml +++ b/.drone.yml @@ -19,6 +19,19 @@ steps: password: from_secret: cf_password + - name: sonarqube + image: sonarsource/sonar-scanner-cli + environment: + SONAR_TOKEN: + from_secret: sonar_token + commands: + - sonar-scanner -Dsonar.projectKey=SmartFit_API + -Dsonar.sources=./app/ + -Dsonar.inclusions=**/*.php + -Dsonar.login=$${SONAR_TOKEN} + -Dsonar.language=php + -Dsonar.host.url=https://codefirst.iut.uca.fr/sonar + - name: deploy_api_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: -- 2.36.3 From 7c483c3338bdc277edd0bd65865479991e3a1cfc Mon Sep 17 00:00:00 2001 From: RemRem Date: Fri, 10 Nov 2023 16:07:19 +0100 Subject: [PATCH 35/44] :bug: sonarqube --- app/database_con.php | 4 ++-- app/routes.php | 8 ++++---- app/token.php | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/database_con.php b/app/database_con.php index fe11c14..d609295 100644 --- a/app/database_con.php +++ b/app/database_con.php @@ -8,7 +8,7 @@ class DatabaseCon{ private string $login; private string $password; - function __construct(){ + public function __construct(){ if (getenv("SMDB_HOST") == null || getenv("SMDB_DATABASE") == null || getenv("SMDB_USER") == null || getenv("SMDB_PASSWORD") == null){ throw new PDOException("ENV variables not found"); } @@ -17,7 +17,7 @@ class DatabaseCon{ $this->password = getenv("SMDB_PASSWORD"); } - function connect(): int|Connection { + public function connect(): int|Connection { try { $connection = new Connection($this->dsn,$this->login,$this->password); } catch (PDOException $e){ diff --git a/app/routes.php b/app/routes.php index 8a20fe4..9e8593f 100644 --- a/app/routes.php +++ b/app/routes.php @@ -1,9 +1,9 @@ path_to_key, 'r'); #$this->key = fread($file, filesize($this->path_to_key)); @@ -18,7 +18,7 @@ class Token { } // Return json containing JWT with uuid and exp - function getNewJsonToken(string $uuid) :array { + public function getNewJsonToken(string $uuid) :array { $payload = [ 'uuid' => $uuid, 'exp' => strtotime("+2month", time()) @@ -28,7 +28,7 @@ class Token { } // Verify the JWT authenticity - function verifyToken(string $jwt) :bool { + public function verifyToken(string $jwt) :bool { try { JWT::decode($jwt, new Key($this->key, 'HS256')); } catch (Exception $e) { @@ -39,7 +39,7 @@ class Token { // Get uuid from JWT // Missing error handling on bad JWT - function getUuidFromToken(string $jwt) :string { + public function getUuidFromToken(string $jwt) :string { $decoded = (array) JWT::decode($jwt, new Key($this->key, 'HS256')); return $decoded['uuid']; } -- 2.36.3 From a77244797aff796179151d8f0a096b2b38b67ea6 Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 15:11:35 +0100 Subject: [PATCH 36/44] add CORS headers --- app/routes.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/routes.php b/app/routes.php index 9e8593f..1329cdc 100644 --- a/app/routes.php +++ b/app/routes.php @@ -5,6 +5,8 @@ require_once "gateway/file_gateway.php"; require_once "database_con.php"; require_once "token.php"; +header("Access-Control-Allow-Origin: *"); +header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); use Psr\Http\Message\ResponseInterface as Response; @@ -224,4 +226,4 @@ return function (App $app) { if($code === -1) return $res->withStatus(500); return $res->withStatus(200); }); -}; \ No newline at end of file +}; -- 2.36.3 From 1f3ea79bdc1ebbb80f6d02049137f4e2846f5b10 Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 15:16:57 +0100 Subject: [PATCH 37/44] no idea --- app/routes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.php b/app/routes.php index 1329cdc..2aeb554 100644 --- a/app/routes.php +++ b/app/routes.php @@ -6,7 +6,7 @@ require_once "database_con.php"; require_once "token.php"; header("Access-Control-Allow-Origin: *"); -header("Access-Control-Allow-Headers: Authorization, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Allow-Origin"); +header("Access-Control-Allow-Headers: *"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); use Psr\Http\Message\ResponseInterface as Response; -- 2.36.3 From 1f31ec3620c0c7f4ec05e51a9bbe65a39981b776 Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 15:59:47 +0100 Subject: [PATCH 38/44] allow credentials for CORS --- app/routes.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/routes.php b/app/routes.php index 2aeb554..1a0f5ae 100644 --- a/app/routes.php +++ b/app/routes.php @@ -8,6 +8,7 @@ require_once "token.php"; header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Headers: *"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); +header("Access-Control-Allow-Credentials: true"); use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; -- 2.36.3 From 59fb7d7e6b0c6ee0be5a1f8dbb68f79dd94beb6f Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 16:10:56 +0100 Subject: [PATCH 39/44] add allowed headers CORS --- app/routes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/routes.php b/app/routes.php index 1a0f5ae..f855de5 100644 --- a/app/routes.php +++ b/app/routes.php @@ -6,7 +6,7 @@ require_once "database_con.php"; require_once "token.php"; header("Access-Control-Allow-Origin: *"); -header("Access-Control-Allow-Headers: *"); +header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization"); header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); header("Access-Control-Allow-Credentials: true"); -- 2.36.3 From 11138c6e88d01bfac058dc22da5645396d22fefb Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 16:22:38 +0100 Subject: [PATCH 40/44] RAAAHAHAHAH --- app/routes.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/routes.php b/app/routes.php index f855de5..f46dccb 100644 --- a/app/routes.php +++ b/app/routes.php @@ -6,8 +6,8 @@ require_once "database_con.php"; require_once "token.php"; header("Access-Control-Allow-Origin: *"); -header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Accept, Origin, Authorization"); -header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE"); +header("Access-Control-Allow-Headers: Access-Control-Allow-Origin, X-Requested-With, Content-Type, Accept, Origin, Authorization"); +header("Access-Control-Allow-Methods: *"); header("Access-Control-Allow-Credentials: true"); use Psr\Http\Message\ResponseInterface as Response; -- 2.36.3 From 44291fb2219c6524d67c34c195a9a5c8b4433284 Mon Sep 17 00:00:00 2001 From: RemRem Date: Mon, 13 Nov 2023 17:23:04 +0100 Subject: [PATCH 41/44] add OPTIONS request handle --- app/routes.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/app/routes.php b/app/routes.php index f46dccb..7cfc944 100644 --- a/app/routes.php +++ b/app/routes.php @@ -13,12 +13,24 @@ header("Access-Control-Allow-Credentials: true"); use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\App; +use SLim\Exception\HttpNotFoundException; use gateway\UserGateway; use Config\Token; use Gateway\FileGateway; return function (App $app) { + $app->options('/{routes:.+}', function ($request, $response, $args) { + return $response; + }); + $app->add(function ($request, $handler) { + $response = $handler->handle($request); + return $response + ->withHeader('Access-Control-Allow-Origin', '*') + ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization') + ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); + }); + $app->get('/', function (Request $req, Response $res) { $res->getBody()->write('SmartFit-API is working!'); return $res; @@ -227,4 +239,8 @@ return function (App $app) { if($code === -1) return $res->withStatus(500); return $res->withStatus(200); }); + + $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function ($request, $response) { + throw new HttpNotFoundException($request); + }); }; -- 2.36.3 From 5e20780cf435ab037e7189dea015db8ddc925636 Mon Sep 17 00:00:00 2001 From: RemRem Date: Tue, 14 Nov 2023 11:01:19 +0100 Subject: [PATCH 42/44] bug: no-conflict on file upload name + add db category and creation_date for file + parse in upload file --- app/database_init.php | 2 ++ app/gateway/file_gateway.php | 14 +++++++++----- app/gateway/user_gateway.php | 4 +--- app/routes.php | 8 ++++++-- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/app/database_init.php b/app/database_init.php index b3e152e..eeb6c36 100644 --- a/app/database_init.php +++ b/app/database_init.php @@ -35,6 +35,8 @@ class DatabaseInit { id UUID PRIMARY KEY, user_id UUID REFERENCES `user`(`id`) ON DELETE CASCADE, filename VARCHAR(100) DEFAULT CURDATE(), + category VARCHAR(50), + creation_date DATETIME, import_date DATE);'; $this->con->executeQuery($query); diff --git a/app/gateway/file_gateway.php b/app/gateway/file_gateway.php index 8b8db99..c4d249f 100644 --- a/app/gateway/file_gateway.php +++ b/app/gateway/file_gateway.php @@ -16,12 +16,14 @@ class FileGateway { } } - public function createFile(string $filename, string $user_uuid) { - $query = "INSERT INTO file VALUES(UUID(), :user_uuid, :filename, CURDATE());"; + public function createFile(string $filename, string $user_uuid, string $category, string $creation_date) { + $query = "INSERT INTO file VALUES(UUID(), :user_uuid, :filename, :category, :creation_date ,CURDATE());"; try { $this->con->executeQuery($query, array( + ':user_uuid' => array($user_uuid, PDO::PARAM_STR), ':filename' => array($filename, PDO::PARAM_STR), - ':user_uuid' => array($user_uuid, PDO::PARAM_STR) + ':category' => array($category, PDO::PARAM_STR), + ':creation_date' => array($creation_date, PDO::PARAM_STR) )); } catch (PDOException $e) { return -1; @@ -61,7 +63,7 @@ class FileGateway { } public function listFiles(string $user_uuid) { - $query = "SELECT f.id, f.filename FROM file f, user u WHERE f.user_id=u.id and u.id=:user_uuid;"; + $query = "SELECT f.id, f.filename, f.category, f.creation_date FROM file f, user u WHERE f.user_id=u.id and u.id=:user_uuid;"; try { $this->con->executeQuery($query, array( ':user_uuid' => array($user_uuid, PDO::PARAM_STR) @@ -76,9 +78,11 @@ class FileGateway { $rows[] = [ 'uuid' => $row['id'], 'filename' => $row['filename'], + 'category' => $row['category'], + 'creation_date' => $row['creation_date'] ]; } return $rows; } -} \ No newline at end of file +} diff --git a/app/gateway/user_gateway.php b/app/gateway/user_gateway.php index f8d4303..1f75dbd 100644 --- a/app/gateway/user_gateway.php +++ b/app/gateway/user_gateway.php @@ -6,8 +6,6 @@ use PDOException; use PDO; use Config\Token; -use function PHPUnit\Framework\isEmpty; - class UserGateway { private Connection $con; private Token $token; @@ -113,4 +111,4 @@ class UserGateway { return 0; } -} \ No newline at end of file +} diff --git a/app/routes.php b/app/routes.php index 7cfc944..d5c4a4b 100644 --- a/app/routes.php +++ b/app/routes.php @@ -225,9 +225,12 @@ return function (App $app) { $uuid = (new Token)->getUuidFromToken($token); $file = $req->getUploadedFiles()['file']; + $category = $req->getParsedBody()['SmartFit_Category']; + $creation_date = $req->getParsedBody()['SmartFit_Date']; $filename = $file->getClientFilename(); + $code = (new FileGateway)->listFiles($uuid); - if(in_array($filename, $code, false)) return $res->withStatus(409); + if(array_search($filename, array_column($code, 'filename'), false) !== false) return $res->withStatus(409); $file_save_folder = $save_folder.'/'.$uuid.'/'; if(!is_dir($file_save_folder)) { @@ -235,8 +238,9 @@ return function (App $app) { } $file->moveTo($file_save_folder.'/'.$filename); - $code = (new FileGateway)->createFile($filename, $uuid); + $code = (new FileGateway)->createFile($filename, $uuid, $category, $creation_date); if($code === -1) return $res->withStatus(500); + return $res->withStatus(200); }); -- 2.36.3 From 388093f605dead76d3686732476a68442b8e806d Mon Sep 17 00:00:00 2001 From: RemRem Date: Tue, 14 Nov 2023 11:11:14 +0100 Subject: [PATCH 43/44] fix ci steps dependency --- .drone.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.drone.yml b/.drone.yml index 3b21f69..cf9e3d1 100644 --- a/.drone.yml +++ b/.drone.yml @@ -31,7 +31,8 @@ steps: -Dsonar.login=$${SONAR_TOKEN} -Dsonar.language=php -Dsonar.host.url=https://codefirst.iut.uca.fr/sonar - + depends-on: [build_api_image] + - name: deploy_api_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: @@ -67,4 +68,4 @@ steps: CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: from_secret: db_password ADMINS: remiarnal,enzojolys,othmanebenjelloun - depends_on: [ deploy_api_image ] \ No newline at end of file + depends_on: [ deploy_api_image ] -- 2.36.3 From 524dce270f06f16c53e9a50b10308a621d52794e Mon Sep 17 00:00:00 2001 From: RemRem Date: Tue, 14 Nov 2023 11:12:32 +0100 Subject: [PATCH 44/44] true fix --- .drone.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index cf9e3d1..771443b 100644 --- a/.drone.yml +++ b/.drone.yml @@ -31,7 +31,7 @@ steps: -Dsonar.login=$${SONAR_TOKEN} -Dsonar.language=php -Dsonar.host.url=https://codefirst.iut.uca.fr/sonar - depends-on: [build_api_image] + depends_on: [build_api_image] - name: deploy_api_image image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest -- 2.36.3