From dcae814fabfc6711bf0d19c6a6d4c3071c6c4707 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 11 Oct 2018 15:38:07 +0200
Subject: [PATCH] Get documents

---
 rest/index.php                                |  6 +--
 .../controllers/DocumentController.php        | 41 +++++++++++++++++++
 src/app/document/models/DocumentModel.php     | 41 +++++++++++++++++++
 src/app/user/models/UserModelAbstract.php     | 19 +++++++++
 .../controllers/AuthenticationController.php  |  8 ++--
 5 files changed, 108 insertions(+), 7 deletions(-)
 create mode 100755 src/app/document/controllers/DocumentController.php
 create mode 100644 src/app/document/models/DocumentModel.php

diff --git a/rest/index.php b/rest/index.php
index 8904de7769..c9b8871745 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -26,10 +26,10 @@ $app = new \Slim\App(['settings' => ['displayErrorDetails' => true, 'determineRo
 
 //Authentication
 $app->add(function (\Slim\Http\Request $request, \Slim\Http\Response $response, callable $next) {
-    $userId = \SrcCore\controllers\AuthenticationController::authentication();
+    $login = \SrcCore\controllers\AuthenticationController::authentication();
 
-    if (!empty($userId)) {
-        $GLOBALS['userId'] = $userId;
+    if (!empty($login)) {
+        $GLOBALS['login'] = $login;
         $response = $next($request, $response);
         return $response;
     } else {
diff --git a/src/app/document/controllers/DocumentController.php b/src/app/document/controllers/DocumentController.php
new file mode 100755
index 0000000000..f853efeba5
--- /dev/null
+++ b/src/app/document/controllers/DocumentController.php
@@ -0,0 +1,41 @@
+<?php
+
+/**
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+*
+*/
+
+/**
+* @brief Resource Controller
+* @author dev@maarch.org
+*/
+
+namespace Document\controllers;
+
+use Document\models\DocumentModel;
+use Slim\Http\Request;
+use Slim\Http\Response;
+use User\models\UserModel;
+
+class DocumentController
+{
+    public function get(Request $request, Response $response)
+    {
+        $data = $request->getQueryParams();
+
+        if (empty($data['offset']) || !is_numeric($data['offset'])) {
+            $data['offset'] = 0;
+        }
+        if (empty($data['limit']) || !is_numeric($data['limit'])) {
+            $data['limit'] = 0;
+        }
+
+        $user = UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
+
+        $documents = DocumentModel::getByUserId(['select' => ['*'], 'userId' => $user['id']]);
+
+        return $response->withJson(['documents' => $documents]);
+    }
+}
diff --git a/src/app/document/models/DocumentModel.php b/src/app/document/models/DocumentModel.php
new file mode 100644
index 0000000000..cd257bb0dc
--- /dev/null
+++ b/src/app/document/models/DocumentModel.php
@@ -0,0 +1,41 @@
+<?php
+
+/**
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+*
+*/
+
+/**
+* @brief Document Model
+* @author dev@maarch.org
+*/
+
+namespace Document\models;
+
+use SrcCore\models\ValidatorModel;
+use SrcCore\models\DatabaseModel;
+
+
+abstract class DocumentModel
+{
+    public static function getByUserId(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['select', 'userId']);
+        ValidatorModel::intVal($aArgs, ['userId']);
+        ValidatorModel::arrayType($aArgs, ['select']);
+        ValidatorModel::intType($aArgs, ['limit', 'offset']);
+
+        $aDocuments = DatabaseModel::select([
+            'select'    => $aArgs['select'],
+            'table'     => ['main_documents'],
+            'where'     => ['processing_user = ?'],
+            'data'      => [$aArgs['userId']],
+            'offset'    => empty($aArgs['offset']) ? 0 : $aArgs['offset'],
+            'limit'     => empty($aArgs['limit']) ? 0 : $aArgs['limit']
+        ]);
+
+        return $aDocuments;
+    }
+}
diff --git a/src/app/user/models/UserModelAbstract.php b/src/app/user/models/UserModelAbstract.php
index 4ecea3b8c0..bc33d7d699 100644
--- a/src/app/user/models/UserModelAbstract.php
+++ b/src/app/user/models/UserModelAbstract.php
@@ -56,6 +56,25 @@ abstract class UserModelAbstract
         return $aUser[0];
     }
 
+    public static function getByLogin(array $aArgs)
+    {
+        ValidatorModel::notEmpty($aArgs, ['login']);
+        ValidatorModel::stringType($aArgs, ['login']);
+
+        $aUser = DatabaseModel::select([
+            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
+            'table'     => ['users'],
+            'where'     => ['login = ?'],
+            'data'      => [$aArgs['login']]
+        ]);
+
+        if (empty($aUser)) {
+            return [];
+        }
+
+        return $aUser[0];
+    }
+
     public static function getByUserId(array $aArgs)
     {
         ValidatorModel::notEmpty($aArgs, ['userId']);
diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php
index e715de515b..c924d450c2 100644
--- a/src/core/controllers/AuthenticationController.php
+++ b/src/core/controllers/AuthenticationController.php
@@ -20,19 +20,19 @@ class AuthenticationController
 {
     public static function authentication()
     {
-        $userId = null;
+        $login = null;
         if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) {
             if (AuthenticationModel::authentication(['userId' => $_SERVER['PHP_AUTH_USER'], 'password' => $_SERVER['PHP_AUTH_PW']])) {
-                $userId = $_SERVER['PHP_AUTH_USER'];
+                $login = $_SERVER['PHP_AUTH_USER'];
             }
         } else {
             $cookie = AuthenticationModel::getCookieAuth();
             if (!empty($cookie) && AuthenticationModel::cookieAuthentication($cookie)) {
                 AuthenticationModel::setCookieAuth(['userId' => $cookie['userId']]);
-                $userId = $cookie['userId'];
+                $login = $cookie['userId'];
             }
         }
 
-        return $userId;
+        return $login;
     }
 }
-- 
GitLab