Code

1f12ef09915f27b0c2e1d73c44cf61769e2f9ecf
[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     }
121     /********************
122       Copy & Paste 
123      ********************/
125     /* Display the copy & paste dialog, if it is currently open */
126     if($this->CPPasswordChange == ""){
127       $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
128       if($ret){
129         return($ret);
130       }
131     }
134     /********************
135       Change password confirmed
136      ********************/
138     /* Perform password change */
139     if (isset($_POST['password_finish'])){
141       /* For security reasons, check if user is allowed to set password again */
142       $dn  = $this->dn;
143       $acl = $this->ui->get_permissions($dn, "users/password");
144       $cacl= $this->ui->get_permissions($dn, "users/user");
146       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
148         /* Check input and feed errors into 'message' */
149         $message= array();
151         /* Sanity checks... */
152         if ($_POST['new_password'] != $_POST['repeated_password']){
154           /* Matching passwords in new and repeated? */
155           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
156         } else {
158           /* Empty password is not permitted by default. */
159           if ($_POST['new_password'] == ""){
160             $message[]= _("The password you've entered as 'New password' is empty.");
161           }
162         }
164         /* Errors, or password change? */
165         if (count($message) != 0){
167           /* Show error message and continue editing */
168           show_errors ($message);
169           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
170         }
172         $config= $this->config;
173         $ldap_ui= $this->config->get_ldap_link();
174         if(isset($this->usertab->dn)){
175           $ldap_ui->cat($this->usertab->dn,array("uid"));
176           $user = $ldap_ui->fetch();
177         }else{
178           $ldap_ui->cat($this->dn,array("uid"));
179           $user = $ldap_ui->fetch();
180         }
181         if((is_array($user))&&(isset($user['uid']))){
182           $username= $user['uid'][0];
183         }
185         /* Set password, perform required steps */
186         if ($this->usertab){
187           if ($this->usertab->password_change_needed()){
188             $obj= $this->usertab->by_object['user'];
189             change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
190             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
191               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
192             }
193             new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
194             unset($this->usertab);
195             $this->usertab= NULL;
196           }
197         } else {
198           change_password ($this->dn, $_POST['new_password']);
199           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
200             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
201           }
202           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
203         }
204       } else {
206         /* Missing permissions, show message */
207         msg_dialog::display(_("Password change failed."),_("You are not allowed to set this users password!"),WARNING_DIALOG);
208       }
209       /* Clean session, delete lock */
210       del_lock ($this->dn);
211       unset ($this->usertab);
212       $this->usertab= NULL;
213       $this->lognames= array();;
214       $this->sn= "";
215       $this->givenName= "";
216       $this->uid= "";
217       unset ($_SESSION['objectinfo']);
218     }
221     /********************
222      Change multiple passwords requested 
223      ********************/
224   
225     if($s_action == "multiple_password_change"){
226       $this->pwd_change_queue = $this->list_get_selected_items();
227     }    
230     /********************
231       Change password requested  
232      ********************/
234     /* Password change requested */
235     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
237       /* Get users whose passwords should be changed. */
238       if(count($this->pwd_change_queue)){
239         $s_entry= array_pop($this->pwd_change_queue);
240       }
242       if(!empty($this->CPPasswordChange)){
243         $s_entry = $this->CPPasswordChange;
244         $this->CPPasswordChange = "";
245       }
247       /* Get 'dn' from posted 'uid' */
248       $this->dn= $this->list[trim($s_entry)]['dn'];
250       /* Load permissions for selected 'dn' and check if
251          we're allowed to remove this 'dn' */
252       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
254         /* User is allowed to change passwords, save 'dn' and 'acl' for next
255            dialog. */
256         $_SESSION['objectinfo']= $this->dn;
257         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
258         
260       } else {
261         /* User is not allowed. Show message and cancel. */
262         msg_dialog::display(_("Password change"),_("You are not allowed to set this users password!"),WARNING_DIALOG);
263       }
264     }
269     /********************
270       Edit existing entry 
271      ********************/
273     /* User wants to edit data? */
274     if (($s_action == "multiple_edit" || $s_action=="edit") && (!isset($this->usertab->config))){
276       $this->dn = array();
277       /* Get 'dn' from posted 'uid', must be unique */
278       if($s_action == "multiple_edit"){
279         foreach($this->list_get_selected_items() as $id){
280           $this->dn[] = $this->list[trim($id)]['dn'];
281         }
282       }else{
283         $this->dn= array($this->list[trim($s_entry)]['dn']);
284       }
286       /* Check locking, save current plugin in 'back_plugin', so
287          the dialog knows where to return. */
288       $users= get_multiple_locks($this->dn);
289       if (count($users)){
290         return(gen_locked_message($users, $this->dn));
291       }
293       /* Lock the current entry, so everyone will get the
294          above dialog */
295       add_lock ($this->dn, $this->ui->dn);
297       /* Register usertab to trigger edit dialog */
298       $tbs = new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
300       if($tbs){
301         $this->usertab = $tbs;
303         /* Switch tab, if it was requested by the user */
304         $this->usertab->current = $s_tab;
306         /* Set ACL and move DN to the headline */
307 #        $this->usertab->set_acl_base($this->dn);
308          $_SESSION['objectinfo']= _("Multiple edit");
309       }
310     }
313     /********************
314       Edit canceled 
315      ********************/
317     /* Reset all relevant data, if we get a _cancel request */
318     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
319       if (isset($this->usertab)){
320         del_lock ($this->usertab->dn);
321         unset ($this->usertab);
322       }
323       $this->usertab= NULL;
324       $this->lognames= array();;
325       $this->sn= "";
326       $this->givenName= "";
327       $this->uid= "";
328       unset ($_SESSION['objectinfo']);
329     }
332     /********************
333       Delete MULTIPLE entries requested, display confirm dialog
334      ********************/
336     if ($s_action=="del_multiple"){
337       $ids = $this->list_get_selected_items();
338       $this->dns = array();
339       if(count($ids)){
341         foreach($ids as $id){
342           $dn = $this->list[$id]['dn'];
343           if (($user= get_lock($dn)) != ""){
344             return(gen_locked_message ($user, $dn));
345           }
346           $this->dns[$id] = $dn; 
347         }
349         $dns_names = "<br><pre>";
350         foreach($this->dns as $dn){
351           $dns_names .= $dn."\n";
352         }
353         $dns_names .="</pre>";
355         /* Lock the current entry, so nobody will edit it during deletion */
356         if (count($this->dns) == 1){
357           $info = sprintf(_("You're about to delete the following entry: %s"), @LDAP::fix($dns_names));
358         } else {
359           $info = sprintf(_("You're about to delete the following entries: %s"), @LDAP::fix($dns_names));
360         }
361         $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
362         $this->current_action = $s_action;
363       }
364     }
367     /********************
368       Delete MULTIPLE entries confirmed 
369      ********************/
371       /* Confirmation for deletion has been passed. Users should be deleted. */
372       if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
373         
374         $this->current_action = "";  
375         
376         /* Remove user by user and check acls before removeing them */
377         foreach($this->dns as $key => $dn){
379           $acl = $this->ui->get_permissions($dn, "users/user"); 
380           if (preg_match('/d/', $acl)){
382             /* Delete request is permitted, perform LDAP action */
383             $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
384             $this->usertab->set_acl_base();
385             $this->usertab->delete ();
386             unset ($this->usertab);
387             $this->usertab= NULL;
388           } else {
389             msg_dialog::display(_("User delete"),sprintf(_("You are not allowed to delete the user '%s'!"),$dn),WARNING_DIALOG);
390             if(isset($this->ui->uid)){
391               new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
392             }
393           }
394           /* Remove lock file after successfull deletion */
395           del_lock ($dn);
396           unset($this->dns[$key]);
397       }
398     }
401     /********************
402       Delete MULTIPLE entries Canceled 
403      ********************/
405     /* Remove lock */
406     if(isset($_POST['delete_multiple_user_cancel'])){
407       foreach($this->dns as $key => $dn){
408         del_lock ($dn);
409         unset($this->dns[$key]);
410       }
411     }
412   
413   
414     /********************
415       Toggle lock status for user
416      ********************/
417   
418     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
420       /* Get entry check current status */
421       $val = $this->list[$s_entry];
422       $pwd = $val['userPassword'][0];
424       if(!preg_match("/^\{[^\}]/",$pwd)){
425         trigger_error("Can not deactivate user which is using clear password encryption.");
426       }else{
428         $locked = false;
429         if(preg_match("/^[^\}]*+\}!/",$pwd)){
430           $locked = true;
431         }
433         /* Create ldap array to update status */
434         $attrs = array("userPassword" => $pwd);
435         if($locked){
436           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
437         }else{
438           $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
439         }
441         /* Write new status back to ldap */
442         $ldap = $this->config->get_ldap_link();
443         $ldap->cd($val['dn']);
444         $ldap->modify($attrs);
445         if($locked){
446           show_ldap_error($ldap->get_error(),_("Could not set user status from locked to unlocked."));
447         }else{
448           show_ldap_error($ldap->get_error(),_("Could not set user status from unlocked to locked."));
449         }
450       }
451     }
454     /********************
455       Delete entry requested, display confirm dialog
456      ********************/
458     /* Remove user was requested */
459     if ($s_action=="del"){
461       /* Get 'dn' from posted 'uid' */
462       $this->dn= $this->list[trim($s_entry)]['dn'];
464       /* Load permissions for selected 'dn' and check if
465          we're allowed to remove this 'dn' */
467       /* Check locking, save current plugin in 'back_plugin', so
468          the dialog knows where to return. */
469       if (($user= get_lock($this->dn)) != ""){
470         return(gen_locked_message ($user, $this->dn));
471       }
473   
474       /* Lock the current entry, so nobody will edit it during deletion */
475 #      add_lock ($this->dn, $this->ui->dn);
476 #      $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
477 #      $smarty->assign("multiple", false);
478 #      return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
479       $this->msg_dialog = new msg_dialog( _("Delete user"),sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)),CONFIRM_DIALOG);
480       $this->current_action = $s_action;
481     }
484     /********************
485       Delete entry confirmed 
486      ********************/
488     /* Confirmation for deletion has been passed. User should be deleted. */
489     if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
491       $this->current_action = ""; 
492  
493       /* Some nice guy may send this as POST, so we've to check
494          for the permissions again. */
496       $acl = $this->ui->get_permissions($this->dn, "users/user"); 
497  
498       if (preg_match('/d/', $acl)){
500         /* Delete request is permitted, perform LDAP action */
501         $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
502         $this->usertab->set_acl_base();
503         $this->usertab->delete ();
504         unset ($this->usertab);
505         $this->usertab= NULL;
506         msg_dialog::display(_("User delted"),_("User successfully removed."),INFO_DIALOG);
507       } else {
509         /* Normally this shouldn't be reached, send some extra
510            logs to notify the administrator */
511         msg_dialog::display(_("User delete"),_("You are not allowed to delete this user!"),WARNING_DIALOG);
513         if(isset($this->ui->uid)){
514           new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
515         }
516       }
518       /* Remove lock file after successfull deletion */
519       del_lock ($this->dn);
520     }
522     
523     /********************
524       Delete entry Canceled 
525      ********************/
527     /* Delete user canceled? */
528     if (isset($_POST['delete_cancel'])){
529       del_lock ($this->dn);
530     }
533     /********************
534       Edit entry finished (Save) 
535      ********************/
537     /* Finish user edit is triggered by the tabulator dialog, so
538        the user wants to save edited data. Check and save at this
539        point. */
540     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
542       /* Check tabs, will feed message array */
543       $this->usertab->last= $this->usertab->current;
544       $this->usertab->save_object();
545       $message= $this->usertab->check();
547       /* Save, or display error message? */
548       if (count($message) == 0){
550         /* No errors. Go ahead and prepare to ask for a password
551            in case we're creating a new user. 'dn' will be 'new'
552            in this case. It is set to the correct value later. */
553         if ($this->dn == "new"){
554           $set_pass= 1;
555         } else {
556           $set_pass= 0;
557         }
559         /* Save user data to ldap */
560         if($this->usertab->save() == 1){
561           return;
562         }
564         if (!isset($_POST['edit_apply'])){
565           /* User has been saved successfully, remove lock from LDAP. */
566           if ($this->dn != "new"){
567             del_lock ($this->dn);
568           }
570           /* In case of new users, ask for a password, skip this for templates */
571           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
572             $this->dn = $this->usertab->dn;
573             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
574           }
576           unset ($this->usertab);
577           $this->usertab= NULL;
578           unset ($_SESSION['objectinfo']);
579         }
580       } else {
581         /* Ok. There seem to be errors regarding to the tab data,
582            show message and continue as usual. */
583         show_errors($message);
584       }
585     }
588     /********************
589       We want to create a new user, so fetch all available user templates 
590      ********************/
592     /* Generate template list */
593     if (($s_action=="new")||($s_action=="create_user_from_tpl")){
595       $this->templates= array();
596       $ldap= $this->config->get_ldap_link();
598       /* Create list of templates */
599       foreach ($this->config->departments as $key => $value){
600     
601         /* Get acls from different ou's */
602         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
603  
604         /* If creation of a new user is allowed, append this template */
605         if (preg_match("/c/",$acl)){
606           
607           /* Search all templates from the current dn */
608           $ldap->cd (get_people_ou().$value);
609           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
611           /* Append */
612           if ($ldap->count() != 0){
613             while ($attrs= $ldap->fetch()){
614               $this->templates[$ldap->getDN()]=
615                 $attrs['uid'][0]." - ".@LDAP::fix($key);
616             }
617             $this->templates['none']= _("none");
618           }
619         }
620       }
622       /* Sort templates */
623       natcasesort ($this->templates);
624       reset ($this->templates);
625     }
628     /********************
629       Create a new user,template, user from template 
630      ********************/
632     /* Check selected options for template */
633     if (isset($_POST['template_continue'])){
634       $message = array();
635       if(!isset($_POST['template']) || (empty($_POST['template']))){
636         $message[] = _("Please select a valid template.");
637       }
638       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
639         $message[]= _("The required field 'Name' is not set.");
640       }
641       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
642         $message[]= _("The required field 'Given name' is not set.");
643       }
644     
645       /* Show error message / continue editing */
646       if (count($message) > 0){
647         show_errors ($message);
649         foreach(array("sn", "givenName", "uid", "template") as $attr){
650           if(isset($_POST[$attr])){
651             $smarty->assign("$attr", $_POST[$attr]);
652           }else{
653             $smarty->assign("$attr", "");
654           }
655         }
656         $smarty->assign("templates",$this->templates);
657         $smarty->assign("got_uid",$this->got_uid);
658         $smarty->assign("edit_uid",false);
659         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
661       }
662     }
664     /* New user/template request */
665     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
666       /* By default we set 'dn' to 'new', all relevant plugins will
667          react on this. */
668       $this->dn= "new";
669       
670       if (isset($this->config->current['IDGEN'])){
671         $this->got_uid= false;
672       } else {
673         $this->got_uid= true;
674       }
676       /* Create new usertab object */
677       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
678       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
679       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
681       /* Take care about templates */
682       if ($s_action=="new_tpl"){
683         $this->is_template= TRUE;
684         $this->usertab->set_template_mode ();
685       } else {
686         $this->is_template= FALSE;
687       }
689       /* Use template if there are any of them */
690       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
691         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
692           $smarty->assign("$attr", $this->$attr);
693         }
694         if ($s_action=="create_user_from_tpl"){
695           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
696         } else {
697           $smarty->assign("template", "none");
698         }
699         $smarty->assign("edit_uid", "");
700         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
701       }
702     }
704     /********************
705       Template selected continue edit
706      ********************/
708     /* Continue template editing */
709     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
711       $this->sn             = $_POST['sn'];
712       $this->givenName      = $_POST['givenName'];
714       /* Check for requred values */
715       $message= array();
716       if ($this->sn == "") {
717         $message[]= _("The required field 'Name' is not set.");
718       }
719       if ($this->givenName == "") {
720         $message[]= _("The required field 'Given name' is not set.");
721       }
723       /* Check if dn is used */
724       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
725       $ldap= $this->config->get_ldap_link();
726       $ldap->cd ($dn);
727       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
728       if ($ldap->count () != 0){
729         $message[]= _("A person with the choosen name is already used in this tree.");
730       }
732       /* Show error message / continue editing */
733       if (count($message) > 0){
734         show_errors ($message);
735       } else {
736         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
737         if (isset($this->config->current['IDGEN']) &&
738             $this->config->current['IDGEN'] != ""){
739           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
740           if (count($uids)){
741             $smarty->assign("edit_uid", "false");
742             $smarty->assign("uids", $uids);
743             $this->uid= current($uids);
744           }
745         } else {
746           $smarty->assign("edit_uid", "");
747           $this->uid= "";
748         }
749         $this->got_uid= true;
750       }
752       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
753         $smarty->assign("$attr", $this->$attr);
754       }
755       if (isset($_POST['template'])){
756         $smarty->assign("template", $_POST['template']);
757       }
758       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
759     }
761     /********************
762       No template selected continue edit
763      ********************/
765     /* No template. Ok. Lets fill data into the normal user dialog */
766     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
767       foreach(array("sn", "givenName", "uid") as $attr){
768         if (isset($_POST[$attr])){
769           $this->usertab->by_object['user']->$attr= $_POST[$attr];
770         }
771       }
772     }
775     /********************
776       Template selected continue edit
777      ********************/
779     /* Finish template preamble */
780     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
782       /* Might not be filled if IDGEN is unset */
783       $this->sn                 = $_POST['sn'];
784       $this->givenName          = $_POST['givenName'];
786       /* Move user supplied data to sub plugins */
787       $this->uid                = $_POST['uid'];
788       $this->usertab->uid       = $this->uid;
789       $this->usertab->sn        = $this->sn;
790       $this->usertab->givenName = $this->givenName;
791       $template_dn              = $_POST['template'];
792       $this->usertab->adapt_from_template($template_dn);
793       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
794       $this->usertab->by_object['user']->base= $template_base;
795     }
796    
797  
798     /********************
799       If no template was selected set base
800      ********************/
802     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
803       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
804     }
807     /********************
808       Display subdialog 
809      ********************/
811     /* Show tab dialog if object is present */
812     if(isset($this->usertab->config)){
813       $display= $this->usertab->execute();
815       /* Don't show buttons if tab dialog requests this */
816       if(isset($this->usertab->by_object)){
817       
818         $dia = FALSE;
819         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
820           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
821         }
823         if(!is_object($dia) && $dia != TRUE){
824           $display.= "<p style=\"text-align:right\">\n";
825           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
826           $display.= "&nbsp;\n";
827           if ($this->dn != "new"){
828             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
829             $display.= "&nbsp;\n";
830           }
831           $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
832           $display.= "</p>";
833         }
834       }
835       return ($display);
836     }
837     
838     /* Check if there is a snapshot dialog open */
839     $base = $this->DivListUsers->selectedBase;
840     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
841       return($str);
842     }
843   
844     /* Return rendered main page */
845         /* Display dialog with system list */
846     $this->DivListUsers->parent = $this;
847     $this->DivListUsers->execute();
849     /* Add departments if subsearch is disabled */
850     if(!$this->DivListUsers->SubSearch){
851       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
852     }
853     $this->reload();
854     $this->DivListUsers->setEntries($this->list);
855     return($this->DivListUsers->Draw());
856   }
859   /* Return departments, that will be included within snapshot detection */
860   function get_used_snapshot_bases()
861   {
862     return(array(get_people_ou().$this->DivListUsers->selectedBase));
863   }  
866   function reload()
867   {
868     /* Set base for all searches */
869     $base= $this->DivListUsers->selectedBase;
870     $this->list =array();
872     /* Get filter configuration */
873     $Regex                = $this->DivListUsers->Regex;
874     $SubSearch            = $this->DivListUsers->SubSearch;
875     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
876     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
877     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
878     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
879     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
880     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
882     /* Setup filter depending on selection */
883     $filter="";
884     if ($this->config->current['SAMBAVERSION'] == 3){
885       $samba= "sambaSamAccount";
886     } else {
887       $samba= "sambaAccount";
888     }
890     if ($ShowFunctionalUsers){
891       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
892                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
893                 "(objectClass=gosaProxyAccount))))";
894     }
895     if ($ShowUnixUsers){
896       $filter.= "(objectClass=posixAccount)";
897     }
898     if ($ShowMailUsers){
899       $filter.= "(objectClass=gosaMailAccount)";
900     }
901     if ($ShowSambaUsers){
902       $filter.= "(objectClass=$samba)";
903     }
904     if ($ShowProxyUsers){
905       $filter.= "(objectClass=gosaProxyAccount)";
906     }
907     if ($ShowTemplates){
908       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
909     } else {
910       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
911     }
912     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
914     /* Generate userlist */
915     $ldap= $this->config->get_ldap_link(TRUE);
917     if ($SubSearch){
918       $ListTemp =  get_list($filter, "users", $base,
919                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
920     } else {
921       $base= get_people_ou().$base;
922       $ListTemp = get_list($filter, "users", $base, 
923                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
924     }
925     $SortTemp = array();
926     $List = array();
927     foreach($ListTemp as $Key => $Entry){
929       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
930        * Else winstations will be listed too, if you use the subtree flag. 
931        */
932       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
933         continue;
934       }else{
936         // Generate caption for rows
937         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
938           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
939         } else {
940           $display= "[".$Entry["uid"][0]."]";
941         }
943         $display = strtolower($display);
944         $List[$display] = $Entry;
945         $SortTemp[$display] = $display;
946       }
947     }
948     sort($SortTemp);
949     reset($SortTemp);
951     $this->list = array();
952     foreach($SortTemp as $Key){
953       $this->list[] = $List[$Key];
954     }
955   }
957   function remove_lock()
958   {
959     /* Remove user lock if a DN is marked as "currently edited" */
960     if (isset($this->usertab->dn)){
961       del_lock ($this->usertab->dn);
962     }
963   }
966   function copyPasteHandling_from_queue($s_action,$s_entry)
967   {
968     /* Check if Copy & Paste is disabled */
969     if(!is_object($this->CopyPasteHandler)){
970       return("");
971     }
973     /* Add a single entry to queue */
974     if($s_action == "cut" || $s_action == "copy"){
975       /* Cleanup object queue */
976       $this->CopyPasteHandler->cleanup_queue();
977       $dn = $this->list[$s_entry]['dn'];
978       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
979     }
981     /* Add entries to queue */
982     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
984       /* Cleanup object queue */
985       $this->CopyPasteHandler->cleanup_queue();
987       /* Add new entries to CP queue */
988       foreach($this->list_get_selected_items() as $id){
989         $dn = $this->list[$id]['dn'];
991         if($s_action == "copy_multiple"){
992           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
993         }
994         if($s_action == "cut_multiple"){
995           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
996         }
997       }
998     }
999     
1000     /* Start pasting entries */
1001     if($s_action == "editPaste"){
1002       $this->start_pasting_copied_objects = TRUE;
1003     }
1005     /* Return C&P dialog */ 
1006     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1007    
1008       /* Load entry from queue and set base */
1009       $this->CopyPasteHandler->load_entry_from_queue();
1010       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1012       /* Get dialog */
1013       $data = $this->CopyPasteHandler->execute();
1015       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1016       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1017         $s_entry = $this->CopyPasteHandler->last_entry();
1018         $this->reload();
1019         foreach($this->list as $key => $entry){
1020           if($entry['dn'] == $s_entry){
1021             $this->CPPasswordChange = $key;
1022           }
1023         }
1024       }
1026       /* Return dialog data */
1027       if(!empty($data) && $this->CPPasswordChange == ""){
1028         return($data);
1029       }
1030     }
1032     /* Automatically disable status for pasting */ 
1033     if(!$this->CopyPasteHandler->entries_queued()){
1034       $this->start_pasting_copied_objects = FALSE;
1035     }
1036     return("");
1037   }
1040   function save_object()
1041   {
1042     /* Handle divlist filter && department selection*/
1043     if(!is_object($this->usertab)){
1044       $this->DivListUsers->save_object();
1045     }
1046   }
1048     
1049   function list_get_selected_items()
1050   {
1051     $ids = array();
1052     foreach($_POST as $name => $value){
1053       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1054         $id   = preg_replace("/^item_selected_/","",$name);
1055         $ids[$id] = $id;
1056       }
1057     }
1058     return($ids);
1059   }
1060   
1062   /* A set of disabled and therefore overloaded functions. They are
1063      not needed in this class. */
1064   function remove_from_parent() { } 
1065   function check() { } 
1066   function save() { } 
1067   function adapt_from_template($dn) { } 
1068   function password_change_needed() { } 
1070 } /* ... class userManagement */
1071 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1072 ?>