Code

d2eecf083f1fed1232b7e0df7ae0142781c88fe3
[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       /* Are we allowed to create a new user or to set the password attribute? */
250       if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
252         /* Check input and feed errors into 'message' */
253         $message= array();
255         /* Sanity checks... */
256         if ($_POST['new_password'] != $_POST['repeated_password']){
258           /* Matching passwords in new and repeated? */
259           $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
260         } else {
262           /* Empty password is not permitted by default. */
263           if ($_POST['new_password'] == ""){
264             msgPool::required(_("New password"));
265           }
266         }
268         /* Errors, or password change? */
269         if (count($message) != 0){
271           /* Show error message and continue editing */
272           msg_dialog::displayChecks($message);
273           return($smarty->fetch(get_template_path('password.tpl', TRUE)));
274         }
276         $config= $this->config;
277         $ldap_ui= $this->config->get_ldap_link();
278         if(isset($this->usertab->dn)){
279           $ldap_ui->cat($this->usertab->dn,array("uid"));
280           $user = $ldap_ui->fetch();
281         }else{
282           $ldap_ui->cat($this->dn,array("uid"));
283           $user = $ldap_ui->fetch();
284         }
285         if((is_array($user))&&(isset($user['uid']))){
286           $username= $user['uid'][0];
287         }
289         /* Set password, perform required steps */
290         if ($this->usertab){
291           if ($this->usertab->password_change_needed()){
292             $obj= $this->usertab->by_object['user'];
293             if(!change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage)){
294               return($smarty->fetch(get_template_path('password.tpl', TRUE)));
295             }
296             if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
297               exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
298             }
299             new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
300             unset($this->usertab);
301             $this->usertab= NULL;
302           }
303         } else {
304           if(!change_password ($this->dn, $_POST['new_password'])){
305             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
306           }
307           if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
308             exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
309           }
310           new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
311         }
312       } else {
314         /* Missing permissions, show message */
315         msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
316       }
317       /* Clean session, delete lock */
318       $this->remove_lock();
319       unset ($this->usertab);
320       $this->usertab= NULL;
321       $this->lognames= array();;
322       $this->sn= "";
323       $this->givenName= "";
324       $this->uid= "";
325       session::un_set('objectinfo');
326     }
329     /********************
330      Change multiple passwords requested 
331      ********************/
332   
333     if($s_action == "multiple_password_change"){
334       $this->pwd_change_queue = $this->list_get_selected_items();
335       $disallowed = array();
336       foreach($this->pwd_change_queue as $key => $id){
337         if(!preg_match("/w/",$this->ui->get_permissions($this->list[trim($id)]['dn'],"users/password"))){
338           unset($this->pwd_change_queue[$key]);
339           $disallowed[] = $this->list[trim($id)]['dn'];
340         }
341       }
342       if(count($disallowed)){
343         msg_dialog::display(_("Permission"),msgPool::permModify($disallowed),INFO_DIALOG);
344       }
345     }    
348     /********************
349       Change password requested  
350      ********************/
352     /* Password change requested */
353     if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
355       /* Get users whose passwords should be changed. */
356       if(count($this->pwd_change_queue)){
357         $s_entry= array_pop($this->pwd_change_queue);
358       }
360       if(!empty($this->CPPasswordChange)){
361         $s_entry = $this->CPPasswordChange;
362         $this->CPPasswordChange = "";
363       }
365       /* Get 'dn' from posted 'uid' */
366       $this->dn= $this->list[trim($s_entry)]['dn'];
368       /* Load permissions for selected 'dn' and check if
369          we're allowed to remove this 'dn' */
370       if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
372         /* User is allowed to change passwords, save 'dn' and 'acl' for next
373            dialog. */
374         session::set('objectinfo',$this->dn);
375         return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
376       } else {
377         /* User is not allowed. Show message and cancel. */
378         msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
379       }
380     }
384      /********************
385       Edit existing entry
386      ********************/
389     /* User wants to edit data? */
390     if (($s_action=="edit") && (!isset($this->usertab->config))){
392       /* Get 'dn' from posted 'uid', must be unique */
393       $this->dn= $this->list[trim($s_entry)]['dn'];
395       /* Check locking, save current plugin in 'back_plugin', so
396          the dialog knows where to return. */
397       if (($user= get_lock($this->dn)) != ""){
398         return(gen_locked_message ($user, $this->dn));
399       }
401       /* Lock the current entry, so everyone will get the
402          above dialog */
403       add_lock ($this->dn, $this->ui->dn);
405       /* Register usertab to trigger edit dialog */
406       $this->usertab= new usertabs($this->config,
407           $this->config->data['TABS']['USERTABS'], $this->dn);
409       /* Switch tab, if it was requested by the user */
410       $this->usertab->current = $s_tab;
412       /* Set ACL and move DN to the headline */
413       $this->usertab->set_acl_base($this->dn);
414       session::set('objectinfo',$this->dn);
415     }
418     /********************
419       Edit multiple entries
420      ********************/
422     /* User wants to edit data? */
423     if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
425       $this->dn = array();
426       foreach($this->list_get_selected_items() as $id){
427         $this->dn[] = $this->list[$id]['dn'];;
428       }
429       $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
430             $this->dn,$this->DivListUsers->selectedBase,"user");
431       if ($tmp->entries_locked()){
432         return($tmp->display_lock_message());
433       }
434       $tmp->lock_entries($this->ui->dn);
435       if($tmp->multiple_available()){
436         $this->usertab = $tmp;
437         $this->usertab->set_active_tab($s_tab);
438         session::set('objectinfo',$this->usertab->get_object_info());
439       }
440     }
443     /********************
444       Edit canceled 
445      ********************/
447     /* Reset all relevant data, if we get a _cancel request */
448     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
449       if (isset($this->usertab)){
450         $this->remove_lock();
451       }
452       $this->usertab= NULL;
453       $this->lognames= array();;
454       $this->sn= "";
455       $this->givenName= "";
456       $this->uid= "";
457       session::un_set('objectinfo');
458     }
461     /********************
462       We want to create a new user, so fetch all available user templates 
463      ********************/
465     /* Generate template list */
466     if ($s_action == "new" || $s_action == "create_user_from_tpl" || $s_action == "templatize_multiple"){
468       $this->templates= array();
469       $ldap= $this->config->get_ldap_link();
471       /* Create list of templates */
472       foreach ($this->config->departments as $key => $value){
473     
474         /* Get acls from different ou's */
475         $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user")       ; 
476  
477         /* If creation of a new user is allowed, append this template */
478         if (preg_match("/c/",$acl)){
479           
480           /* Search all templates from the current dn */
481           $ldap->cd (get_people_ou().$value);
482           $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
484           /* Append */
485           if ($ldap->count() != 0){
486             while ($attrs= $ldap->fetch()){
487               $this->templates[$ldap->getDN()]=
488                 $attrs['uid'][0]." - ".@LDAP::fix($key);
489             }
490             if ($s_action != "templatize_multiple"){
491               $this->templates['none']= _("none");
492             }
493           }
494         }
495       }
497       /* Sort templates */
498       natcasesort ($this->templates);
499       reset ($this->templates);
500     }
503     /********************
504       Apply template to multiple entries requested, display confirm dialog
505      ********************/
507     if ($s_action=="templatize_multiple"){
508       $ids = $this->list_get_selected_items();
509       $this->dns = array();
510       if(count($ids)){
512         foreach($ids as $id){
513           $dn = $this->list[$id]['dn'];
514           if (($user= get_lock($dn)) != ""){
515             return(gen_locked_message ($user, $dn));
516           }
517           $this->dns[$id] = $dn; 
518         }
519       }
521       $smarty->assign("templates", $this->templates);
523       return($smarty->fetch(get_template_path('templatize.tpl', TRUE)));
524     }
526     /* Perform templatizing after the button has been pressed */
527     if ($s_action == "templatize_continue"){
529       $acl = $this->ui->get_permissions($_POST['template'], "users/user");
531       /* Template readable? */
532       if (preg_match('/r/', $acl)){
533         $template_dn= $_POST['template'];
535         foreach ($this->dns as $dn){
536           $acl = $this->ui->get_permissions($_POST['template'], "users/user");
537           if (preg_match('/w/', $acl)){
538             $usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
539             $usertab->adapt_from_template($template_dn, array("sn", "givenName", "uid"));
540             $usertab->save();
541             unset ($usertab);
542             $usertab= NULL;
543           } else {
544             msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to modify object '%s'!"), $dn), ERROR_DIALOG);
545           }
546         }
547       } else {
548         msg_dialog::display(_("Permission error"), _("You have no permission to use this template!"), ERROR_DIALOG);
549       }
551     }
554     /********************
555       Delete MULTIPLE entries requested, display confirm dialog
556      ********************/
558     if ($s_action=="del_multiple" || $s_action == "del"){
560       if($s_action == "del"){
562         /* Get 'dn' from posted 'uid' */
563         $ids = array($s_entry);
564       }else{
565         $ids = $this->list_get_selected_items();
566       }
568       $this->dns = array();
569       if(count($ids)){
570         $disallowed = array();
571         foreach($ids as $id){
572           $dn = $this->list[$id]['dn'];
573           $acl = $this->ui->get_permissions($dn, "users/user"); 
574           if(preg_match("/d/",$acl)){
575             $this->dns[$id] = $dn;
576           }else{
577             $disallowed[] = $dn;
578           }
579         }
580         
581         if(count($disallowed)){
582           msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
583         }
585         /* Check locks */
586         if ($user= get_multiple_locks($this->dns)){
587           return(gen_locked_message($user,$this->dns));
588         }
590         $dns_names = array();
591         foreach($this->dns as $dn){
592           $dns_names[] = @LDAP::fix($dn);
593         }
595         add_lock($this->dns, $this->ui->dn);
597         /* Lock the current entry, so nobody will edit it during deletion */
598         $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
600         /* Lock the current entry, so nobody will edit it during deletion */
601         $smarty->assign("info", msgPool::deleteInfo($dns_names));
602         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
603       }
604     }
607     /********************
608       Delete MULTIPLE entries confirmed 
609      ********************/
611     if(isset($_POST['delete_user_confirm'])){
613       /* Remove user by user and check acls before removeing them */
614       foreach($this->dns as $key => $dn){
616         $acl = $this->ui->get_permissions($dn, "users/user"); 
617         if (preg_match('/d/', $acl)){
619           /* Delete request is permitted, perform LDAP action */
620           $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
621           $this->usertab->set_acl_base();
622           $this->usertab->delete ();
623           unset ($this->usertab);
624           $this->usertab= NULL;
625         } else {
626           msg_dialog::display(_("Warning"),msgPool::permDelete($dn),WARNING_DIALOG);
627           if(isset($this->ui->uid)){
628             new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
629           }
630         }
631       }
632       /* Remove lock file after successfull deletion */
633       $this->remove_lock();
634       $this->dns = array();
635     }
637   
638     /********************
639       Toggle lock status for user
640      ********************/
641   
642     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
644       /* Get entry check current status */
645       $val = $this->list[$s_entry];
646       $pwd = $val['userPassword'][0];
649       if (!preg_match("/w/",$this->ui->get_permissions($val['dn'],"users/password"))){
651         /* Missing permissions, show message */
652         msg_dialog::display(_("Password change"),_("You have no permission to change the lock status for this user!"),WARNING_DIALOG);
654       }else{
656         if(!preg_match("/^\{[^\}]/",$pwd)){
657           trigger_error("Can not deactivate user which is using clear password encryption.");
658         }else{
660           $locked = false;
661           if(preg_match("/^[^\}]*+\}!/",$pwd)){
662             $locked = true;
663           }
665           /* Create ldap array to update status */
666           $attrs = array("userPassword" => $pwd);
667           if($locked){
668             $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
669           }else{
670             $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
671           }
673           /* Write new status back to ldap */
674           $ldap = $this->config->get_ldap_link();
675           $ldap->cd($val['dn']);
676           $ldap->modify($attrs);
677           if (!$ldap->success()){
678             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
679           }
680         }
681       }
682     }
685     /********************
686       Delete entry Canceled 
687      ********************/
689     /* Delete user canceled? */
690     if (isset($_POST['delete_cancel'])){
692       /* Remove lock file after successfull deletion */
693       $this->remove_lock();
694       $this->dns = array();
695     }
698     /********************
699       Edit entry finished (Save) 
700      ********************/
702     /* Finish user edit is triggered by the tabulator dialog, so
703        the user wants to save edited data. Check and save at this
704        point. */
705     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
707       /* Check tabs, will feed message array */
708       $this->usertab->last= $this->usertab->current;
709       $this->usertab->save_object();
710       $message= $this->usertab->check();
712       /* Save, or display error message? */
713       if (count($message) == 0){
715         /* No errors. Go ahead and prepare to ask for a password
716            in case we're creating a new user. 'dn' will be 'new'
717            in this case. It is set to the correct value later. */
718         if ($this->dn == "new"){
719           $set_pass= 1;
720         } else {
721           $set_pass= 0;
722         }
724         /* Save user data to ldap */
725         if($this->usertab->save() == 1){
726           return;
727         }
729         if (!isset($_POST['edit_apply'])){
730           /* User has been saved successfully, remove lock from LDAP. */
731           if ($this->dn != "new"){
732             $this->remove_lock();
733           }
735           /* In case of new users, ask for a password, skip this for templates */
736           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
737             $this->dn = $this->usertab->dn;
738             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
739           }
741           unset ($this->usertab);
742           $this->usertab= NULL;
743           session::un_set('objectinfo');
744         }else{
745         
746           /* Reinitialize tab */
747           if($this->usertab instanceof tabs){
748             $this->usertab->re_init();
749           }
750         }
751       } else {
752         /* Ok. There seem to be errors regarding to the tab data,
753            show message and continue as usual. */
754         msg_dialog::displayChecks($message);
755       }
756     }
760     /********************
761       Create a new user,template, user from template 
762      ********************/
764     /* Check selected options for template */
765     if (isset($_POST['template_continue'])){
766       $message = array();
767       if(!isset($_POST['template']) || (empty($_POST['template']))){
768         $message[]= msgPool::invalid(_("Template"));
769       }
770       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
771         $message[]= msgPool::required(_("Name"));
772       }
773       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
774         $message[]= msgPool::required(_("Given name"));
775       }
776     
777       /* Show error message / continue editing */
778       if (count($message) > 0){
779         msg_dialog::displayChecks($message);
781         foreach(array("sn", "givenName", "uid", "template") as $attr){
782           if(isset($_POST[$attr])){
783             $smarty->assign("$attr", $_POST[$attr]);
784           }else{
785             $smarty->assign("$attr", "");
786           }
787         }
788         $smarty->assign("templates",$this->templates);
789         $smarty->assign("got_uid",$this->got_uid);
790         $smarty->assign("edit_uid",false);
791         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
793       }
794     }
796     /* New user/template request */
797     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
798       /* By default we set 'dn' to 'new', all relevant plugins will
799          react on this. */
800       $this->dn= "new";
801       
802       if (isset($this->config->current['IDGEN'])){
803         $this->got_uid= false;
804       } else {
805         $this->got_uid= true;
806       }
808       /* Create new usertab object */
809       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
810       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
811       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
813       /* Take care about templates */
814       if ($s_action=="new_tpl"){
815         $this->is_template= TRUE;
816         $this->usertab->set_template_mode ();
817       } else {
818         $this->is_template= FALSE;
819       }
821       /* Use template if there are any of them */
822       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
823         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
824           $smarty->assign("$attr", $this->$attr);
825         }
826         if ($s_action=="create_user_from_tpl"){
827           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
828         } else {
829           $smarty->assign("template", "none");
830         }
831         $smarty->assign("edit_uid", "");
832         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
833       }
834     }
836     /********************
837       Template selected continue edit
838      ********************/
840     /* Continue template editing */
841     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
843       $this->sn             = $_POST['sn'];
844       $this->givenName      = $_POST['givenName'];
846       /* Check for requred values */
847       $message= array();
848       if ($this->sn == "") {
849         $message[]= msgPool::required(_("Name"));
850       }
851       if ($this->givenName == "") {
852         $message[]= msgPool::required(_("Given name"));
853       }
855       /* Check if dn is used */
856       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
857       $ldap= $this->config->get_ldap_link();
858       $ldap->cd ($dn);
859       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
860       if ($ldap->count () != 0){
861         msgPool::duplicated(_("Name"));
862       }
864       /* Show error message / continue editing */
865       if (count($message) > 0){
866         msg_dialog::displayChecks($message);
867       } else {
868         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
869         if (isset($this->config->current['IDGEN']) &&
870             $this->config->current['IDGEN'] != ""){
871           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
872           if (count($uids)){
873             $smarty->assign("edit_uid", "false");
874             $smarty->assign("uids", $uids);
875             $this->uid= current($uids);
876           }
877         } else {
878           $smarty->assign("edit_uid", "");
879           $this->uid= "";
880         }
881         $this->got_uid= true;
882       }
884       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
885         $smarty->assign("$attr", $this->$attr);
886       }
887       if (isset($_POST['template'])){
888         $smarty->assign("template", $_POST['template']);
889       }
890       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
891     }
893     /********************
894       No template selected continue edit
895      ********************/
897     /* No template. Ok. Lets fill data into the normal user dialog */
898     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
899       foreach(array("sn", "givenName", "uid") as $attr){
900         if (isset($_POST[$attr])){
901           $this->usertab->by_object['user']->$attr= $_POST[$attr];
902         }
903       }
904     }
907     /********************
908       Template selected continue edit
909      ********************/
911     /* Finish template preamble */
912     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
914       /* Might not be filled if IDGEN is unset */
915       $this->sn                 = $_POST['sn'];
916       $this->givenName          = $_POST['givenName'];
918       /* Move user supplied data to sub plugins */
919       $this->uid                = $_POST['uid'];
920       $this->usertab->uid       = $this->uid;
921       $this->usertab->sn        = $this->sn;
922       $this->usertab->givenName = $this->givenName;
923       $template_dn              = $_POST['template'];
924       $this->usertab->adapt_from_template($template_dn);
925       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
926       $this->usertab->by_object['user']->base= $template_base;
927     }
928    
929  
930     /********************
931       If no template was selected set base
932      ********************/
934     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
935       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
936     }
939     /********************
940       Display subdialog 
941      ********************/
943     /* Show tab dialog if object is present */
944     if(isset($this->usertab->config)){
946       $display= $this->usertab->execute();
948       /* Don't show buttons if tab dialog requests this */
949       
950         $dia = FALSE;
951         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
952           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
953         }
955         if(!is_object($dia) && $dia != TRUE){
956           $display.= "<p style=\"text-align:right\">\n";
957           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
958           $display.= "&nbsp;\n";
959           if ($this->dn != "new"){
960             $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
961             $display.= "&nbsp;\n";
962           }
963           $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
964           $display.= "</p>";
965         }
966       return ($display);
967     }
968     
969     /* Check if there is a snapshot dialog open */
970     $base = $this->DivListUsers->selectedBase;
971     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
972       return($str);
973     }
974   
975     /* Return rendered main page */
976         /* Display dialog with system list */
977     $this->DivListUsers->parent = $this;
978     $this->DivListUsers->execute();
980     /* Add departments if subsearch is disabled */
981     if(!$this->DivListUsers->SubSearch){
982       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
983     }
984     $this->reload();
985     $this->DivListUsers->setEntries($this->list);
986     return($this->DivListUsers->Draw());
987   }
990   /* Return departments, that will be included within snapshot detection */
991   function get_used_snapshot_bases()
992   {
993     return(array(get_people_ou().$this->DivListUsers->selectedBase));
994   }  
997   function reload()
998   {
999     /* Set base for all searches */
1000     $base= $this->DivListUsers->selectedBase;
1001     $this->list =array();
1003     /* Get filter configuration */
1004     $Regex                = $this->DivListUsers->Regex;
1005     $SubSearch            = $this->DivListUsers->SubSearch;
1006     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
1007     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
1008     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
1009     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
1010     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
1011     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
1013     /* Setup filter depending on selection */
1014     $filter="";
1015     if ($this->config->current['SAMBAVERSION'] == 3){
1016       $samba= "sambaSamAccount";
1017     } else {
1018       $samba= "sambaAccount";
1019     }
1021     if ($ShowFunctionalUsers){
1022       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
1023                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
1024                 "(objectClass=gosaProxyAccount))))";
1025     }
1026     if ($ShowUnixUsers){
1027       $filter.= "(objectClass=posixAccount)";
1028     }
1029     if ($ShowMailUsers){
1030       $filter.= "(objectClass=gosaMailAccount)";
1031     }
1032     if ($ShowSambaUsers){
1033       $filter.= "(objectClass=$samba)";
1034     }
1035     if ($ShowProxyUsers){
1036       $filter.= "(objectClass=gosaProxyAccount)";
1037     }
1038     if ($ShowTemplates){
1039       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1040     } else {
1041       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1042     }
1043     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1045     /* Generate userlist */
1046     $ldap= $this->config->get_ldap_link(TRUE);
1048     if ($SubSearch){
1049       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1050                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1051     } else {
1052       $base= get_people_ou().$base;
1053       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1054                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1055     }
1056     $SortTemp = array();
1057     $List = array();
1058     foreach($ListTemp as $Key => $Entry){
1060       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1061        * Else winstations will be listed too, if you use the subtree flag. 
1062        */
1063       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1064         continue;
1065       }else{
1067         // Generate caption for rows
1068         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1069           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1070         } else {
1071           $display= "[".$Entry["uid"][0]."]";
1072         }
1074         $display = strtolower($display);
1075         $List[$display] = $Entry;
1076         $SortTemp[$display] = $display;
1077       }
1078     }
1079     natcasesort($SortTemp);
1080     reset($SortTemp);
1082     $this->list = array();
1083     foreach($SortTemp as $Key){
1084       $this->list[] = $List[$Key];
1085     }
1086   }
1089   function remove_lock()
1090   {
1091     /* Remove user lock if a DN is marked as "currently edited" */
1092     if (isset($this->usertab->dn)){
1093       del_lock ($this->usertab->dn);
1094     }
1095     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
1096       del_lock($this->dn);
1097     }
1098     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
1099       del_lock($this->dns);
1100     }
1101   }
1104   function copyPasteHandling_from_queue($s_action,$s_entry)
1105   {
1106     /* Check if Copy & Paste is disabled */
1107     if(!is_object($this->CopyPasteHandler)){
1108       return("");
1109     }
1111     /* Add a single entry to queue */
1112     if($s_action == "cut" || $s_action == "copy"){
1113       /* Cleanup object queue */
1114       $this->CopyPasteHandler->cleanup_queue();
1115       $dn = $this->list[$s_entry]['dn'];
1116       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1117     }
1119     /* Add entries to queue */
1120     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1122       /* Cleanup object queue */
1123       $this->CopyPasteHandler->cleanup_queue();
1125       /* Add new entries to CP queue */
1126       foreach($this->list_get_selected_items() as $id){
1127         $dn = $this->list[$id]['dn'];
1129         if($s_action == "copy_multiple"){
1130           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1131         }
1132         if($s_action == "cut_multiple"){
1133           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1134         }
1135       }
1136     }
1137     
1138     /* Start pasting entries */
1139     if($s_action == "editPaste"){
1140       $this->start_pasting_copied_objects = TRUE;
1141     }
1143     /* Return C&P dialog */ 
1144     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1145    
1146       /* Get dialog */
1147       $data = $this->CopyPasteHandler->execute();
1148       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1150       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1151       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1152         $s_entry = $this->CopyPasteHandler->last_entry();
1153         $this->reload();
1154         foreach($this->list as $key => $entry){
1155           if($entry['dn'] == $s_entry){
1156             $this->CPPasswordChange = $key;
1157           }
1158         }
1159       }
1161       /* Return dialog data */
1162       if(!empty($data) && $this->CPPasswordChange == ""){
1163         return($data);
1164       }
1165     }
1167     /* Automatically disable status for pasting */ 
1168     if(!$this->CopyPasteHandler->entries_queued()){
1169       $this->start_pasting_copied_objects = FALSE;
1170     }
1171     return("");
1172   }
1175   function save_object()
1176   {
1177     /* Handle divlist filter && department selection*/
1178     if(!is_object($this->usertab)){
1179       $this->DivListUsers->save_object();
1180     }
1181     if(is_object($this->CopyPasteHandler)){
1182       $this->CopyPasteHandler->save_object();
1183     }
1184   }
1186     
1187   function list_get_selected_items()
1188   {
1189     $ids = array();
1190     foreach($_POST as $name => $value){
1191       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1192         $id   = preg_replace("/^item_selected_/","",$name);
1193         $ids[$id] = $id;
1194       }
1195     }
1196     return($ids);
1197   }
1198   
1200   /* A set of disabled and therefore overloaded functions. They are
1201      not needed in this class. */
1202   function remove_from_parent() { } 
1203   function check() { } 
1204   function save() { } 
1205   function adapt_from_template($dn, $skip= array()) { } 
1206   function password_change_needed() { } 
1208 } /* ... class userManagement */
1209 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1210 ?>