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     }
155     if(isset($_POST['menu_action']) && preg_match("/^templatize_multiple/",$_POST['menu_action'])){
156       $s_action = "templatize_multiple";
157     }
159     if(isset($_POST['menu_action']) && preg_match("/^event/",$_POST['menu_action'])){
160       $s_action = $_POST['menu_action'];
161     }
164     /********************
165       Create notification event 
166      ********************/
168     if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
169       $ids = $this->list_get_selected_items();
170       $uids = array();
171       foreach($ids as $id){
172         $uids[] = $this->list[$id]['uid'][0];
173       }
174       if(count($uids)){
175         $events = DaemonEvent::get_event_types(USER_EVENT);
176         $event = preg_replace("/^event_/","",$s_action);
177         if(isset($events['BY_CLASS'][$event])){
178           $type = $events['BY_CLASS'][$event];
179           $this->usertab = new $type['CLASS_NAME']($this->config);
180           $this->usertab->add_users($uids);
181           $this->usertab->set_type(TRIGGERED_EVENT);
182         }
183       }
184     }
186     /* Abort event dialog */
187     if(isset($_POST['abort_event_dialog'])){
188       $this->usertab = FALSE;
189     }
191     /* Save event */
192     if(isset($_POST['save_event_dialog'])){
193       $this->usertab->save_object();
194       $msgs = $this->usertab->check();
195       if(count($msgs)){
196         msg_dialog::displayChecks($msgs);
197       }else{
199         $o_queue = new gosaSupportDaemon();
200         $o_queue->append($this->usertab);
201         if($o_queue->is_error()){
202           msg_dialog::display(_("Infrastructure error"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
203         }else{
204           $this->usertab = FALSE;
205         }
206       }
207     }
209     /* Display event */
210     if($this->usertab instanceof DaemonEvent){
211       $this->usertab->save_object();
212       return($this->usertab->execute());
213     }
216     /********************
217       Copy & Paste 
218      ********************/
220     /* Display the copy & paste dialog, if it is currently open */
221     if($this->CPPasswordChange == ""){
222       $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
223       if($ret){
224         return($ret);
225       }
226     }
229     /********************
230       Change password confirmed
231      ********************/
233     /* Perform password change */
234     if (isset($_POST['password_finish'])){
236       /* For security reasons, check if user is allowed to set password again */
237       $dn  = $this->dn;
238       $acl = $this->ui->get_permissions($dn, "users/password");
239       $cacl= $this->ui->get_permissions($dn, "users/user");
241       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
243         /* Check input and feed errors into 'message' */
244         $message= array();
246         /* Sanity checks... */
247         if ($_POST['new_password'] != $_POST['repeated_password']){
249           /* Matching passwords in new and repeated? */
250           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
251         } else {
253           /* Empty password is not permitted by default. */
254           if ($_POST['new_password'] == ""){
255             msgPool::required(_("New password"));
256           }
257         }
259         /* Errors, or password change? */
260         if (count($message) != 0){
262           /* Show error message and continue editing */
263           msg_dialog::displayChecks($message);
264           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
265         }
267         $config= $this->config;
268         $ldap_ui= $this->config->get_ldap_link();
269         if(isset($this->usertab->dn)){
270           $ldap_ui->cat($this->usertab->dn,array("uid"));
271           $user = $ldap_ui->fetch();
272         }else{
273           $ldap_ui->cat($this->dn,array("uid"));
274           $user = $ldap_ui->fetch();
275         }
276         if((is_array($user))&&(isset($user['uid']))){
277           $username= $user['uid'][0];
278         }
280         /* Set password, perform required steps */
281         if ($this->usertab){
282           if ($this->usertab->password_change_needed()){
283             $obj= $this->usertab->by_object['user'];
284             change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
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->usertab->dn,array(),"Password has been changed");
289             unset($this->usertab);
290             $this->usertab= NULL;
291           }
292         } else {
293           change_password ($this->dn, $_POST['new_password']);
294           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
295             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
296           }
297           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
298         }
299       } else {
301         /* Missing permissions, show message */
302         msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
303       }
304       /* Clean session, delete lock */
305       del_lock ($this->dn);
306       unset ($this->usertab);
307       $this->usertab= NULL;
308       $this->lognames= array();;
309       $this->sn= "";
310       $this->givenName= "";
311       $this->uid= "";
312       session::un_set('objectinfo');
313     }
316     /********************
317      Change multiple passwords requested 
318      ********************/
319   
320     if($s_action == "multiple_password_change"){
321       $this->pwd_change_queue = $this->list_get_selected_items();
322     }    
325     /********************
326       Change password requested  
327      ********************/
329     /* Password change requested */
330     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
332       /* Get users whose passwords should be changed. */
333       if(count($this->pwd_change_queue)){
334         $s_entry= array_pop($this->pwd_change_queue);
335       }
337       if(!empty($this->CPPasswordChange)){
338         $s_entry = $this->CPPasswordChange;
339         $this->CPPasswordChange = "";
340       }
342       /* Get 'dn' from posted 'uid' */
343       $this->dn= $this->list[trim($s_entry)]['dn'];
345       /* Load permissions for selected 'dn' and check if
346          we're allowed to remove this 'dn' */
347       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
349         /* User is allowed to change passwords, save 'dn' and 'acl' for next
350            dialog. */
351         session::set('objectinfo',$this->dn);
352         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
353         
355       } else {
356         /* User is not allowed. Show message and cancel. */
357         msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
358       }
359     }
363      /********************
364       Edit existing entry
365      ********************/
367     /* User wants to edit data? */
368     if (($s_action=="edit") && (!isset($this->usertab->config))){
370       /* Get 'dn' from posted 'uid', must be unique */
371       $this->dn= $this->list[trim($s_entry)]['dn'];
373       /* Check locking, save current plugin in 'back_plugin', so
374          the dialog knows where to return. */
375       if (($user= get_lock($this->dn)) != ""){
376         return(gen_locked_message ($user, $this->dn));
377       }
379       /* Lock the current entry, so everyone will get the
380          above dialog */
381       add_lock ($this->dn, $this->ui->dn);
383       /* Register usertab to trigger edit dialog */
384       $this->usertab= new usertabs($this->config,
385           $this->config->data['TABS']['USERTABS'], $this->dn);
387       /* Switch tab, if it was requested by the user */
388       $this->usertab->current = $s_tab;
390       /* Set ACL and move DN to the headline */
391       $this->usertab->set_acl_base($this->dn);
392       session::set('objectinfo',$this->dn);
393     }
396     /********************
397       Edit multiple entries
398      ********************/
400     /* User wants to edit data? */
401     if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
403       $this->dn = array();
404       foreach($this->list_get_selected_items() as $id){
405         $this->dn[] = $this->list[$id]['dn'];;
406       }
407       $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
408             $this->dn,$this->DivListUsers->selectedBase,"user");
409       if ($tmp->entries_locked()){
410         return($tmp->display_lock_message());
411       }
412       $tmp->lock_entries($this->ui->dn);
413       if($tmp->multiple_available()){
414         $this->usertab = $tmp;
415         $this->usertab->set_active_tab($s_tab);
416         session::set('objectinfo',$this->usertab->get_object_info());
417       }
418     }
421     /********************
422       Edit canceled 
423      ********************/
425     /* Reset all relevant data, if we get a _cancel request */
426     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
427       if (isset($this->usertab)){
428         del_lock ($this->usertab->dn);
429         unset ($this->usertab);
430       }
431       $this->usertab= NULL;
432       $this->lognames= array();;
433       $this->sn= "";
434       $this->givenName= "";
435       $this->uid= "";
436       session::un_set('objectinfo');
437     }
440     /********************
441       Apply template to multiple entries requested, display confirm dialog
442      ********************/
443     # TODO: show dialog to choose
444     #       * template
445     #       * when the template is choosen refresh the list of
446     #         available attributes with a checkbox - everything checked
447     #       * when pressing apply do the stuff below
448     if ($s_action=="ttemplatize_multiple"){
449       $ids = $this->list_get_selected_items();
450       $this->dns = array();
451       if(count($ids)){
453         foreach($ids as $id){
454           $dn = $this->list[$id]['dn'];
455           if (($user= get_lock($dn)) != ""){
456             return(gen_locked_message ($user, $dn));
457           }
458           $this->dns[$id] = $dn; 
459         }
460       }
462       # Test stuff
463       foreach ($this->dns as $dn){
465         echo "Template test with '$dn'<br>";
466         $template_dn= "cn=super44 super44,ou=people,dc=gonicus,dc=de";
467         $usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
468         $usertab->adapt_from_template($template_dn, array("sn", "givenName", "uid"));
469         $usertab->save();
470         unset ($usertab);
471         $usertab= NULL;
472       }
474     }
477     /********************
478       Delete MULTIPLE entries requested, display confirm dialog
479      ********************/
481     if ($s_action=="del_multiple"){
482       $ids = $this->list_get_selected_items();
483       $this->dns = array();
484       if(count($ids)){
486         foreach($ids as $id){
487           $dn = $this->list[$id]['dn'];
488           if (($user= get_lock($dn)) != ""){
489             return(gen_locked_message ($user, $dn));
490           }
491           $this->dns[$id] = $dn; 
492         }
494         $dns_names = array();
495         foreach($this->dns as $dn){
496           $dns_names[] = @LDAP::fix($dn);
497         }
499         /* Lock the current entry, so nobody will edit it during deletion */
500         $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
501         $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
502         $this->current_action = $s_action;
503       }
504     }
507     /********************
508       Delete MULTIPLE entries confirmed 
509      ********************/
511       /* Confirmation for deletion has been passed. Users should be deleted. */
512       if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
513         
514         $this->current_action = "";  
515         
516         /* Remove user by user and check acls before removeing them */
517         foreach($this->dns as $key => $dn){
519           $acl = $this->ui->get_permissions($dn, "users/user"); 
520           if (preg_match('/d/', $acl)){
522             /* Delete request is permitted, perform LDAP action */
523             $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
524             $this->usertab->set_acl_base();
525             $this->usertab->delete ();
526             unset ($this->usertab);
527             $this->usertab= NULL;
528           } else {
529             msg_dialog::display(_("Warning"),msgPool::permDelete($dn),WARNING_DIALOG);
530             if(isset($this->ui->uid)){
531               new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
532             }
533           }
534           /* Remove lock file after successfull deletion */
535           del_lock ($dn);
536           unset($this->dns[$key]);
537       }
538     }
541     /********************
542       Delete MULTIPLE entries Canceled 
543      ********************/
545     /* Remove lock */
546     if(isset($_POST['delete_multiple_user_cancel'])){
547       foreach($this->dns as $key => $dn){
548         del_lock ($dn);
549         unset($this->dns[$key]);
550       }
551     }
552   
553   
554     /********************
555       Toggle lock status for user
556      ********************/
557   
558     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
560       /* Get entry check current status */
561       $val = $this->list[$s_entry];
562       $pwd = $val['userPassword'][0];
564       if(!preg_match("/^\{[^\}]/",$pwd)){
565         trigger_error("Can not deactivate user which is using clear password encryption.");
566       }else{
568         $locked = false;
569         if(preg_match("/^[^\}]*+\}!/",$pwd)){
570           $locked = true;
571         }
573         /* Create ldap array to update status */
574         $attrs = array("userPassword" => $pwd);
575         if($locked){
576           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
577         }else{
578           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
579         }
581         /* Write new status back to ldap */
582         $ldap = $this->config->get_ldap_link();
583         $ldap->cd($val['dn']);
584         $ldap->modify($attrs);
585         if (!$ldap->success()){
586           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
587         }
588       }
589     }
592     /********************
593       Delete entry requested, display confirm dialog
594      ********************/
596     /* Remove user was requested */
597     if ($s_action=="del"){
599       /* Get 'dn' from posted 'uid' */
600       $this->dn= $this->list[trim($s_entry)]['dn'];
602       /* Load permissions for selected 'dn' and check if
603          we're allowed to remove this 'dn' */
605       /* Check locking, save current plugin in 'back_plugin', so
606          the dialog knows where to return. */
607       if (($user= get_lock($this->dn)) != ""){
608         return(gen_locked_message ($user, $this->dn));
609       }
611       $this->msg_dialog = new msg_dialog( _("Delete user"),msgPool::deleteInfo(@LDAP::fix($this->dn),_("user")),CONFIRM_DIALOG);
612       $this->current_action = $s_action;
613     }
616     /********************
617       Delete entry confirmed 
618      ********************/
620     /* Confirmation for deletion has been passed. User should be deleted. */
621     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
623       $this->current_action = ""; 
624  
625       /* Some nice guy may send this as POST, so we've to check
626          for the permissions again. */
628       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
629  
630       if (preg_match('/d/', $acl)){
632         /* Delete request is permitted, perform LDAP action */
633         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
634         $this->usertab->set_acl_base();
635         $this->usertab->delete ();
636         unset ($this->usertab);
637         $this->usertab= NULL;
638         msg_dialog::display(_("Information"),_("User successfully removed."),INFO_DIALOG);
639       } else {
641         /* Normally this shouldn't be reached, send some extra
642            logs to notify the administrator */
643         msg_dialog::display(_("Warning"),msgPool::permDelete(),WARNING_DIALOG);
645         if(isset($this->ui->uid)){
646           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
647         }
648       }
650       /* Remove lock file after successfull deletion */
651       del_lock ($this->dn);
652     }
654     
655     /********************
656       Delete entry Canceled 
657      ********************/
659     /* Delete user canceled? */
660     if (isset($_POST['delete_cancel'])){
661       del_lock ($this->dn);
662     }
665     /********************
666       Edit entry finished (Save) 
667      ********************/
669     /* Finish user edit is triggered by the tabulator dialog, so
670        the user wants to save edited data. Check and save at this
671        point. */
672     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
674       /* Check tabs, will feed message array */
675       $this->usertab->last= $this->usertab->current;
676       $this->usertab->save_object();
677       $message= $this->usertab->check();
679       /* Save, or display error message? */
680       if (count($message) == 0){
682         /* No errors. Go ahead and prepare to ask for a password
683            in case we're creating a new user. 'dn' will be 'new'
684            in this case. It is set to the correct value later. */
685         if ($this->dn == "new"){
686           $set_pass= 1;
687         } else {
688           $set_pass= 0;
689         }
691         /* Save user data to ldap */
692         if($this->usertab->save() == 1){
693           return;
694         }
696         if (!isset($_POST['edit_apply'])){
697           /* User has been saved successfully, remove lock from LDAP. */
698           if ($this->dn != "new"){
699             del_lock ($this->dn);
700           }
702           /* In case of new users, ask for a password, skip this for templates */
703           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
704             $this->dn = $this->usertab->dn;
705             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
706           }
708           unset ($this->usertab);
709           $this->usertab= NULL;
710           session::un_set('objectinfo');
711         }
712       } else {
713         /* Ok. There seem to be errors regarding to the tab data,
714            show message and continue as usual. */
715         msg_dialog::displayChecks($message);
716       }
717     }
720     /********************
721       We want to create a new user, so fetch all available user templates 
722      ********************/
724     /* Generate template list */
725     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
727       $this->templates= array();
728       $ldap= $this->config->get_ldap_link();
730       /* Create list of templates */
731       foreach ($this->config->departments as $key => $value){
732     
733         /* Get acls from different ou's */
734         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
735  
736         /* If creation of a new user is allowed, append this template */
737         if (preg_match("/c/",$acl)){
738           
739           /* Search all templates from the current dn */
740           $ldap->cd (get_people_ou().$value);
741           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
743           /* Append */
744           if ($ldap->count() != 0){
745             while ($attrs= $ldap->fetch()){
746               $this->templates[$ldap->getDN()]=
747                 $attrs['uid'][0]." - ".@LDAP::fix($key);
748             }
749             $this->templates['none']= _("none");
750           }
751         }
752       }
754       /* Sort templates */
755       natcasesort ($this->templates);
756       reset ($this->templates);
757     }
760     /********************
761       Create a new user,template, user from template 
762      ********************/
764     /* Check selected options for template */
765     if (isset($_POST['template_continue'])){
766       $message = array();
767       if(!isset($_POST['template']) || (empty($_POST['template']))){
768         $message[]= msgPool::invalid(_("Template"));
769       }
770       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
771         $message[]= msgPool::required(_("Name"));
772       }
773       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
774         $message[]= msgPool::required(_("Given name"));
775       }
776     
777       /* Show error message / continue editing */
778       if (count($message) > 0){
779         msg_dialog::displayChecks($message);
781         foreach(array("sn", "givenName", "uid", "template") as $attr){
782           if(isset($_POST[$attr])){
783             $smarty->assign("$attr", $_POST[$attr]);
784           }else{
785             $smarty->assign("$attr", "");
786           }
787         }
788         $smarty->assign("templates",$this->templates);
789         $smarty->assign("got_uid",$this->got_uid);
790         $smarty->assign("edit_uid",false);
791         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
793       }
794     }
796     /* New user/template request */
797     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
798       /* By default we set 'dn' to 'new', all relevant plugins will
799          react on this. */
800       $this->dn= "new";
801       
802       if (isset($this->config->current['IDGEN'])){
803         $this->got_uid= false;
804       } else {
805         $this->got_uid= true;
806       }
808       /* Create new usertab object */
809       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
810       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
811       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
813       /* Take care about templates */
814       if ($s_action=="new_tpl"){
815         $this->is_template= TRUE;
816         $this->usertab->set_template_mode ();
817       } else {
818         $this->is_template= FALSE;
819       }
821       /* Use template if there are any of them */
822       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
823         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
824           $smarty->assign("$attr", $this->$attr);
825         }
826         if ($s_action=="create_user_from_tpl"){
827           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
828         } else {
829           $smarty->assign("template", "none");
830         }
831         $smarty->assign("edit_uid", "");
832         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
833       }
834     }
836     /********************
837       Template selected continue edit
838      ********************/
840     /* Continue template editing */
841     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
843       $this->sn             = $_POST['sn'];
844       $this->givenName      = $_POST['givenName'];
846       /* Check for requred values */
847       $message= array();
848       if ($this->sn == "") {
849         $message[]= msgPool::required(_("Name"));
850       }
851       if ($this->givenName == "") {
852         $message[]= msgPool::required(_("Given name"));
853       }
855       /* Check if dn is used */
856       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
857       $ldap= $this->config->get_ldap_link();
858       $ldap->cd ($dn);
859       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
860       if ($ldap->count () != 0){
861         msgPool::duplicated(_("Name"));
862       }
864       /* Show error message / continue editing */
865       if (count($message) > 0){
866         msg_dialog::displayChecks($message);
867       } else {
868         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
869         if (isset($this->config->current['IDGEN']) &&
870             $this->config->current['IDGEN'] != ""){
871           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
872           if (count($uids)){
873             $smarty->assign("edit_uid", "false");
874             $smarty->assign("uids", $uids);
875             $this->uid= current($uids);
876           }
877         } else {
878           $smarty->assign("edit_uid", "");
879           $this->uid= "";
880         }
881         $this->got_uid= true;
882       }
884       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
885         $smarty->assign("$attr", $this->$attr);
886       }
887       if (isset($_POST['template'])){
888         $smarty->assign("template", $_POST['template']);
889       }
890       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
891     }
893     /********************
894       No template selected continue edit
895      ********************/
897     /* No template. Ok. Lets fill data into the normal user dialog */
898     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
899       foreach(array("sn", "givenName", "uid") as $attr){
900         if (isset($_POST[$attr])){
901           $this->usertab->by_object['user']->$attr= $_POST[$attr];
902         }
903       }
904     }
907     /********************
908       Template selected continue edit
909      ********************/
911     /* Finish template preamble */
912     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
914       /* Might not be filled if IDGEN is unset */
915       $this->sn                 = $_POST['sn'];
916       $this->givenName          = $_POST['givenName'];
918       /* Move user supplied data to sub plugins */
919       $this->uid                = $_POST['uid'];
920       $this->usertab->uid       = $this->uid;
921       $this->usertab->sn        = $this->sn;
922       $this->usertab->givenName = $this->givenName;
923       $template_dn              = $_POST['template'];
924       $this->usertab->adapt_from_template($template_dn);
925       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
926       $this->usertab->by_object['user']->base= $template_base;
927     }
928    
929  
930     /********************
931       If no template was selected set base
932      ********************/
934     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
935       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
936     }
939     /********************
940       Display subdialog 
941      ********************/
943     /* Show tab dialog if object is present */
944     if(isset($this->usertab->config)){
946       $display= $this->usertab->execute();
948       /* Don't show buttons if tab dialog requests this */
949       
950         $dia = FALSE;
951         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
952           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
953         }
955         if(!is_object($dia) && $dia != TRUE){
956           $display.= "<p style=\"text-align:right\">\n";
957           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
958           $display.= "&nbsp;\n";
959           if ($this->dn != "new"){
960             $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
961             $display.= "&nbsp;\n";
962           }
963           $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
964           $display.= "</p>";
965         }
966       return ($display);
967     }
968     
969     /* Check if there is a snapshot dialog open */
970     $base = $this->DivListUsers->selectedBase;
971     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
972       return($str);
973     }
974   
975     /* Return rendered main page */
976         /* Display dialog with system list */
977     $this->DivListUsers->parent = $this;
978     $this->DivListUsers->execute();
980     /* Add departments if subsearch is disabled */
981     if(!$this->DivListUsers->SubSearch){
982       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
983     }
984     $this->reload();
985     $this->DivListUsers->setEntries($this->list);
986     return($this->DivListUsers->Draw());
987   }
990   /* Return departments, that will be included within snapshot detection */
991   function get_used_snapshot_bases()
992   {
993     return(array(get_people_ou().$this->DivListUsers->selectedBase));
994   }  
997   function reload()
998   {
999     /* Set base for all searches */
1000     $base= $this->DivListUsers->selectedBase;
1001     $this->list =array();
1003     /* Get filter configuration */
1004     $Regex                = $this->DivListUsers->Regex;
1005     $SubSearch            = $this->DivListUsers->SubSearch;
1006     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
1007     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
1008     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
1009     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
1010     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
1011     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
1013     /* Setup filter depending on selection */
1014     $filter="";
1015     if ($this->config->current['SAMBAVERSION'] == 3){
1016       $samba= "sambaSamAccount";
1017     } else {
1018       $samba= "sambaAccount";
1019     }
1021     if ($ShowFunctionalUsers){
1022       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
1023                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
1024                 "(objectClass=gosaProxyAccount))))";
1025     }
1026     if ($ShowUnixUsers){
1027       $filter.= "(objectClass=posixAccount)";
1028     }
1029     if ($ShowMailUsers){
1030       $filter.= "(objectClass=gosaMailAccount)";
1031     }
1032     if ($ShowSambaUsers){
1033       $filter.= "(objectClass=$samba)";
1034     }
1035     if ($ShowProxyUsers){
1036       $filter.= "(objectClass=gosaProxyAccount)";
1037     }
1038     if ($ShowTemplates){
1039       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1040     } else {
1041       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1042     }
1043     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1045     /* Generate userlist */
1046     $ldap= $this->config->get_ldap_link(TRUE);
1048     if ($SubSearch){
1049       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1050                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1051     } else {
1052       $base= get_people_ou().$base;
1053       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1054                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1055     }
1056     $SortTemp = array();
1057     $List = array();
1058     foreach($ListTemp as $Key => $Entry){
1060       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1061        * Else winstations will be listed too, if you use the subtree flag. 
1062        */
1063       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1064         continue;
1065       }else{
1067         // Generate caption for rows
1068         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1069           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1070         } else {
1071           $display= "[".$Entry["uid"][0]."]";
1072         }
1074         $display = strtolower($display);
1075         $List[$display] = $Entry;
1076         $SortTemp[$display] = $display;
1077       }
1078     }
1079     natcasesort($SortTemp);
1080     reset($SortTemp);
1082     $this->list = array();
1083     foreach($SortTemp as $Key){
1084       $this->list[] = $List[$Key];
1085     }
1086   }
1088   function remove_lock()
1089   {
1090     /* Remove user lock if a DN is marked as "currently edited" */
1091     if (isset($this->usertab->dn)){
1092       del_lock ($this->usertab->dn);
1093     }
1094   }
1097   function copyPasteHandling_from_queue($s_action,$s_entry)
1098   {
1099     /* Check if Copy & Paste is disabled */
1100     if(!is_object($this->CopyPasteHandler)){
1101       return("");
1102     }
1104     /* Add a single entry to queue */
1105     if($s_action == "cut" || $s_action == "copy"){
1106       /* Cleanup object queue */
1107       $this->CopyPasteHandler->cleanup_queue();
1108       $dn = $this->list[$s_entry]['dn'];
1109       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1110     }
1112     /* Add entries to queue */
1113     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1115       /* Cleanup object queue */
1116       $this->CopyPasteHandler->cleanup_queue();
1118       /* Add new entries to CP queue */
1119       foreach($this->list_get_selected_items() as $id){
1120         $dn = $this->list[$id]['dn'];
1122         if($s_action == "copy_multiple"){
1123           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1124         }
1125         if($s_action == "cut_multiple"){
1126           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1127         }
1128       }
1129     }
1130     
1131     /* Start pasting entries */
1132     if($s_action == "editPaste"){
1133       $this->start_pasting_copied_objects = TRUE;
1134     }
1136     /* Return C&P dialog */ 
1137     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1138    
1139       /* Get dialog */
1140       $data = $this->CopyPasteHandler->execute();
1141       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1143       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1144       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1145         $s_entry = $this->CopyPasteHandler->last_entry();
1146         $this->reload();
1147         foreach($this->list as $key => $entry){
1148           if($entry['dn'] == $s_entry){
1149             $this->CPPasswordChange = $key;
1150           }
1151         }
1152       }
1154       /* Return dialog data */
1155       if(!empty($data) && $this->CPPasswordChange == ""){
1156         return($data);
1157       }
1158     }
1160     /* Automatically disable status for pasting */ 
1161     if(!$this->CopyPasteHandler->entries_queued()){
1162       $this->start_pasting_copied_objects = FALSE;
1163     }
1164     return("");
1165   }
1168   function save_object()
1169   {
1170     /* Handle divlist filter && department selection*/
1171     if(!is_object($this->usertab)){
1172       $this->DivListUsers->save_object();
1173     }
1174   }
1176     
1177   function list_get_selected_items()
1178   {
1179     $ids = array();
1180     foreach($_POST as $name => $value){
1181       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1182         $id   = preg_replace("/^item_selected_/","",$name);
1183         $ids[$id] = $id;
1184       }
1185     }
1186     return($ids);
1187   }
1188   
1190   /* A set of disabled and therefore overloaded functions. They are
1191      not needed in this class. */
1192   function remove_from_parent() { } 
1193   function check() { } 
1194   function save() { } 
1195   function adapt_from_template($dn, $skip= array()) { } 
1196   function password_change_needed() { } 
1198 } /* ... class userManagement */
1199 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1200 ?>