Code

Reverted debug changes. ..
[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 $msg_dialog= NULL;
42   
43   var $dns =array();
45   function userManagement(&$config, $ui)
46   {
47     /* Save configuration for internal use */
48     $this->config= &$config;
49     $this->ui= &$ui;
51     /* Copy & Paste handler */
52     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
53       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
54     }
56     /* Creat dialog object */
57     $this->DivListUsers = new divListUsers($this->config,$this);
59   }
62   function execute()
63   {
64     /* Call parent execute */
65     plugin::execute();
67     /* LOCK MESSAGE Vars */
68     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/","/^item_selected/","/^remove_multiple_users/","/^multiple_edit/","/menu_action/"));
70     $smarty       = get_smarty();                 // Smarty instance
71     $s_action     = "";                           // Contains the action to be taken
72     $s_entry      = "";                           // The value for s_action
74     /* Edit entry button pressed? */
75     if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
76       $s_action= "edit";
77       $s_entry= validate($_GET['id']);
78     }
80     /* Test relevant POST values */  
81     foreach($_POST as $key => $val){
83       /* Get every possible POST combination and set s_action/s_entry accordingly */
84       foreach(array("del"       => "user_del",    
85                     "edit"      => "user_edit",
86                     "new"       => "user_new",
87                     "new_tpl"   => "user_tplnew",
88                     "del_multiple" => "^remove_multiple_users",
89                     "create_user_from_tpl"          => "userfrom_tpl",
90                     "change_pw" => "user_chgpw", 
91                     "editPaste" => "editPaste",  
92                     "copy_multiple" => "multiple_copy_users",
93                     "multiple_edit" => "multiple_edit",
94                     "cut_multiple" => "multiple_cut_users",
95                     "multiple_password_change" => "multiple_password_change",
96                     "copy"      => "^copy",
97                     "toggle_lock_status" => "toggle_lock_status",
98                     "cut"       => "^cut") as $act => $name){
100         if (preg_match("/".$name.".*/", $key)){
101           $s_action= $act;
102           $s_entry= preg_replace("/".$name."_/i", "", $key);
103           break;
104         }
105       }
106       
107     } /* ...Test POST */
109     /* Remove coordinate prefix from POST, required by some browsers */
110     $s_entry= preg_replace("/_.$/", "", $s_entry);
112     /* Seperate possibly encoded tab and entry, default to tab "user" */
113     if(preg_match("/.*-.*/", $s_entry)){
114       $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
115       $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
116     }else{
117       $s_tab= "user";
118     }
120     if(!$this->config->search($s_tab, 'class',array('tabs'))){
121       $s_tab = "user";
122     }
124     if (isset($_POST['menu_action'])){
126             /* handle C&P from layers menu */
127             if(preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
128               $s_action = "copy_multiple";
129             }
130             if(preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
131               $s_action = "cut_multiple";
132             }
133             if(preg_match("/^editPaste/",$_POST['menu_action'])){
134               $s_action = "editPaste";
135             }
137             /* Create options */
138             if($_POST['menu_action'] == "user_new"){
139               $s_action = "new";
140             }
141             if($_POST['menu_action'] == "user_tplnew"){
142               $s_action = "new_tpl";
143             }
144             if($_POST['menu_action'] == "multiple_edit"){
145               $s_action = "multiple_edit";
146             }
148             /* handle remove from layers menu */
149             if(preg_match("/^multiple_password_change/",$_POST['menu_action'])){
150               $s_action = "multiple_password_change";
151             }
153             /* handle remove from layers menu */
154             if(preg_match("/^remove_multiple/",$_POST['menu_action'])){
155               $s_action = "del_multiple";
156             }
157             if(preg_match("/^templatize_multiple/",$_POST['menu_action'])){
158               $s_action = "templatize_multiple";
159             }
161             if(preg_match("/^event/",$_POST['menu_action'])){
162               $s_action = $_POST['menu_action'];
163             }
164     }
166     /* Use template */
167     if(isset($_POST['templatize_continue'])){
168       $s_action = "templatize_continue";
169     }
172     /********************
173       Create notification event 
174      ********************/
176     if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
177       $ids = $this->list_get_selected_items();
178       $uids = array();
179       foreach($ids as $id){
180         $uids[] = $this->list[$id]['uid'][0];
181       }
182       if(count($uids)){
183         $events = DaemonEvent::get_event_types(USER_EVENT);
184         $event = preg_replace("/^event_/","",$s_action);
185         if(isset($events['BY_CLASS'][$event])){
186           $type = $events['BY_CLASS'][$event];
187           $this->usertab = new $type['CLASS_NAME']($this->config);
188           $this->usertab->add_users($uids);
189           $this->usertab->set_type(TRIGGERED_EVENT);
190         }
191       }
192     }
194     /* Abort event dialog */
195     if(isset($_POST['abort_event_dialog'])){
196       $this->usertab = FALSE;
197     }
199     /* Save event */
200     if(isset($_POST['save_event_dialog'])){
201       $this->usertab->save_object();
202       $msgs = $this->usertab->check();
203       if(count($msgs)){
204         msg_dialog::displayChecks($msgs);
205       }else{
207         $o_queue = new gosaSupportDaemon();
208         $o_queue->append($this->usertab);
209         if($o_queue->is_error()){
210           msg_dialog::display(_("Infrastructure error"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
211         }else{
212           $this->usertab = FALSE;
213         }
214       }
215     }
217     /* Display event */
218     if($this->usertab instanceof DaemonEvent){
219       $this->usertab->save_object();
220       return($this->usertab->execute());
221     }
224     /********************
225       Copy & Paste 
226      ********************/
228     /* Display the copy & paste dialog, if it is currently open */
229     if($this->CPPasswordChange == ""){
230       $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
231       if($ret){
232         return($ret);
233       }
234     }
237     /********************
238       Change password confirmed
239      ********************/
241     /* Perform password change */
242     if (isset($_POST['password_finish'])){
244       /* For security reasons, check if user is allowed to set password again */
245       $dn  = $this->dn;
246       $acl = $this->ui->get_permissions($dn, "users/password");
247       $cacl= $this->ui->get_permissions($dn, "users/user");
249       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
251         /* Check input and feed errors into 'message' */
252         $message= array();
254         /* Sanity checks... */
255         if ($_POST['new_password'] != $_POST['repeated_password']){
257           /* Matching passwords in new and repeated? */
258           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
259         } else {
261           /* Empty password is not permitted by default. */
262           if ($_POST['new_password'] == ""){
263             msgPool::required(_("New password"));
264           }
265         }
267         /* Errors, or password change? */
268         if (count($message) != 0){
270           /* Show error message and continue editing */
271           msg_dialog::displayChecks($message);
272           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
273         }
275         $config= $this->config;
276         $ldap_ui= $this->config->get_ldap_link();
277         if(isset($this->usertab->dn)){
278           $ldap_ui->cat($this->usertab->dn,array("uid"));
279           $user = $ldap_ui->fetch();
280         }else{
281           $ldap_ui->cat($this->dn,array("uid"));
282           $user = $ldap_ui->fetch();
283         }
284         if((is_array($user))&&(isset($user['uid']))){
285           $username= $user['uid'][0];
286         }
288         /* Set password, perform required steps */
289         if ($this->usertab){
290           if ($this->usertab->password_change_needed()){
291             $obj= $this->usertab->by_object['user'];
292             if(!change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage)){
293               return($smarty->fetch(get_template_path('password.tpl', TRUE)));
294             }
295             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
296               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
297             }
298             new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
299             unset($this->usertab);
300             $this->usertab= NULL;
301           }
302         } else {
303           if(!change_password ($this->dn, $_POST['new_password'])){
304             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
305           }
306           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
307             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
308           }
309           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
310         }
311       } else {
313         /* Missing permissions, show message */
314         msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
315       }
316       /* Clean session, delete lock */
317       $this->remove_lock();
318       unset ($this->usertab);
319       $this->usertab= NULL;
320       $this->lognames= array();;
321       $this->sn= "";
322       $this->givenName= "";
323       $this->uid= "";
324       session::un_set('objectinfo');
325     }
328     /********************
329      Change multiple passwords requested 
330      ********************/
331   
332     if($s_action == "multiple_password_change"){
333       $this->pwd_change_queue = $this->list_get_selected_items();
334     }    
337     /********************
338       Change password requested  
339      ********************/
341     /* Password change requested */
342     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
344       /* Get users whose passwords should be changed. */
345       if(count($this->pwd_change_queue)){
346         $s_entry= array_pop($this->pwd_change_queue);
347       }
349       if(!empty($this->CPPasswordChange)){
350         $s_entry = $this->CPPasswordChange;
351         $this->CPPasswordChange = "";
352       }
354       /* Get 'dn' from posted 'uid' */
355       $this->dn= $this->list[trim($s_entry)]['dn'];
357       /* Load permissions for selected 'dn' and check if
358          we're allowed to remove this 'dn' */
359       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
361         /* User is allowed to change passwords, save 'dn' and 'acl' for next
362            dialog. */
363         session::set('objectinfo',$this->dn);
364         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
365         
367       } else {
368         /* User is not allowed. Show message and cancel. */
369         msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
370       }
371     }
375      /********************
376       Edit existing entry
377      ********************/
380     /* User wants to edit data? */
381     if (($s_action=="edit") && (!isset($this->usertab->config))){
383       /* Get 'dn' from posted 'uid', must be unique */
384       $this->dn= $this->list[trim($s_entry)]['dn'];
386       /* Check locking, save current plugin in 'back_plugin', so
387          the dialog knows where to return. */
388       if (($user= get_lock($this->dn)) != ""){
389         return(gen_locked_message ($user, $this->dn));
390       }
392       /* Lock the current entry, so everyone will get the
393          above dialog */
394       add_lock ($this->dn, $this->ui->dn);
396       /* Register usertab to trigger edit dialog */
397       $this->usertab= new usertabs($this->config,
398           $this->config->data['TABS']['USERTABS'], $this->dn);
400       /* Switch tab, if it was requested by the user */
401       $this->usertab->current = $s_tab;
403       /* Set ACL and move DN to the headline */
404       $this->usertab->set_acl_base($this->dn);
405       session::set('objectinfo',$this->dn);
406     }
409     /********************
410       Edit multiple entries
411      ********************/
413     /* User wants to edit data? */
414     if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
416       $this->dn = array();
417       foreach($this->list_get_selected_items() as $id){
418         $this->dn[] = $this->list[$id]['dn'];;
419       }
420       $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
421             $this->dn,$this->DivListUsers->selectedBase,"user");
422       if ($tmp->entries_locked()){
423         return($tmp->display_lock_message());
424       }
425       $tmp->lock_entries($this->ui->dn);
426       if($tmp->multiple_available()){
427         $this->usertab = $tmp;
428         $this->usertab->set_active_tab($s_tab);
429         session::set('objectinfo',$this->usertab->get_object_info());
430       }
431     }
434     /********************
435       Edit canceled 
436      ********************/
438     /* Reset all relevant data, if we get a _cancel request */
439     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
440       if (isset($this->usertab)){
441         $this->remove_lock();
442       }
443       $this->usertab= NULL;
444       $this->lognames= array();;
445       $this->sn= "";
446       $this->givenName= "";
447       $this->uid= "";
448       session::un_set('objectinfo');
449     }
452     /********************
453       We want to create a new user, so fetch all available user templates 
454      ********************/
456     /* Generate template list */
457     if ($s_action == "new" || $s_action == "create_user_from_tpl" || $s_action == "templatize_multiple"){
459       $this->templates= array();
460       $ldap= $this->config->get_ldap_link();
462       /* Create list of templates */
463       foreach ($this->config->departments as $key => $value){
464     
465         /* Get acls from different ou's */
466         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
467  
468         /* If creation of a new user is allowed, append this template */
469         if (preg_match("/c/",$acl)){
470           
471           /* Search all templates from the current dn */
472           $ldap->cd (get_people_ou().$value);
473           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
475           /* Append */
476           if ($ldap->count() != 0){
477             while ($attrs= $ldap->fetch()){
478               $this->templates[$ldap->getDN()]=
479                 $attrs['uid'][0]." - ".@LDAP::fix($key);
480             }
481             if ($s_action != "templatize_multiple"){
482                     $this->templates['none']= _("none");
483             }
484           }
485         }
486       }
488       /* Sort templates */
489       natcasesort ($this->templates);
490       reset ($this->templates);
491     }
494     /********************
495       Apply template to multiple entries requested, display confirm dialog
496      ********************/
498     if ($s_action=="templatize_multiple"){
499       $ids = $this->list_get_selected_items();
500       $this->dns = array();
501       if(count($ids)){
503         foreach($ids as $id){
504           $dn = $this->list[$id]['dn'];
505           if (($user= get_lock($dn)) != ""){
506             return(gen_locked_message ($user, $dn));
507           }
508           $this->dns[$id] = $dn; 
509         }
510       }
512       $smarty->assign("templates", $this->templates);
514       return($smarty->fetch(get_template_path('templatize.tpl', TRUE)));
515     }
517     /* Perform templatizing after the button has been pressed */
518     if ($s_action == "templatize_continue"){
520       $acl = $this->ui->get_permissions($_POST['template'], "users/user");
522       /* Template readable? */
523       if (preg_match('/r/', $acl)){
524         $template_dn= $_POST['template'];
526         foreach ($this->dns as $dn){
527           $acl = $this->ui->get_permissions($_POST['template'], "users/user");
528           if (preg_match('/w/', $acl)){
529                   $usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
530                   $usertab->adapt_from_template($template_dn, array("sn", "givenName", "uid"));
531                   $usertab->save();
532                   unset ($usertab);
533                   $usertab= NULL;
534           } else {
535                   msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to modify object '%s'!"), $dn), ERROR_DIALOG);
536           }
537         }
538       } else {
539         msg_dialog::display(_("Permission error"), _("You have no permission to use this template!"), ERROR_DIALOG);
540       }
542     }
545     /********************
546       Delete MULTIPLE entries requested, display confirm dialog
547      ********************/
549     if ($s_action=="del_multiple" || $s_action == "del"){
551       if($s_action == "del"){
553         /* Get 'dn' from posted 'uid' */
554         $ids = array($s_entry);
555       }else{
556         $ids = $this->list_get_selected_items();
557       }
559       $this->dns = array();
560       if(count($ids)){
561         foreach($ids as $id){
562           $this->dns[$id] = $this->list[$id]['dn'];
563         }
565         /* Check locks */
566         if ($user= get_multiple_locks($this->dns)){
567           return(gen_locked_message($user,$this->dns));
568         }
570         $dns_names = array();
571         foreach($this->dns as $dn){
572           $dns_names[] = @LDAP::fix($dn);
573         }
575         add_lock($this->dns, $this->ui->dn);
577         /* Lock the current entry, so nobody will edit it during deletion */
578         $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
580         /* Lock the current entry, so nobody will edit it during deletion */
581         $smarty->assign("info", msgPool::deleteInfo($dns_names));
582         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
585       }
586     }
589     /********************
590       Delete MULTIPLE entries confirmed 
591      ********************/
593     if(isset($_POST['delete_user_confirm'])){
595       /* Remove user by user and check acls before removeing them */
596       foreach($this->dns as $key => $dn){
598         $acl = $this->ui->get_permissions($dn, "users/user"); 
599         if (preg_match('/d/', $acl)){
601           /* Delete request is permitted, perform LDAP action */
602           $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
603           $this->usertab->set_acl_base();
604           $this->usertab->delete ();
605           unset ($this->usertab);
606           $this->usertab= NULL;
607         } else {
608           msg_dialog::display(_("Warning"),msgPool::permDelete($dn),WARNING_DIALOG);
609           if(isset($this->ui->uid)){
610             new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
611           }
612         }
613       }
614       /* Remove lock file after successfull deletion */
615       $this->remove_lock();
616       $this->dns = array();
617     }
619   
620     /********************
621       Toggle lock status for user
622      ********************/
623   
624     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
626       /* Get entry check current status */
627       $val = $this->list[$s_entry];
628       $pwd = $val['userPassword'][0];
630       if(!preg_match("/^\{[^\}]/",$pwd)){
631         trigger_error("Can not deactivate user which is using clear password encryption.");
632       }else{
634         $locked = false;
635         if(preg_match("/^[^\}]*+\}!/",$pwd)){
636           $locked = true;
637         }
639         /* Create ldap array to update status */
640         $attrs = array("userPassword" => $pwd);
641         if($locked){
642           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
643         }else{
644           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
645         }
647         /* Write new status back to ldap */
648         $ldap = $this->config->get_ldap_link();
649         $ldap->cd($val['dn']);
650         $ldap->modify($attrs);
651         if (!$ldap->success()){
652           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
653         }
654       }
655     }
658     /********************
659       Delete entry Canceled 
660      ********************/
662     /* Delete user canceled? */
663     if (isset($_POST['delete_cancel'])){
665       /* Remove lock file after successfull deletion */
666       $this->remove_lock();
667       $this->dns = array();
668     }
671     /********************
672       Edit entry finished (Save) 
673      ********************/
675     /* Finish user edit is triggered by the tabulator dialog, so
676        the user wants to save edited data. Check and save at this
677        point. */
678     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
680       /* Check tabs, will feed message array */
681       $this->usertab->last= $this->usertab->current;
682       $this->usertab->save_object();
683       $message= $this->usertab->check();
685       /* Save, or display error message? */
686       if (count($message) == 0){
688         /* No errors. Go ahead and prepare to ask for a password
689            in case we're creating a new user. 'dn' will be 'new'
690            in this case. It is set to the correct value later. */
691         if ($this->dn == "new"){
692           $set_pass= 1;
693         } else {
694           $set_pass= 0;
695         }
697         /* Save user data to ldap */
698         if($this->usertab->save() == 1){
699           return;
700         }
702         if (!isset($_POST['edit_apply'])){
703           /* User has been saved successfully, remove lock from LDAP. */
704           if ($this->dn != "new"){
705             $this->remove_lock();
706           }
708           /* In case of new users, ask for a password, skip this for templates */
709           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
710             $this->dn = $this->usertab->dn;
711             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
712           }
714           unset ($this->usertab);
715           $this->usertab= NULL;
716           session::un_set('objectinfo');
717         }else{
718         
719           /* Reinitialize tab */
720           if($this->usertab instanceof tabs){
721             $this->usertab->re_init();
722           }
723         }
724       } else {
725         /* Ok. There seem to be errors regarding to the tab data,
726            show message and continue as usual. */
727         msg_dialog::displayChecks($message);
728       }
729     }
733     /********************
734       Create a new user,template, user from template 
735      ********************/
737     /* Check selected options for template */
738     if (isset($_POST['template_continue'])){
739       $message = array();
740       if(!isset($_POST['template']) || (empty($_POST['template']))){
741         $message[]= msgPool::invalid(_("Template"));
742       }
743       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
744         $message[]= msgPool::required(_("Name"));
745       }
746       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
747         $message[]= msgPool::required(_("Given name"));
748       }
749     
750       /* Show error message / continue editing */
751       if (count($message) > 0){
752         msg_dialog::displayChecks($message);
754         foreach(array("sn", "givenName", "uid", "template") as $attr){
755           if(isset($_POST[$attr])){
756             $smarty->assign("$attr", $_POST[$attr]);
757           }else{
758             $smarty->assign("$attr", "");
759           }
760         }
761         $smarty->assign("templates",$this->templates);
762         $smarty->assign("got_uid",$this->got_uid);
763         $smarty->assign("edit_uid",false);
764         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
766       }
767     }
769     /* New user/template request */
770     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
771       /* By default we set 'dn' to 'new', all relevant plugins will
772          react on this. */
773       $this->dn= "new";
774       
775       if (isset($this->config->current['IDGEN'])){
776         $this->got_uid= false;
777       } else {
778         $this->got_uid= true;
779       }
781       /* Create new usertab object */
782       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
783       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
784       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
786       /* Take care about templates */
787       if ($s_action=="new_tpl"){
788         $this->is_template= TRUE;
789         $this->usertab->set_template_mode ();
790       } else {
791         $this->is_template= FALSE;
792       }
794       /* Use template if there are any of them */
795       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
796         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
797           $smarty->assign("$attr", $this->$attr);
798         }
799         if ($s_action=="create_user_from_tpl"){
800           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
801         } else {
802           $smarty->assign("template", "none");
803         }
804         $smarty->assign("edit_uid", "");
805         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
806       }
807     }
809     /********************
810       Template selected continue edit
811      ********************/
813     /* Continue template editing */
814     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
816       $this->sn             = $_POST['sn'];
817       $this->givenName      = $_POST['givenName'];
819       /* Check for requred values */
820       $message= array();
821       if ($this->sn == "") {
822         $message[]= msgPool::required(_("Name"));
823       }
824       if ($this->givenName == "") {
825         $message[]= msgPool::required(_("Given name"));
826       }
828       /* Check if dn is used */
829       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
830       $ldap= $this->config->get_ldap_link();
831       $ldap->cd ($dn);
832       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
833       if ($ldap->count () != 0){
834         msgPool::duplicated(_("Name"));
835       }
837       /* Show error message / continue editing */
838       if (count($message) > 0){
839         msg_dialog::displayChecks($message);
840       } else {
841         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
842         if (isset($this->config->current['IDGEN']) &&
843             $this->config->current['IDGEN'] != ""){
844           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
845           if (count($uids)){
846             $smarty->assign("edit_uid", "false");
847             $smarty->assign("uids", $uids);
848             $this->uid= current($uids);
849           }
850         } else {
851           $smarty->assign("edit_uid", "");
852           $this->uid= "";
853         }
854         $this->got_uid= true;
855       }
857       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
858         $smarty->assign("$attr", $this->$attr);
859       }
860       if (isset($_POST['template'])){
861         $smarty->assign("template", $_POST['template']);
862       }
863       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
864     }
866     /********************
867       No template selected continue edit
868      ********************/
870     /* No template. Ok. Lets fill data into the normal user dialog */
871     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
872       foreach(array("sn", "givenName", "uid") as $attr){
873         if (isset($_POST[$attr])){
874           $this->usertab->by_object['user']->$attr= $_POST[$attr];
875         }
876       }
877     }
880     /********************
881       Template selected continue edit
882      ********************/
884     /* Finish template preamble */
885     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
887       /* Might not be filled if IDGEN is unset */
888       $this->sn                 = $_POST['sn'];
889       $this->givenName          = $_POST['givenName'];
891       /* Move user supplied data to sub plugins */
892       $this->uid                = $_POST['uid'];
893       $this->usertab->uid       = $this->uid;
894       $this->usertab->sn        = $this->sn;
895       $this->usertab->givenName = $this->givenName;
896       $template_dn              = $_POST['template'];
897       $this->usertab->adapt_from_template($template_dn);
898       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
899       $this->usertab->by_object['user']->base= $template_base;
900     }
901    
902  
903     /********************
904       If no template was selected set base
905      ********************/
907     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
908       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
909     }
912     /********************
913       Display subdialog 
914      ********************/
916     /* Show tab dialog if object is present */
917     if(isset($this->usertab->config)){
919       $display= $this->usertab->execute();
921       /* Don't show buttons if tab dialog requests this */
922       
923         $dia = FALSE;
924         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
925           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
926         }
928         if(!is_object($dia) && $dia != TRUE){
929           $display.= "<p style=\"text-align:right\">\n";
930           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
931           $display.= "&nbsp;\n";
932           if ($this->dn != "new"){
933             $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
934             $display.= "&nbsp;\n";
935           }
936           $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
937           $display.= "</p>";
938         }
939       return ($display);
940     }
941     
942     /* Check if there is a snapshot dialog open */
943     $base = $this->DivListUsers->selectedBase;
944     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
945       return($str);
946     }
947   
948     /* Return rendered main page */
949         /* Display dialog with system list */
950     $this->DivListUsers->parent = $this;
951     $this->DivListUsers->execute();
953     /* Add departments if subsearch is disabled */
954     if(!$this->DivListUsers->SubSearch){
955       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
956     }
957     $this->reload();
958     $this->DivListUsers->setEntries($this->list);
959     return($this->DivListUsers->Draw());
960   }
963   /* Return departments, that will be included within snapshot detection */
964   function get_used_snapshot_bases()
965   {
966     return(array(get_people_ou().$this->DivListUsers->selectedBase));
967   }  
970   function reload()
971   {
972     /* Set base for all searches */
973     $base= $this->DivListUsers->selectedBase;
974     $this->list =array();
976     /* Get filter configuration */
977     $Regex                = $this->DivListUsers->Regex;
978     $SubSearch            = $this->DivListUsers->SubSearch;
979     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
980     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
981     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
982     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
983     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
984     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
986     /* Setup filter depending on selection */
987     $filter="";
988     if ($this->config->current['SAMBAVERSION'] == 3){
989       $samba= "sambaSamAccount";
990     } else {
991       $samba= "sambaAccount";
992     }
994     if ($ShowFunctionalUsers){
995       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
996                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
997                 "(objectClass=gosaProxyAccount))))";
998     }
999     if ($ShowUnixUsers){
1000       $filter.= "(objectClass=posixAccount)";
1001     }
1002     if ($ShowMailUsers){
1003       $filter.= "(objectClass=gosaMailAccount)";
1004     }
1005     if ($ShowSambaUsers){
1006       $filter.= "(objectClass=$samba)";
1007     }
1008     if ($ShowProxyUsers){
1009       $filter.= "(objectClass=gosaProxyAccount)";
1010     }
1011     if ($ShowTemplates){
1012       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1013     } else {
1014       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1015     }
1016     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1018     /* Generate userlist */
1019     $ldap= $this->config->get_ldap_link(TRUE);
1021     if ($SubSearch){
1022       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1023                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1024     } else {
1025       $base= get_people_ou().$base;
1026       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1027                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1028     }
1029     $SortTemp = array();
1030     $List = array();
1031     foreach($ListTemp as $Key => $Entry){
1033       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1034        * Else winstations will be listed too, if you use the subtree flag. 
1035        */
1036       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1037         continue;
1038       }else{
1040         // Generate caption for rows
1041         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1042           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1043         } else {
1044           $display= "[".$Entry["uid"][0]."]";
1045         }
1047         $display = strtolower($display);
1048         $List[$display] = $Entry;
1049         $SortTemp[$display] = $display;
1050       }
1051     }
1052     natcasesort($SortTemp);
1053     reset($SortTemp);
1055     $this->list = array();
1056     foreach($SortTemp as $Key){
1057       $this->list[] = $List[$Key];
1058     }
1059   }
1062   function remove_lock()
1063   {
1064     /* Remove user lock if a DN is marked as "currently edited" */
1065     if (isset($this->usertab->dn)){
1066       del_lock ($this->usertab->dn);
1067     }
1068     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
1069       del_lock($this->dn);
1070     }
1071     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
1072       del_lock($this->dns);
1073     }
1074   }
1077   function copyPasteHandling_from_queue($s_action,$s_entry)
1078   {
1079     /* Check if Copy & Paste is disabled */
1080     if(!is_object($this->CopyPasteHandler)){
1081       return("");
1082     }
1084     /* Add a single entry to queue */
1085     if($s_action == "cut" || $s_action == "copy"){
1086       /* Cleanup object queue */
1087       $this->CopyPasteHandler->cleanup_queue();
1088       $dn = $this->list[$s_entry]['dn'];
1089       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1090     }
1092     /* Add entries to queue */
1093     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1095       /* Cleanup object queue */
1096       $this->CopyPasteHandler->cleanup_queue();
1098       /* Add new entries to CP queue */
1099       foreach($this->list_get_selected_items() as $id){
1100         $dn = $this->list[$id]['dn'];
1102         if($s_action == "copy_multiple"){
1103           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1104         }
1105         if($s_action == "cut_multiple"){
1106           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1107         }
1108       }
1109     }
1110     
1111     /* Start pasting entries */
1112     if($s_action == "editPaste"){
1113       $this->start_pasting_copied_objects = TRUE;
1114     }
1116     /* Return C&P dialog */ 
1117     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1118    
1119       /* Get dialog */
1120       $data = $this->CopyPasteHandler->execute();
1121       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1123       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1124       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1125         $s_entry = $this->CopyPasteHandler->last_entry();
1126         $this->reload();
1127         foreach($this->list as $key => $entry){
1128           if($entry['dn'] == $s_entry){
1129             $this->CPPasswordChange = $key;
1130           }
1131         }
1132       }
1134       /* Return dialog data */
1135       if(!empty($data) && $this->CPPasswordChange == ""){
1136         return($data);
1137       }
1138     }
1140     /* Automatically disable status for pasting */ 
1141     if(!$this->CopyPasteHandler->entries_queued()){
1142       $this->start_pasting_copied_objects = FALSE;
1143     }
1144     return("");
1145   }
1148   function save_object()
1149   {
1150     /* Handle divlist filter && department selection*/
1151     if(!is_object($this->usertab)){
1152       $this->DivListUsers->save_object();
1153     }
1154     if(is_object($this->CopyPasteHandler)){
1155       $this->CopyPasteHandler->save_object();
1156     }
1157   }
1159     
1160   function list_get_selected_items()
1161   {
1162     $ids = array();
1163     foreach($_POST as $name => $value){
1164       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1165         $id   = preg_replace("/^item_selected_/","",$name);
1166         $ids[$id] = $id;
1167       }
1168     }
1169     return($ids);
1170   }
1171   
1173   /* A set of disabled and therefore overloaded functions. They are
1174      not needed in this class. */
1175   function remove_from_parent() { } 
1176   function check() { } 
1177   function save() { } 
1178   function adapt_from_template($dn, $skip= array()) { } 
1179   function password_change_needed() { } 
1181 } /* ... class userManagement */
1182 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1183 ?>