Code

Added multiple edit checks to samba
[gosa.git] / gosa-core / plugins / personal / samba / class_sambaAccount.inc
index bd71c8f7ae27eccddaee0e3891d5c1519dfc7e6e..3fb309566cd97248d047a5e980a59bdb6fb529f3 100644 (file)
@@ -91,6 +91,13 @@ class sambaAccount extends plugin
   var $uid= "";
   var $CopyPasteVars = array("kickoff_time_set","logoff_time_set","logon_time_set","mungedObject","orig_sambaDomainName");
 
+  var $multiple_support = TRUE;
+
+  /* Only used  for multiple edit */
+  var $temporary_disable = FALSE;
+  var $no_password_required = FALSE;
+  var $multiple_sambaUserWorkstations = array();
+
   function sambaAccount (&$config, $dn= NULL)
   {
     /* Load attributes depending on the samba version */
@@ -213,28 +220,31 @@ class sambaAccount extends plugin
       return ($display);
     }
 
-    /* Show tab dialog headers */
-    $display= "";
-    if ($this->parent !== NULL){
-      if ($this->is_account){
-        $display= $this->show_disable_header(_("Remove samba account"),
-            _("This account has samba features enabled. You can disable them by clicking below."));
-      } else {
-        $obj= $this->parent->by_object['posixAccount'];
+    $display ="";
+    if(!$this->multiple_support_active){
 
-        /* Samba3 dependency on posix accounts are enabled
-           in the moment, because I need to rely on unique
-           uidNumbers. There'll be a better solution later
-           on. */
-        if ($obj->is_account){
-
-          $display= $this->show_enable_header(_("Create samba account"),
-              _("This account has samba features disabled. You can enable them by clicking below."));
+      /* Show tab dialog headers */
+      $display= "";
+      if ($this->parent !== NULL){
+        if ($this->is_account){
+          $display= $this->show_disable_header(_("Remove samba account"),
+              _("This account has samba features enabled. You can disable them by clicking below."));
         } else {
-          $display= $this->show_enable_header(_("Create samba account"),
-              _("This account has samba features disabled. Posix features are needed for samba accounts, enable them first."), TRUE);
+          $obj= $this->parent->by_object['posixAccount'];
+
+          /* Samba3 dependency on posix accounts are enabled
+             in the moment, because I need to rely on unique
+             uidNumbers. There'll be a better solution later
+             on. */
+          if ($obj->is_account){
+            $display= $this->show_enable_header(_("Create samba account"),
+                _("This account has samba features disabled. You can enable them by clicking below."));
+          } else {
+            $display= $this->show_enable_header(_("Create samba account"),
+                _("This account has samba features disabled. Posix features are needed for samba accounts, enable them first."), TRUE);
+          }
+          return ($display);
         }
-        return ($display);
       }
     }
 
@@ -265,7 +275,6 @@ class sambaAccount extends plugin
       return($this->dialog->execute());
     }
 
-
     /* Prepare templating */
     $smarty= get_smarty();
 
@@ -303,13 +312,21 @@ class sambaAccount extends plugin
 
       if($this->acl_is_writeable("sambaUserWorkstations",$SkipWrite)){
 
-        $tmp= $this->sambaUserWorkstations;
-        foreach($_POST['workstation_list'] as $name){
-          $tmp= preg_replace("/$name/", '', $tmp);
-          $this->is_modified= TRUE;
+        if($this->multiple_support_active){
+          foreach($_POST['workstation_list'] as $name){
+            if(isset($this->multiple_sambaUserWorkstations[trim($name)])){
+              unset($this->multiple_sambaUserWorkstations[trim($name)]);
+            }
+          } 
+        }else{
+          $tmp= $this->sambaUserWorkstations;
+          foreach($_POST['workstation_list'] as $name){
+            $tmp= preg_replace("/$name/", '', $tmp);
+            $this->is_modified= TRUE;
+          }
+          $tmp= preg_replace('/,+/', ',', $tmp);
+          $this->sambaUserWorkstations= trim($tmp, ',');
         }
-        $tmp= preg_replace('/,+/', ',', $tmp);
-        $this->sambaUserWorkstations= trim($tmp, ',');
       }
     }
 
@@ -329,12 +346,19 @@ class sambaAccount extends plugin
 
     /* Add user workstation? */
     if (isset($_POST["add_ws_finish"]) && isset($_POST['wslist'])){
-      $tmp= $this->sambaUserWorkstations;
-      foreach($_POST['wslist'] as $ws){
-        $tmp.= ",$ws";
+
+      if($this->multiple_support_active){
+        foreach($_POST['wslist'] as $ws){
+          $this->multiple_sambaUserWorkstations[trim($we)] = array("Name" => trim($ws), "UsedByAllUsers" => TRUE);
+        }
+      }else{
+        $tmp= $this->sambaUserWorkstations;
+        foreach($_POST['wslist'] as $ws){
+          $tmp.= ",$ws";
+        }
+        $tmp= preg_replace('/,+/', ',', $tmp);
+        $this->sambaUserWorkstations= trim($tmp, ',');
       }
-      $tmp= preg_replace('/,+/', ',', $tmp);
-      $this->sambaUserWorkstations= trim($tmp, ',');
       $this->is_modified= TRUE;
     }
 
@@ -359,8 +383,17 @@ class sambaAccount extends plugin
 
       /* Get workstation list */
       $exclude= "";
-      foreach(split(',', $this->sambaUserWorkstations) as $ws){
-        $exclude.= "(cn=$ws$)";
+
+      if($this->multiple_support_active){
+        foreach($this->multiple_sambaUserWorkstations as $ws){
+          if($ws['UsedByAllUsers']){
+            $exclude.= "(cn=".$ws['Name']."$)";
+          }
+        }
+      }else{
+        foreach(split(',', $this->sambaUserWorkstations) as $ws){
+          $exclude.= "(cn=$ws$)";
+        }
       }
       if ($exclude != ""){
         $exclude= "(!(|$exclude))";
@@ -551,6 +584,7 @@ class sambaAccount extends plugin
       $smarty->assign("CtxMaxIdleTimeF",
                       $this->mungedObject->getCtxMaxIdleTimeF()?"checked":"");
 
+      
       /* Fill sambaUserWorkstations */
       $ws= split(",", $this->sambaUserWorkstations);
       sort($ws);
@@ -558,6 +592,9 @@ class sambaAccount extends plugin
       /* Tidy checks for empty option, and smarty will produce one if array[0]="" */
       if(($ws[0]=="")&&(count($ws)==1)) $ws=array();
 
+      if($this->multiple_support_active){
+        $smarty->assign("multiple_workstations",$this->multiple_sambaUserWorkstations);
+      }  
 
       $smarty->assign("workstations", $ws);
     }
@@ -626,7 +663,30 @@ class sambaAccount extends plugin
       }
     }
 
+
+    foreach($this->attributes as $attr){
+      if(in_array($attr,$this->multi_boxes)){
+        $smarty->assign("use_".$attr,TRUE);
+      }else{
+        $smarty->assign("use_".$attr,FALSE);
+      }
+    }
+    foreach(array("allow_pwchange","tslogin","CtxWFHomeDir","CtxWFHomeDirDrive","CtxWFProfilePath",
+          "inherit","CtxWorkDirectory","CtxInitialProgram","CtxMaxConnectionTimeF","CtxMaxConnectionTime","CtxMaxDisconnectionTimeF",
+          "CtxMaxDisconnectionTime","CtxMaxIdleTimeF","CtxMaxIdleTime","connectclientdrives",
+          "onnectclientprinters","defaultprinter","shadow","brokenconn",
+          "reconn","allow_pwchange","connectclientprinters","no_password_required","temporary_disable", 
+          "password_expires","logon_time_set","logoff_time_set","kickoff_time_set","SetSambaLogonHours",
+          "workstation_list") as $attr){
+      if(in_array($attr,$this->multi_boxes)){
+        $smarty->assign("use_".$attr,TRUE);
+      }else{
+        $smarty->assign("use_".$attr,FALSE);
+      }
+    }
+
     /* Show main page */
+    $smarty->assign("multiple_support",$this->multiple_support_active);
     if ($this->samba3){
       $display.= $smarty->fetch (get_template_path('samba3.tpl', TRUE, dirname(__FILE__)));
     } else {
@@ -742,9 +802,11 @@ class sambaAccount extends plugin
         $this->sambaPwdCanChange= $tmp;
       }
       $tmp= "UX";
+      $this->no_password_required = FALSE;
       if (isset($_POST["no_password_required"])){
         if ($_POST["no_password_required"] == 1){
           $tmp.= "N";
+          $this->no_password_required = TRUE;
         }
       }
       if (isset($_POST["password_expires"])){
@@ -754,8 +816,10 @@ class sambaAccount extends plugin
       } else {
         $this->password_expires= 0;
       }
+      $this->temporary_disable = FALSE;
       if (isset($_POST["temporary_disable"])){
         if ($_POST["temporary_disable"] == 1){
+          $this->temporary_disable = TRUE;
           if (is_integer(strpos($this->sambaAcctFlags, "L"))) {
             $tmp.= "L";
           } else {
@@ -1136,6 +1200,342 @@ class sambaAccount extends plugin
             "sambaUserWorkstations" => _("Allow connection from")))
               );
   }    
+
+  function enable_multiple_support()
+  {
+    plugin::enable_multiple_support();
+    if($this->samba3){
+      $this->multiple_support_active = TRUE;
+    }else{
+      $this->multiple_support_active = FALSE;
+    }
+  } 
+
+  function multiple_save_object()
+  {
+    if (isset($_POST['sambaTab'])){
+      $this->save_object();
+      plugin::multiple_save_object();
+      foreach(array("allow_pwchange","tslogin","CtxWFHomeDir","CtxWFHomeDirDrive","CtxWFProfilePath",
+            "inherit","CtxWorkDirectory","CtxInitialProgram","CtxMaxConnectionTimeF","CtxMaxConnectionTime","CtxMaxDisconnectionTimeF",
+            "CtxMaxDisconnectionTime","CtxMaxIdleTimeF","CtxMaxIdleTime","connectclientdrives",
+            "onnectclientprinters","defaultprinter","shadow","brokenconn",
+            "reconn","allow_pwchange","connectclientprinters","no_password_required","temporary_disable",
+            "password_expires","logon_time_set","logoff_time_set","kickoff_time_set","SetSambaLogonHours",
+            "workstation_list") as $attr){
+        if(isset($_POST["use_".$attr])){
+          $this->multi_boxes[] = $attr;
+        }
+      }
+    }
+  }
+
+
+  function multiple_check()
+  {
+    $message = plugin::multiple_check();
+
+    /* Strings */
+    foreach (array( "sambaHomePath" => _("Home directory"),
+          "sambaProfilePath" => _("Profile path")) as $key => $val){
+      if (in_array($key,$this->multi_boxes) && !$this->mungedObject->is_samba_path($this->$key)){
+        $message[]= sprintf(_("The value specified as '%s' contains invalid characters!"), $val);
+      }
+    }
+
+    /* Numeric values */
+    foreach (array( "CtxMaxConnectionTime"    => _("Connection"),
+                    "CtxMaxDisconnectionTime" => _("Disconnection"),
+                    "CtxMaxIdleTime"          => _("IDLE")) as $key => $val){
+      if (in_array($key,$this->multi_boxes) && 
+          isset($this->mungedObject->ctx[$key]) && 
+          !is_id($this->mungedObject->ctx[$key]) && $val != 0){
+        $message[]= sprintf(_("The timeout property '%s' is checked and contains invalid or no characters!"), $val);
+      }
+    }
+
+    /* Too many workstations? Windows usrmgr only supports eight */
+    if (substr_count($this->sambaUserWorkstations, ",") >= 8){
+      $message[]= _("The windows user manager only allows eight clients. You've specified more than eight.");
+    }
+    return($message);
+  }
+
+  
+  function get_multi_init_values()
+  {
+    $ret = plugin::get_multi_init_values();
+
+    /* Parse given sambaUserWorkstations into array
+     *  to allow "init_multiple_support()" to detect multiple used workstations.
+     *  Those workstations will be displayed in light grey.
+     */
+    $tmp2 = array("count" => 0);
+    $tmp = split(",", $this->sambaUserWorkstations);
+    foreach($tmp as $station){
+      $station = trim($station);
+      if(!empty($station)){
+        $tmp2[] = $station;
+        $tmp2['count'] ++;
+      }
+    } 
+    $ret['sambaUserWorkstations'] = $tmp2;
+    return($ret);
+  }
+
+
+
+  function init_multiple_support($attrs,$all)
+  {
+    plugin::init_multiple_support($attrs,$all);
+
+    $this->multiple_sambaUserWorkstations = array();
+    if(isset($all['sambaUserWorkstations'])){
+      for($i = 0 ; $i < $all['sambaUserWorkstations']['count'] ; $i++){
+        $station = trim($all['sambaUserWorkstations'][$i]);
+        $this->multiple_sambaUserWorkstations[$station] = array("Name" => $station, "UsedByAllUsers" => FALSE);
+      }
+    }
+    if(isset($attrs['sambaUserWorkstations'])){
+      for($i = 0 ; $i < $attrs['sambaUserWorkstations']['count'] ; $i++){
+        $station = trim($attrs['sambaUserWorkstations'][$i]);
+        $this->multiple_sambaUserWorkstations[$station] = array("Name" => $station, "UsedByAllUsers" => TRUE);
+      }
+    }
+  }
+
+  function multiple_execute()
+  {
+    return($this->execute());
+  } 
+
+  function get_multi_edit_values()
+  {
+    $ret = plugin::get_multi_edit_values();
+
+    /* Terminal Server  */
+    if(in_array("tslogin",$this->multi_boxes)){
+      $ret['tslogin'] = $this->mungedObject->getTsLogin();
+    }
+    if(in_array("CtxWFHomeDirDrive",$this->multi_boxes)){
+      $ret['CtxWFHomeDirDrive'] = $this->mungedObject->ctx['CtxWFHomeDirDrive'];
+    }
+    if(in_array("CtxWFHomeDir",$this->multi_boxes)){
+      $ret['CtxWFHomeDir'] = $this->mungedObject->ctx['CtxWFHomeDir'];
+    }
+    if(in_array("CtxWFProfilePath",$this->multi_boxes)){
+      $ret['CtxWFProfilePath'] = $this->mungedObject->ctx['CtxWFProfilePath'];
+    }
+
+    if(in_array("inherit",$this->multi_boxes)){
+      $ret['inherit'] = $this->mungedObject->getInheritMode();
+    }       
+    if(in_array("CtxInitialProgram",$this->multi_boxes)){
+      $ret['CtxInitialProgram'] = $this->mungedObject->ctx['CtxInitialProgram'];
+    } 
+    if(in_array("CtxWorkDirectory",$this->multi_boxes)){
+      $ret['CtxWorkDirectory'] = $this->mungedObject->ctx['CtxWorkDirectory'];
+    } 
+
+    /* Time Limits. Be careful here, there are some negations  */
+    if(in_array("CtxMaxConnectionTimeF",$this->multi_boxes)){
+      $ret["CtxMaxConnectionTimeF"]   =  !$this->mungedObject->getCtxMaxConnectionTimeF();
+      if(!$ret["CtxMaxConnectionTimeF"]){
+        $ret["CtxMaxConnectionTime"]   =  $this->mungedObject->ctx['CtxMaxConnectionTime'];
+      }
+    }
+    if(in_array("CtxMaxDisconnectionTimeF",$this->multi_boxes)){
+      $ret["CtxMaxDisconnectionTimeF"]=  !$this->mungedObject->getCtxMaxDisconnectionTimeF();
+      if(!$ret["CtxMaxDisconnectionTimeF"]){
+        $ret["CtxMaxDisconnectionTime"]=  $this->mungedObject->ctx['CtxMaxDisconnectionTime'];
+      }
+    }
+    if(in_array("CtxMaxIdleTimeF",$this->multi_boxes)){
+      $ret["CtxMaxIdleTimeF"]         =  !$this->mungedObject->getCtxMaxIdleTimeF();
+      if(!$ret["CtxMaxIdleTimeF"]){
+        $ret["CtxMaxIdleTime"]         =  $this->mungedObject->ctx['CtxMaxIdleTime'];
+      }
+    }
+
+    /* Client Devices */
+    if(in_array("connectclientdrives",$this->multi_boxes)){
+      $ret["connectclientdrives"]     =  $this->mungedObject->getConnectClientDrives();
+    }
+    if(in_array("connectclientprinters",$this->multi_boxes)){
+      $ret["connectclientprinters"]   =  $this->mungedObject->getConnectClientPrinters();
+    }
+    if(in_array("defaultprinter",$this->multi_boxes)){
+      $ret["defaultprinter"]          =  $this->mungedObject->getDefaultPrinter();
+    }
+
+    /* Misc */
+    if(in_array("shadow",$this->multi_boxes)){
+      $ret["shadow"]    =$this->mungedObject->getShadow();
+    }
+    if(in_array("brokenconn",$this->multi_boxes)){
+      $ret["brokenconn"]=$this->mungedObject->getBrokenConn();
+    }
+    if(in_array("reconn",$this->multi_boxes)){
+      $ret["reconn"]    =$this->mungedObject->getReConn();
+    }
+
+    /* Flags */
+    if(in_array("allow_pwchange",$this->multi_boxes)){
+      $ret['sambaPwdCanChange'] = $this->sambaPwdCanChange;
+      $ret['pwdCanChange']      = $this->pwdCanChange;
+    }
+  
+    if(in_array("password_expires",$this->multi_boxes)){
+      $ret['password_expires']  = $this->password_expires;
+      $ret['sambaPwdMustChange']= $this->sambaPwdMustChange;
+    }
+    if(in_array("logon_time_set",$this->multi_boxes)){
+      $ret['logon_time_set'] = $this->logon_time_set;
+      $ret['sambaLogonTime'] = $this->sambaLogonTime;
+    }
+    if(in_array("logoff_time_set",$this->multi_boxes)){
+      $ret['logoff_time_set'] = $this->logoff_time_set;
+      $ret['sambaLogoffTime'] = $this->sambaLogoffTime;
+    }
+    if(in_array("kickoff_time_set",$this->multi_boxes)){
+      $ret['kickoff_time_set'] = $this->kickoff_time_set;
+      $ret['sambaKickoffTime'] = $this->sambaKickoffTime;
+    }
+
+    if(in_array("no_password_required",$this->multi_boxes)){
+      $ret['no_password_required'] = $this->no_password_required;
+    }
+
+    if(in_array("temporary_disable",$this->multi_boxes)){
+      $ret['temporary_disable'] = $this->temporary_disable;
+    }
+    
+    if(in_array("SetSambaLogonHours",$this->multi_boxes)){
+      $ret['sambaLogonHours'] = $this->sambaLogonHours;
+    }
+
+    if(in_array("workstation_list",$this->multi_boxes)){
+      $ret['multiple_sambaUserWorkstations'] = $this->multiple_sambaUserWorkstations;
+    }
+    return($ret);
+  }
+
+  function set_multi_edit_values($values)
+  {
+    plugin::set_multi_edit_values($values);
+
+    /* Prepare current workstation settings to be merged 
+     *  with multiple edit settings.
+     */
+    if(isset($values['multiple_sambaUserWorkstations'])){
+      $cur_ws = array();
+      $m_ws = $values['multiple_sambaUserWorkstations'];
+
+      /* Prepare current settings to be merged */
+      if(isset($this->sambaUserWorkstations)){
+        $ttmp = split(",",$this->sambaUserWorkstations);
+        foreach($ttmp as $station){
+          $station = trim($station);
+          if(!empty($station)){
+            $cur_ws[$station] = array("Name" => $station, "UsedByAllUsers" => TRUE);
+          }
+        }
+      }
+
+      /* Unset removed workstations */
+      foreach($cur_ws as $cur_name => $cur_station){
+        if(!isset($m_ws[$cur_name])){
+          unset($cur_ws[$cur_name]);
+        }
+      }
+
+      /* Add all added workstations */
+      foreach($m_ws as $name => $station){
+        if($station['UsedByAllUsers']){
+          $cur_ws[$name] = $station;
+        }
+      }
+
+      $this->sambaUserWorkstations = "";
+      foreach($cur_ws as $name => $ws){
+        $this->sambaUserWorkstations .= $name.",";
+      }
+      $this->sambaUserWorkstations=preg_replace("/,$/","",$this->sambaUserWorkstations);
+    }
+
+    /* Enable disabled terminal login, this is inverted somehow */
+    if(isset($values['tslogin']))   $this->mungedObject->setTsLogin(!$values['tslogin']);
+  
+    /* Imherit client configuration */
+    if(isset($values['inherit']))   $this->mungedObject->setInheritMode($values['inherit']);
+  
+    /* Get all ctx values posted */
+    $ctx = array("CtxWFHomeDirDrive","CtxWFHomeDir","CtxWFProfilePath","CtxInitialProgram","CtxWorkDirectory",
+                 "CtxMaxConnectionTime","CtxMaxDisconnectionTime","CtxMaxIdleTime");
+    foreach($ctx as $attr){
+      if(isset($values[$attr])){
+        $this->mungedObject->ctx[$attr] = $values[$attr] ;
+      }
+    }
+
+    if(isset($values['CtxMaxConnectionTimeF']))   $this->mungedObject->setCtxMaxConnectionTimeF($values['CtxMaxConnectionTimeF']);
+    if(isset($values['CtxMaxDisconnectionTimeF']))$this->mungedObject->setCtxMaxDisconnectionTimeF($values['CtxMaxDisconnectionTimeF']);
+    if(isset($values['CtxMaxIdleTimeF']))         $this->mungedObject->setCtxMaxIdleTimeF($values['CtxMaxIdleTimeF']);
+
+    if(isset($values['connectclientdrives']))   $this->mungedObject->setConnectClientDrives($values['connectclientdrives']);
+    if(isset($values['connectclientprinters'])) $this->mungedObject->setConnectClientPrinters($values['connectclientprinters']);
+    if(isset($values['defaultprinter']))        $this->mungedObject->setDefaultPrinter($values['defaultprinter']);
+
+    if(isset($values['shadow']))        $this->mungedObject->setShadow($values['shadow'],$values['shadow']);
+    if(isset($values['brokenconn']))    $this->mungedObject->setBrokenConn($values['brokenconn'],$values['brokenconn']);
+    if(isset($values['reconn']))        $this->mungedObject->setReConn($values['reconn'],$values['reconn']);
+
+  
+    if(isset($values['sambaPwdCanChange']))  $this->sambaPwdCanChange  = $values['sambaPwdCanChange'];
+    if(isset($values['pwdCanChange']))       $this->pwdCanChange       = $values['pwdCanChange'];
+
+    
+    
+
+    if(isset($values['password_expires'])){
+      $this->password_expires = $values['password_expires'];
+      $this->sambaPwdMustChange = $values['sambaPwdMustChange'];
+    }
+    if(isset($values['logon_time_set'])){
+      $this->logon_time_set = $values['logon_time_set'];
+      $this->sambaLogonTime = $values['sambaLogonTime'];
+    }
+    if(isset($values['logoff_time_set'])){
+      $this->logoff_time_set = $values['logoff_time_set'];
+      $this->sambaLogoffTime = $values['sambaLogoffTime'];
+    }
+    if(isset($values['kickoff_time_set'])){
+      $this->kickoff_time_set = $values['kickoff_time_set'];
+      $this->sambaKickoffTime = $values['sambaKickoffTime'];
+    }
+
+    if(isset($values['no_password_required'])){
+      if($values['no_password_required']){
+        if(!preg_match("/N/",$this->sambaAcctFlags)) {
+          $this->sambaAcctFlags = preg_replace("/ /","N",$this->sambaAcctFlags,1);
+        }
+      }else{
+        $this->sambaAcctFlags = preg_replace("/N/"," ",$this->sambaAcctFlags,1);
+      }
+    }      
+
+    if(isset($values['temporary_disable'])){
+      if($values['temporary_disable']){
+        if(preg_match("/L/",$this->sambaAcctFlags)) {
+          // Keep L
+        }else{
+          $this->sambaAcctFlags = preg_replace("/ /","D",$this->sambaAcctFlags,1);
+        }
+      }else{
+        $this->sambaAcctFlags = preg_replace("/D/"," ",$this->sambaAcctFlags,1);
+      }
+    }
+  }
 }
 
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler: