Code

Some layer menu fixes
[gosa.git] / plugins / admin / users / class_userManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003-2006 - Cajus Pollmeier <pollmeier@gonicus.de>
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class userManagement extends plugin
22 {
23   /* Plugin definitions */
24   var $plHeadline= "Users";
25   var $plDescription= "This does something";
27   /* Dialog attributes */
28   var $usertab              = NULL;
29   var $ui                   = NULL;
30   var $templates            = array();
31   var $got_uid              = false;
32   var $CopyPasteHandler     = NULL;
33   var $CPPasswordChange     = ""; // Contains the entry id which should get a new password
34   var $DivListUsers;
36   var $pwd_change_queue     = array();
38   var $start_pasting_copied_objects = FALSE;
39   var $current_action ="";
40   var $msg_dialog= NULL;
41   
42   function userManagement(&$config, $ui)
43   {
44     /* Save configuration for internal use */
45     $this->config= &$config;
46     $this->ui= &$ui;
48     /* Copy & Paste handler */
49     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
50       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
51     }
53     /* Creat dialog object */
54     $this->DivListUsers = new divListUsers($this->config,$this);
56   }
59   function execute()
60   {
61     /* Call parent execute */
62     plugin::execute();
64     /* LOCK MESSAGE Vars */
65     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/","/^item_selected/","/^remove_multiple_users/","/^multiple_edit/");
67     $smarty       = get_smarty();                 // Smarty instance
68     $s_action     = "";                           // Contains the action to be taken
69     $s_entry      = "";                           // The value for s_action
71     /* Edit entry button pressed? */
72     if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
73       $s_action= "edit";
74       $s_entry= validate($_GET['id']);
75     }
77     /* Test relevant POST values */  
78     foreach($_POST as $key => $val){
80       /* Get every possible POST combination and set s_action/s_entry accordingly */
81       foreach(array("del"       => "user_del",    
82                     "edit"      => "user_edit",
83                     "new"       => "user_new",
84                     "new_tpl"   => "user_tplnew",
85                     "del_multiple" => "^remove_multiple_users",
86                     "create_user_from_tpl"          => "userfrom_tpl",
87                     "change_pw" => "user_chgpw", 
88                     "editPaste" => "editPaste",  
89                     "copy_multiple" => "multiple_copy_users",
90                     "multiple_edit" => "multiple_edit",
91                     "cut_multiple" => "multiple_cut_users",
92                     "multiple_password_change" => "multiple_password_change",
93                     "copy"      => "^copy",
94                     "toggle_lock_status" => "toggle_lock_status",
95                     "cut"       => "^cut") as $act => $name){
97         if (preg_match("/".$name.".*/", $key)){
98           $s_action= $act;
99           $s_entry= preg_replace("/".$name."_/i", "", $key);
100           break;
101         }
102       }
103       
104     } /* ...Test POST */
106     /* Remove coordinate prefix from POST, required by some browsers */
107     $s_entry= preg_replace("/_.$/", "", $s_entry);
109     /* Seperate possibly encoded tab and entry, default to tab "user" */
110     if(preg_match("/.*-.*/", $s_entry)){
111       $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
112       $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
113     }else{
114       $s_tab= "user";
115     }
117     if(!$this->config->search($s_tab, 'class',array('tabs'))){
118       $s_tab = "user";
119     }
122     /* handle C&P from layers menu */
123     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
124       $s_action = "copy_multiple";
125     }
126     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
127       $s_action = "cut_multiple";
128     }
129     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
130       $s_action = "editPaste";
131     }
133     /* Create options */
134     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_new"){
135       $s_action = "new";
136     }
137     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_tplnew"){
138       $s_action = "new_tpl";
139     }
140     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "multiple_edit"){
141       $s_action = "multiple_edit";
142     }
144     /* handle remove from layers menu */
145     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
146       $s_action = "del_multiple";
147     }
150     /********************
151       Copy & Paste 
152      ********************/
154     /* Display the copy & paste dialog, if it is currently open */
155     if($this->CPPasswordChange == ""){
156       $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
157       if($ret){
158         return($ret);
159       }
160     }
163     /********************
164       Change password confirmed
165      ********************/
167     /* Perform password change */
168     if (isset($_POST['password_finish'])){
170       /* For security reasons, check if user is allowed to set password again */
171       $dn  = $this->dn;
172       $acl = $this->ui->get_permissions($dn, "users/password");
173       $cacl= $this->ui->get_permissions($dn, "users/user");
175       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
177         /* Check input and feed errors into 'message' */
178         $message= array();
180         /* Sanity checks... */
181         if ($_POST['new_password'] != $_POST['repeated_password']){
183           /* Matching passwords in new and repeated? */
184           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
185         } else {
187           /* Empty password is not permitted by default. */
188           if ($_POST['new_password'] == ""){
189             $message[]= _("The password you've entered as 'New password' is empty.");
190           }
191         }
193         /* Errors, or password change? */
194         if (count($message) != 0){
196           /* Show error message and continue editing */
197           show_errors ($message);
198           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
199         }
201         $config= $this->config;
202         $ldap_ui= $this->config->get_ldap_link();
203         if(isset($this->usertab->dn)){
204           $ldap_ui->cat($this->usertab->dn,array("uid"));
205           $user = $ldap_ui->fetch();
206         }else{
207           $ldap_ui->cat($this->dn,array("uid"));
208           $user = $ldap_ui->fetch();
209         }
210         if((is_array($user))&&(isset($user['uid']))){
211           $username= $user['uid'][0];
212         }
214         /* Set password, perform required steps */
215         if ($this->usertab){
216           if ($this->usertab->password_change_needed()){
217             $obj= $this->usertab->by_object['user'];
218             change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
219             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
220               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
221             }
222             new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
223             unset($this->usertab);
224             $this->usertab= NULL;
225           }
226         } else {
227           change_password ($this->dn, $_POST['new_password']);
228           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
229             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
230           }
231           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
232         }
233       } else {
235         /* Missing permissions, show message */
236         msg_dialog::display(_("Password change failed."),_("You are not allowed to set this users password!"),WARNING_DIALOG);
237       }
238       /* Clean session, delete lock */
239       del_lock ($this->dn);
240       unset ($this->usertab);
241       $this->usertab= NULL;
242       $this->lognames= array();;
243       $this->sn= "";
244       $this->givenName= "";
245       $this->uid= "";
246       unset ($_SESSION['objectinfo']);
247     }
250     /********************
251      Change multiple passwords requested 
252      ********************/
253   
254     if($s_action == "multiple_password_change"){
255       $this->pwd_change_queue = $this->list_get_selected_items();
256     }    
259     /********************
260       Change password requested  
261      ********************/
263     /* Password change requested */
264     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
266       /* Get users whose passwords should be changed. */
267       if(count($this->pwd_change_queue)){
268         $s_entry= array_pop($this->pwd_change_queue);
269       }
271       if(!empty($this->CPPasswordChange)){
272         $s_entry = $this->CPPasswordChange;
273         $this->CPPasswordChange = "";
274       }
276       /* Get 'dn' from posted 'uid' */
277       $this->dn= $this->list[trim($s_entry)]['dn'];
279       /* Load permissions for selected 'dn' and check if
280          we're allowed to remove this 'dn' */
281       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
283         /* User is allowed to change passwords, save 'dn' and 'acl' for next
284            dialog. */
285         $_SESSION['objectinfo']= $this->dn;
286         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
287         
289       } else {
290         /* User is not allowed. Show message and cancel. */
291         msg_dialog::display(_("Password change"),_("You are not allowed to set this users password!"),WARNING_DIALOG);
292       }
293     }
297      /********************
298       Edit existing entry
299      ********************/
301     /* User wants to edit data? */
302     if (($s_action=="edit") && (!isset($this->usertab->config))){
304       /* Get 'dn' from posted 'uid', must be unique */
305       $this->dn= $this->list[trim($s_entry)]['dn'];
307       /* Check locking, save current plugin in 'back_plugin', so
308          the dialog knows where to return. */
309       if (($user= get_lock($this->dn)) != ""){
310         return(gen_locked_message ($user, $this->dn));
311       }
313       /* Lock the current entry, so everyone will get the
314          above dialog */
315       add_lock ($this->dn, $this->ui->dn);
317       /* Register usertab to trigger edit dialog */
318       $this->usertab= new usertabs($this->config,
319           $this->config->data['TABS']['USERTABS'], $this->dn);
321       /* Switch tab, if it was requested by the user */
322       $this->usertab->current = $s_tab;
324       /* Set ACL and move DN to the headline */
325       $this->usertab->set_acl_base($this->dn);
326       $_SESSION['objectinfo']= $this->dn;
327     }
329  
331     /********************
332       Edit multiple entries
333      ********************/
335     /* User wants to edit data? */
336     if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
338       $this->dn = array();
339       foreach($this->list_get_selected_items() as $id){
340         $this->dn[] = $this->list[$id]['dn'];;
341       }
342       $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],$this->dn);
343       if ($tmp->entries_locked()){
344         return($tmp->display_lock_message());
345       }
346       $tmp->lock_entries($this->ui->dn);
347       if($tmp->multiple_available()){
348         $this->usertab = $tmp;
349         $this->usertab->set_active_tab($s_tab);
350         $_SESSION['objectinfo']= $this->usertab->get_object_info();
351       }
352     }
355     /********************
356       Edit canceled 
357      ********************/
359     /* Reset all relevant data, if we get a _cancel request */
360     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
361       if (isset($this->usertab)){
362         del_lock ($this->usertab->dn);
363         unset ($this->usertab);
364       }
365       $this->usertab= NULL;
366       $this->lognames= array();;
367       $this->sn= "";
368       $this->givenName= "";
369       $this->uid= "";
370       unset ($_SESSION['objectinfo']);
371     }
374     /********************
375       Delete MULTIPLE entries requested, display confirm dialog
376      ********************/
378     if ($s_action=="del_multiple"){
379       $ids = $this->list_get_selected_items();
380       $this->dns = array();
381       if(count($ids)){
383         foreach($ids as $id){
384           $dn = $this->list[$id]['dn'];
385           if (($user= get_lock($dn)) != ""){
386             return(gen_locked_message ($user, $dn));
387           }
388           $this->dns[$id] = $dn; 
389         }
391         $dns_names = "<br><pre>";
392         foreach($this->dns as $dn){
393           $dns_names .= $dn."\n";
394         }
395         $dns_names .="</pre>";
397         /* Lock the current entry, so nobody will edit it during deletion */
398         if (count($this->dns) == 1){
399           $info = sprintf(_("You're about to delete the following entry: %s"), @LDAP::fix($dns_names));
400         } else {
401           $info = sprintf(_("You're about to delete the following entries: %s"), @LDAP::fix($dns_names));
402         }
403         $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
404         $this->current_action = $s_action;
405       }
406     }
409     /********************
410       Delete MULTIPLE entries confirmed 
411      ********************/
413       /* Confirmation for deletion has been passed. Users should be deleted. */
414       if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
415         
416         $this->current_action = "";  
417         
418         /* Remove user by user and check acls before removeing them */
419         foreach($this->dns as $key => $dn){
421           $acl = $this->ui->get_permissions($dn, "users/user"); 
422           if (preg_match('/d/', $acl)){
424             /* Delete request is permitted, perform LDAP action */
425             $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
426             $this->usertab->set_acl_base();
427             $this->usertab->delete ();
428             unset ($this->usertab);
429             $this->usertab= NULL;
430           } else {
431             msg_dialog::display(_("User delete"),sprintf(_("You are not allowed to delete the user '%s'!"),$dn),WARNING_DIALOG);
432             if(isset($this->ui->uid)){
433               new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
434             }
435           }
436           /* Remove lock file after successfull deletion */
437           del_lock ($dn);
438           unset($this->dns[$key]);
439       }
440     }
443     /********************
444       Delete MULTIPLE entries Canceled 
445      ********************/
447     /* Remove lock */
448     if(isset($_POST['delete_multiple_user_cancel'])){
449       foreach($this->dns as $key => $dn){
450         del_lock ($dn);
451         unset($this->dns[$key]);
452       }
453     }
454   
455   
456     /********************
457       Toggle lock status for user
458      ********************/
459   
460     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
462       /* Get entry check current status */
463       $val = $this->list[$s_entry];
464       $pwd = $val['userPassword'][0];
466       if(!preg_match("/^\{[^\}]/",$pwd)){
467         trigger_error("Can not deactivate user which is using clear password encryption.");
468       }else{
470         $locked = false;
471         if(preg_match("/^[^\}]*+\}!/",$pwd)){
472           $locked = true;
473         }
475         /* Create ldap array to update status */
476         $attrs = array("userPassword" => $pwd);
477         if($locked){
478           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
479         }else{
480           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
481         }
483         /* Write new status back to ldap */
484         $ldap = $this->config->get_ldap_link();
485         $ldap->cd($val['dn']);
486         $ldap->modify($attrs);
487         if($locked){
488           show_ldap_error($ldap->get_error(),_("Could not set user status from locked to unlocked."));
489         }else{
490           show_ldap_error($ldap->get_error(),_("Could not set user status from unlocked to locked."));
491         }
492       }
493     }
496     /********************
497       Delete entry requested, display confirm dialog
498      ********************/
500     /* Remove user was requested */
501     if ($s_action=="del"){
503       /* Get 'dn' from posted 'uid' */
504       $this->dn= $this->list[trim($s_entry)]['dn'];
506       /* Load permissions for selected 'dn' and check if
507          we're allowed to remove this 'dn' */
509       /* Check locking, save current plugin in 'back_plugin', so
510          the dialog knows where to return. */
511       if (($user= get_lock($this->dn)) != ""){
512         return(gen_locked_message ($user, $this->dn));
513       }
515   
516       /* Lock the current entry, so nobody will edit it during deletion */
517 #      add_lock ($this->dn, $this->ui->dn);
518 #      $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
519 #      $smarty->assign("multiple", false);
520 #      return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
521       $this->msg_dialog = new msg_dialog( _("Delete user"),sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)),CONFIRM_DIALOG);
522       $this->current_action = $s_action;
523     }
526     /********************
527       Delete entry confirmed 
528      ********************/
530     /* Confirmation for deletion has been passed. User should be deleted. */
531     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
533       $this->current_action = ""; 
534  
535       /* Some nice guy may send this as POST, so we've to check
536          for the permissions again. */
538       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
539  
540       if (preg_match('/d/', $acl)){
542         /* Delete request is permitted, perform LDAP action */
543         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
544         $this->usertab->set_acl_base();
545         $this->usertab->delete ();
546         unset ($this->usertab);
547         $this->usertab= NULL;
548         msg_dialog::display(_("User delted"),_("User successfully removed."),INFO_DIALOG);
549       } else {
551         /* Normally this shouldn't be reached, send some extra
552            logs to notify the administrator */
553         msg_dialog::display(_("User delete"),_("You are not allowed to delete this user!"),WARNING_DIALOG);
555         if(isset($this->ui->uid)){
556           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
557         }
558       }
560       /* Remove lock file after successfull deletion */
561       del_lock ($this->dn);
562     }
564     
565     /********************
566       Delete entry Canceled 
567      ********************/
569     /* Delete user canceled? */
570     if (isset($_POST['delete_cancel'])){
571       del_lock ($this->dn);
572     }
575     /********************
576       Edit entry finished (Save) 
577      ********************/
579     /* Finish user edit is triggered by the tabulator dialog, so
580        the user wants to save edited data. Check and save at this
581        point. */
582     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
584       /* Check tabs, will feed message array */
585       $this->usertab->last= $this->usertab->current;
586       $this->usertab->save_object();
587       $message= $this->usertab->check();
589       /* Save, or display error message? */
590       if (count($message) == 0){
592         /* No errors. Go ahead and prepare to ask for a password
593            in case we're creating a new user. 'dn' will be 'new'
594            in this case. It is set to the correct value later. */
595         if ($this->dn == "new"){
596           $set_pass= 1;
597         } else {
598           $set_pass= 0;
599         }
601         /* Save user data to ldap */
602         if($this->usertab->save() == 1){
603           return;
604         }
606         if (!isset($_POST['edit_apply'])){
607           /* User has been saved successfully, remove lock from LDAP. */
608           if ($this->dn != "new"){
609             del_lock ($this->dn);
610           }
612           /* In case of new users, ask for a password, skip this for templates */
613           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
614             $this->dn = $this->usertab->dn;
615             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
616           }
618           unset ($this->usertab);
619           $this->usertab= NULL;
620           unset ($_SESSION['objectinfo']);
621         }
622       } else {
623         /* Ok. There seem to be errors regarding to the tab data,
624            show message and continue as usual. */
625         show_errors($message);
626       }
627     }
630     /********************
631       We want to create a new user, so fetch all available user templates 
632      ********************/
634     /* Generate template list */
635     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
637       $this->templates= array();
638       $ldap= $this->config->get_ldap_link();
640       /* Create list of templates */
641       foreach ($this->config->departments as $key => $value){
642     
643         /* Get acls from different ou's */
644         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
645  
646         /* If creation of a new user is allowed, append this template */
647         if (preg_match("/c/",$acl)){
648           
649           /* Search all templates from the current dn */
650           $ldap->cd (get_people_ou().$value);
651           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
653           /* Append */
654           if ($ldap->count() != 0){
655             while ($attrs= $ldap->fetch()){
656               $this->templates[$ldap->getDN()]=
657                 $attrs['uid'][0]." - ".@LDAP::fix($key);
658             }
659             $this->templates['none']= _("none");
660           }
661         }
662       }
664       /* Sort templates */
665       natcasesort ($this->templates);
666       reset ($this->templates);
667     }
670     /********************
671       Create a new user,template, user from template 
672      ********************/
674     /* Check selected options for template */
675     if (isset($_POST['template_continue'])){
676       $message = array();
677       if(!isset($_POST['template']) || (empty($_POST['template']))){
678         $message[] = _("Please select a valid template.");
679       }
680       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
681         $message[]= _("The required field 'Name' is not set.");
682       }
683       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
684         $message[]= _("The required field 'Given name' is not set.");
685       }
686     
687       /* Show error message / continue editing */
688       if (count($message) > 0){
689         show_errors ($message);
691         foreach(array("sn", "givenName", "uid", "template") as $attr){
692           if(isset($_POST[$attr])){
693             $smarty->assign("$attr", $_POST[$attr]);
694           }else{
695             $smarty->assign("$attr", "");
696           }
697         }
698         $smarty->assign("templates",$this->templates);
699         $smarty->assign("got_uid",$this->got_uid);
700         $smarty->assign("edit_uid",false);
701         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
703       }
704     }
706     /* New user/template request */
707     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
708       /* By default we set 'dn' to 'new', all relevant plugins will
709          react on this. */
710       $this->dn= "new";
711       
712       if (isset($this->config->current['IDGEN'])){
713         $this->got_uid= false;
714       } else {
715         $this->got_uid= true;
716       }
718       /* Create new usertab object */
719       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
720       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
721       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
723       /* Take care about templates */
724       if ($s_action=="new_tpl"){
725         $this->is_template= TRUE;
726         $this->usertab->set_template_mode ();
727       } else {
728         $this->is_template= FALSE;
729       }
731       /* Use template if there are any of them */
732       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
733         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
734           $smarty->assign("$attr", $this->$attr);
735         }
736         if ($s_action=="create_user_from_tpl"){
737           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
738         } else {
739           $smarty->assign("template", "none");
740         }
741         $smarty->assign("edit_uid", "");
742         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
743       }
744     }
746     /********************
747       Template selected continue edit
748      ********************/
750     /* Continue template editing */
751     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
753       $this->sn             = $_POST['sn'];
754       $this->givenName      = $_POST['givenName'];
756       /* Check for requred values */
757       $message= array();
758       if ($this->sn == "") {
759         $message[]= _("The required field 'Name' is not set.");
760       }
761       if ($this->givenName == "") {
762         $message[]= _("The required field 'Given name' is not set.");
763       }
765       /* Check if dn is used */
766       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
767       $ldap= $this->config->get_ldap_link();
768       $ldap->cd ($dn);
769       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
770       if ($ldap->count () != 0){
771         $message[]= _("A person with the choosen name is already used in this tree.");
772       }
774       /* Show error message / continue editing */
775       if (count($message) > 0){
776         show_errors ($message);
777       } else {
778         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
779         if (isset($this->config->current['IDGEN']) &&
780             $this->config->current['IDGEN'] != ""){
781           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
782           if (count($uids)){
783             $smarty->assign("edit_uid", "false");
784             $smarty->assign("uids", $uids);
785             $this->uid= current($uids);
786           }
787         } else {
788           $smarty->assign("edit_uid", "");
789           $this->uid= "";
790         }
791         $this->got_uid= true;
792       }
794       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
795         $smarty->assign("$attr", $this->$attr);
796       }
797       if (isset($_POST['template'])){
798         $smarty->assign("template", $_POST['template']);
799       }
800       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
801     }
803     /********************
804       No template selected continue edit
805      ********************/
807     /* No template. Ok. Lets fill data into the normal user dialog */
808     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
809       foreach(array("sn", "givenName", "uid") as $attr){
810         if (isset($_POST[$attr])){
811           $this->usertab->by_object['user']->$attr= $_POST[$attr];
812         }
813       }
814     }
817     /********************
818       Template selected continue edit
819      ********************/
821     /* Finish template preamble */
822     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
824       /* Might not be filled if IDGEN is unset */
825       $this->sn                 = $_POST['sn'];
826       $this->givenName          = $_POST['givenName'];
828       /* Move user supplied data to sub plugins */
829       $this->uid                = $_POST['uid'];
830       $this->usertab->uid       = $this->uid;
831       $this->usertab->sn        = $this->sn;
832       $this->usertab->givenName = $this->givenName;
833       $template_dn              = $_POST['template'];
834       $this->usertab->adapt_from_template($template_dn);
835       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
836       $this->usertab->by_object['user']->base= $template_base;
837     }
838    
839  
840     /********************
841       If no template was selected set base
842      ********************/
844     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
845       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
846     }
849     /********************
850       Display subdialog 
851      ********************/
853     /* Show tab dialog if object is present */
854     if(isset($this->usertab->config)){
855       $display= $this->usertab->execute();
857       /* Don't show buttons if tab dialog requests this */
858       
859         $dia = FALSE;
860         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
861           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
862         }
864         if(!is_object($dia) && $dia != TRUE){
865           $display.= "<p style=\"text-align:right\">\n";
866           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
867           $display.= "&nbsp;\n";
868           if ($this->dn != "new"){
869             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
870             $display.= "&nbsp;\n";
871           }
872           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
873           $display.= "</p>";
874         }
875       return ($display);
876     }
877     
878     /* Check if there is a snapshot dialog open */
879     $base = $this->DivListUsers->selectedBase;
880     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
881       return($str);
882     }
883   
884     /* Return rendered main page */
885         /* Display dialog with system list */
886     $this->DivListUsers->parent = $this;
887     $this->DivListUsers->execute();
889     /* Add departments if subsearch is disabled */
890     if(!$this->DivListUsers->SubSearch){
891       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
892     }
893     $this->reload();
894     $this->DivListUsers->setEntries($this->list);
895     return($this->DivListUsers->Draw());
896   }
899   /* Return departments, that will be included within snapshot detection */
900   function get_used_snapshot_bases()
901   {
902     return(array(get_people_ou().$this->DivListUsers->selectedBase));
903   }  
906   function reload()
907   {
908     /* Set base for all searches */
909     $base= $this->DivListUsers->selectedBase;
910     $this->list =array();
912     /* Get filter configuration */
913     $Regex                = $this->DivListUsers->Regex;
914     $SubSearch            = $this->DivListUsers->SubSearch;
915     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
916     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
917     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
918     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
919     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
920     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
922     /* Setup filter depending on selection */
923     $filter="";
924     if ($this->config->current['SAMBAVERSION'] == 3){
925       $samba= "sambaSamAccount";
926     } else {
927       $samba= "sambaAccount";
928     }
930     if ($ShowFunctionalUsers){
931       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
932                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
933                 "(objectClass=gosaProxyAccount))))";
934     }
935     if ($ShowUnixUsers){
936       $filter.= "(objectClass=posixAccount)";
937     }
938     if ($ShowMailUsers){
939       $filter.= "(objectClass=gosaMailAccount)";
940     }
941     if ($ShowSambaUsers){
942       $filter.= "(objectClass=$samba)";
943     }
944     if ($ShowProxyUsers){
945       $filter.= "(objectClass=gosaProxyAccount)";
946     }
947     if ($ShowTemplates){
948       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
949     } else {
950       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
951     }
952     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
954     /* Generate userlist */
955     $ldap= $this->config->get_ldap_link(TRUE);
957     if ($SubSearch){
958       $ListTemp =  get_list($filter, "users", $base,
959                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
960     } else {
961       $base= get_people_ou().$base;
962       $ListTemp = get_list($filter, "users", $base, 
963                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
964     }
965     $SortTemp = array();
966     $List = array();
967     foreach($ListTemp as $Key => $Entry){
969       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
970        * Else winstations will be listed too, if you use the subtree flag. 
971        */
972       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
973         continue;
974       }else{
976         // Generate caption for rows
977         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
978           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
979         } else {
980           $display= "[".$Entry["uid"][0]."]";
981         }
983         $display = strtolower($display);
984         $List[$display] = $Entry;
985         $SortTemp[$display] = $display;
986       }
987     }
988     sort($SortTemp);
989     reset($SortTemp);
991     $this->list = array();
992     foreach($SortTemp as $Key){
993       $this->list[] = $List[$Key];
994     }
995   }
997   function remove_lock()
998   {
999     /* Remove user lock if a DN is marked as "currently edited" */
1000     if (isset($this->usertab->dn)){
1001       del_lock ($this->usertab->dn);
1002     }
1003   }
1006   function copyPasteHandling_from_queue($s_action,$s_entry)
1007   {
1008     /* Check if Copy & Paste is disabled */
1009     if(!is_object($this->CopyPasteHandler)){
1010       return("");
1011     }
1013     /* Add a single entry to queue */
1014     if($s_action == "cut" || $s_action == "copy"){
1015       /* Cleanup object queue */
1016       $this->CopyPasteHandler->cleanup_queue();
1017       $dn = $this->list[$s_entry]['dn'];
1018       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1019     }
1021     /* Add entries to queue */
1022     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1024       /* Cleanup object queue */
1025       $this->CopyPasteHandler->cleanup_queue();
1027       /* Add new entries to CP queue */
1028       foreach($this->list_get_selected_items() as $id){
1029         $dn = $this->list[$id]['dn'];
1031         if($s_action == "copy_multiple"){
1032           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1033         }
1034         if($s_action == "cut_multiple"){
1035           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1036         }
1037       }
1038     }
1039     
1040     /* Start pasting entries */
1041     if($s_action == "editPaste"){
1042       $this->start_pasting_copied_objects = TRUE;
1043     }
1045     /* Return C&P dialog */ 
1046     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1047    
1048       /* Load entry from queue and set base */
1049       $this->CopyPasteHandler->load_entry_from_queue();
1050       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1052       /* Get dialog */
1053       $data = $this->CopyPasteHandler->execute();
1055       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1056       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1057         $s_entry = $this->CopyPasteHandler->last_entry();
1058         $this->reload();
1059         foreach($this->list as $key => $entry){
1060           if($entry['dn'] == $s_entry){
1061             $this->CPPasswordChange = $key;
1062           }
1063         }
1064       }
1066       /* Return dialog data */
1067       if(!empty($data) && $this->CPPasswordChange == ""){
1068         return($data);
1069       }
1070     }
1072     /* Automatically disable status for pasting */ 
1073     if(!$this->CopyPasteHandler->entries_queued()){
1074       $this->start_pasting_copied_objects = FALSE;
1075     }
1076     return("");
1077   }
1080   function save_object()
1081   {
1082     /* Handle divlist filter && department selection*/
1083     if(!is_object($this->usertab)){
1084       $this->DivListUsers->save_object();
1085     }
1086   }
1088     
1089   function list_get_selected_items()
1090   {
1091     $ids = array();
1092     foreach($_POST as $name => $value){
1093       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1094         $id   = preg_replace("/^item_selected_/","",$name);
1095         $ids[$id] = $id;
1096       }
1097     }
1098     return($ids);
1099   }
1100   
1102   /* A set of disabled and therefore overloaded functions. They are
1103      not needed in this class. */
1104   function remove_from_parent() { } 
1105   function check() { } 
1106   function save() { } 
1107   function adapt_from_template($dn) { } 
1108   function password_change_needed() { } 
1110 } /* ... class userManagement */
1111 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1112 ?>