Code

Updated management class
authorhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 27 Oct 2009 14:04:41 +0000 (14:04 +0000)
committerhickert <hickert@594d385d-05f5-0310-b6e9-bd551577e9d8>
Tue, 27 Oct 2009 14:04:41 +0000 (14:04 +0000)
-Moved snapshot function from plugin to SnapshotHandler class..

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@14654 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/include/class_SnapShotDialog.inc
gosa-core/include/class_SnapshotHandler.inc
gosa-core/include/class_management.inc
gosa-core/plugins/admin/users/class_userManagement.inc

index b13f1cdbec430e5c0552ed634eedd552287668e7..aafd2e5ed191f02a9d09caee0bf04acce97d3594 100644 (file)
@@ -61,21 +61,13 @@ class SnapShotDialog extends plugin
                plugin::execute();
                $smarty = get_smarty();
 
-    if(!isset($this->parent->acl_module) || !count($this->parent->acl_module)){
-      trigger_error("Could not detect acl_module in parent object (".get_class($this->parent).").");
-      return("");
-    }
-
     $ui = get_userinfo();
-
                $once = true;
                foreach($_POST as $name => $value){
                        if((preg_match("/^RemoveSnapShot_/",$name)) && ($once)){
                                $once = false;
-       
         $entry = preg_replace("/^RemoveSnapShot_/","",$name);
         $entry = base64_decode(preg_replace("/_[xy]$/","",$entry));
-
         $found = false;
         foreach($this->last_list as $t_stamp => $obj){
           if($obj['dn'] == $entry){
index 287fe94b5a8af111bfeb8f114cfec067def92e84..d371db5fcdd3d0b142a28ac27612722598b8abf1 100644 (file)
@@ -212,6 +212,8 @@ class SnapshotHandler {
       $snapshotLdap= $ldap;
     }
 
+    $objectBase= preg_replace("/^[^,]*./","",$dn);
+
     // Initialize base
     $base= preg_replace("/".preg_quote($this->config->current['BASE'], '/')."$/",
                         "", $objectBase).$this->snapshotLdapBase;
@@ -242,6 +244,282 @@ class SnapshotHandler {
     return($tmp);
   }
 
+
+   /* Create a snapshot of the current object */
+  function create_snapshot($dn, $description= array())
+  {
+
+    /* Check if snapshot functionality is enabled */
+    if(!$this->snapshotEnabled()){
+      return;
+    }
+
+    /* Get configuration from gosa.conf */
+    $config = $this->config;
+
+    /* Create lokal ldap connection */
+    $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+
+    /* check if there are special server configurations for snapshots */
+    if($config->get_cfg_value("snapshotURI") == ""){
+
+      /* Source and destination server are both the same, just copy source to dest obj */
+      $ldap_to      = $ldap;
+      $snapldapbase = $this->config->current['BASE'];
+
+    }else{
+      $server         = $config->get_cfg_value("snapshotURI");
+      $user           = $config->get_cfg_value("snapshotAdminDn");
+      $password       = $this->config->get_credentials($config->get_cfg_value("snapshotAdminPassword"));
+      $snapldapbase   = $config->get_cfg_value("snapshotBase");
+
+      $ldap_to        = new ldapMultiplexer(new LDAP($user,$password, $server));
+      $ldap_to -> cd($snapldapbase);
+
+      if (!$ldap_to->success()){
+        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
+      }
+
+    }
+
+    /* check if the dn exists */
+    if ($ldap->dn_exists($dn)){
+
+      /* Extract seconds & mysecs, they are used as entry index */
+      list($usec, $sec)= explode(" ", microtime());
+
+      /* Collect some infos */
+      $base           = $this->config->current['BASE'];
+      $snap_base      = $config->get_cfg_value("snapshotBase");
+      $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
+      $new_base       = preg_replace("/".preg_quote($base, '/')."$/","",$base_of_object).$snap_base;
+
+      /* Create object */
+#$data             = preg_replace('/^dn:.*\n/', '', $ldap->gen_ldif($dn,"(!(objectClass=gosaDepartment))"));
+      $data             = $ldap->gen_ldif($dn,"(&(!(objectClass=gosaDepartment))(!(objectClass=FAIclass)))");
+      $newName          = str_replace(".", "", $sec."-".$usec);
+      $target= array();
+      $target['objectClass']            = array("top", "gosaSnapshotObject");
+      $target['gosaSnapshotData']       = gzcompress($data, 6);
+      $target['gosaSnapshotType']       = "snapshot";
+      $target['gosaSnapshotDN']         = $dn;
+      $target['description']            = $description;
+      $target['gosaSnapshotTimestamp']  = $newName;
+
+      /* Insert the new snapshot
+         But we have to check first, if the given gosaSnapshotTimestamp
+         is already used, in this case we should increment this value till there is
+         an unused value. */
+      $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
+      $ldap_to->cat($new_dn);
+      while($ldap_to->count()){
+        $ldap_to->cat($new_dn);
+        $newName = str_replace(".", "", $sec."-".($usec++));
+        $new_dn                           = "gosaSnapshotTimestamp=".$newName.",".$new_base;
+        $target['gosaSnapshotTimestamp']  = $newName;
+      }
+      /* Inset this new snapshot */
+      $ldap_to->cd($snapldapbase);
+      $ldap_to->create_missing_trees($snapldapbase);
+      $ldap_to->create_missing_trees($new_base);
+      $ldap_to->cd($new_dn);
+      $ldap_to->add($target);
+      if (!$ldap_to->success()){
+        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $new_dn, LDAP_ADD, get_class()));
+      }
+
+      if (!$ldap->success()){
+        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $new_base, "", get_class()));
+      }
+
+    }
+  }
+
+  function remove_snapshot($dn)
+  {
+    $ui       = get_userinfo();
+    $old_dn   = $this->dn;
+    $this->dn = $dn;
+    $ldap = $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $ldap->rmdir_recursive($this->dn);
+    if(!$ldap->success()){
+      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn));
+    }
+    $this->dn = $old_dn;
+  }
+ /* returns true if snapshots are enabled, and false if it is disalbed
+     There will also be some errors psoted, if the configuration failed */
+  function snapshotEnabled()
+  {
+    return $this->config->snapshotEnabled();
+  }
+
+
+  /* Return available snapshots for the given base
+   */
+  function Available_SnapsShots($dn,$raw = false)
+  {
+    if(!$this->snapshotEnabled()) return(array());
+
+    /* Create an additional ldap object which
+       points to our ldap snapshot server */
+    $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $cfg= &$this->config->current;
+
+    /* check if there are special server configurations for snapshots */
+    if($this->config->get_cfg_value("snapshotURI") == ""){
+      $ldap_to      = $ldap;
+    }else{
+      $server         = $this->config->get_cfg_value("snapshotURI");
+      $user           = $this->config->get_cfg_value("snapshotAdminDn");
+      $password       = $this->config->get_credentials($this->config->get_cfg_value("snapshotAdminPassword"));
+      $snapldapbase   = $this->config->get_cfg_value("snapshotBase");
+      $ldap_to        = new ldapMultiplexer(new LDAP($user,$password, $server));
+      $ldap_to -> cd($snapldapbase);
+      if (!$ldap_to->success()){
+        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
+      }
+    }
+    /* Prepare bases and some other infos */
+    $base           = $this->config->current['BASE'];
+    $snap_base      = $this->config->get_cfg_value("snapshotBase");
+    $base_of_object = preg_replace ('/^[^,]+,/i', '', $dn);
+    $new_base       = preg_replace("/".preg_quote($base, '/')."$/","",$base_of_object).$snap_base;
+    $tmp            = array();
+
+    /* Fetch all objects with  gosaSnapshotDN=$dn */
+    $ldap_to->cd($new_base);
+    $ldap_to->ls("(&(objectClass=gosaSnapshotObject)(gosaSnapshotDN=".$dn."))",$new_base,
+        array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
+
+    /* Put results into a list and add description if missing */
+    while($entry = $ldap_to->fetch()){
+      if(!isset($entry['description'][0])){
+        $entry['description'][0]  = "";
+      }
+      $tmp[] = $entry;
+    }
+
+    /* Return the raw array, or format the result */
+    if($raw){
+      return($tmp);
+    }else{
+      $tmp2 = array();
+      foreach($tmp as $entry){
+        $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
+      }
+    }
+    return($tmp2);
+  }
+ function getAllDeletedSnapshots($base_of_object,$raw = false)
+  {
+    if(!$this->snapshotEnabled()) return(array());
+
+    /* Create an additional ldap object which
+       points to our ldap snapshot server */
+    $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $cfg= &$this->config->current;
+
+    /* check if there are special server configurations for snapshots */
+    if($this->config->get_cfg_value("snapshotURI") == ""){
+      $ldap_to      = $ldap;
+    }else{
+      $server         = $this->config->get_cfg_value("snapshotURI");
+      $user           = $this->config->get_cfg_value("snapshotAdminDn");
+      $password       = $this->config->get_credentials($this->config->get_cfg_value("snapshotAdminPassword"));
+      $snapldapbase   = $this->config->get_cfg_value("snapshotBase");
+      $ldap_to        = new ldapMultiplexer(new LDAP($user,$password, $server));
+      $ldap_to -> cd($snapldapbase);
+      if (!$ldap_to->success()){
+        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
+      }
+    }
+
+    /* Prepare bases */
+    $base           = $this->config->current['BASE'];
+    $snap_base      = $this->config->get_cfg_value("snapshotBase");
+    $new_base       = preg_replace("/".preg_quote($base, '/')."$/","",$base_of_object).$snap_base;
+  /* Fetch all objects and check if they do not exist anymore */
+    $ui = get_userinfo();
+    $tmp = array();
+    $ldap_to->cd($new_base);
+    $ldap_to->ls("(objectClass=gosaSnapshotObject)",$new_base,array("gosaSnapshotType","gosaSnapshotTimestamp","gosaSnapshotDN","description"));
+    while($entry = $ldap_to->fetch()){
+
+      $chk =  str_replace($new_base,"",$entry['dn']);
+      if(preg_match("/,ou=/",$chk)) continue;
+
+      if(!isset($entry['description'][0])){
+        $entry['description'][0]  = "";
+      }
+      $tmp[] = $entry;
+    }
+
+    /* Check if entry still exists */
+    foreach($tmp as $key => $entry){
+      $ldap->cat($entry['gosaSnapshotDN'][0]);
+      if($ldap->count()){
+        unset($tmp[$key]);
+      }
+    }
+
+    /* Format result as requested */
+    if($raw) {
+      return($tmp);
+    }else{
+      $tmp2 = array();
+      foreach($tmp as $key => $entry){
+        $tmp2[base64_encode($entry['dn'])] = $entry['description'][0];
+      }
+    }
+    return($tmp2);
+  }
+
+
+ /* Restore selected snapshot */
+  function restore_snapshot($dn)
+  {
+    if(!$this->snapshotEnabled()) return(array());
+
+    $ldap= $this->config->get_ldap_link();
+    $ldap->cd($this->config->current['BASE']);
+    $cfg= &$this->config->current;
+
+    /* check if there are special server configurations for snapshots */
+    if($this->config->get_cfg_value("snapshotURI") == ""){
+      $ldap_to      = $ldap;
+    }else{
+      $server         = $this->config->get_cfg_value("snapshotURI");
+      $user           = $this->config->get_cfg_value("snapshotAdminDn");
+      $password       = $this->config->get_credentials($this->config->get_cfg_value("snapshotAdminPassword"));
+      $snapldapbase   = $this->config->get_cfg_value("snapshotBase");
+      $ldap_to        = new ldapMultiplexer(new LDAP($user,$password, $server));
+      $ldap_to -> cd($snapldapbase);
+      if (!$ldap_to->success()){
+        msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap_to->get_error(), $snapldapbase, "", get_class()));
+      }
+    }
+
+    /* Get the snapshot */
+    $ldap_to->cat($dn);
+    $restoreObject = $ldap_to->fetch();
+
+    /* Prepare import string */
+    $data  = gzuncompress($ldap_to->get_attribute($dn,'gosaSnapshotData'));
+
+    /* Import the given data */
+    $err = "";
+    $ldap->import_complete_ldif($data,$err,false,false);
+    if (!$ldap->success()){
+      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, "", get_class()));
+    }
+  }
+
+
 }
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>
index 5d6ea8ed4b28919f62c97d083f6fec479d5cd235..fcd690c0c295561ed9d080c5f1d57c5570667dfa 100644 (file)
@@ -34,6 +34,8 @@ class management
   protected $dn = "";
   protected $dns = array();
 
+  protected $storagePoints = array();
+
   protected $last_dn = "";
   protected $last_dns = array();
 
@@ -117,6 +119,15 @@ class management
       return($this->getHeader().$display);
     }
 
+    // Set current restore base for snapshot handling.
+    if(is_object($this->snapHandler)){
+      $bases = array();
+      foreach($this->storagePoints as $sp){
+        $bases[] = $sp.$this->headpage->getBase();
+      }
+      $this->snapHandler->setSnapshotBases($bases);
+    }
+      
     $this->headpage->update();
     $display = $this->headpage->render();
     return($this->getHeader().$display);
@@ -245,7 +256,20 @@ class management
     if(isset($_POST['edit_apply']))  $action['action'] = "apply";    
     if(isset($_POST['edit_finish'])) $action['action'] = "save";    
     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";    
-    if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";    
+    if(isset($_POST['delete_confirmed'])) $action['action'] = "removeConfirmed";   
+
+    // Detect Snapshot actions
+    if(isset($_POST['CreateSnapshot'])) $action['action'] = "saveSnapshot";   
+    if(isset($_POST['CancelSnapshot'])) $action['action'] = "cancelSnapshot";   
+    foreach($_POST as $name => $value){
+      $once =TRUE;
+      if(preg_match("/^RestoreSnapShot_/",$name) && $once){
+        $once = FALSE;
+        $entry = base64_decode(preg_replace("/^RestoreSnapShot_([^_]*)_[xy]$/i","\\1",$name));
+        $action['action'] = "restoreSnapshot";
+        $action['targets'] = array($entry);
+      }
+    }
 
     return($action);
   }
@@ -260,6 +284,84 @@ class management
     }
   } 
 
+  function createSnapshotDialog($action="",$target=array(),$all=array())
+  {
+    foreach($target as $entry){
+      if(!empty($entry) && $this->ui->allow_snapshot_create($entry,$this->aclCategory)){
+        $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
+        $this->dialogObject->aclCategories = array($this->aclCategory);
+        
+      }else{
+        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to create a snapshot for %s."),$entry),
+            ERROR_DIALOG);
+      }
+    }
+  }
+
+
+  function saveSnapshot()
+  {
+    $this->dialogObject->save_object();
+    $msgs = $this->dialogObject->check();
+    if(count($msgs)){
+      foreach($msgs as $msg){
+        msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
+      }
+    }else{
+      $this->dn =  $this->dialogObject->dn;
+      $this->snapHandler->create_snapshot( $this->dn,$this->dialogObject->CurrentDescription);
+      $this->closeDialogs();
+    }
+  }
+
+
+  function restoreSnapshot($action="",$target=array(),$all=array())
+  {
+    $entry = array_pop($target);
+    if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
+      $this->snapHandler->restore_snapshot($entry);
+      $this->closeDialogs();
+    }else{
+      msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
+          ERROR_DIALOG);
+    }
+  }
+
+
+  function restoreSnapshotDialog($action="",$target=array(),$all=array())
+  {
+    // Set current restore base for snapshot handling.
+    if(is_object($this->snapHandler)){
+      $bases = array();
+      foreach($this->storagePoints as $sp){
+        $bases[] = $sp.$this->headpage->getBase();
+      }
+    }
+    if(!count($target)){ 
+      $entry = $this->headpage->getBase();
+      if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
+        $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
+        $this->dialogObject->set_snapshot_bases($bases);
+        $this->dialogObject->display_all_removed_objects = true;
+        $this->dialogObject->display_restore_dialog = true;
+      }else{
+        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
+            ERROR_DIALOG);
+      } 
+    }else{
+      $entry = array_pop($target);
+      if(!empty($entry) && $this->ui->allow_snapshot_restore($entry,$this->aclCategory)){
+        $this->dialogObject = new SnapShotDialog($this->config,$entry,$this);
+        $this->dialogObject->set_snapshot_bases($bases);
+        $this->dialogObject->display_restore_dialog = true;
+      }else{
+        msg_dialog::display(_("Permission"),sprintf(_("You are not allowed to restore a snapshot for %s."),$entry),
+            ERROR_DIALOG);
+      } 
+    }
+  }
+
+
   function newEntry($action="",$target=array(),$all=array())
   {
     // Check locking & lock entry if required 
index b15d6bfcffb4cadd570dca0f93461f1a1901f538..173b75460454a06c83c9e9789e3ca70d753ed59c 100644 (file)
@@ -45,10 +45,12 @@ class userManagement extends management
   {
     $this->config = $config;
     $this->ui = $ui;
-    
+   
+    $this->storagePoints = array(get_ou("userRDN"));
     // Build filter
     $filter = new filter(get_template_path("user-filter.xml", true));
-    $filter->setObjectStorage("ou=people,");
+    $filter->setObjectStorage($this->storagePoints);
     $this->setFilter($filter);
 
     // Build headpage
@@ -79,6 +81,12 @@ class userManagement extends management
     $this->registerAction("cut",    "copyPasteHandler");
     $this->registerAction("paste",  "copyPasteHandler");
 
+    $this->registerAction("snapshot",    "createSnapshotDialog");
+    $this->registerAction("restore",     "restoreSnapshotDialog");
+    $this->registerAction("saveSnapshot","saveSnapshot");
+    $this->registerAction("restoreSnapshot","restoreSnapshot");
+    $this->registerAction("cancelSnapshot","closeDialogs");
+
     // Register special user actions 
     $this->registerAction("lock",   "lockEntry");
     $this->registerAction("lockUsers",   "lockUsers");