From bcf0d90d4d471b55b377bdd832a2b1147bfaf91c Mon Sep 17 00:00:00 2001
From: Thibault Dockx <thibault.dockx@fusiondirectory.org>
Date: Tue, 4 Apr 2023 15:42:11 +0100
Subject: [PATCH] :sparkles: Feat(Snapshots) - Adds backend configuration.

Adds backend configuration allowing source / origin of data
definition. Rentention properties and automatic snapshot.
---
 contrib/openldap/core-fd-conf.schema    |  29 ++++++-
 plugins/config/class_configInLdap.inc   |  20 -----
 plugins/config/class_snapshotConfig.inc | 106 ++++++++++++++++++++++++
 3 files changed, 133 insertions(+), 22 deletions(-)
 create mode 100644 plugins/config/class_snapshotConfig.inc

diff --git a/contrib/openldap/core-fd-conf.schema b/contrib/openldap/core-fd-conf.schema
index 98f049a17..191abfa00 100644
--- a/contrib/openldap/core-fd-conf.schema
+++ b/contrib/openldap/core-fd-conf.schema
@@ -361,6 +361,31 @@ attributetype ( 1.3.6.1.4.1.38414.8.17.2 NAME 'fdSnapshotBase'
   SYNTAX 1.3.6.1.4.1.1466.115.121.1.26
   SINGLE-VALUE)
 
+attributetype ( 1.3.6.1.4.1.38414.8.17.3 NAME 'fdEnableAutomaticSnapshots'
+  DESC 'FusionDirectory - Weither or not to enable snapshots'
+  EQUALITY booleanMatch
+  SYNTAX 1.3.6.1.4.1.1466.115.121.1.7
+  SINGLE-VALUE )
+
+attributetype ( 1.3.6.1.4.1.38414.8.17.4 NAME 'fdSnapshotMinRetention'
+    DESC 'Minimum number of snapshots to be kept in store'
+    EQUALITY integerMatch
+    SYNTAX 1.3.6.1.4.1.1466.115.121.1.27
+    SINGLE-VALUE )
+
+attributetype ( 1.3.6.1.4.1.38414.8.17.5 NAME 'fdSnapshotRetentionDays'
+    DESC 'Number of days a snapshot should be kept'
+    EQUALITY integerMatch
+    SYNTAX 1.3.6.1.4.1.1466.115.121.1.27
+    SINGLE-VALUE )
+
+attributetype (  1.3.6.1.4.1.38414.8.17.6 NAME 'fdSnapshotSourceData'
+  DESC 'Possible Origin / Source of data received '
+  EQUALITY octetStringMatch
+  SUBSTR caseIgnoreSubstringsMatch
+  SYNTAX 1.3.6.1.4.1.1466.115.121.1.40)
+  SINGLE-VALUE)
+
 # Miscellaneous
 
 attributetype ( 1.3.6.1.4.1.38414.8.18.2 NAME 'fdTabHook'
@@ -643,9 +668,9 @@ objectclass ( 1.3.6.1.4.1.38414.8.2.1 NAME 'fusionDirectoryConf'
     fdPluginsMenuBlacklist $ fdManagementConfig $ fdManagementUserConfig $
     fdAclTabOnObjects $ fdDepartmentCategories $ fdAclTargetFilterLimit $
     fdIncrementalModifierStates $
-    fdSslCaCertPath $ fdSslKeyPath $ fdSslCertPath $
+    fdSslCaCertPath $ fdSslKeyPath $ fdSslCertPath $ fdSnapshotRetentionDays $ fdSnapshotSourceData $
     fdCasActivated $ fdCasServerCaCertPath $ fdCasHost $ fdCasPort $ fdCasContext $ fdCasVerbose $
-    fdLoginMethod $ fdCasLibraryBool $ fdCasClientServiceName
+    fdLoginMethod $ fdCasLibraryBool $ fdCasClientServiceName $ fdEnableAutomaticSnapshots $ fdSnapshotMinRetention
   ) )
 
 objectclass ( 1.3.6.1.4.1.38414.8.2.2 NAME 'fusionDirectoryPluginsConf'
diff --git a/plugins/config/class_configInLdap.inc b/plugins/config/class_configInLdap.inc
index 6ff659791..8cf0034b4 100644
--- a/plugins/config/class_configInLdap.inc
+++ b/plugins/config/class_configInLdap.inc
@@ -101,16 +101,6 @@ class configInLdap extends simplePlugin
             'fdSchemaCheck', FALSE,
             TRUE
           ),
-          new BooleanAttribute(
-            _('Enable snapshots'), _('This enables you to save certain states of entries and restore them later on.'),
-            'fdEnableSnapshots', FALSE,
-            TRUE
-          ),
-          new StringAttribute(
-            _('Snapshot base'), _('The base where snapshots should be stored inside the LDAP directory.'),
-            'fdSnapshotBase', FALSE,
-            'ou=snapshots,'.$config->current['BASE']
-          ),
           new BooleanAttribute(
             _('Wildcard foreign keys'), _('Enables wildcard searches like member=* when moving a whole department. This will open all existing groups and roles to make sure foreign keys are respected. Slow on big trees.'),
             'fdWildcardForeignKeys', FALSE,
@@ -475,16 +465,6 @@ class configInLdap extends simplePlugin
 
     $this->fusionConfigMd5 = md5_file(CACHE_DIR."/".CLASS_CACHE);
 
-    $this->attributesAccess['fdEnableSnapshots']->setManagedAttributes(
-      [
-        'disable' => [
-          FALSE => [
-            'fdSnapshotBase',
-          ]
-        ]
-      ]
-    );
-
     $this->attributesAccess['fdForceSSL']->setManagedAttributes(
       [
         'disable' => [
diff --git a/plugins/config/class_snapshotConfig.inc b/plugins/config/class_snapshotConfig.inc
new file mode 100644
index 000000000..8c260e082
--- /dev/null
+++ b/plugins/config/class_snapshotConfig.inc
@@ -0,0 +1,106 @@
+<?php
+/*
+This code is part of FusionDirectory (http://www.fusiondirectory.org/)
+Copyright (C) 2012-2023 FusionDirectory
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+*/
+
+class snapshotsConfig extends simplePlugin
+{
+  static function plInfo (): array
+  {
+    return [
+      'plShortName'     => _('Snapshots'),
+      'plDescription'   => _('FusionDirectory Snapshot Configuration'),
+      'plObjectClass'   => ['fusionDirectoryConf'],
+      'plObjectType'    => ['configuration'],
+      'plProvidedAcls'  => parent::generatePlProvidedAcls(static::getAttributesInfo())
+    ];
+  }
+
+  static function getAttributesInfo (): array
+  {
+    global $config;
+
+    return [
+      'snapshotsConf' => [
+        'name'  => _('Snapshots Configuration'),
+        'attrs' => [
+          new BooleanAttribute(
+            _('Enable snapshots'), _('This enables you to save certain states of entries and restore them later on.'),
+            'fdEnableSnapshots', FALSE,
+            TRUE
+          ),
+          new BooleanAttribute(
+            _('Enable automatic snapshots'), _('This enables you to automatically create a snapshot upon saving if any modifications have been found.'),
+            'fdEnableAutomaticSnapshots', FALSE,
+            FALSE
+          ),
+          new StringAttribute(
+            _('Snapshot base'), _('The base where snapshots should be stored inside the LDAP directory.'),
+            'fdSnapshotBase', FALSE,
+            'ou=snapshots,'.$config->current['BASE']
+          ),
+        ]
+      ],
+      'snapshotsAdvanceConf' => [
+        'name'  => _('Snapshots Advance Configuration'),
+        'attrs' => [
+          new IntAttribute(
+            _('Minimum number of snapshots to be kept'), _('Set the minimum number of snapshots to be kept'),
+            'fdSnapshotMinRetention', FALSE, '', FALSE, ''
+          ),
+          new IntAttribute(
+            _('Retention time in days'), _('Set the retention time in days for a snapshots to be kept'),
+            'fdSnapshotRetentionDays', FALSE, '', FALSE, ''
+          ),
+        ]
+      ],
+      'OriginDataSource' => [
+        'name' => _('List of available sources / origin of data'),
+        'attrs' => [
+          new SetAttribute(
+            new StringAttribute(
+              _('Origin / source of data'), _('Origin / Source of data'),
+              'fdSnapshotSourceData', FALSE,
+            )
+          ),
+        ]
+      ],
+    ];
+  }
+
+  function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE)
+  {
+    global $config;
+    parent::__construct($dn, $object, $parent, $mainTab);
+
+    $this->attributesAccess['fdEnableSnapshots']->setManagedAttributes(
+      [
+        'disable' => [
+          FALSE => [
+            'fdSnapshotBase',
+            'fdEnableAutomaticSnapshots',
+            'fdSnapshotMinRetention',
+            'fdSnapshotRetentionDays',
+          ]
+        ]
+      ]
+    );
+  }
+
+}
+
-- 
GitLab