Code

Additional multiple edit checkin.
[gosa.git] / plugins / admin / users / class_userManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2006 - Cajus Pollmeier <pollmeier@gonicus.de>
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class userManagement extends plugin
22 {
23   /* Plugin definitions */
24   var $plHeadline= "Users";
25   var $plDescription= "This does something";
27   /* Dialog attributes */
28   var $usertab              = NULL;
29   var $ui                   = NULL;
30   var $templates            = array();
31   var $got_uid              = false;
32   var $CopyPasteHandler     = NULL;
33   var $CPPasswordChange     = ""; // Contains the entry id which should get a new password
34   var $DivListUsers;
36   var $pwd_change_queue     = array();
38   var $start_pasting_copied_objects = FALSE;
39   var $current_action ="";
40   var $msg_dialog= NULL;
41   
42   function userManagement(&$config, $ui)
43   {
44     /* Save configuration for internal use */
45     $this->config= &$config;
46     $this->ui= &$ui;
48     /* Copy & Paste handler */
49     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
50       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
51     }
53     /* Creat dialog object */
54     $this->DivListUsers = new divListUsers($this->config,$this);
56   }
59   function execute()
60   {
61     /* Call parent execute */
62     plugin::execute();
64     /* LOCK MESSAGE Vars */
65     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/","/^item_selected/","/^remove_multiple_users/","/^multiple_edit/");
67     $smarty       = get_smarty();                 // Smarty instance
68     $s_action     = "";                           // Contains the action to be taken
69     $s_entry      = "";                           // The value for s_action
71     /* Edit entry button pressed? */
72     if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
73       $s_action= "edit";
74       $s_entry= validate($_GET['id']);
75     }
77     /* Test relevant POST values */  
78     foreach($_POST as $key => $val){
80       /* Get every possible POST combination and set s_action/s_entry accordingly */
81       foreach(array("del"       => "user_del",    
82                     "edit"      => "user_edit",
83                     "new"       => "user_new",
84                     "new_tpl"   => "user_tplnew",
85                     "del_multiple" => "^remove_multiple_users",
86                     "create_user_from_tpl"          => "userfrom_tpl",
87                     "change_pw" => "user_chgpw", 
88                     "editPaste" => "editPaste",  
89                     "copy_multiple" => "multiple_copy_users",
90                     "multiple_edit" => "multiple_edit",
91                     "cut_multiple" => "multiple_cut_users",
92                     "multiple_password_change" => "multiple_password_change",
93                     "copy"      => "^copy",
94                     "toggle_lock_status" => "toggle_lock_status",
95                     "cut"       => "^cut") as $act => $name){
97         if (preg_match("/".$name.".*/", $key)){
98           $s_action= $act;
99           $s_entry= preg_replace("/".$name."_/i", "", $key);
100           break;
101         }
102       }
103       
104     } /* ...Test POST */
106     /* Remove coordinate prefix from POST, required by some browsers */
107     $s_entry= preg_replace("/_.$/", "", $s_entry);
109     /* Seperate possibly encoded tab and entry, default to tab "user" */
110     if(preg_match("/.*-.*/", $s_entry)){
111       $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
112       $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
113     }else{
114       $s_tab= "user";
115     }
117     if(!$this->config->search($s_tab, 'class',array('tabs'))){
118       $s_tab = "user";
119     }
121     /********************
122       Copy & Paste 
123      ********************/
125     /* Display the copy & paste dialog, if it is currently open */
126     if($this->CPPasswordChange == ""){
127       $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
128       if($ret){
129         return($ret);
130       }
131     }
134     /********************
135       Change password confirmed
136      ********************/
138     /* Perform password change */
139     if (isset($_POST['password_finish'])){
141       /* For security reasons, check if user is allowed to set password again */
142       $dn  = $this->dn;
143       $acl = $this->ui->get_permissions($dn, "users/password");
144       $cacl= $this->ui->get_permissions($dn, "users/user");
146       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
148         /* Check input and feed errors into 'message' */
149         $message= array();
151         /* Sanity checks... */
152         if ($_POST['new_password'] != $_POST['repeated_password']){
154           /* Matching passwords in new and repeated? */
155           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
156         } else {
158           /* Empty password is not permitted by default. */
159           if ($_POST['new_password'] == ""){
160             $message[]= _("The password you've entered as 'New password' is empty.");
161           }
162         }
164         /* Errors, or password change? */
165         if (count($message) != 0){
167           /* Show error message and continue editing */
168           show_errors ($message);
169           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
170         }
172         $config= $this->config;
173         $ldap_ui= $this->config->get_ldap_link();
174         if(isset($this->usertab->dn)){
175           $ldap_ui->cat($this->usertab->dn,array("uid"));
176           $user = $ldap_ui->fetch();
177         }else{
178           $ldap_ui->cat($this->dn,array("uid"));
179           $user = $ldap_ui->fetch();
180         }
181         if((is_array($user))&&(isset($user['uid']))){
182           $username= $user['uid'][0];
183         }
185         /* Set password, perform required steps */
186         if ($this->usertab){
187           if ($this->usertab->password_change_needed()){
188             $obj= $this->usertab->by_object['user'];
189             change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
190             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
191               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
192             }
193             new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
194             unset($this->usertab);
195             $this->usertab= NULL;
196           }
197         } else {
198           change_password ($this->dn, $_POST['new_password']);
199           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
200             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
201           }
202           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
203         }
204       } else {
206         /* Missing permissions, show message */
207         msg_dialog::display(_("Password change failed."),_("You are not allowed to set this users password!"),WARNING_DIALOG);
208       }
209       /* Clean session, delete lock */
210       del_lock ($this->dn);
211       unset ($this->usertab);
212       $this->usertab= NULL;
213       $this->lognames= array();;
214       $this->sn= "";
215       $this->givenName= "";
216       $this->uid= "";
217       unset ($_SESSION['objectinfo']);
218     }
221     /********************
222      Change multiple passwords requested 
223      ********************/
224   
225     if($s_action == "multiple_password_change"){
226       $this->pwd_change_queue = $this->list_get_selected_items();
227     }    
230     /********************
231       Change password requested  
232      ********************/
234     /* Password change requested */
235     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
237       /* Get users whose passwords should be changed. */
238       if(count($this->pwd_change_queue)){
239         $s_entry= array_pop($this->pwd_change_queue);
240       }
242       if(!empty($this->CPPasswordChange)){
243         $s_entry = $this->CPPasswordChange;
244         $this->CPPasswordChange = "";
245       }
247       /* Get 'dn' from posted 'uid' */
248       $this->dn= $this->list[trim($s_entry)]['dn'];
250       /* Load permissions for selected 'dn' and check if
251          we're allowed to remove this 'dn' */
252       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
254         /* User is allowed to change passwords, save 'dn' and 'acl' for next
255            dialog. */
256         $_SESSION['objectinfo']= $this->dn;
257         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
258         
260       } else {
261         /* User is not allowed. Show message and cancel. */
262         msg_dialog::display(_("Password change"),_("You are not allowed to set this users password!"),WARNING_DIALOG);
263       }
264     }
268      /********************
269       Edit existing entry
270      ********************/
272     /* User wants to edit data? */
273     if (($s_action=="edit") && (!isset($this->usertab->config))){
275       /* Get 'dn' from posted 'uid', must be unique */
276       $this->dn= $this->list[trim($s_entry)]['dn'];
278       /* Check locking, save current plugin in 'back_plugin', so
279          the dialog knows where to return. */
280       if (($user= get_lock($this->dn)) != ""){
281         return(gen_locked_message ($user, $this->dn));
282       }
284       /* Lock the current entry, so everyone will get the
285          above dialog */
286       add_lock ($this->dn, $this->ui->dn);
288       /* Register usertab to trigger edit dialog */
289       $this->usertab= new usertabs($this->config,
290           $this->config->data['TABS']['USERTABS'], $this->dn);
292       /* Switch tab, if it was requested by the user */
293       $this->usertab->current = $s_tab;
295       /* Set ACL and move DN to the headline */
296       $this->usertab->set_acl_base($this->dn);
297       $_SESSION['objectinfo']= $this->dn;
298     }
300  
302     /********************
303       Edit multiple entries
304      ********************/
306     /* User wants to edit data? */
307     if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
309       $this->dn = array();
310       foreach($this->list_get_selected_items() as $id){
311         $this->dn[] = $this->list[$id]['dn'];;
312       }
313       $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],$this->dn);
314       if ($tmp->entries_locked()){
315         return($tmp->display_lock_message());
316       }
317       $tmp->lock_entries($this->ui->dn);
318       if($tmp->multiple_available()){
319         $this->usertab = $tmp;
320         $this->usertab->set_active_tab($s_tab);
321         $_SESSION['objectinfo']= $this->usertab->get_object_info();
322       }
323     }
326     /********************
327       Edit canceled 
328      ********************/
330     /* Reset all relevant data, if we get a _cancel request */
331     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
332       if (isset($this->usertab)){
333         del_lock ($this->usertab->dn);
334         unset ($this->usertab);
335       }
336       $this->usertab= NULL;
337       $this->lognames= array();;
338       $this->sn= "";
339       $this->givenName= "";
340       $this->uid= "";
341       unset ($_SESSION['objectinfo']);
342     }
345     /********************
346       Delete MULTIPLE entries requested, display confirm dialog
347      ********************/
349     if ($s_action=="del_multiple"){
350       $ids = $this->list_get_selected_items();
351       $this->dns = array();
352       if(count($ids)){
354         foreach($ids as $id){
355           $dn = $this->list[$id]['dn'];
356           if (($user= get_lock($dn)) != ""){
357             return(gen_locked_message ($user, $dn));
358           }
359           $this->dns[$id] = $dn; 
360         }
362         $dns_names = "<br><pre>";
363         foreach($this->dns as $dn){
364           $dns_names .= $dn."\n";
365         }
366         $dns_names .="</pre>";
368         /* Lock the current entry, so nobody will edit it during deletion */
369         if (count($this->dns) == 1){
370           $info = sprintf(_("You're about to delete the following entry: %s"), @LDAP::fix($dns_names));
371         } else {
372           $info = sprintf(_("You're about to delete the following entries: %s"), @LDAP::fix($dns_names));
373         }
374         $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
375         $this->current_action = $s_action;
376       }
377     }
380     /********************
381       Delete MULTIPLE entries confirmed 
382      ********************/
384       /* Confirmation for deletion has been passed. Users should be deleted. */
385       if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
386         
387         $this->current_action = "";  
388         
389         /* Remove user by user and check acls before removeing them */
390         foreach($this->dns as $key => $dn){
392           $acl = $this->ui->get_permissions($dn, "users/user"); 
393           if (preg_match('/d/', $acl)){
395             /* Delete request is permitted, perform LDAP action */
396             $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
397             $this->usertab->set_acl_base();
398             $this->usertab->delete ();
399             unset ($this->usertab);
400             $this->usertab= NULL;
401           } else {
402             msg_dialog::display(_("User delete"),sprintf(_("You are not allowed to delete the user '%s'!"),$dn),WARNING_DIALOG);
403             if(isset($this->ui->uid)){
404               new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
405             }
406           }
407           /* Remove lock file after successfull deletion */
408           del_lock ($dn);
409           unset($this->dns[$key]);
410       }
411     }
414     /********************
415       Delete MULTIPLE entries Canceled 
416      ********************/
418     /* Remove lock */
419     if(isset($_POST['delete_multiple_user_cancel'])){
420       foreach($this->dns as $key => $dn){
421         del_lock ($dn);
422         unset($this->dns[$key]);
423       }
424     }
425   
426   
427     /********************
428       Toggle lock status for user
429      ********************/
430   
431     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
433       /* Get entry check current status */
434       $val = $this->list[$s_entry];
435       $pwd = $val['userPassword'][0];
437       if(!preg_match("/^\{[^\}]/",$pwd)){
438         trigger_error("Can not deactivate user which is using clear password encryption.");
439       }else{
441         $locked = false;
442         if(preg_match("/^[^\}]*+\}!/",$pwd)){
443           $locked = true;
444         }
446         /* Create ldap array to update status */
447         $attrs = array("userPassword" => $pwd);
448         if($locked){
449           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
450         }else{
451           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
452         }
454         /* Write new status back to ldap */
455         $ldap = $this->config->get_ldap_link();
456         $ldap->cd($val['dn']);
457         $ldap->modify($attrs);
458         if($locked){
459           show_ldap_error($ldap->get_error(),_("Could not set user status from locked to unlocked."));
460         }else{
461           show_ldap_error($ldap->get_error(),_("Could not set user status from unlocked to locked."));
462         }
463       }
464     }
467     /********************
468       Delete entry requested, display confirm dialog
469      ********************/
471     /* Remove user was requested */
472     if ($s_action=="del"){
474       /* Get 'dn' from posted 'uid' */
475       $this->dn= $this->list[trim($s_entry)]['dn'];
477       /* Load permissions for selected 'dn' and check if
478          we're allowed to remove this 'dn' */
480       /* Check locking, save current plugin in 'back_plugin', so
481          the dialog knows where to return. */
482       if (($user= get_lock($this->dn)) != ""){
483         return(gen_locked_message ($user, $this->dn));
484       }
486   
487       /* Lock the current entry, so nobody will edit it during deletion */
488 #      add_lock ($this->dn, $this->ui->dn);
489 #      $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
490 #      $smarty->assign("multiple", false);
491 #      return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
492       $this->msg_dialog = new msg_dialog( _("Delete user"),sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)),CONFIRM_DIALOG);
493       $this->current_action = $s_action;
494     }
497     /********************
498       Delete entry confirmed 
499      ********************/
501     /* Confirmation for deletion has been passed. User should be deleted. */
502     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
504       $this->current_action = ""; 
505  
506       /* Some nice guy may send this as POST, so we've to check
507          for the permissions again. */
509       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
510  
511       if (preg_match('/d/', $acl)){
513         /* Delete request is permitted, perform LDAP action */
514         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
515         $this->usertab->set_acl_base();
516         $this->usertab->delete ();
517         unset ($this->usertab);
518         $this->usertab= NULL;
519         msg_dialog::display(_("User delted"),_("User successfully removed."),INFO_DIALOG);
520       } else {
522         /* Normally this shouldn't be reached, send some extra
523            logs to notify the administrator */
524         msg_dialog::display(_("User delete"),_("You are not allowed to delete this user!"),WARNING_DIALOG);
526         if(isset($this->ui->uid)){
527           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
528         }
529       }
531       /* Remove lock file after successfull deletion */
532       del_lock ($this->dn);
533     }
535     
536     /********************
537       Delete entry Canceled 
538      ********************/
540     /* Delete user canceled? */
541     if (isset($_POST['delete_cancel'])){
542       del_lock ($this->dn);
543     }
546     /********************
547       Edit entry finished (Save) 
548      ********************/
550     /* Finish user edit is triggered by the tabulator dialog, so
551        the user wants to save edited data. Check and save at this
552        point. */
553     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
555       /* Check tabs, will feed message array */
556       $this->usertab->last= $this->usertab->current;
557       $this->usertab->save_object();
558       $message= $this->usertab->check();
560       /* Save, or display error message? */
561       if (count($message) == 0){
563         /* No errors. Go ahead and prepare to ask for a password
564            in case we're creating a new user. 'dn' will be 'new'
565            in this case. It is set to the correct value later. */
566         if ($this->dn == "new"){
567           $set_pass= 1;
568         } else {
569           $set_pass= 0;
570         }
572         /* Save user data to ldap */
573         if($this->usertab->save() == 1){
574           return;
575         }
577         if (!isset($_POST['edit_apply'])){
578           /* User has been saved successfully, remove lock from LDAP. */
579           if ($this->dn != "new"){
580             del_lock ($this->dn);
581           }
583           /* In case of new users, ask for a password, skip this for templates */
584           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
585             $this->dn = $this->usertab->dn;
586             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
587           }
589           unset ($this->usertab);
590           $this->usertab= NULL;
591           unset ($_SESSION['objectinfo']);
592         }
593       } else {
594         /* Ok. There seem to be errors regarding to the tab data,
595            show message and continue as usual. */
596         show_errors($message);
597       }
598     }
601     /********************
602       We want to create a new user, so fetch all available user templates 
603      ********************/
605     /* Generate template list */
606     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
608       $this->templates= array();
609       $ldap= $this->config->get_ldap_link();
611       /* Create list of templates */
612       foreach ($this->config->departments as $key => $value){
613     
614         /* Get acls from different ou's */
615         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
616  
617         /* If creation of a new user is allowed, append this template */
618         if (preg_match("/c/",$acl)){
619           
620           /* Search all templates from the current dn */
621           $ldap->cd (get_people_ou().$value);
622           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
624           /* Append */
625           if ($ldap->count() != 0){
626             while ($attrs= $ldap->fetch()){
627               $this->templates[$ldap->getDN()]=
628                 $attrs['uid'][0]." - ".@LDAP::fix($key);
629             }
630             $this->templates['none']= _("none");
631           }
632         }
633       }
635       /* Sort templates */
636       natcasesort ($this->templates);
637       reset ($this->templates);
638     }
641     /********************
642       Create a new user,template, user from template 
643      ********************/
645     /* Check selected options for template */
646     if (isset($_POST['template_continue'])){
647       $message = array();
648       if(!isset($_POST['template']) || (empty($_POST['template']))){
649         $message[] = _("Please select a valid template.");
650       }
651       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
652         $message[]= _("The required field 'Name' is not set.");
653       }
654       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
655         $message[]= _("The required field 'Given name' is not set.");
656       }
657     
658       /* Show error message / continue editing */
659       if (count($message) > 0){
660         show_errors ($message);
662         foreach(array("sn", "givenName", "uid", "template") as $attr){
663           if(isset($_POST[$attr])){
664             $smarty->assign("$attr", $_POST[$attr]);
665           }else{
666             $smarty->assign("$attr", "");
667           }
668         }
669         $smarty->assign("templates",$this->templates);
670         $smarty->assign("got_uid",$this->got_uid);
671         $smarty->assign("edit_uid",false);
672         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
674       }
675     }
677     /* New user/template request */
678     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
679       /* By default we set 'dn' to 'new', all relevant plugins will
680          react on this. */
681       $this->dn= "new";
682       
683       if (isset($this->config->current['IDGEN'])){
684         $this->got_uid= false;
685       } else {
686         $this->got_uid= true;
687       }
689       /* Create new usertab object */
690       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
691       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
692       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
694       /* Take care about templates */
695       if ($s_action=="new_tpl"){
696         $this->is_template= TRUE;
697         $this->usertab->set_template_mode ();
698       } else {
699         $this->is_template= FALSE;
700       }
702       /* Use template if there are any of them */
703       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
704         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
705           $smarty->assign("$attr", $this->$attr);
706         }
707         if ($s_action=="create_user_from_tpl"){
708           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
709         } else {
710           $smarty->assign("template", "none");
711         }
712         $smarty->assign("edit_uid", "");
713         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
714       }
715     }
717     /********************
718       Template selected continue edit
719      ********************/
721     /* Continue template editing */
722     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
724       $this->sn             = $_POST['sn'];
725       $this->givenName      = $_POST['givenName'];
727       /* Check for requred values */
728       $message= array();
729       if ($this->sn == "") {
730         $message[]= _("The required field 'Name' is not set.");
731       }
732       if ($this->givenName == "") {
733         $message[]= _("The required field 'Given name' is not set.");
734       }
736       /* Check if dn is used */
737       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
738       $ldap= $this->config->get_ldap_link();
739       $ldap->cd ($dn);
740       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
741       if ($ldap->count () != 0){
742         $message[]= _("A person with the choosen name is already used in this tree.");
743       }
745       /* Show error message / continue editing */
746       if (count($message) > 0){
747         show_errors ($message);
748       } else {
749         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
750         if (isset($this->config->current['IDGEN']) &&
751             $this->config->current['IDGEN'] != ""){
752           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
753           if (count($uids)){
754             $smarty->assign("edit_uid", "false");
755             $smarty->assign("uids", $uids);
756             $this->uid= current($uids);
757           }
758         } else {
759           $smarty->assign("edit_uid", "");
760           $this->uid= "";
761         }
762         $this->got_uid= true;
763       }
765       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
766         $smarty->assign("$attr", $this->$attr);
767       }
768       if (isset($_POST['template'])){
769         $smarty->assign("template", $_POST['template']);
770       }
771       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
772     }
774     /********************
775       No template selected continue edit
776      ********************/
778     /* No template. Ok. Lets fill data into the normal user dialog */
779     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
780       foreach(array("sn", "givenName", "uid") as $attr){
781         if (isset($_POST[$attr])){
782           $this->usertab->by_object['user']->$attr= $_POST[$attr];
783         }
784       }
785     }
788     /********************
789       Template selected continue edit
790      ********************/
792     /* Finish template preamble */
793     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
795       /* Might not be filled if IDGEN is unset */
796       $this->sn                 = $_POST['sn'];
797       $this->givenName          = $_POST['givenName'];
799       /* Move user supplied data to sub plugins */
800       $this->uid                = $_POST['uid'];
801       $this->usertab->uid       = $this->uid;
802       $this->usertab->sn        = $this->sn;
803       $this->usertab->givenName = $this->givenName;
804       $template_dn              = $_POST['template'];
805       $this->usertab->adapt_from_template($template_dn);
806       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
807       $this->usertab->by_object['user']->base= $template_base;
808     }
809    
810  
811     /********************
812       If no template was selected set base
813      ********************/
815     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
816       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
817     }
820     /********************
821       Display subdialog 
822      ********************/
824     /* Show tab dialog if object is present */
825     if(isset($this->usertab->config)){
826       $display= $this->usertab->execute();
828       /* Don't show buttons if tab dialog requests this */
829       
830         $dia = FALSE;
831         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
832           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
833         }
835         if(!is_object($dia) && $dia != TRUE){
836           $display.= "<p style=\"text-align:right\">\n";
837           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
838           $display.= "&nbsp;\n";
839           if ($this->dn != "new"){
840             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
841             $display.= "&nbsp;\n";
842           }
843           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
844           $display.= "</p>";
845         }
846       return ($display);
847     }
848     
849     /* Check if there is a snapshot dialog open */
850     $base = $this->DivListUsers->selectedBase;
851     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
852       return($str);
853     }
854   
855     /* Return rendered main page */
856         /* Display dialog with system list */
857     $this->DivListUsers->parent = $this;
858     $this->DivListUsers->execute();
860     /* Add departments if subsearch is disabled */
861     if(!$this->DivListUsers->SubSearch){
862       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
863     }
864     $this->reload();
865     $this->DivListUsers->setEntries($this->list);
866     return($this->DivListUsers->Draw());
867   }
870   /* Return departments, that will be included within snapshot detection */
871   function get_used_snapshot_bases()
872   {
873     return(array(get_people_ou().$this->DivListUsers->selectedBase));
874   }  
877   function reload()
878   {
879     /* Set base for all searches */
880     $base= $this->DivListUsers->selectedBase;
881     $this->list =array();
883     /* Get filter configuration */
884     $Regex                = $this->DivListUsers->Regex;
885     $SubSearch            = $this->DivListUsers->SubSearch;
886     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
887     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
888     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
889     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
890     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
891     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
893     /* Setup filter depending on selection */
894     $filter="";
895     if ($this->config->current['SAMBAVERSION'] == 3){
896       $samba= "sambaSamAccount";
897     } else {
898       $samba= "sambaAccount";
899     }
901     if ($ShowFunctionalUsers){
902       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
903                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
904                 "(objectClass=gosaProxyAccount))))";
905     }
906     if ($ShowUnixUsers){
907       $filter.= "(objectClass=posixAccount)";
908     }
909     if ($ShowMailUsers){
910       $filter.= "(objectClass=gosaMailAccount)";
911     }
912     if ($ShowSambaUsers){
913       $filter.= "(objectClass=$samba)";
914     }
915     if ($ShowProxyUsers){
916       $filter.= "(objectClass=gosaProxyAccount)";
917     }
918     if ($ShowTemplates){
919       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
920     } else {
921       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
922     }
923     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
925     /* Generate userlist */
926     $ldap= $this->config->get_ldap_link(TRUE);
928     if ($SubSearch){
929       $ListTemp =  get_list($filter, "users", $base,
930                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
931     } else {
932       $base= get_people_ou().$base;
933       $ListTemp = get_list($filter, "users", $base, 
934                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
935     }
936     $SortTemp = array();
937     $List = array();
938     foreach($ListTemp as $Key => $Entry){
940       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
941        * Else winstations will be listed too, if you use the subtree flag. 
942        */
943       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
944         continue;
945       }else{
947         // Generate caption for rows
948         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
949           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
950         } else {
951           $display= "[".$Entry["uid"][0]."]";
952         }
954         $display = strtolower($display);
955         $List[$display] = $Entry;
956         $SortTemp[$display] = $display;
957       }
958     }
959     sort($SortTemp);
960     reset($SortTemp);
962     $this->list = array();
963     foreach($SortTemp as $Key){
964       $this->list[] = $List[$Key];
965     }
966   }
968   function remove_lock()
969   {
970     /* Remove user lock if a DN is marked as "currently edited" */
971     if (isset($this->usertab->dn)){
972       del_lock ($this->usertab->dn);
973     }
974   }
977   function copyPasteHandling_from_queue($s_action,$s_entry)
978   {
979     /* Check if Copy & Paste is disabled */
980     if(!is_object($this->CopyPasteHandler)){
981       return("");
982     }
984     /* Add a single entry to queue */
985     if($s_action == "cut" || $s_action == "copy"){
986       /* Cleanup object queue */
987       $this->CopyPasteHandler->cleanup_queue();
988       $dn = $this->list[$s_entry]['dn'];
989       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
990     }
992     /* Add entries to queue */
993     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
995       /* Cleanup object queue */
996       $this->CopyPasteHandler->cleanup_queue();
998       /* Add new entries to CP queue */
999       foreach($this->list_get_selected_items() as $id){
1000         $dn = $this->list[$id]['dn'];
1002         if($s_action == "copy_multiple"){
1003           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1004         }
1005         if($s_action == "cut_multiple"){
1006           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1007         }
1008       }
1009     }
1010     
1011     /* Start pasting entries */
1012     if($s_action == "editPaste"){
1013       $this->start_pasting_copied_objects = TRUE;
1014     }
1016     /* Return C&P dialog */ 
1017     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1018    
1019       /* Load entry from queue and set base */
1020       $this->CopyPasteHandler->load_entry_from_queue();
1021       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1023       /* Get dialog */
1024       $data = $this->CopyPasteHandler->execute();
1026       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1027       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1028         $s_entry = $this->CopyPasteHandler->last_entry();
1029         $this->reload();
1030         foreach($this->list as $key => $entry){
1031           if($entry['dn'] == $s_entry){
1032             $this->CPPasswordChange = $key;
1033           }
1034         }
1035       }
1037       /* Return dialog data */
1038       if(!empty($data) && $this->CPPasswordChange == ""){
1039         return($data);
1040       }
1041     }
1043     /* Automatically disable status for pasting */ 
1044     if(!$this->CopyPasteHandler->entries_queued()){
1045       $this->start_pasting_copied_objects = FALSE;
1046     }
1047     return("");
1048   }
1051   function save_object()
1052   {
1053     /* Handle divlist filter && department selection*/
1054     if(!is_object($this->usertab)){
1055       $this->DivListUsers->save_object();
1056     }
1057   }
1059     
1060   function list_get_selected_items()
1061   {
1062     $ids = array();
1063     foreach($_POST as $name => $value){
1064       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1065         $id   = preg_replace("/^item_selected_/","",$name);
1066         $ids[$id] = $id;
1067       }
1068     }
1069     return($ids);
1070   }
1071   
1073   /* A set of disabled and therefore overloaded functions. They are
1074      not needed in this class. */
1075   function remove_from_parent() { } 
1076   function check() { } 
1077   function save() { } 
1078   function adapt_from_template($dn) { } 
1079   function password_change_needed() { } 
1081 } /* ... class userManagement */
1082 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1083 ?>