Code

Added dummy function for adapting from templates for existing entries
[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     }
439     /* Apply templates */
440     if ($s_action=="templatize_multiple"){
441       $ids = $this->list_get_selected_items();
442       $this->dns = array();
443       if(count($ids)){
445         foreach($ids as $id){
446           $dn = $this->list[$id]['dn'];
447           if (($user= get_lock($dn)) != ""){
448             return(gen_locked_message ($user, $dn));
449           }
450           $this->dns[$id] = $dn; 
451         }
452       }
454       # Test stuff
455       foreach ($this->dns as $dn){
457         echo "Template test with '$dn'<br>";
458         $template_dn= "cn=super44 super44,ou=people,dc=gonicus,dc=de";
459         $usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
460         $usertab->adapt_from_template($template_dn, array("sn", "givenName", "uid"));
461         #$usertab->save();
462         unset ($usertab);
463         $usertab= NULL;
464       }
466     }
468     /********************
469       Delete MULTIPLE entries requested, display confirm dialog
470      ********************/
472     if ($s_action=="del_multiple"){
473       $ids = $this->list_get_selected_items();
474       $this->dns = array();
475       if(count($ids)){
477         foreach($ids as $id){
478           $dn = $this->list[$id]['dn'];
479           if (($user= get_lock($dn)) != ""){
480             return(gen_locked_message ($user, $dn));
481           }
482           $this->dns[$id] = $dn; 
483         }
485         $dns_names = array();
486         foreach($this->dns as $dn){
487           $dns_names[] = @LDAP::fix($dn);
488         }
490         /* Lock the current entry, so nobody will edit it during deletion */
491         $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
492         $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
493         $this->current_action = $s_action;
494       }
495     }
498     /********************
499       Delete MULTIPLE entries confirmed 
500      ********************/
502       /* Confirmation for deletion has been passed. Users should be deleted. */
503       if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
504         
505         $this->current_action = "";  
506         
507         /* Remove user by user and check acls before removeing them */
508         foreach($this->dns as $key => $dn){
510           $acl = $this->ui->get_permissions($dn, "users/user"); 
511           if (preg_match('/d/', $acl)){
513             /* Delete request is permitted, perform LDAP action */
514             $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
515             $this->usertab->set_acl_base();
516             $this->usertab->delete ();
517             unset ($this->usertab);
518             $this->usertab= NULL;
519           } else {
520             msg_dialog::display(_("Warning"),msgPool::permDelete($dn),WARNING_DIALOG);
521             if(isset($this->ui->uid)){
522               new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
523             }
524           }
525           /* Remove lock file after successfull deletion */
526           del_lock ($dn);
527           unset($this->dns[$key]);
528       }
529     }
532     /********************
533       Delete MULTIPLE entries Canceled 
534      ********************/
536     /* Remove lock */
537     if(isset($_POST['delete_multiple_user_cancel'])){
538       foreach($this->dns as $key => $dn){
539         del_lock ($dn);
540         unset($this->dns[$key]);
541       }
542     }
543   
544   
545     /********************
546       Toggle lock status for user
547      ********************/
548   
549     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
551       /* Get entry check current status */
552       $val = $this->list[$s_entry];
553       $pwd = $val['userPassword'][0];
555       if(!preg_match("/^\{[^\}]/",$pwd)){
556         trigger_error("Can not deactivate user which is using clear password encryption.");
557       }else{
559         $locked = false;
560         if(preg_match("/^[^\}]*+\}!/",$pwd)){
561           $locked = true;
562         }
564         /* Create ldap array to update status */
565         $attrs = array("userPassword" => $pwd);
566         if($locked){
567           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
568         }else{
569           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
570         }
572         /* Write new status back to ldap */
573         $ldap = $this->config->get_ldap_link();
574         $ldap->cd($val['dn']);
575         $ldap->modify($attrs);
576         if (!$ldap->success()){
577           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
578         }
579       }
580     }
583     /********************
584       Delete entry requested, display confirm dialog
585      ********************/
587     /* Remove user was requested */
588     if ($s_action=="del"){
590       /* Get 'dn' from posted 'uid' */
591       $this->dn= $this->list[trim($s_entry)]['dn'];
593       /* Load permissions for selected 'dn' and check if
594          we're allowed to remove this 'dn' */
596       /* Check locking, save current plugin in 'back_plugin', so
597          the dialog knows where to return. */
598       if (($user= get_lock($this->dn)) != ""){
599         return(gen_locked_message ($user, $this->dn));
600       }
602       $this->msg_dialog = new msg_dialog( _("Delete user"),msgPool::deleteInfo(@LDAP::fix($this->dn),_("user")),CONFIRM_DIALOG);
603       $this->current_action = $s_action;
604     }
607     /********************
608       Delete entry confirmed 
609      ********************/
611     /* Confirmation for deletion has been passed. User should be deleted. */
612     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
614       $this->current_action = ""; 
615  
616       /* Some nice guy may send this as POST, so we've to check
617          for the permissions again. */
619       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
620  
621       if (preg_match('/d/', $acl)){
623         /* Delete request is permitted, perform LDAP action */
624         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
625         $this->usertab->set_acl_base();
626         $this->usertab->delete ();
627         unset ($this->usertab);
628         $this->usertab= NULL;
629         msg_dialog::display(_("Information"),_("User successfully removed."),INFO_DIALOG);
630       } else {
632         /* Normally this shouldn't be reached, send some extra
633            logs to notify the administrator */
634         msg_dialog::display(_("Warning"),msgPool::permDelete(),WARNING_DIALOG);
636         if(isset($this->ui->uid)){
637           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
638         }
639       }
641       /* Remove lock file after successfull deletion */
642       del_lock ($this->dn);
643     }
645     
646     /********************
647       Delete entry Canceled 
648      ********************/
650     /* Delete user canceled? */
651     if (isset($_POST['delete_cancel'])){
652       del_lock ($this->dn);
653     }
656     /********************
657       Edit entry finished (Save) 
658      ********************/
660     /* Finish user edit is triggered by the tabulator dialog, so
661        the user wants to save edited data. Check and save at this
662        point. */
663     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
665       /* Check tabs, will feed message array */
666       $this->usertab->last= $this->usertab->current;
667       $this->usertab->save_object();
668       $message= $this->usertab->check();
670       /* Save, or display error message? */
671       if (count($message) == 0){
673         /* No errors. Go ahead and prepare to ask for a password
674            in case we're creating a new user. 'dn' will be 'new'
675            in this case. It is set to the correct value later. */
676         if ($this->dn == "new"){
677           $set_pass= 1;
678         } else {
679           $set_pass= 0;
680         }
682         /* Save user data to ldap */
683         if($this->usertab->save() == 1){
684           return;
685         }
687         if (!isset($_POST['edit_apply'])){
688           /* User has been saved successfully, remove lock from LDAP. */
689           if ($this->dn != "new"){
690             del_lock ($this->dn);
691           }
693           /* In case of new users, ask for a password, skip this for templates */
694           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
695             $this->dn = $this->usertab->dn;
696             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
697           }
699           unset ($this->usertab);
700           $this->usertab= NULL;
701           session::un_set('objectinfo');
702         }
703       } else {
704         /* Ok. There seem to be errors regarding to the tab data,
705            show message and continue as usual. */
706         msg_dialog::displayChecks($message);
707       }
708     }
711     /********************
712       We want to create a new user, so fetch all available user templates 
713      ********************/
715     /* Generate template list */
716     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
718       $this->templates= array();
719       $ldap= $this->config->get_ldap_link();
721       /* Create list of templates */
722       foreach ($this->config->departments as $key => $value){
723     
724         /* Get acls from different ou's */
725         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
726  
727         /* If creation of a new user is allowed, append this template */
728         if (preg_match("/c/",$acl)){
729           
730           /* Search all templates from the current dn */
731           $ldap->cd (get_people_ou().$value);
732           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
734           /* Append */
735           if ($ldap->count() != 0){
736             while ($attrs= $ldap->fetch()){
737               $this->templates[$ldap->getDN()]=
738                 $attrs['uid'][0]." - ".@LDAP::fix($key);
739             }
740             $this->templates['none']= _("none");
741           }
742         }
743       }
745       /* Sort templates */
746       natcasesort ($this->templates);
747       reset ($this->templates);
748     }
751     /********************
752       Create a new user,template, user from template 
753      ********************/
755     /* Check selected options for template */
756     if (isset($_POST['template_continue'])){
757       $message = array();
758       if(!isset($_POST['template']) || (empty($_POST['template']))){
759         $message[]= msgPool::invalid(_("Template"));
760       }
761       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
762         $message[]= msgPool::required(_("Name"));
763       }
764       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
765         $message[]= msgPool::required(_("Given name"));
766       }
767     
768       /* Show error message / continue editing */
769       if (count($message) > 0){
770         msg_dialog::displayChecks($message);
772         foreach(array("sn", "givenName", "uid", "template") as $attr){
773           if(isset($_POST[$attr])){
774             $smarty->assign("$attr", $_POST[$attr]);
775           }else{
776             $smarty->assign("$attr", "");
777           }
778         }
779         $smarty->assign("templates",$this->templates);
780         $smarty->assign("got_uid",$this->got_uid);
781         $smarty->assign("edit_uid",false);
782         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
784       }
785     }
787     /* New user/template request */
788     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
789       /* By default we set 'dn' to 'new', all relevant plugins will
790          react on this. */
791       $this->dn= "new";
792       
793       if (isset($this->config->current['IDGEN'])){
794         $this->got_uid= false;
795       } else {
796         $this->got_uid= true;
797       }
799       /* Create new usertab object */
800       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
801       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
802       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
804       /* Take care about templates */
805       if ($s_action=="new_tpl"){
806         $this->is_template= TRUE;
807         $this->usertab->set_template_mode ();
808       } else {
809         $this->is_template= FALSE;
810       }
812       /* Use template if there are any of them */
813       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
814         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
815           $smarty->assign("$attr", $this->$attr);
816         }
817         if ($s_action=="create_user_from_tpl"){
818           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
819         } else {
820           $smarty->assign("template", "none");
821         }
822         $smarty->assign("edit_uid", "");
823         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
824       }
825     }
827     /********************
828       Template selected continue edit
829      ********************/
831     /* Continue template editing */
832     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
834       $this->sn             = $_POST['sn'];
835       $this->givenName      = $_POST['givenName'];
837       /* Check for requred values */
838       $message= array();
839       if ($this->sn == "") {
840         $message[]= msgPool::required(_("Name"));
841       }
842       if ($this->givenName == "") {
843         $message[]= msgPool::required(_("Given name"));
844       }
846       /* Check if dn is used */
847       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
848       $ldap= $this->config->get_ldap_link();
849       $ldap->cd ($dn);
850       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
851       if ($ldap->count () != 0){
852         msgPool::duplicated(_("Name"));
853       }
855       /* Show error message / continue editing */
856       if (count($message) > 0){
857         msg_dialog::displayChecks($message);
858       } else {
859         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
860         if (isset($this->config->current['IDGEN']) &&
861             $this->config->current['IDGEN'] != ""){
862           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
863           if (count($uids)){
864             $smarty->assign("edit_uid", "false");
865             $smarty->assign("uids", $uids);
866             $this->uid= current($uids);
867           }
868         } else {
869           $smarty->assign("edit_uid", "");
870           $this->uid= "";
871         }
872         $this->got_uid= true;
873       }
875       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
876         $smarty->assign("$attr", $this->$attr);
877       }
878       if (isset($_POST['template'])){
879         $smarty->assign("template", $_POST['template']);
880       }
881       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
882     }
884     /********************
885       No template selected continue edit
886      ********************/
888     /* No template. Ok. Lets fill data into the normal user dialog */
889     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
890       foreach(array("sn", "givenName", "uid") as $attr){
891         if (isset($_POST[$attr])){
892           $this->usertab->by_object['user']->$attr= $_POST[$attr];
893         }
894       }
895     }
898     /********************
899       Template selected continue edit
900      ********************/
902     /* Finish template preamble */
903     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
905       /* Might not be filled if IDGEN is unset */
906       $this->sn                 = $_POST['sn'];
907       $this->givenName          = $_POST['givenName'];
909       /* Move user supplied data to sub plugins */
910       $this->uid                = $_POST['uid'];
911       $this->usertab->uid       = $this->uid;
912       $this->usertab->sn        = $this->sn;
913       $this->usertab->givenName = $this->givenName;
914       $template_dn              = $_POST['template'];
915       $this->usertab->adapt_from_template($template_dn);
916       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
917       $this->usertab->by_object['user']->base= $template_base;
918     }
919    
920  
921     /********************
922       If no template was selected set base
923      ********************/
925     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
926       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
927     }
930     /********************
931       Display subdialog 
932      ********************/
934     /* Show tab dialog if object is present */
935     if(isset($this->usertab->config)){
937       $display= $this->usertab->execute();
939       /* Don't show buttons if tab dialog requests this */
940       
941         $dia = FALSE;
942         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
943           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
944         }
946         if(!is_object($dia) && $dia != TRUE){
947           $display.= "<p style=\"text-align:right\">\n";
948           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
949           $display.= "&nbsp;\n";
950           if ($this->dn != "new"){
951             $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
952             $display.= "&nbsp;\n";
953           }
954           $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
955           $display.= "</p>";
956         }
957       return ($display);
958     }
959     
960     /* Check if there is a snapshot dialog open */
961     $base = $this->DivListUsers->selectedBase;
962     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
963       return($str);
964     }
965   
966     /* Return rendered main page */
967         /* Display dialog with system list */
968     $this->DivListUsers->parent = $this;
969     $this->DivListUsers->execute();
971     /* Add departments if subsearch is disabled */
972     if(!$this->DivListUsers->SubSearch){
973       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
974     }
975     $this->reload();
976     $this->DivListUsers->setEntries($this->list);
977     return($this->DivListUsers->Draw());
978   }
981   /* Return departments, that will be included within snapshot detection */
982   function get_used_snapshot_bases()
983   {
984     return(array(get_people_ou().$this->DivListUsers->selectedBase));
985   }  
988   function reload()
989   {
990     /* Set base for all searches */
991     $base= $this->DivListUsers->selectedBase;
992     $this->list =array();
994     /* Get filter configuration */
995     $Regex                = $this->DivListUsers->Regex;
996     $SubSearch            = $this->DivListUsers->SubSearch;
997     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
998     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
999     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
1000     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
1001     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
1002     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
1004     /* Setup filter depending on selection */
1005     $filter="";
1006     if ($this->config->current['SAMBAVERSION'] == 3){
1007       $samba= "sambaSamAccount";
1008     } else {
1009       $samba= "sambaAccount";
1010     }
1012     if ($ShowFunctionalUsers){
1013       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
1014                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
1015                 "(objectClass=gosaProxyAccount))))";
1016     }
1017     if ($ShowUnixUsers){
1018       $filter.= "(objectClass=posixAccount)";
1019     }
1020     if ($ShowMailUsers){
1021       $filter.= "(objectClass=gosaMailAccount)";
1022     }
1023     if ($ShowSambaUsers){
1024       $filter.= "(objectClass=$samba)";
1025     }
1026     if ($ShowProxyUsers){
1027       $filter.= "(objectClass=gosaProxyAccount)";
1028     }
1029     if ($ShowTemplates){
1030       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1031     } else {
1032       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1033     }
1034     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1036     /* Generate userlist */
1037     $ldap= $this->config->get_ldap_link(TRUE);
1039     if ($SubSearch){
1040       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1041                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1042     } else {
1043       $base= get_people_ou().$base;
1044       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1045                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1046     }
1047     $SortTemp = array();
1048     $List = array();
1049     foreach($ListTemp as $Key => $Entry){
1051       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1052        * Else winstations will be listed too, if you use the subtree flag. 
1053        */
1054       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1055         continue;
1056       }else{
1058         // Generate caption for rows
1059         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1060           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1061         } else {
1062           $display= "[".$Entry["uid"][0]."]";
1063         }
1065         $display = strtolower($display);
1066         $List[$display] = $Entry;
1067         $SortTemp[$display] = $display;
1068       }
1069     }
1070     natcasesort($SortTemp);
1071     reset($SortTemp);
1073     $this->list = array();
1074     foreach($SortTemp as $Key){
1075       $this->list[] = $List[$Key];
1076     }
1077   }
1079   function remove_lock()
1080   {
1081     /* Remove user lock if a DN is marked as "currently edited" */
1082     if (isset($this->usertab->dn)){
1083       del_lock ($this->usertab->dn);
1084     }
1085   }
1088   function copyPasteHandling_from_queue($s_action,$s_entry)
1089   {
1090     /* Check if Copy & Paste is disabled */
1091     if(!is_object($this->CopyPasteHandler)){
1092       return("");
1093     }
1095     /* Add a single entry to queue */
1096     if($s_action == "cut" || $s_action == "copy"){
1097       /* Cleanup object queue */
1098       $this->CopyPasteHandler->cleanup_queue();
1099       $dn = $this->list[$s_entry]['dn'];
1100       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1101     }
1103     /* Add entries to queue */
1104     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1106       /* Cleanup object queue */
1107       $this->CopyPasteHandler->cleanup_queue();
1109       /* Add new entries to CP queue */
1110       foreach($this->list_get_selected_items() as $id){
1111         $dn = $this->list[$id]['dn'];
1113         if($s_action == "copy_multiple"){
1114           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1115         }
1116         if($s_action == "cut_multiple"){
1117           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1118         }
1119       }
1120     }
1121     
1122     /* Start pasting entries */
1123     if($s_action == "editPaste"){
1124       $this->start_pasting_copied_objects = TRUE;
1125     }
1127     /* Return C&P dialog */ 
1128     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1129    
1130       /* Get dialog */
1131       $data = $this->CopyPasteHandler->execute();
1132       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1134       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1135       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1136         $s_entry = $this->CopyPasteHandler->last_entry();
1137         $this->reload();
1138         foreach($this->list as $key => $entry){
1139           if($entry['dn'] == $s_entry){
1140             $this->CPPasswordChange = $key;
1141           }
1142         }
1143       }
1145       /* Return dialog data */
1146       if(!empty($data) && $this->CPPasswordChange == ""){
1147         return($data);
1148       }
1149     }
1151     /* Automatically disable status for pasting */ 
1152     if(!$this->CopyPasteHandler->entries_queued()){
1153       $this->start_pasting_copied_objects = FALSE;
1154     }
1155     return("");
1156   }
1159   function save_object()
1160   {
1161     /* Handle divlist filter && department selection*/
1162     if(!is_object($this->usertab)){
1163       $this->DivListUsers->save_object();
1164     }
1165   }
1167     
1168   function list_get_selected_items()
1169   {
1170     $ids = array();
1171     foreach($_POST as $name => $value){
1172       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1173         $id   = preg_replace("/^item_selected_/","",$name);
1174         $ids[$id] = $id;
1175       }
1176     }
1177     return($ids);
1178   }
1179   
1181   /* A set of disabled and therefore overloaded functions. They are
1182      not needed in this class. */
1183   function remove_from_parent() { } 
1184   function check() { } 
1185   function save() { } 
1186   function adapt_from_template($dn, $skip= array()) { } 
1187   function password_change_needed() { } 
1189 } /* ... class userManagement */
1190 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1191 ?>