Code

Updated strings
[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           msg_dialog::displayChecks($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 (!$ldap->success()){
545           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
546         }
547       }
548     }
551     /********************
552       Delete entry requested, display confirm dialog
553      ********************/
555     /* Remove user was requested */
556     if ($s_action=="del"){
558       /* Get 'dn' from posted 'uid' */
559       $this->dn= $this->list[trim($s_entry)]['dn'];
561       /* Load permissions for selected 'dn' and check if
562          we're allowed to remove this 'dn' */
564       /* Check locking, save current plugin in 'back_plugin', so
565          the dialog knows where to return. */
566       if (($user= get_lock($this->dn)) != ""){
567         return(gen_locked_message ($user, $this->dn));
568       }
570   
571       /* Lock the current entry, so nobody will edit it during deletion */
572 #      add_lock ($this->dn, $this->ui->dn);
573 #      $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
574 #      $smarty->assign("multiple", false);
575 #      return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
576       $this->msg_dialog = new msg_dialog( _("Delete user"),sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)),CONFIRM_DIALOG);
577       $this->current_action = $s_action;
578     }
581     /********************
582       Delete entry confirmed 
583      ********************/
585     /* Confirmation for deletion has been passed. User should be deleted. */
586     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
588       $this->current_action = ""; 
589  
590       /* Some nice guy may send this as POST, so we've to check
591          for the permissions again. */
593       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
594  
595       if (preg_match('/d/', $acl)){
597         /* Delete request is permitted, perform LDAP action */
598         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
599         $this->usertab->set_acl_base();
600         $this->usertab->delete ();
601         unset ($this->usertab);
602         $this->usertab= NULL;
603         msg_dialog::display(_("User deleted"),_("User successfully removed."),INFO_DIALOG);
604       } else {
606         /* Normally this shouldn't be reached, send some extra
607            logs to notify the administrator */
608         msg_dialog::display(_("User delete"),_("You are not allowed to delete this user!"),WARNING_DIALOG);
610         if(isset($this->ui->uid)){
611           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
612         }
613       }
615       /* Remove lock file after successfull deletion */
616       del_lock ($this->dn);
617     }
619     
620     /********************
621       Delete entry Canceled 
622      ********************/
624     /* Delete user canceled? */
625     if (isset($_POST['delete_cancel'])){
626       del_lock ($this->dn);
627     }
630     /********************
631       Edit entry finished (Save) 
632      ********************/
634     /* Finish user edit is triggered by the tabulator dialog, so
635        the user wants to save edited data. Check and save at this
636        point. */
637     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
639       /* Check tabs, will feed message array */
640       $this->usertab->last= $this->usertab->current;
641       $this->usertab->save_object();
642       $message= $this->usertab->check();
644       /* Save, or display error message? */
645       if (count($message) == 0){
647         /* No errors. Go ahead and prepare to ask for a password
648            in case we're creating a new user. 'dn' will be 'new'
649            in this case. It is set to the correct value later. */
650         if ($this->dn == "new"){
651           $set_pass= 1;
652         } else {
653           $set_pass= 0;
654         }
656         /* Save user data to ldap */
657         if($this->usertab->save() == 1){
658           return;
659         }
661         if (!isset($_POST['edit_apply'])){
662           /* User has been saved successfully, remove lock from LDAP. */
663           if ($this->dn != "new"){
664             del_lock ($this->dn);
665           }
667           /* In case of new users, ask for a password, skip this for templates */
668           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
669             $this->dn = $this->usertab->dn;
670             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
671           }
673           unset ($this->usertab);
674           $this->usertab= NULL;
675           session::un_set('objectinfo');
676         }
677       } else {
678         /* Ok. There seem to be errors regarding to the tab data,
679            show message and continue as usual. */
680         msg_dialog::displayChecks($message);
681       }
682     }
685     /********************
686       We want to create a new user, so fetch all available user templates 
687      ********************/
689     /* Generate template list */
690     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
692       $this->templates= array();
693       $ldap= $this->config->get_ldap_link();
695       /* Create list of templates */
696       foreach ($this->config->departments as $key => $value){
697     
698         /* Get acls from different ou's */
699         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
700  
701         /* If creation of a new user is allowed, append this template */
702         if (preg_match("/c/",$acl)){
703           
704           /* Search all templates from the current dn */
705           $ldap->cd (get_people_ou().$value);
706           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
708           /* Append */
709           if ($ldap->count() != 0){
710             while ($attrs= $ldap->fetch()){
711               $this->templates[$ldap->getDN()]=
712                 $attrs['uid'][0]." - ".@LDAP::fix($key);
713             }
714             $this->templates['none']= _("none");
715           }
716         }
717       }
719       /* Sort templates */
720       natcasesort ($this->templates);
721       reset ($this->templates);
722     }
725     /********************
726       Create a new user,template, user from template 
727      ********************/
729     /* Check selected options for template */
730     if (isset($_POST['template_continue'])){
731       $message = array();
732       if(!isset($_POST['template']) || (empty($_POST['template']))){
733         $message[]= msgPool::invalid(_("Template"));
734       }
735       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
736         $message[]= msgPool::required(_("Name"));
737       }
738       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
739         $message[]= msgPool::required(_("Given name"));
740       }
741     
742       /* Show error message / continue editing */
743       if (count($message) > 0){
744         msg_dialog::displayChecks($message);
746         foreach(array("sn", "givenName", "uid", "template") as $attr){
747           if(isset($_POST[$attr])){
748             $smarty->assign("$attr", $_POST[$attr]);
749           }else{
750             $smarty->assign("$attr", "");
751           }
752         }
753         $smarty->assign("templates",$this->templates);
754         $smarty->assign("got_uid",$this->got_uid);
755         $smarty->assign("edit_uid",false);
756         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
758       }
759     }
761     /* New user/template request */
762     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
763       /* By default we set 'dn' to 'new', all relevant plugins will
764          react on this. */
765       $this->dn= "new";
766       
767       if (isset($this->config->current['IDGEN'])){
768         $this->got_uid= false;
769       } else {
770         $this->got_uid= true;
771       }
773       /* Create new usertab object */
774       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
775       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
776       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
778       /* Take care about templates */
779       if ($s_action=="new_tpl"){
780         $this->is_template= TRUE;
781         $this->usertab->set_template_mode ();
782       } else {
783         $this->is_template= FALSE;
784       }
786       /* Use template if there are any of them */
787       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
788         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
789           $smarty->assign("$attr", $this->$attr);
790         }
791         if ($s_action=="create_user_from_tpl"){
792           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
793         } else {
794           $smarty->assign("template", "none");
795         }
796         $smarty->assign("edit_uid", "");
797         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
798       }
799     }
801     /********************
802       Template selected continue edit
803      ********************/
805     /* Continue template editing */
806     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
808       $this->sn             = $_POST['sn'];
809       $this->givenName      = $_POST['givenName'];
811       /* Check for requred values */
812       $message= array();
813       if ($this->sn == "") {
814         $message[]= msgPool::required(_("Name"));
815       }
816       if ($this->givenName == "") {
817         $message[]= msgPool::required(_("Given name"));
818       }
820       /* Check if dn is used */
821       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
822       $ldap= $this->config->get_ldap_link();
823       $ldap->cd ($dn);
824       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
825       if ($ldap->count () != 0){
826         msgPool::duplicated(_("Name"));
827       }
829       /* Show error message / continue editing */
830       if (count($message) > 0){
831         msg_dialog::displayChecks($message);
832       } else {
833         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
834         if (isset($this->config->current['IDGEN']) &&
835             $this->config->current['IDGEN'] != ""){
836           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
837           if (count($uids)){
838             $smarty->assign("edit_uid", "false");
839             $smarty->assign("uids", $uids);
840             $this->uid= current($uids);
841           }
842         } else {
843           $smarty->assign("edit_uid", "");
844           $this->uid= "";
845         }
846         $this->got_uid= true;
847       }
849       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
850         $smarty->assign("$attr", $this->$attr);
851       }
852       if (isset($_POST['template'])){
853         $smarty->assign("template", $_POST['template']);
854       }
855       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
856     }
858     /********************
859       No template selected continue edit
860      ********************/
862     /* No template. Ok. Lets fill data into the normal user dialog */
863     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
864       foreach(array("sn", "givenName", "uid") as $attr){
865         if (isset($_POST[$attr])){
866           $this->usertab->by_object['user']->$attr= $_POST[$attr];
867         }
868       }
869     }
872     /********************
873       Template selected continue edit
874      ********************/
876     /* Finish template preamble */
877     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
879       /* Might not be filled if IDGEN is unset */
880       $this->sn                 = $_POST['sn'];
881       $this->givenName          = $_POST['givenName'];
883       /* Move user supplied data to sub plugins */
884       $this->uid                = $_POST['uid'];
885       $this->usertab->uid       = $this->uid;
886       $this->usertab->sn        = $this->sn;
887       $this->usertab->givenName = $this->givenName;
888       $template_dn              = $_POST['template'];
889       $this->usertab->adapt_from_template($template_dn);
890       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
891       $this->usertab->by_object['user']->base= $template_base;
892     }
893    
894  
895     /********************
896       If no template was selected set base
897      ********************/
899     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
900       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
901     }
904     /********************
905       Display subdialog 
906      ********************/
908     /* Show tab dialog if object is present */
909     if(isset($this->usertab->config)){
911       $display= $this->usertab->execute();
913       /* Don't show buttons if tab dialog requests this */
914       
915         $dia = FALSE;
916         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
917           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
918         }
920         if(!is_object($dia) && $dia != TRUE){
921           $display.= "<p style=\"text-align:right\">\n";
922           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
923           $display.= "&nbsp;\n";
924           if ($this->dn != "new"){
925             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
926             $display.= "&nbsp;\n";
927           }
928           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
929           $display.= "</p>";
930         }
931       return ($display);
932     }
933     
934     /* Check if there is a snapshot dialog open */
935     $base = $this->DivListUsers->selectedBase;
936     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
937       return($str);
938     }
939   
940     /* Return rendered main page */
941         /* Display dialog with system list */
942     $this->DivListUsers->parent = $this;
943     $this->DivListUsers->execute();
945     /* Add departments if subsearch is disabled */
946     if(!$this->DivListUsers->SubSearch){
947       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
948     }
949     $this->reload();
950     $this->DivListUsers->setEntries($this->list);
951     return($this->DivListUsers->Draw());
952   }
955   /* Return departments, that will be included within snapshot detection */
956   function get_used_snapshot_bases()
957   {
958     return(array(get_people_ou().$this->DivListUsers->selectedBase));
959   }  
962   function reload()
963   {
964     /* Set base for all searches */
965     $base= $this->DivListUsers->selectedBase;
966     $this->list =array();
968     /* Get filter configuration */
969     $Regex                = $this->DivListUsers->Regex;
970     $SubSearch            = $this->DivListUsers->SubSearch;
971     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
972     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
973     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
974     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
975     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
976     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
978     /* Setup filter depending on selection */
979     $filter="";
980     if ($this->config->current['SAMBAVERSION'] == 3){
981       $samba= "sambaSamAccount";
982     } else {
983       $samba= "sambaAccount";
984     }
986     if ($ShowFunctionalUsers){
987       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
988                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
989                 "(objectClass=gosaProxyAccount))))";
990     }
991     if ($ShowUnixUsers){
992       $filter.= "(objectClass=posixAccount)";
993     }
994     if ($ShowMailUsers){
995       $filter.= "(objectClass=gosaMailAccount)";
996     }
997     if ($ShowSambaUsers){
998       $filter.= "(objectClass=$samba)";
999     }
1000     if ($ShowProxyUsers){
1001       $filter.= "(objectClass=gosaProxyAccount)";
1002     }
1003     if ($ShowTemplates){
1004       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1005     } else {
1006       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1007     }
1008     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1010     /* Generate userlist */
1011     $ldap= $this->config->get_ldap_link(TRUE);
1013     if ($SubSearch){
1014       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1015                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1016     } else {
1017       $base= get_people_ou().$base;
1018       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1019                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1020     }
1021     $SortTemp = array();
1022     $List = array();
1023     foreach($ListTemp as $Key => $Entry){
1025       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1026        * Else winstations will be listed too, if you use the subtree flag. 
1027        */
1028       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1029         continue;
1030       }else{
1032         // Generate caption for rows
1033         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1034           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1035         } else {
1036           $display= "[".$Entry["uid"][0]."]";
1037         }
1039         $display = strtolower($display);
1040         $List[$display] = $Entry;
1041         $SortTemp[$display] = $display;
1042       }
1043     }
1044     natcasesort($SortTemp);
1045     reset($SortTemp);
1047     $this->list = array();
1048     foreach($SortTemp as $Key){
1049       $this->list[] = $List[$Key];
1050     }
1051   }
1053   function remove_lock()
1054   {
1055     /* Remove user lock if a DN is marked as "currently edited" */
1056     if (isset($this->usertab->dn)){
1057       del_lock ($this->usertab->dn);
1058     }
1059   }
1062   function copyPasteHandling_from_queue($s_action,$s_entry)
1063   {
1064     /* Check if Copy & Paste is disabled */
1065     if(!is_object($this->CopyPasteHandler)){
1066       return("");
1067     }
1069     /* Add a single entry to queue */
1070     if($s_action == "cut" || $s_action == "copy"){
1071       /* Cleanup object queue */
1072       $this->CopyPasteHandler->cleanup_queue();
1073       $dn = $this->list[$s_entry]['dn'];
1074       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1075     }
1077     /* Add entries to queue */
1078     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1080       /* Cleanup object queue */
1081       $this->CopyPasteHandler->cleanup_queue();
1083       /* Add new entries to CP queue */
1084       foreach($this->list_get_selected_items() as $id){
1085         $dn = $this->list[$id]['dn'];
1087         if($s_action == "copy_multiple"){
1088           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1089         }
1090         if($s_action == "cut_multiple"){
1091           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1092         }
1093       }
1094     }
1095     
1096     /* Start pasting entries */
1097     if($s_action == "editPaste"){
1098       $this->start_pasting_copied_objects = TRUE;
1099     }
1101     /* Return C&P dialog */ 
1102     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1103    
1104       /* Get dialog */
1105       $data = $this->CopyPasteHandler->execute();
1106       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1108       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1109       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1110         $s_entry = $this->CopyPasteHandler->last_entry();
1111         $this->reload();
1112         foreach($this->list as $key => $entry){
1113           if($entry['dn'] == $s_entry){
1114             $this->CPPasswordChange = $key;
1115           }
1116         }
1117       }
1119       /* Return dialog data */
1120       if(!empty($data) && $this->CPPasswordChange == ""){
1121         return($data);
1122       }
1123     }
1125     /* Automatically disable status for pasting */ 
1126     if(!$this->CopyPasteHandler->entries_queued()){
1127       $this->start_pasting_copied_objects = FALSE;
1128     }
1129     return("");
1130   }
1133   function save_object()
1134   {
1135     /* Handle divlist filter && department selection*/
1136     if(!is_object($this->usertab)){
1137       $this->DivListUsers->save_object();
1138     }
1139   }
1141     
1142   function list_get_selected_items()
1143   {
1144     $ids = array();
1145     foreach($_POST as $name => $value){
1146       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1147         $id   = preg_replace("/^item_selected_/","",$name);
1148         $ids[$id] = $id;
1149       }
1150     }
1151     return($ids);
1152   }
1153   
1155   /* A set of disabled and therefore overloaded functions. They are
1156      not needed in this class. */
1157   function remove_from_parent() { } 
1158   function check() { } 
1159   function save() { } 
1160   function adapt_from_template($dn) { } 
1161   function password_change_needed() { } 
1163 } /* ... class userManagement */
1164 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1165 ?>