From 7041b32250413f5161793c2407bbc47f3c5e264d Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Mon, 6 Apr 2020 11:48:46 +0200
Subject: [PATCH] FEAT #13678 TIME 2:00 Refactoring res_attachment typist

---
 migration/20.10/2010.sql                           |  8 ++++++++
 .../controllers/AttachmentController.php           | 14 ++++++--------
 .../ReceiveMessageExchangeController.php           |  5 +++--
 src/app/resource/controllers/StoreController.php   |  4 ++--
 .../controllers/SignatureBookController.php        |  2 +-
 src/core/controllers/AuthenticationController.php  |  6 ------
 .../app/attachment/AttachmentControllerTest.php    |  2 +-
 7 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/migration/20.10/2010.sql b/migration/20.10/2010.sql
index 5c7a23834e1..7a6f90b736e 100755
--- a/migration/20.10/2010.sql
+++ b/migration/20.10/2010.sql
@@ -70,6 +70,14 @@ DO $$ BEGIN
         UPDATE baskets SET basket_clause = REGEXP_REPLACE(basket_clause, 'from res_mark_as_read WHERE user_id(\s*)=(\s*)@user', 'from res_mark_as_read WHERE user_id = @user_id', 'gmi');
     END IF;
 END$$;
+DO $$ BEGIN
+    IF (SELECT count(column_name) from information_schema.columns where table_name = 'res_attachments' and column_name = 'typist' and data_type != 'integer') THEN
+        ALTER TABLE res_attachments ADD COLUMN typist_tmp INTEGER;
+        UPDATE res_attachments set typist_tmp = (select id FROM users where users.user_id = res_attachments.typist);
+        ALTER TABLE res_attachments DROP COLUMN IF EXISTS typist;
+        ALTER TABLE res_attachments RENAME COLUMN typist_tmp TO typist;
+    END IF;
+END$$;
 
 
 /* RE CREATE VIEWS */
diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index 489217d7e7f..fa8133aa8f8 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -104,8 +104,7 @@ class AttachmentController
         if ($attachment['modificationDate'] == $attachment['creationDate']) {
             $attachment['modificationDate'] = null;
         }
-        $typist = UserModel::getByLogin(['login' => $attachment['typist'], 'select' => ['id', 'firstname', 'lastname']]);
-        $attachment['typist'] = $typist['id'];
+        $typist = UserModel::getById(['id' => $attachment['typist'], 'select' => ['firstname', 'lastname']]);
         $attachment['typistLabel'] = $typist['firstname']. ' ' .$typist['lastname'];
         $attachment['modifiedBy'] = UserModel::getLabelledUserById(['id' => $attachment['modifiedBy']]);
 
@@ -118,8 +117,8 @@ class AttachmentController
         if (!empty($attachment['originId'])) {
             $oldVersions = AttachmentModel::get([
                 'select'    => ['res_id as "resId"', 'relation'],
-                'where'     => ['(origin_id = ? OR res_id = ?)', 'res_id != ?', 'status not in (?)', 'attachment_type not in (?)'],
-                'data'      => [$attachment['originId'], $attachment['originId'], $args['id'], ['DEL'], $excludeAttachmentTypes],
+                'where'     => ['(origin_id = ? OR res_id = ?)', 'res_id != ?', 'status not in (?)'],
+                'data'      => [$attachment['originId'], $attachment['originId'], $args['id'], ['DEL']],
                 'orderBy'   => ['relation DESC']
             ]);
         }
@@ -150,7 +149,7 @@ class AttachmentController
         if (!ResController::hasRightByResId(['resId' => [$attachment['res_id_master']], 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(400)->withJson(['errors' => 'Attachment out of perimeter']);
         }
-        if ($GLOBALS['userId'] != $attachment['typist'] && !PrivilegeController::hasPrivilege(['privilegeId' => 'manage_attachments', 'userId' => $GLOBALS['id']])) {
+        if ($GLOBALS['id'] != $attachment['typist'] && !PrivilegeController::hasPrivilege(['privilegeId' => 'manage_attachments', 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Attachment out of perimeter']);
         }
 
@@ -231,7 +230,7 @@ class AttachmentController
         if (!ResController::hasRightByResId(['resId' => [$attachment['res_id_master']], 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
         }
-        if ($GLOBALS['userId'] != $attachment['typist'] && !PrivilegeController::hasPrivilege(['privilegeId' => 'manage_attachments', 'userId' => $GLOBALS['id']])) {
+        if ($GLOBALS['id'] != $attachment['typist'] && !PrivilegeController::hasPrivilege(['privilegeId' => 'manage_attachments', 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
@@ -294,8 +293,7 @@ class AttachmentController
             if ($attachment['modificationDate'] == $attachment['creationDate']) {
                 $attachments[$key]['modificationDate'] = null;
             }
-            $typist = UserModel::getByLogin(['login' => $attachment['typist'], 'select' => ['id', 'firstname', 'lastname']]);
-            $attachments[$key]['typist'] = $typist['id'];
+            $typist = UserModel::getById(['id' => $attachment['typist'], 'select' => ['firstname', 'lastname']]);
             $attachments[$key]['typistLabel'] = $typist['firstname']. ' ' .$typist['lastname'];
             $attachments[$key]['modifiedBy'] = UserModel::getLabelledUserById(['id' => $attachment['modifiedBy']]);
 
diff --git a/src/app/external/messageExchange/controllers/ReceiveMessageExchangeController.php b/src/app/external/messageExchange/controllers/ReceiveMessageExchangeController.php
index 536f7493873..6216942307b 100755
--- a/src/app/external/messageExchange/controllers/ReceiveMessageExchangeController.php
+++ b/src/app/external/messageExchange/controllers/ReceiveMessageExchangeController.php
@@ -383,12 +383,13 @@ class ReceiveMessageExchangeController
                 $BinaryDataObjectInfo = self::getBinaryDataObjectInfo(["binaryDataObject" => $dataObjectPackage->BinaryDataObject, "binaryDataObjectId" => $attachmentDataObjectId]);
                 $filename             = $BinaryDataObjectInfo->Attachment->filename;
                 $fileFormat           = substr($filename, strrpos($filename, '.') + 1);
-                
+
+                $user = UserModel::getByLogin(['login' => 'superadmin', 'select' => ['id']]);
                 $allDatas = [
                     'title'        => $attachmentContent->Title[0],
                     'encodedFile'  => $BinaryDataObjectInfo->Attachment->value,
                     'format'       => $fileFormat,
-                    'typist'       => 'superadmin',
+                    'typist'       => $user['id'],
                     'resIdMaster'  => $resIdMaster,
                     'type'         => $defaultConfig['attachment_type']
                 ];
diff --git a/src/app/resource/controllers/StoreController.php b/src/app/resource/controllers/StoreController.php
index c7e42b11bd2..5682d6bc612 100755
--- a/src/app/resource/controllers/StoreController.php
+++ b/src/app/resource/controllers/StoreController.php
@@ -321,8 +321,8 @@ class StoreController
             AttachmentModel::update(['set' => ['status' => 'OBS'], 'where' => ['(origin_id = ? OR res_id = ?)'], 'data' => [$args['originId'], $args['originId']]]);
             $shouldBeInSignatureBook = $relations[0]['in_signature_book'];
         }
-        $typist = $GLOBALS['userId'];
-        if (!empty($args['typist']) && !is_numeric($args['typist'])) {
+        $typist = $GLOBALS['id'];
+        if (!empty($args['typist']) && is_numeric($args['typist'])) {
             $typist = $args['typist'];
         }
 
diff --git a/src/app/signatureBook/controllers/SignatureBookController.php b/src/app/signatureBook/controllers/SignatureBookController.php
index 69f65c60ee3..efe0474c7f3 100755
--- a/src/app/signatureBook/controllers/SignatureBookController.php
+++ b/src/app/signatureBook/controllers/SignatureBookController.php
@@ -650,7 +650,7 @@ class SignatureBookController
         $attachment = AttachmentModel::getById(['id' => $args['id'], 'select' => ['res_id_master', 'typist']]);
         if (empty($attachment) || !ResController::hasRightByResId(['resId' => [$attachment['res_id_master']], 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
-        } elseif ($attachment['typist'] != $GLOBALS['userId'] && !PrivilegeController::hasPrivilege(['privilegeId' => 'sign_document', 'userId' => $GLOBALS['id']])) {
+        } elseif ($attachment['typist'] != $GLOBALS['id'] && !PrivilegeController::hasPrivilege(['privilegeId' => 'sign_document', 'userId' => $GLOBALS['id']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Privilege forbidden']);
         }
 
diff --git a/src/core/controllers/AuthenticationController.php b/src/core/controllers/AuthenticationController.php
index fa1b6df16a0..7ef2ec71895 100755
--- a/src/core/controllers/AuthenticationController.php
+++ b/src/core/controllers/AuthenticationController.php
@@ -63,12 +63,6 @@ class AuthenticationController
                 }
             }
         } else {
-            // $cookie = AuthenticationModel::getCookieAuth();
-            // if (!empty($cookie) && AuthenticationModel::cookieAuthentication($cookie)) {
-            //     AuthenticationModel::setCookieAuth(['userId' => $cookie['userId']]);
-            //     $userId = $cookie['userId'];
-            // }
-
            if (!empty($authorizationHeaders)) {
                $token = null;
                foreach ($authorizationHeaders as $authorizationHeader) {
diff --git a/test/unitTests/app/attachment/AttachmentControllerTest.php b/test/unitTests/app/attachment/AttachmentControllerTest.php
index b55ab0d490a..e590c0db9b8 100755
--- a/test/unitTests/app/attachment/AttachmentControllerTest.php
+++ b/test/unitTests/app/attachment/AttachmentControllerTest.php
@@ -61,7 +61,7 @@ class AttachmentControllerTest extends TestCase
         $this->assertSame($aArgs['type'], $res['attachment_type']);
         $this->assertSame('txt', $res['format']);
         $this->assertSame('A_TRA', $res['status']);
-        $this->assertSame('superadmin', $res['typist']);
+        $this->assertSame(24, $res['typist']);
         $this->assertSame(1, $res['relation']);
         $this->assertSame($aArgs['chrono'], $res['identifier']);
         $this->assertNotNull($res['path']);
-- 
GitLab