Code

Updated netatalk class
[gosa.git] / gosa-plugins / netatalk / personal / netatalk / class_netatalk.inc
index 5a79b94ef2b8edbd7703cedc2523d1a82dec85a0..86303b93776bb39dc042fc9e904b084f289e4437 100644 (file)
@@ -33,8 +33,8 @@
 class netatalk extends plugin {
 
   /* Definitions */
-  var $plHeadline = "Netatalk";
-  var $plDescription = "Manage netatalk account";
+  var $plHeadline     = "Netatalk";
+  var $plDescription  = "Manage Netatalk account";
 
   var $view_logged = FALSE;
 
@@ -51,26 +51,26 @@ class netatalk extends plugin {
   var $mountDirectory         = "/Network/Servers";
 
   /* Attributes to save to LDAP */
-  var $attributes = array ("apple-user-homeurl", "apple-user-homeDirectory");
-  var $CopyPasteVars= array("apple_user_homeurl", "apple_user_homeDirectory","apple_user_share","shares_settings","apple_user_homepath_raw",
-      "apple_user_homeurl_raw","apple_user_homeurl_xml","apple_user_homeurl","selectedshare","mountDirectory");
+  var $attributes = array ("apple_user_homeurl", "apple_user_homeDirectory");
+  var $CopyPasteVars= array("apple_user_homeurl", "apple_user_homeDirectory",
+      "apple_user_share","shares_settings","apple_user_homepath_raw",
+      "apple_user_homeurl_raw","apple_user_homeurl_xml","apple_user_homeurl",
+      "selectedshare","mountDirectory");
 
   /* Attributes to use in smarty template */
   var $smarty_attributes = array ("apple_user_homepath_raw", "shares", "selectedshare");
 
   /* Attributes to save from $_POST */
-  var $post_attributes = array ("apple_user_share", "apple_user_homepath_raw");
+  var $post_attributes = array ("netatalkShare"         => "apple_user_share", 
+                                "netatalkUserHomepath"  => "apple_user_homepath_raw");
 
   /* Objectclasses */
   var $objectclasses = array ("apple-user");
-
-  /* Checkboxes */
-  var $is_chk_box = array ();
-
   var $uid ="";  
 
   /* The constructor just saves a copy of the config. You may add what ever you need. */
-  function netatalk(&$config, $dn = NULL) {
+  function netatalk(&$config, $dn = NULL) 
+  {
 
     /* Include config object */
     $this->config = $config;
@@ -81,22 +81,31 @@ class netatalk extends plugin {
       $this->uid = $this->attrs['uid'][0];
     }
 
-    /* Copy needed attributes */
+    /* Netatalk attribute include '-' and we can't handle thos attribute names.
+       Copy all thos attribute into a useable name.
+     */
     foreach($this->attributes as $val) {
-      if (isset($this->attrs["$val"][0])) {
-        $name = str_replace('-', '_', $val);
-        $this->$name = $this->attrs["$val"][0];
+      $name = str_replace('_', '-', $val);
+      if (isset($this->attrs[$name][0])) {
+        $this->$val = $this->attrs[$name][0];
       }
     }
 
+    /* Extract homepath value 
+     */
     if (strlen($this->apple_user_homeDirectory) >0) {
-      $this->apple_user_homepath_raw = substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
+      $this->apple_user_homepath_raw = 
+        substr($this->apple_user_homeDirectory, strrpos($this->apple_user_homeDirectory, '/') + 1 );
     }
 
     /* get share list an set default values */
     $this->get_netatalk_shares(); 
     $this->apple_user_share = $this->selectedshare;
 
+    if(!$this->is_account){
+        $this->apple_user_share = key($this->shares);
+    }
+
     /* Save initial account state */
     $this->initially_was_account = $this->is_account;
   }
@@ -109,18 +118,11 @@ class netatalk extends plugin {
     /* Get netatalk shares */
     $this->shares = array();
     $ldap = $this->config->get_ldap_link();
-
-    if($this->dn === "new" || $this->dn === NULL) {
-      $base  = session::get('CurrentMainBase');
-    } else {
-      $base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/","",$this->dn);
-    }
-
-    $ldap->cd($base);
+    $ldap->cd($this->config->current['BASE']);
     $ldap->search ("(&(objectClass=mount)(|(mountType=url)(mountType=nfs))(cn=*))");
 
     while ($attrs = $ldap->fetch()){
-      $tmp  = split(":", $attrs["cn"][0]);
+      $tmp  = explode(":", $attrs["cn"][0]);
       $host = trim($tmp[0]);
       $dir  = trim($tmp[1]);
       $mountType = trim($attrs["mountType"][0]);
@@ -167,8 +169,8 @@ class netatalk extends plugin {
 
     /* Do we represent a valid account? */
     if (!$this->is_account && $this->parent === NULL) {
-      $display = "<img alt=\"\"src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".msgPool::noValidExtension(_("netatalk"))."</b>";
-
+      $display = "<img alt=\"\"src=\"images/small-error.png\" align=\"middle\">&nbsp;<b>".
+        msgPool::noValidExtension(_("Netatalk"))."</b>";
       $display .= back_to_main();
       return ($display);
     }
@@ -176,35 +178,36 @@ class netatalk extends plugin {
     /* Show tab dialog headers */
     if ($this->parent !== NULL) {
       if ($this->is_account) {
-        $display = $this->show_disable_header(msgPool::removeFeaturesButton(_("netatalk")), msgPool::featuresEnabled(_("netatalk")));
+        $display = $this->show_disable_header(msgPool::removeFeaturesButton(_("Netatalk")), 
+            msgPool::featuresEnabled(_("Netatalk")));
       } else {
         $errmsg="";
         $obj = $this->parent->by_object['posixAccount'];
         if  (!($obj->is_account) ) {
-          $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")), msgPool::featuresDisabled(_("netatalk"), _("POSIX")), TRUE);
+          $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Netatalk")), 
+              msgPool::featuresDisabled(_("Netatalk"), _("POSIX")), TRUE);
         } elseif (count($this->shares)== 0) {
-          $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")), msgPool::featuresDisabled(_("netatalk"), _("netatalk or NFS share")), TRUE);
+          $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Netatalk")), 
+              msgPool::featuresDisabled(_("Netatalk"), _("Netatalk or NFS share")), TRUE);
         } else {
-          $display = $this->show_enable_header(msgPool::addFeaturesButton(_("netatalk")), msgPool::featuresDisabled(_("netatalk")), TRUE);
+          $display = $this->show_enable_header(msgPool::addFeaturesButton(_("Netatalk")), 
+              msgPool::featuresDisabled(_("Netatalk")));
         } 
         return ($display);
       }
     }
 
-    /* Assign attributes and ACL to smarty */
-    $SkipWrite = (!isset($this->parent) || !$this->parent) && !session::is_set('edit');
-    $smarty->assign("netatalkShareACL", $this->getacl("netatalkShare",$SkipWrite));
-    $smarty->assign("netatalkUserHomepathACL", $this->getacl("netatalkUserHomepath",$SkipWrite));
-
+    /* Assign attributes 
+     */
     foreach ($this->smarty_attributes as $val) {
-      $smarty->assign("$val", $this-> $val);
-      if (in_array($val, $this->is_chk_box)) {
-        if ($this-> $val == "checked") {
-          $smarty->assign($val."CHK", " checked ");
-        } else {
-          $smarty->assign($val."CHK", "");
-        }
-      }
+      $smarty->assign("$val", set_post($this-> $val));
+    }
+
+    /* Assign ACLs 
+     */
+    $tmp = $this->plInfo();
+    foreach($tmp['plProvidedAcls'] as $name => $desc){
+      $smarty->assign($name."ACL",$this->getacl($name));
     }
 
     /* Let smarty fetch and process the page. */
@@ -214,18 +217,20 @@ class netatalk extends plugin {
 
 
   /* Check if we have correct data */
-  function check() {
+  function check() 
+  {
     $message = array ();
-
     if (strlen($this->apple_user_share) == 0) {
       $message[] = msgPool::required(_("Share"));
     }
-
     return ($message);
   }
 
+
   /* Save to LDAP */
-  function save() {
+  function save() 
+  {
+
     /* remove a / at the end of the homepath, we neither need it there nor
      * do we want to check for it later.
      */
@@ -240,36 +245,32 @@ class netatalk extends plugin {
     /* Convert raw data to wished format */
     if ($this->is_account) {
       if($mountType=="url") {
-        $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.$this->apple_user_homepath_raw.'</path></home_dir>';
-        $this->apple_user_homeurl = base64_encode($this->apple_user_homeurl_xml);
+        $this->apple_user_homeurl_xml = '<home_dir><url>afp://'.$host.$dir . '</url><path>'.
+          $this->apple_user_homepath_raw.'</path></home_dir>';
+        $this->apple_user_homeurl = $this->apple_user_homeurl_xml;
       } else {
         $this->apple_user_homeurl = "";
       }
-      $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .$dir . '/' . $this->apple_user_homepath_raw;
+      $this->apple_user_homeDirectory = $this->mountDirectory . '/' . $host .
+        $dir . '/' . $this->apple_user_homepath_raw;
     } else {
       $this->apple_user_homeurl = "";
       $this->apple_user_homeDirectory = "";
     }
 
     $ldap = $this->config->get_ldap_link();
-
-    /* Reset array of used attributes, because plugin::save() 
-       will not work with '-' in attributes names 
-       after calling save restore attributes array */
-    $attributes = $this->attributes;
-    $this->attributes = array();
     plugin :: save();
-    $this->attributes = $attributes;
 
-    /* Do attribute conversion */
+    /* Transform variable names from '_' to '-'.
+     */
     foreach ($this->attributes as $val) {
-      $name = str_replace('-', '_', $val);
-      if ($this->$name != "") {
-        $this->attrs[$val] = $this->$name;
+      unset($this->attrs[$val]);
+      $name = str_replace('_', '-', $val);
+      if ($this->$val != "") {
+        $this->attrs[$name] = $this->$val;
       } else {
-        $this->attrs[$val] = array();
+        $this->attrs[$name] = array();
       }
-      unset ($this->attrs[$name]);
     }
 
     /* Write back to ldap */
@@ -297,34 +298,31 @@ class netatalk extends plugin {
     }
   }
 
-  /* Use Save_object for every Post handling */
-  function save_object() {
+
+  /* Use Save_object for every Post handling 
+   */
+  function save_object() 
+  {
     if (isset ($_POST['netatalkTab'])) {
       /* Save ldap attributes */
       plugin :: save_object();
 
-      foreach($this->post_attributes as $val) {
+      foreach($this->post_attributes as $acl => $val) {
+        if(!preg_match("/w/",$this->getacl($acl))) continue;
         if (isset ($_POST[$val])) {
-          $this->$val = $_POST[$val];
+          $this->$val = get_post($val);
         } else {
           $this->$val = "";
         }
       }
-
-      /* Specialhandling for checkboxes */
-      foreach ($this->is_chk_box as $val) {
-        if (isset ($_POST[$val])) {
-          $this-> $val = "checked";
-        } else {
-          $this-> $val = "unchecked";
-        }
-      }
-
       $this->apple_user_homeurl_raw = 'afp://' . $this->apple_user_share;
     }
   }
 
-  function remove_from_parent() {
+
+  function remove_from_parent() 
+  {
+
     /* Cancel if there's nothing to do here */
     if (!$this->initially_was_account) {
       return;
@@ -335,14 +333,18 @@ class netatalk extends plugin {
 
     /* Remove and write to LDAP */
     plugin :: remove_from_parent();
+    $this->cleanup();
 
-    /* Adapt attributes if needed */
-    //     $method= new $this->method($this->config);
-    //     $method->fixAttributesOnRemove($this);
+    /* Attribute name conversion "_" to "-" */
+    foreach($this->attributes as $val){
+      unset($this->attrs[$val]);
+      $name = preg_replace("/_/","-",$val);
+      $this->attrs[$name] = array();
+    }
 
     @ DEBUG(DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__, $this->attributes, "Save");
     $ldap->cd($this->dn);
-    $this->cleanup();
+
     $ldap->modify($this->attrs);
 
     new log("remove","users/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
@@ -370,7 +372,9 @@ class netatalk extends plugin {
           "plSection"     => array("personal" => _("My account")),
           "plCategory"    => array("users"),
           "plOptions"         => array(),
-
+          "plRequirements"=> array(
+              'ldapSchema' => array('apple-user' => ''),
+              'onFailureDisablePlugin' => array(get_class())),
           "plProvidedAcls"  => array(
             "netatalkUserHomepath"   =>  _("User home path"),
             "netatalkShare"          =>  _("Share"))