Code

Fixed date of birth && userPicture
[gosa.git] / plugins / personal / connectivity / class_opengwAccount.inc
index 0980c8867f7c18662b1160a01c081b37e841beb7..c1da16c24cef60de5fc5964c942891caa586ebc6 100644 (file)
 
 class opengwAccount extends plugin
 {
-  /* Definitions */
-  var $plHeadline       = "Opengroupware account";
-  var $plDescription    = "This does something";
+       /* Definitions */
+       var $plHeadline       = "Opengroupware account";
+       var $plDescription    = "This does something";
 
-  /* CLI vars */
-  var $cli_summary      = "Manage users webdav account";
-  var $cli_description  = "Some longer text\nfor help";
-  var $cli_parameters   = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
+       /* attribute list for save action */
+       var $attributes       = array();
+       var $objectclasses    = array();
 
-  /* attribute list for save action */
-  var $attributes= array();
-  var $objectclasses= array("gosaWebdavAccount");
+  var $SetPassword   = false;
 
-  var $handle   = NULL;
+       /* Attribute mapping opengroupware->ldap  */
+       var $attrsToUse    = array( "salutation"  =>"vocation",
+                       "name"        =>"sn",
+                       "firstname"   =>"givenName",
+                       "login"       =>"uid",
+                       "degree"      =>"academicTitle",
+                       "birthday"    =>"dateOfBirth",
+                       "sex"         =>"gender",
+                       "street"      =>"street",
+                       "zip"         =>"postalCode",
+                       "value_string"=>"mail",
+                       "number"      =>"telephoneNumber"
+                       );
 
-  function opengwAccount ($config, $dn= NULL)
-  {
-    plugin::plugin ($config, $dn);
-    $this->handle = new ogw("ogo","","vserver-18","ogo");//username,$password,$host,$db
-    $info['login'] = $this->uid;
-    if($this->handle->CheckExistence()){
-      $this->info = $this->handle->GetInfo();
-    }else{
-      /* Generic attributes */
-      $info['salutation']         = $this->attrs['vocation'];
-      $info['name']               = $this->attrs['sn'];
-      $info['firstname']          = $this->attrs['givenName'];
-      $info['login']              = $this->attrs['uid'];
-      $info['description']        = $info['firstname']." ".$info['name'];
-      $info['degree']             = $this->attrs['academicTitle'];
-      $info['birthday']           = $this->attrs['dateOfBirth'];
-      
-      if($this->attrs['gender'] == "F"){
-        $info['sex']                = "female";
-      }else{
-        $info['sex']                = "male";
-      }
+       var $serverCon            =  false;
+       var $handle               = NULL;
+       var $is_account           = false;
+       var $initialy_was_account = false;
+  var $OGWPassword          = "";
+  var $ReadOnly = false;
 
-      /* address settings */
-      $info['street']             = $this->attrs['street'];//"iweg 2";
-      $info['zip']                = $this->attrs['postalCode'];//"59821";
-//      $info['country']            = $this->attrs[''];//"Arnbserg";
-      $info['zipcity']            = $info['zip']." ".$info['country'];
-//      $info['state']              = $this->attrs[''];//"Germany";
+       function opengwAccount ($config, $dn= NULL)
+       {
+               plugin::plugin ($config, $dn);
 
-      /* the email address*/
-      $info['value_string']       = $this->attrs['mail'];
+               $this->info=array();
 
-      /* telephone number */
-      $info['number']             = $this->attrs['telephoneNumber'];
+               /* is no account and was no account */
+               $this->initialy_was_account = false;
+               $this->is_account           = false;
 
-      /* Selectable in GOsa */
-      $info['template_user_id']   = 10130;
-      $info['is_locked']          = 0;
-      $info['LocationTeamID']     = "";
-      $info['TeamIDis']           = array();
-    }
-    $this->info = $info;
-    $this->handle->SetInfos($info);
-  }
+               /* check if datebase funktions are available, and if database configurations are available */ 
+               if(is_callable("pg_connect")){
 
-  function execute()
-  {
-    /* Show tab dialog headers */
-    $display= "";
+                       /* Get configurations */
+                       if(search_config($this->config->data,"opengwAccount","USERNAME")){
+                               $atr = array();
+                               $atr['LOGIN']   =       search_config($this->config->data,"opengwAccount","USERNAME");
+                               $atr['PASSWORD']=       search_config($this->config->data,"opengwAccount","PASSWORD");
+                               $atr['SERVER']  =       search_config($this->config->data,"opengwAccount","DATAHOST");
+                               $atr['DB']              =       search_config($this->config->data,"opengwAccount","DATABASE");
+                               $this->serverCon = $atr;
+                       }else{
+                               $this->serverCon  = $this->config->data['SERVERS']['OPENGROUPWARE'];
+                       }
+                       $this->handle     = new ogw($this->serverCon['LOGIN'],$this->serverCon['PASSWORD'],$this->serverCon['SERVER'],$this->serverCon['DB']);
 
-    print_a($this->info);
+                       /* Check if current configuration allows database connection */
+                       if($this->handle->connected){
 
-    /* Show main page */
-    $smarty= get_smarty();
+                               /* Set login name, to check if this is_account */
+                               if(isset($this->attrs['uid'][0])){
+                                       $this->handle->info['login'] = $this->attrs['uid'][0];
+                               }else{
+                                       $this->handle->info['login'] = ""; 
+                               }
 
-    if ($this->is_account){
-      $smarty->assign("is_account", "true");
-    } else {
-      $smarty->assign("is_account", "false");
-    }
-    $display.= $smarty->fetch (get_template_path('opengw.tpl', TRUE, dirname(__FILE__)));
-    return ($display);
-  }
+                               /* If this is account get data from database first */
+                               if($this->handle->CheckExistence()){
+                                       $this->info = $this->handle->GetInfos($this->attrs['uid'][0]);
 
-  function remove_from_parent()
-  {
-    if(chkacl($this->acl,"webdavAccount")==""){
-      /* Cancel if there's nothing to do here */
-      if (!$this->initially_was_account){
-        return;
-      }
+                                       /* This is an account */
+                                       $this->initialy_was_account = true;
+                                       $this->is_account= true;
+                               }else{
+                                       /* this is no account */ 
+                                       $this->initialy_was_account = false;
+                                       $this->is_account= false;
+
+                                       /* Selectable in GOsa */
+                                       $this->info['template_user_id']   = 0;
+                                       $this->info['is_locked']          = 0;
+                                       $this->info['LocationTeamID']     = 0;
+                                       $this->info['TeamIDis']           = array();
+                               }
+
+                               /* Set settings from ldap */
+                               foreach($this->attrsToUse as $name=>$attr){
+                                       if(isset($this->attrs[$attr][0])){
+                                               $this->info[$name] = $this->attrs[$attr][0];
+                                       }else{
+                                               $this->info[$name] = false;
+                                       }
+                               } 
+
+                               /* Description is displayed as 'Nickname' */
+                               $this->info['description']          = $this->info['login'];
 
-      plugin::remove_from_parent();
-      $ldap= $this->config->get_ldap_link();
+                               if($this->info['sex'] == "F"){
+                                       $this->info['sex']                = "female";
+                               }else{
+                                       $this->info['sex']                = "male";
+                               }
 
-      $ldap->cd($this->dn);
-      @DEBUG (DEBUG_LDAP, __LINE__, __FUNCTION__, __FILE__,
-          $this->attributes, "Save");
-      $ldap->modify($this->attrs);
-      show_ldap_error($ldap->get_error());
+                       }
 
-      /* Optionally execute a command after we're done */
-      $this->handle_post_events('remove');
+                       /* Transmit data back to ogw handle */
+                       $this->handle->SetInfos($this->info);
+               }
+       }
+
+       function execute()
+       {
+               /* Show tab dialog headers */
+               $display= "";
+               $smarty= get_smarty();
+
+               /* set default values */
+    $tmp = $this->plInfo();
+    foreach($tmp['plProvidedAcls'] as $acl => $desc){
+      $smarty->assign($acl."ACL",$this->getacl($acl,$this->ReadOnly));
+    } 
+
+    if((!$this->ReadOnly)&&(($this->is_account && $this->acl_is_removeable()) || (!$this->is_account && $this->acl_is_createable()))) {
+      $smarty->assign('opengwAccountACL', "");
+    }else{
+      $smarty->assign('opengwAccountACL', " disabled ");
     }
-  }
 
+               /* Check database extension */    
+               if(!is_callable("pg_connect")){
+                       print_red(_("OpenGroupware: Your configuration is missing a postgresql extension. Can't perform any database queries."));
+               }else
 
-  /* Save data to object */
-  function save_object()
-  {
-    /* Do we need to flip is_account state? */
-    if (isset($_POST['connectivityTab'])){
-      if (isset($_POST['webdav'])){
-        if (!$this->is_account && $_POST['webdav'] == "B"){
-          $this->is_account= TRUE;
+                       /* Check if config exists */
+                       if(search_config($this->config->data,"opengwAccount","DATABASE")==""){
+                               print_red(_("OpenGroupware: Missing database configuration for opengroupware. Can't get or set any informations."));
+                       }else{
+
+                               /* Create handle */
+                               $this->handle     = new ogw($this->serverCon['LOGIN'],$this->serverCon['PASSWORD'],$this->serverCon['SERVER'],$this->serverCon['DB']);
+
+                               /* Check if we are successfully connected to db */
+                               if(!$this->handle->connected){
+                                       print_red(_("OpenGroupware: Can't connect to specified database. Please check given configuration twice."));
+                               }else{
+
+                                       /* Assign LocationTeams */
+                                       $tmp = array(""=>"none");
+                                       foreach($this->handle->validLocationTeam as $id){
+                                               $tmp[$id['company_id']] = $id['description']; 
+                                       }
+                                       $smarty->assign("validLocationTeam",$tmp);
+                                       $smarty->assign("validLocationTeams",array_flip($tmp));
+                                       $smarty->assign("LocationTeam",$this->info['LocationTeamID']);
+
+                                       /* Assign TemplateUsers*/
+                                       $tmp = array();
+                                       foreach($this->handle->validTemplateUser as $id){
+                                               $tmp[$id['company_id']] = $id['name']; 
+                                       }
+                                       $smarty->assign("validTemplateUser",$tmp);
+                                       $smarty->assign("validTemplateUsers",array_flip($tmp));
+                                       $smarty->assign("TemplateUser",$this->info['template_user_id']);
+
+                                       /* Create Team membership */ 
+                                       $str = "";
+                                       if(($this->is_account)==""){
+                                               $dis = "";
+                                       }else{
+                                               $dis = " disabled ";
+                                       }
+
+          if($this->acl_is_readable("Teams")){
+            foreach($this->handle->validTeams as $id){
+              if(in_array($id['company_id'],$this->info['TeamIDis'])){
+                $str .= "<input ".$dis." type='checkbox' value='".$id['company_id']."' 
+                  checked name='team_".base64_encode($id['description'])."'>".$id['description']."<br>";
+              }else{
+                $str .= "<input ".$dis." type='checkbox' value='".$id['company_id']."' 
+                  name='team_".base64_encode($id['description'])."'>".$id['description']."<br>";
+              }
+            }
+          }
+                                       $smarty->assign("validTeams",$str);
+
+                                       /* Assign ogw checkbox */
+                                       if($this->is_account){
+                                               $smarty->assign("OGWstate","");
+                                               $smarty->assign("is_account", " checked ");
+                                       }else{
+                                               $smarty->assign("is_account", " ");
+                                               $smarty->assign("OGWstate"," disabled ");
+                                       }
+
+                                       /* Assign lock status */
+                                       if($this->info['is_locked']){
+                                               $smarty->assign("is_lockedCHK", " checked ");
+                                       }else{
+                                               $smarty->assign("is_lockedCHK", "  ");
+                                       }
+                                       $smarty->assign("is_locked", $this->info['is_locked']);
+
+                               } 
+                       }
+               $display.= $smarty->fetch (get_template_path('opengw.tpl', TRUE, dirname(__FILE__)));
+               return ($display);
+       }
+
+       function remove_from_parent()
+       {
+               /* remove database entries */
+               if($this->initialy_was_account){
+                       $this->handle = new ogw($this->serverCon['LOGIN'],$this->serverCon['PASSWORD'],$this->serverCon['SERVER'],$this->serverCon['DB']);
+                       $this->handle->SetInfos($this->info);
+                       $this->handle->Perform("REMOVE");
+               }
+       }
+
+
+       /* Save data to object */
+       function save_object()
+       {
+               /* get post data */
+               if($this->is_account){
+
+      /* Get selected team chkboxes */
+      $this->info['TeamIDis'] = array();
+      if($this->acl_is_writeable("Teams")) {
+        foreach($_POST as $name => $value ){
+          if(preg_match("/team_/i",$name)){
+            if(!in_array($value,$this->info['TeamIDis'])){
+              $this->info['TeamIDis'][]=$value;
+            }
+          }
         }
-      } else {
-        $this->is_account= FALSE;
       }
-    }
 
-    plugin::save_object();
-    if (isset($_POST["WEBDAVStatus"])){
-      $this->WEBDAVStatus = "disabled";
-    } else {
-      $this->WEBDAVStatus = "enabled";
-    }
-  }
 
+                       /* Get location Team*/
+                       if(isset($_POST['LocationTeam']) && $this->acl_is_writeable("LocationTeam")){
+                               $this->info['LocationTeamID'] = $_POST['LocationTeam'];
+                       }
 
-  /* Save to LDAP */
-  function save()
-  {
-    if(chkacl($this->acl,"webdavAccount")==""){
-      plugin::save();
-
-      /* Write back to ldap */
-      $ldap= $this->config->get_ldap_link();
-      $ldap->cd($this->dn);
-      $ldap->modify($this->attrs);
-      show_ldap_error($ldap->get_error());
-
-      /* Optionally execute a command after we're done */
-      if ($this->initially_was_account == $this->is_account){
-        if ($this->is_modified){
-          $this->handle_post_events("mofify");
+                       /* Get template user */
+                       if(isset($_POST['TemplateUser']) && $this->acl_is_writeable("TemplateUser") ){
+                               $this->info['template_user_id'] = $_POST['TemplateUser'];
+                       }
+
+      /* get lock status */
+      if($this->acl_is_writeable("Locked")){
+        if(isset($_POST['is_locked'])){
+          $this->info['is_locked'] = $_POST['is_locked'];
+        }else{
+          $this->info['is_locked'] = 0;
         }
-      } else {
-        $this->handle_post_events("add");
       }
-    }
+               }
+
+               /* change account status */
+               if(isset($_POST['is_account'])){
+      if($this->acl_is_createable()){
+        $this->is_account = $_POST['is_account'];
+      }
+    }else{
+      if($this->acl_is_removeable()){
+        $this->is_account = false;//$_POST['is_account'];
+      }
+               }
+       }
+
+
+       /* Save to LDAP */
+       function save()
+       {
+               /* Save data */ 
+               $this->handle = new ogw($this->serverCon['LOGIN'],$this->serverCon['PASSWORD'],$this->serverCon['SERVER'],$this->serverCon['DB']);
+               $this->handle->SetInfos($this->info);
+               $this->handle->checkInfos();
+               if($this->initialy_was_account){
+                       $this->handle->Perform("EDIT");
+               }else{
+                       $this->handle->Perform("ADD");
+               }
+       }
+
+  
+  function PrepareForCopyPaste($src){
+    // Nothing to do, because this object can't be copied ... yet
   }
 
-}
 
+ /* Return plugin informations for acl handling 
+      #FIXME This is only an enable/disable checkbox for this account, there is possibly a better solution available later */  
+  function plInfo()
+  {
+    return (array(
+          "plShortName"     => _("Opengroupware"),
+          "plDescription"   => _("Open groupware account settings"),
+          "plSelfModify"    => TRUE,
+          "plDepends"       => array("user"),
+          "plPriority"      => 3,                                 // Position in tabs
+          "plSection"       => "personal",                        // This belongs to personal
+          "plCategory"      => array("users"),
+          "plOptions"       => array(),
+
+          "plProvidedAcls"  => array(
+            "LocationTeam"  => _("Location team"),
+            "TemplateUser"  => _("Template user"),
+            "Locked"        => _("Locked"),
+            "Teams"         => _("Teams"))
+          ));
+  }
+}
 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
 ?>