From aaef9aa6e92141b3b4e34f5fc1d0c3bd521437e8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=B4me=20Chilliet?= <come@opensides.be>
Date: Wed, 12 Jun 2019 16:23:38 +0200
Subject: [PATCH] :sparkles: feat(management) Add buttons to reset management
 configuration

issue #5986
---
 include/class_config.inc                      | 47 ++++++++++++----
 .../class_ManagementConfigurationDialog.inc   | 53 ++++++++++++++++++-
 2 files changed, 88 insertions(+), 12 deletions(-)

diff --git a/include/class_config.inc b/include/class_config.inc
index 964b66a01..a01e7e825 100644
--- a/include/class_config.inc
+++ b/include/class_config.inc
@@ -537,11 +537,10 @@ class config
   /*!
    * \brief Update the management config in the LDAP and the cache
    */
-  public function updateManagementConfig (string $managementClass, array $managementConfig, bool $userConfig = FALSE): array
+  public function updateManagementConfig (string $managementClass, $managementConfig, bool $userConfig = FALSE): array
   {
     global $ui;
 
-    $managementConfig = json_encode($managementConfig);
     $changes = [];
     if ($userConfig) {
       if (!isset($this->current['MANAGEMENTUSERCONFIG'][$ui->dn])) {
@@ -558,9 +557,14 @@ class config
       $attrib         = 'fdManagementConfig';
       $prefix         = $managementClass;
     }
+
+    if ($managementConfig !== NULL) {
+      $managementConfig = json_encode($managementConfig);
+    }
+
     if (isset($currentConfig[$managementClass])) {
       /* If there already was a config for this class, remove it */
-      if ($currentConfig[$managementClass] == $managementConfig) {
+      if ($currentConfig[$managementClass] === $managementConfig) {
         /* Unless it's the same one and we've got nothing to do */
         return [];
       }
@@ -570,21 +574,44 @@ class config
         'values'  => [$prefix.':'.$currentConfig[$managementClass]],
       ];
     }
-    /* Add the new one */
-    $changes[] = [
-      'attrib'  => $attrib,
-      'modtype' => LDAP_MODIFY_BATCH_ADD,
-      'values'  => [$prefix.':'.$managementConfig],
-    ];
+
+    if ($managementConfig !== NULL) {
+      /* Add the new one, if any */
+      $changes[] = [
+        'attrib'  => $attrib,
+        'modtype' => LDAP_MODIFY_BATCH_ADD,
+        'values'  => [$prefix.':'.$managementConfig],
+      ];
+    }
     $ldap = $this->get_ldap_link();
     $ldap->cd(CONFIGRDN.$this->current['BASE']);
     if (!$ldap->modify_batch($changes)) {
       return [$ldap->get_error()];
     }
-    $currentConfig[$managementClass] = $managementConfig;
+
+    if ($managementConfig !== NULL) {
+      $currentConfig[$managementClass] = $managementConfig;
+    } else {
+      unset($currentConfig[$managementClass]);
+    }
+
     return [];
   }
 
+  /*!
+   * \brief Test if there is a stored management config
+   */
+  public function hasManagementConfig (string $managementClass, bool $userConfig = FALSE): bool
+  {
+    global $ui;
+
+    if ($userConfig) {
+      return isset($this->current['MANAGEMENTUSERCONFIG'][$ui->dn][$managementClass]);
+    } else {
+      return isset($this->current['MANAGEMENTCONFIG'][$managementClass]);
+    }
+  }
+
   /*!
    * \brief Returns the config for a management class, or NULL
    */
diff --git a/include/management/class_ManagementConfigurationDialog.inc b/include/management/class_ManagementConfigurationDialog.inc
index 9d3b0ea7d..56f0be70e 100644
--- a/include/management/class_ManagementConfigurationDialog.inc
+++ b/include/management/class_ManagementConfigurationDialog.inc
@@ -88,12 +88,24 @@ class ManagementConfigurationDialog extends ManagementDialog
             FALSE,
             'ManagementConfiguration'
           ),
+          new ButtonAttribute(
+            _('Forget my persistent configuration'), _('Delete the persistent configuration for this management page so that the default one is used'),
+            'resetInLdapUser', _('Forget'),
+            NULL, '',
+            'ManagementConfiguration'
+          ),
           new BooleanAttribute(
             _('Global default'), _('Should this configuration be saved in the LDAP as the default configuration for this management page for all users'),
             'saveInLdap', FALSE,
             FALSE,
             'fdManagementConfig'
           ),
+          new ButtonAttribute(
+            _('Forget global default'), _('Delete the global default configuration for this management page so that the default one is used'),
+            'resetInLdap', _('Forget'),
+            NULL, '',
+            'fdManagementConfig'
+          ),
         ]
       ],
     ];
@@ -101,12 +113,28 @@ class ManagementConfigurationDialog extends ManagementDialog
 
   function __construct (management $parent)
   {
+    global $config;
+
     parent::__construct(NULL, NULL, $parent);
 
     $this->attributesAccess['saveInLdap']->setInLdap(FALSE);
+    $this->attributesAccess['saveInLdapUser']->setInLdap(FALSE);
     if (!$this->attrIsWriteable('saveInLdap')) {
       $this->attributesAccess['saveInLdap']->setVisible(FALSE);
     }
+    $this->attributesAccess['resetInLdap']->setInLdap(FALSE);
+    $this->attributesAccess['resetInLdapUser']->setInLdap(FALSE);
+    if (!$this->attrIsWriteable('resetInLdap')) {
+      $this->attributesAccess['resetInLdap']->setVisible(FALSE);
+    }
+
+    if (!$config->hasManagementConfig(get_class($this->parent), TRUE)) {
+      $this->attributesAccess['resetInLdapUser']->setVisible(FALSE);
+    }
+    if (!$config->hasManagementConfig(get_class($this->parent), FALSE)) {
+      $this->attributesAccess['resetInLdap']->setVisible(FALSE);
+    }
+
     $this->attributesAccess['managementColumns']->setInLdap(FALSE);
     $this->attributesAccess['managementColumns']->setLinearRendering(FALSE);
     $columnInfos  = $this->parent->getColumnConfiguration();
@@ -129,11 +157,12 @@ class ManagementConfigurationDialog extends ManagementDialog
   {
     global $config, $ui;
 
-    $noAclAttrs = ['managementColumns', 'saveInLdapUser'];
+    $noAclAttrs   = ['managementColumns', 'saveInLdapUser', 'resetInLdapUser'];
+    $configAttrs  = ['saveInLdap', 'resetInLdap'];
 
     if ((is_object($attr) && in_array($attr->getLdapName(), $noAclAttrs)) || in_array($attr, $noAclAttrs)) {
       return TRUE;
-    } elseif (($attr === 'saveInLdap') || (is_object($attr) && ($attr->getLdapName() == 'saveInLdap'))) {
+    } elseif ((is_object($attr) && in_array($attr->getLdapName(), $configAttrs)) || in_array($attr, $configAttrs)) {
       $acl = $ui->get_permissions(CONFIGRDN.$config->current['BASE'], 'configuration/configInLdap', 'fdManagementConfig', $this->readOnly());
       return (strpos($acl, 'w') !== FALSE);
     } else {
@@ -141,6 +170,26 @@ class ManagementConfigurationDialog extends ManagementDialog
     }
   }
 
+  function handle_resetInLdapUser ()
+  {
+    global $config;
+    $errors = $config->updateManagementConfig(get_class($this->parent), NULL, TRUE);
+    msg_dialog::displayChecks($errors);
+    if (empty($errors)) {
+      $this->attributesAccess['resetInLdapUser']->setVisible(FALSE);
+    }
+  }
+
+  function handle_resetInLdap ()
+  {
+    global $config;
+    $errors = $config->updateManagementConfig(get_class($this->parent), NULL, FALSE);
+    msg_dialog::displayChecks($errors);
+    if (empty($errors)) {
+      $this->attributesAccess['resetInLdap']->setVisible(FALSE);
+    }
+  }
+
   function execute (): string
   {
     global $config, $ui;
-- 
GitLab