Code

Updated user Management
[gosa.git] / gosa-core / plugins / admin / users / class_userManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id$$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class userManagement extends plugin
24 {
25   /* Plugin definitions */
26   var $plHeadline= "Users";
27   var $plDescription= "Manage users";
29   /* Dialog attributes */
30   var $usertab              = NULL;
31   var $ui                   = NULL;
32   var $templates            = array();
33   var $got_uid              = false;
34   var $CopyPasteHandler     = NULL;
35   var $CPPasswordChange     = ""; // Contains the entry id which should get a new password
36   var $DivListUsers;
38   var $pwd_change_queue     = array();
40   var $start_pasting_copied_objects = FALSE;
41   var $current_action ="";
42   var $msg_dialog= NULL;
43   
44   function userManagement(&$config, $ui)
45   {
46     /* Save configuration for internal use */
47     $this->config= &$config;
48     $this->ui= &$ui;
50     /* Copy & Paste handler */
51     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
52       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
53     }
55     /* Creat dialog object */
56     $this->DivListUsers = new divListUsers($this->config,$this);
58   }
61   function execute()
62   {
63     /* Call parent execute */
64     plugin::execute();
66     /* LOCK MESSAGE Vars */
67     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/","/^item_selected/","/^remove_multiple_users/","/^multiple_edit/","/menu_action/"));
69     $smarty       = get_smarty();                 // Smarty instance
70     $s_action     = "";                           // Contains the action to be taken
71     $s_entry      = "";                           // The value for s_action
73     /* Edit entry button pressed? */
74     if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
75       $s_action= "edit";
76       $s_entry= validate($_GET['id']);
77     }
79     /* Test relevant POST values */  
80     foreach($_POST as $key => $val){
82       /* Get every possible POST combination and set s_action/s_entry accordingly */
83       foreach(array("del"       => "user_del",    
84                     "edit"      => "user_edit",
85                     "new"       => "user_new",
86                     "new_tpl"   => "user_tplnew",
87                     "del_multiple" => "^remove_multiple_users",
88                     "create_user_from_tpl"          => "userfrom_tpl",
89                     "change_pw" => "user_chgpw", 
90                     "editPaste" => "editPaste",  
91                     "copy_multiple" => "multiple_copy_users",
92                     "multiple_edit" => "multiple_edit",
93                     "cut_multiple" => "multiple_cut_users",
94                     "multiple_password_change" => "multiple_password_change",
95                     "copy"      => "^copy",
96                     "toggle_lock_status" => "toggle_lock_status",
97                     "cut"       => "^cut") as $act => $name){
99         if (preg_match("/".$name.".*/", $key)){
100           $s_action= $act;
101           $s_entry= preg_replace("/".$name."_/i", "", $key);
102           break;
103         }
104       }
105       
106     } /* ...Test POST */
108     /* Remove coordinate prefix from POST, required by some browsers */
109     $s_entry= preg_replace("/_.$/", "", $s_entry);
111     /* Seperate possibly encoded tab and entry, default to tab "user" */
112     if(preg_match("/.*-.*/", $s_entry)){
113       $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
114       $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
115     }else{
116       $s_tab= "user";
117     }
119     if(!$this->config->search($s_tab, 'class',array('tabs'))){
120       $s_tab = "user";
121     }
124     /* handle C&P from layers menu */
125     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
126       $s_action = "copy_multiple";
127     }
128     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
129       $s_action = "cut_multiple";
130     }
131     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
132       $s_action = "editPaste";
133     }
135     /* Create options */
136     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_new"){
137       $s_action = "new";
138     }
139     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_tplnew"){
140       $s_action = "new_tpl";
141     }
142     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "multiple_edit"){
143       $s_action = "multiple_edit";
144     }
146     /* handle remove from layers menu */
147     if(isset($_POST['menu_action']) && preg_match("/^multiple_password_change/",$_POST['menu_action'])){
148       $s_action = "multiple_password_change";
149     }
151     /* handle remove from layers menu */
152     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
153       $s_action = "del_multiple";
154     }
156     if(isset($_POST['menu_action']) && preg_match("/^event/",$_POST['menu_action'])){
157       $s_action = $_POST['menu_action'];
158     }
161     /********************
162       Create notification event 
163      ********************/
165     if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
166       $ids = $this->list_get_selected_items();
167       $uids = array();
168       foreach($ids as $id){
169         $uids[] = $this->list[$id]['uid'][0];
170       }
171       if(count($uids)){
172         $events = DaemonEvent::get_event_types(USER_EVENT);
173         $event = preg_replace("/^event_/","",$s_action);
174         if(isset($events['BY_CLASS'][$event])){
175           $type = $events['BY_CLASS'][$event];
176           $this->usertab = new $type['CLASS_NAME']($this->config);
177           $this->usertab->add_targets($uids);
178           $this->usertab->set_type(TRIGGERED_EVENT);
179         }
180       }
181     }
183     /* Abort event dialog */
184     if(isset($_POST['abort_event_dialog'])){
185       $this->usertab = FALSE;
186     }
188     /* Save event */
189     if(isset($_POST['save_event_dialog'])){
190       $o_queue = new gosaSupportDaemon();
191       $o_queue->append($this->usertab);
192       if($o_queue->is_error()){
193         msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
194               $o_queue->get_error()),ERROR_DIALOG);
195       }else{
196         $this->usertab = FALSE;
197       }
198     }
200     /* Display event */
201     if($this->usertab instanceof DaemonEvent){
202       $this->usertab->save_object();
203       return($this->usertab->execute());
204     }
207     /********************
208       Copy & Paste 
209      ********************/
211     /* Display the copy & paste dialog, if it is currently open */
212     if($this->CPPasswordChange == ""){
213       $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
214       if($ret){
215         return($ret);
216       }
217     }
220     /********************
221       Change password confirmed
222      ********************/
224     /* Perform password change */
225     if (isset($_POST['password_finish'])){
227       /* For security reasons, check if user is allowed to set password again */
228       $dn  = $this->dn;
229       $acl = $this->ui->get_permissions($dn, "users/password");
230       $cacl= $this->ui->get_permissions($dn, "users/user");
232       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
234         /* Check input and feed errors into 'message' */
235         $message= array();
237         /* Sanity checks... */
238         if ($_POST['new_password'] != $_POST['repeated_password']){
240           /* Matching passwords in new and repeated? */
241           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
242         } else {
244           /* Empty password is not permitted by default. */
245           if ($_POST['new_password'] == ""){
246             msgPool::required(_("New password"));
247           }
248         }
250         /* Errors, or password change? */
251         if (count($message) != 0){
253           /* Show error message and continue editing */
254           show_errors ($message);
255           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
256         }
258         $config= $this->config;
259         $ldap_ui= $this->config->get_ldap_link();
260         if(isset($this->usertab->dn)){
261           $ldap_ui->cat($this->usertab->dn,array("uid"));
262           $user = $ldap_ui->fetch();
263         }else{
264           $ldap_ui->cat($this->dn,array("uid"));
265           $user = $ldap_ui->fetch();
266         }
267         if((is_array($user))&&(isset($user['uid']))){
268           $username= $user['uid'][0];
269         }
271         /* Set password, perform required steps */
272         if ($this->usertab){
273           if ($this->usertab->password_change_needed()){
274             $obj= $this->usertab->by_object['user'];
275             change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
276             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
277               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
278             }
279             new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
280             unset($this->usertab);
281             $this->usertab= NULL;
282           }
283         } else {
284           change_password ($this->dn, $_POST['new_password']);
285           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
286             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
287           }
288           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
289         }
290       } else {
292         /* Missing permissions, show message */
293         msg_dialog::display(_("Password change failed."),_("You are not allowed to set this users password!"),WARNING_DIALOG);
294       }
295       /* Clean session, delete lock */
296       del_lock ($this->dn);
297       unset ($this->usertab);
298       $this->usertab= NULL;
299       $this->lognames= array();;
300       $this->sn= "";
301       $this->givenName= "";
302       $this->uid= "";
303       session::un_set('objectinfo');
304     }
307     /********************
308      Change multiple passwords requested 
309      ********************/
310   
311     if($s_action == "multiple_password_change"){
312       $this->pwd_change_queue = $this->list_get_selected_items();
313     }    
316     /********************
317       Change password requested  
318      ********************/
320     /* Password change requested */
321     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
323       /* Get users whose passwords should be changed. */
324       if(count($this->pwd_change_queue)){
325         $s_entry= array_pop($this->pwd_change_queue);
326       }
328       if(!empty($this->CPPasswordChange)){
329         $s_entry = $this->CPPasswordChange;
330         $this->CPPasswordChange = "";
331       }
333       /* Get 'dn' from posted 'uid' */
334       $this->dn= $this->list[trim($s_entry)]['dn'];
336       /* Load permissions for selected 'dn' and check if
337          we're allowed to remove this 'dn' */
338       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
340         /* User is allowed to change passwords, save 'dn' and 'acl' for next
341            dialog. */
342         session::set('objectinfo',$this->dn);
343         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
344         
346       } else {
347         /* User is not allowed. Show message and cancel. */
348         msg_dialog::display(_("Password change"),_("You are not allowed to set this users password!"),WARNING_DIALOG);
349       }
350     }
354      /********************
355       Edit existing entry
356      ********************/
358     /* User wants to edit data? */
359     if (($s_action=="edit") && (!isset($this->usertab->config))){
361       /* Get 'dn' from posted 'uid', must be unique */
362       $this->dn= $this->list[trim($s_entry)]['dn'];
364       /* Check locking, save current plugin in 'back_plugin', so
365          the dialog knows where to return. */
366       if (($user= get_lock($this->dn)) != ""){
367         return(gen_locked_message ($user, $this->dn));
368       }
370       /* Lock the current entry, so everyone will get the
371          above dialog */
372       add_lock ($this->dn, $this->ui->dn);
374       /* Register usertab to trigger edit dialog */
375       $this->usertab= new usertabs($this->config,
376           $this->config->data['TABS']['USERTABS'], $this->dn);
378       /* Switch tab, if it was requested by the user */
379       $this->usertab->current = $s_tab;
381       /* Set ACL and move DN to the headline */
382       $this->usertab->set_acl_base($this->dn);
383       session::set('objectinfo',$this->dn);
384     }
387     /********************
388       Edit multiple entries
389      ********************/
391     /* User wants to edit data? */
392     if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
394       $this->dn = array();
395       foreach($this->list_get_selected_items() as $id){
396         $this->dn[] = $this->list[$id]['dn'];;
397       }
398       $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
399             $this->dn,$this->DivListUsers->selectedBase,"user");
400       if ($tmp->entries_locked()){
401         return($tmp->display_lock_message());
402       }
403       $tmp->lock_entries($this->ui->dn);
404       if($tmp->multiple_available()){
405         $this->usertab = $tmp;
406         $this->usertab->set_active_tab($s_tab);
407         session::set('objectinfo',$this->usertab->get_object_info());
408       }
409     }
412     /********************
413       Edit canceled 
414      ********************/
416     /* Reset all relevant data, if we get a _cancel request */
417     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
418       if (isset($this->usertab)){
419         del_lock ($this->usertab->dn);
420         unset ($this->usertab);
421       }
422       $this->usertab= NULL;
423       $this->lognames= array();;
424       $this->sn= "";
425       $this->givenName= "";
426       $this->uid= "";
427       session::un_set('objectinfo');
428     }
431     /********************
432       Delete MULTIPLE entries requested, display confirm dialog
433      ********************/
435     if ($s_action=="del_multiple"){
436       $ids = $this->list_get_selected_items();
437       $this->dns = array();
438       if(count($ids)){
440         foreach($ids as $id){
441           $dn = $this->list[$id]['dn'];
442           if (($user= get_lock($dn)) != ""){
443             return(gen_locked_message ($user, $dn));
444           }
445           $this->dns[$id] = $dn; 
446         }
448         $dns_names = "<br><pre>";
449         foreach($this->dns as $dn){
450           $dns_names .= $dn."\n";
451         }
452         $dns_names .="</pre>";
454         /* Lock the current entry, so nobody will edit it during deletion */
455         if (count($this->dns) == 1){
456           $info = sprintf(_("You're about to delete the following entry: %s"), @LDAP::fix($dns_names));
457         } else {
458           $info = sprintf(_("You're about to delete the following entries: %s"), @LDAP::fix($dns_names));
459         }
460         $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
461         $this->current_action = $s_action;
462       }
463     }
466     /********************
467       Delete MULTIPLE entries confirmed 
468      ********************/
470       /* Confirmation for deletion has been passed. Users should be deleted. */
471       if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
472         
473         $this->current_action = "";  
474         
475         /* Remove user by user and check acls before removeing them */
476         foreach($this->dns as $key => $dn){
478           $acl = $this->ui->get_permissions($dn, "users/user"); 
479           if (preg_match('/d/', $acl)){
481             /* Delete request is permitted, perform LDAP action */
482             $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
483             $this->usertab->set_acl_base();
484             $this->usertab->delete ();
485             unset ($this->usertab);
486             $this->usertab= NULL;
487           } else {
488             msg_dialog::display(_("User delete"),sprintf(_("You are not allowed to delete the user '%s'!"),$dn),WARNING_DIALOG);
489             if(isset($this->ui->uid)){
490               new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
491             }
492           }
493           /* Remove lock file after successfull deletion */
494           del_lock ($dn);
495           unset($this->dns[$key]);
496       }
497     }
500     /********************
501       Delete MULTIPLE entries Canceled 
502      ********************/
504     /* Remove lock */
505     if(isset($_POST['delete_multiple_user_cancel'])){
506       foreach($this->dns as $key => $dn){
507         del_lock ($dn);
508         unset($this->dns[$key]);
509       }
510     }
511   
512   
513     /********************
514       Toggle lock status for user
515      ********************/
516   
517     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
519       /* Get entry check current status */
520       $val = $this->list[$s_entry];
521       $pwd = $val['userPassword'][0];
523       if(!preg_match("/^\{[^\}]/",$pwd)){
524         trigger_error("Can not deactivate user which is using clear password encryption.");
525       }else{
527         $locked = false;
528         if(preg_match("/^[^\}]*+\}!/",$pwd)){
529           $locked = true;
530         }
532         /* Create ldap array to update status */
533         $attrs = array("userPassword" => $pwd);
534         if($locked){
535           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
536         }else{
537           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
538         }
540         /* Write new status back to ldap */
541         $ldap = $this->config->get_ldap_link();
542         $ldap->cd($val['dn']);
543         $ldap->modify($attrs);
544         if($locked){
545           show_ldap_error($ldap->get_error(),_("Could not set user status from locked to unlocked."));
546         }else{
547           show_ldap_error($ldap->get_error(),_("Could not set user status from unlocked to locked."));
548         }
549       }
550     }
553     /********************
554       Delete entry requested, display confirm dialog
555      ********************/
557     /* Remove user was requested */
558     if ($s_action=="del"){
560       /* Get 'dn' from posted 'uid' */
561       $this->dn= $this->list[trim($s_entry)]['dn'];
563       /* Load permissions for selected 'dn' and check if
564          we're allowed to remove this 'dn' */
566       /* Check locking, save current plugin in 'back_plugin', so
567          the dialog knows where to return. */
568       if (($user= get_lock($this->dn)) != ""){
569         return(gen_locked_message ($user, $this->dn));
570       }
572   
573       /* Lock the current entry, so nobody will edit it during deletion */
574 #      add_lock ($this->dn, $this->ui->dn);
575 #      $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
576 #      $smarty->assign("multiple", false);
577 #      return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
578       $this->msg_dialog = new msg_dialog( _("Delete user"),sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)),CONFIRM_DIALOG);
579       $this->current_action = $s_action;
580     }
583     /********************
584       Delete entry confirmed 
585      ********************/
587     /* Confirmation for deletion has been passed. User should be deleted. */
588     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
590       $this->current_action = ""; 
591  
592       /* Some nice guy may send this as POST, so we've to check
593          for the permissions again. */
595       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
596  
597       if (preg_match('/d/', $acl)){
599         /* Delete request is permitted, perform LDAP action */
600         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
601         $this->usertab->set_acl_base();
602         $this->usertab->delete ();
603         unset ($this->usertab);
604         $this->usertab= NULL;
605         msg_dialog::display(_("User deleted"),_("User successfully removed."),INFO_DIALOG);
606       } else {
608         /* Normally this shouldn't be reached, send some extra
609            logs to notify the administrator */
610         msg_dialog::display(_("User delete"),_("You are not allowed to delete this user!"),WARNING_DIALOG);
612         if(isset($this->ui->uid)){
613           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
614         }
615       }
617       /* Remove lock file after successfull deletion */
618       del_lock ($this->dn);
619     }
621     
622     /********************
623       Delete entry Canceled 
624      ********************/
626     /* Delete user canceled? */
627     if (isset($_POST['delete_cancel'])){
628       del_lock ($this->dn);
629     }
632     /********************
633       Edit entry finished (Save) 
634      ********************/
636     /* Finish user edit is triggered by the tabulator dialog, so
637        the user wants to save edited data. Check and save at this
638        point. */
639     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
641       /* Check tabs, will feed message array */
642       $this->usertab->last= $this->usertab->current;
643       $this->usertab->save_object();
644       $message= $this->usertab->check();
646       /* Save, or display error message? */
647       if (count($message) == 0){
649         /* No errors. Go ahead and prepare to ask for a password
650            in case we're creating a new user. 'dn' will be 'new'
651            in this case. It is set to the correct value later. */
652         if ($this->dn == "new"){
653           $set_pass= 1;
654         } else {
655           $set_pass= 0;
656         }
658         /* Save user data to ldap */
659         if($this->usertab->save() == 1){
660           return;
661         }
663         if (!isset($_POST['edit_apply'])){
664           /* User has been saved successfully, remove lock from LDAP. */
665           if ($this->dn != "new"){
666             del_lock ($this->dn);
667           }
669           /* In case of new users, ask for a password, skip this for templates */
670           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
671             $this->dn = $this->usertab->dn;
672             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
673           }
675           unset ($this->usertab);
676           $this->usertab= NULL;
677           session::un_set('objectinfo');
678         }
679       } else {
680         /* Ok. There seem to be errors regarding to the tab data,
681            show message and continue as usual. */
682         show_errors($message);
683       }
684     }
687     /********************
688       We want to create a new user, so fetch all available user templates 
689      ********************/
691     /* Generate template list */
692     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
694       $this->templates= array();
695       $ldap= $this->config->get_ldap_link();
697       /* Create list of templates */
698       foreach ($this->config->departments as $key => $value){
699     
700         /* Get acls from different ou's */
701         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
702  
703         /* If creation of a new user is allowed, append this template */
704         if (preg_match("/c/",$acl)){
705           
706           /* Search all templates from the current dn */
707           $ldap->cd (get_people_ou().$value);
708           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
710           /* Append */
711           if ($ldap->count() != 0){
712             while ($attrs= $ldap->fetch()){
713               $this->templates[$ldap->getDN()]=
714                 $attrs['uid'][0]." - ".@LDAP::fix($key);
715             }
716             $this->templates['none']= _("none");
717           }
718         }
719       }
721       /* Sort templates */
722       natcasesort ($this->templates);
723       reset ($this->templates);
724     }
727     /********************
728       Create a new user,template, user from template 
729      ********************/
731     /* Check selected options for template */
732     if (isset($_POST['template_continue'])){
733       $message = array();
734       if(!isset($_POST['template']) || (empty($_POST['template']))){
735         $message[]= msgPool::invalid(_("Template"));
736       }
737       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
738         $message[]= msgPool::required(_("Name"));
739       }
740       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
741         $message[]= msgPool::required(_("Given name"));
742       }
743     
744       /* Show error message / continue editing */
745       if (count($message) > 0){
746         show_errors ($message);
748         foreach(array("sn", "givenName", "uid", "template") as $attr){
749           if(isset($_POST[$attr])){
750             $smarty->assign("$attr", $_POST[$attr]);
751           }else{
752             $smarty->assign("$attr", "");
753           }
754         }
755         $smarty->assign("templates",$this->templates);
756         $smarty->assign("got_uid",$this->got_uid);
757         $smarty->assign("edit_uid",false);
758         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
760       }
761     }
763     /* New user/template request */
764     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
765       /* By default we set 'dn' to 'new', all relevant plugins will
766          react on this. */
767       $this->dn= "new";
768       
769       if (isset($this->config->current['IDGEN'])){
770         $this->got_uid= false;
771       } else {
772         $this->got_uid= true;
773       }
775       /* Create new usertab object */
776       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
777       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
778       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
780       /* Take care about templates */
781       if ($s_action=="new_tpl"){
782         $this->is_template= TRUE;
783         $this->usertab->set_template_mode ();
784       } else {
785         $this->is_template= FALSE;
786       }
788       /* Use template if there are any of them */
789       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
790         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
791           $smarty->assign("$attr", $this->$attr);
792         }
793         if ($s_action=="create_user_from_tpl"){
794           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
795         } else {
796           $smarty->assign("template", "none");
797         }
798         $smarty->assign("edit_uid", "");
799         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
800       }
801     }
803     /********************
804       Template selected continue edit
805      ********************/
807     /* Continue template editing */
808     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
810       $this->sn             = $_POST['sn'];
811       $this->givenName      = $_POST['givenName'];
813       /* Check for requred values */
814       $message= array();
815       if ($this->sn == "") {
816         $message[]= msgPool::required(_("Name"));
817       }
818       if ($this->givenName == "") {
819         $message[]= msgPool::required(_("Given name"));
820       }
822       /* Check if dn is used */
823       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
824       $ldap= $this->config->get_ldap_link();
825       $ldap->cd ($dn);
826       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
827       if ($ldap->count () != 0){
828         msgPool::duplicated(_("Name"));
829       }
831       /* Show error message / continue editing */
832       if (count($message) > 0){
833         show_errors ($message);
834       } else {
835         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
836         if (isset($this->config->current['IDGEN']) &&
837             $this->config->current['IDGEN'] != ""){
838           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
839           if (count($uids)){
840             $smarty->assign("edit_uid", "false");
841             $smarty->assign("uids", $uids);
842             $this->uid= current($uids);
843           }
844         } else {
845           $smarty->assign("edit_uid", "");
846           $this->uid= "";
847         }
848         $this->got_uid= true;
849       }
851       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
852         $smarty->assign("$attr", $this->$attr);
853       }
854       if (isset($_POST['template'])){
855         $smarty->assign("template", $_POST['template']);
856       }
857       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
858     }
860     /********************
861       No template selected continue edit
862      ********************/
864     /* No template. Ok. Lets fill data into the normal user dialog */
865     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
866       foreach(array("sn", "givenName", "uid") as $attr){
867         if (isset($_POST[$attr])){
868           $this->usertab->by_object['user']->$attr= $_POST[$attr];
869         }
870       }
871     }
874     /********************
875       Template selected continue edit
876      ********************/
878     /* Finish template preamble */
879     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
881       /* Might not be filled if IDGEN is unset */
882       $this->sn                 = $_POST['sn'];
883       $this->givenName          = $_POST['givenName'];
885       /* Move user supplied data to sub plugins */
886       $this->uid                = $_POST['uid'];
887       $this->usertab->uid       = $this->uid;
888       $this->usertab->sn        = $this->sn;
889       $this->usertab->givenName = $this->givenName;
890       $template_dn              = $_POST['template'];
891       $this->usertab->adapt_from_template($template_dn);
892       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
893       $this->usertab->by_object['user']->base= $template_base;
894     }
895    
896  
897     /********************
898       If no template was selected set base
899      ********************/
901     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
902       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
903     }
906     /********************
907       Display subdialog 
908      ********************/
910     /* Show tab dialog if object is present */
911     if(isset($this->usertab->config)){
913       $display= $this->usertab->execute();
915       /* Don't show buttons if tab dialog requests this */
916       
917         $dia = FALSE;
918         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
919           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
920         }
922         if(!is_object($dia) && $dia != TRUE){
923           $display.= "<p style=\"text-align:right\">\n";
924           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
925           $display.= "&nbsp;\n";
926           if ($this->dn != "new"){
927             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
928             $display.= "&nbsp;\n";
929           }
930           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
931           $display.= "</p>";
932         }
933       return ($display);
934     }
935     
936     /* Check if there is a snapshot dialog open */
937     $base = $this->DivListUsers->selectedBase;
938     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
939       return($str);
940     }
941   
942     /* Return rendered main page */
943         /* Display dialog with system list */
944     $this->DivListUsers->parent = $this;
945     $this->DivListUsers->execute();
947     /* Add departments if subsearch is disabled */
948     if(!$this->DivListUsers->SubSearch){
949       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
950     }
951     $this->reload();
952     $this->DivListUsers->setEntries($this->list);
953     return($this->DivListUsers->Draw());
954   }
957   /* Return departments, that will be included within snapshot detection */
958   function get_used_snapshot_bases()
959   {
960     return(array(get_people_ou().$this->DivListUsers->selectedBase));
961   }  
964   function reload()
965   {
966     /* Set base for all searches */
967     $base= $this->DivListUsers->selectedBase;
968     $this->list =array();
970     /* Get filter configuration */
971     $Regex                = $this->DivListUsers->Regex;
972     $SubSearch            = $this->DivListUsers->SubSearch;
973     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
974     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
975     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
976     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
977     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
978     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
980     /* Setup filter depending on selection */
981     $filter="";
982     if ($this->config->current['SAMBAVERSION'] == 3){
983       $samba= "sambaSamAccount";
984     } else {
985       $samba= "sambaAccount";
986     }
988     if ($ShowFunctionalUsers){
989       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
990                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
991                 "(objectClass=gosaProxyAccount))))";
992     }
993     if ($ShowUnixUsers){
994       $filter.= "(objectClass=posixAccount)";
995     }
996     if ($ShowMailUsers){
997       $filter.= "(objectClass=gosaMailAccount)";
998     }
999     if ($ShowSambaUsers){
1000       $filter.= "(objectClass=$samba)";
1001     }
1002     if ($ShowProxyUsers){
1003       $filter.= "(objectClass=gosaProxyAccount)";
1004     }
1005     if ($ShowTemplates){
1006       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1007     } else {
1008       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1009     }
1010     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1012     /* Generate userlist */
1013     $ldap= $this->config->get_ldap_link(TRUE);
1015     if ($SubSearch){
1016       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1017                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1018     } else {
1019       $base= get_people_ou().$base;
1020       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1021                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1022     }
1023     $SortTemp = array();
1024     $List = array();
1025     foreach($ListTemp as $Key => $Entry){
1027       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1028        * Else winstations will be listed too, if you use the subtree flag. 
1029        */
1030       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1031         continue;
1032       }else{
1034         // Generate caption for rows
1035         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1036           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1037         } else {
1038           $display= "[".$Entry["uid"][0]."]";
1039         }
1041         $display = strtolower($display);
1042         $List[$display] = $Entry;
1043         $SortTemp[$display] = $display;
1044       }
1045     }
1046     natcasesort($SortTemp);
1047     reset($SortTemp);
1049     $this->list = array();
1050     foreach($SortTemp as $Key){
1051       $this->list[] = $List[$Key];
1052     }
1053   }
1055   function remove_lock()
1056   {
1057     /* Remove user lock if a DN is marked as "currently edited" */
1058     if (isset($this->usertab->dn)){
1059       del_lock ($this->usertab->dn);
1060     }
1061   }
1064   function copyPasteHandling_from_queue($s_action,$s_entry)
1065   {
1066     /* Check if Copy & Paste is disabled */
1067     if(!is_object($this->CopyPasteHandler)){
1068       return("");
1069     }
1071     /* Add a single entry to queue */
1072     if($s_action == "cut" || $s_action == "copy"){
1073       /* Cleanup object queue */
1074       $this->CopyPasteHandler->cleanup_queue();
1075       $dn = $this->list[$s_entry]['dn'];
1076       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1077     }
1079     /* Add entries to queue */
1080     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1082       /* Cleanup object queue */
1083       $this->CopyPasteHandler->cleanup_queue();
1085       /* Add new entries to CP queue */
1086       foreach($this->list_get_selected_items() as $id){
1087         $dn = $this->list[$id]['dn'];
1089         if($s_action == "copy_multiple"){
1090           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1091         }
1092         if($s_action == "cut_multiple"){
1093           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1094         }
1095       }
1096     }
1097     
1098     /* Start pasting entries */
1099     if($s_action == "editPaste"){
1100       $this->start_pasting_copied_objects = TRUE;
1101     }
1103     /* Return C&P dialog */ 
1104     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1105    
1106       /* Get dialog */
1107       $data = $this->CopyPasteHandler->execute();
1108       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1110       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1111       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1112         $s_entry = $this->CopyPasteHandler->last_entry();
1113         $this->reload();
1114         foreach($this->list as $key => $entry){
1115           if($entry['dn'] == $s_entry){
1116             $this->CPPasswordChange = $key;
1117           }
1118         }
1119       }
1121       /* Return dialog data */
1122       if(!empty($data) && $this->CPPasswordChange == ""){
1123         return($data);
1124       }
1125     }
1127     /* Automatically disable status for pasting */ 
1128     if(!$this->CopyPasteHandler->entries_queued()){
1129       $this->start_pasting_copied_objects = FALSE;
1130     }
1131     return("");
1132   }
1135   function save_object()
1136   {
1137     /* Handle divlist filter && department selection*/
1138     if(!is_object($this->usertab)){
1139       $this->DivListUsers->save_object();
1140     }
1141   }
1143     
1144   function list_get_selected_items()
1145   {
1146     $ids = array();
1147     foreach($_POST as $name => $value){
1148       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1149         $id   = preg_replace("/^item_selected_/","",$name);
1150         $ids[$id] = $id;
1151       }
1152     }
1153     return($ids);
1154   }
1155   
1157   /* A set of disabled and therefore overloaded functions. They are
1158      not needed in this class. */
1159   function remove_from_parent() { } 
1160   function check() { } 
1161   function save() { } 
1162   function adapt_from_template($dn) { } 
1163   function password_change_needed() { } 
1165 } /* ... class userManagement */
1166 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1167 ?>