From 62e4affaacb398fd38089b56e1040d64c4d1d37a Mon Sep 17 00:00:00 2001
From: David Siegfried <david.siegfried@uni-vechta.de>
Date: Sat, 15 Jul 2023 13:47:37 +0000
Subject: [PATCH] add missing log-actions, closes #2865

Closes #2865

Merge request studip/studip!1945
---
 .../5.4.12_add_missing_log_actions.php        | 52 +++++++++++++++++++
 lib/models/FileRef.php                        | 12 +++++
 lib/models/Folder.php                         | 11 ++++
 lib/models/User.class.php                     | 20 +++++++
 4 files changed, 95 insertions(+)
 create mode 100644 db/migrations/5.4.12_add_missing_log_actions.php

diff --git a/db/migrations/5.4.12_add_missing_log_actions.php b/db/migrations/5.4.12_add_missing_log_actions.php
new file mode 100644
index 00000000000..0a821997df0
--- /dev/null
+++ b/db/migrations/5.4.12_add_missing_log_actions.php
@@ -0,0 +1,52 @@
+<?php
+
+final class AddMissingLogActions extends Migration
+{
+    public function description()
+    {
+        return 'add missing log-actions';
+    }
+
+    public function up()
+    {
+        DBManager::get()->exec("
+            INSERT IGNORE INTO `log_actions`
+            SET `action_id` = MD5('USER_LOCK'),
+                `name` = 'USER_LOCK',
+                `description` = 'Nutzer wird gesperrt',
+                `info_template` = '%user sperrt %user(%affected) (%info)',
+                `active` = '1',
+                `expires` = '0'
+        ");
+
+        DBManager::get()->exec("
+            INSERT IGNORE INTO `log_actions`
+            SET `action_id` = MD5('FILE_DELETE'),
+                `name` = 'FILE_DELETE',
+                `description` = 'Nutzer löscht Datei',
+                `info_template` = '%user löscht Datei %info (File-Id: %affected)',
+                `active` = '1',
+                `expires` = '0'
+        ");
+        DBManager::get()->exec("
+            INSERT IGNORE INTO `log_actions`
+            SET `action_id` = MD5('FOLDER_DELETE'),
+                `name` = 'FOLDER_DELETE',
+                `description` = 'Nutzer löscht Ordner',
+                `info_template` = '%user löscht Datei %info (Folder-Id: %affected)',
+                `active` = '1',
+                `expires` = '0'
+        ");
+    }
+
+    public function down()
+    {
+        $actions = ['USER_LOCK', 'FILE_DELETE', 'FOLDER_DELETE'];
+
+        DBManager::get()->execute(
+            "DELETE `log_events` FROM `log_events` JOIN `log_actions` USING (`action_id`) WHERE `name` IN (?)",
+            [$actions]
+        );
+        DBManager::get()->execute("DELETE FROM `log_actions` WHERE `name` IN (?)", [$actions]);
+    }
+}
diff --git a/lib/models/FileRef.php b/lib/models/FileRef.php
index f440d50e30e..fe878a840f2 100644
--- a/lib/models/FileRef.php
+++ b/lib/models/FileRef.php
@@ -67,6 +67,7 @@ class FileRef extends SimpleORMap implements PrivacyObject, FeedbackRange
 
         $config['registered_callbacks']['after_delete'][] = 'cbRemoveFileIfOrphaned';
         $config['registered_callbacks']['after_delete'][] = 'cbRemoveFeedbackElements';
+        $config['registered_callbacks']['before_delete'][] = 'cbLogDeleteFileRef';
         $config['registered_callbacks']['before_store'][] = 'cbMakeUniqueFilename';
 
         parent::configure($config);
@@ -76,6 +77,17 @@ class FileRef extends SimpleORMap implements PrivacyObject, FeedbackRange
     protected $download_url;
     public $path_to_blob;
 
+
+    public function cbLogDeleteFileRef()
+    {
+        StudipLog::log('FILE_DELETE',
+            $this->id,
+            null,
+            sprintf(
+                $this->name
+            )
+        );
+    }
     /**
      * This callback is called after deleting a FileRef.
      * It removes the File object that is associated with the FileRef,
diff --git a/lib/models/Folder.php b/lib/models/Folder.php
index d1d778a75d5..9faa76a7541 100644
--- a/lib/models/Folder.php
+++ b/lib/models/Folder.php
@@ -76,6 +76,7 @@ class Folder extends SimpleORMap implements FeedbackRange
 
         $config['registered_callbacks']['before_store'][] = 'cbMakeUniqueName';
         $config['registered_callbacks']['after_delete'][] = 'cbRemoveFeedbackElements';
+        $config['registered_callbacks']['before_delete'][] = 'cbLogDeleteFolder';
 
         $config['additional_fields']['is_empty']['get'] = function ($folder) {
             return count($folder->file_refs) + count($folder->subfolders) === 0;
@@ -84,6 +85,16 @@ class Folder extends SimpleORMap implements FeedbackRange
         parent::configure($config);
     }
 
+    protected function cbLogDeleteFolder()
+    {
+        StudipLog::log('FOLDER_DELETE',
+            $this->id,
+            null,
+            sprintf(
+                $this->name
+            )
+        );
+    }
     /**
      * Creates a top folder (root directory) for a Stud.IP object given by range_id and range_type.
      *
diff --git a/lib/models/User.class.php b/lib/models/User.class.php
index 5f10a7e870e..47ad6f63d78 100644
--- a/lib/models/User.class.php
+++ b/lib/models/User.class.php
@@ -207,6 +207,7 @@ class User extends AuthUserMd5 implements Range, PrivacyObject
 
         $config['registered_callbacks']['after_delete'][] = 'cbRemoveFeedback';
         $config['registered_callbacks']['before_store'][] = 'cbClearCaches';
+        $config['registered_callbacks']['before_store'][] = 'cbStudipLog';
 
         $info = new UserInfo();
         $info_meta = $info->getTableMetadata();
@@ -224,6 +225,25 @@ class User extends AuthUserMd5 implements Range, PrivacyObject
         parent::configure($config);
     }
 
+    /**
+     * @param $type string type of callback
+     */
+    protected function cbStudipLog($type)
+    {
+        if ($type == 'before_store' && !$this->isNew()) {
+            if ($this->isFieldDirty('locked') && $this->isFieldDirty('lock_comment') && (int)$this->locked === 1) {
+                StudipLog::log('USER_LOCK',
+                    $this->user_id,
+                    null,
+                    sprintf(
+                        'Kommentar: %s',
+                        $this->lock_comment
+                    )
+                );
+            }
+        }
+    }
+
     /**
      * Returns the currently authenticated user.
      *
-- 
GitLab