Code

Updated copy & paste base adaption
[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         if(count($this->dns)){
587           /* Check locks */
588           if ($user= get_multiple_locks($this->dns)){
589             return(gen_locked_message($user,$this->dns));
590           }
592           $dns_names = array();
593           foreach($this->dns as $dn){
594             $dns_names[] = @LDAP::fix($dn);
595           }
597           add_lock($this->dns, $this->ui->dn);
599           /* Lock the current entry, so nobody will edit it during deletion */
600           $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
602           /* Lock the current entry, so nobody will edit it during deletion */
603           $smarty->assign("info", msgPool::deleteInfo($dns_names));
604           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
605         }
606       }
607     }
610     /********************
611       Delete MULTIPLE entries confirmed 
612      ********************/
614     if(isset($_POST['delete_user_confirm'])){
616       /* Remove user by user and check acls before removeing them */
617       foreach($this->dns as $key => $dn){
619         $acl = $this->ui->get_permissions($dn, "users/user"); 
620         if (preg_match('/d/', $acl)){
622           /* Delete request is permitted, perform LDAP action */
623           $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
624           $this->usertab->set_acl_base();
625           $this->usertab->delete ();
626           unset ($this->usertab);
627           $this->usertab= NULL;
628         } else {
629           msg_dialog::display(_("Warning"),msgPool::permDelete($dn),WARNING_DIALOG);
630           if(isset($this->ui->uid)){
631             new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
632           }
633         }
634       }
635       /* Remove lock file after successfull deletion */
636       $this->remove_lock();
637       $this->dns = array();
638     }
640   
641     /********************
642       Toggle lock status for user
643      ********************/
644   
645     if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
647       /* Get entry check current status */
648       $val = $this->list[$s_entry];
649       $pwd = $val['userPassword'][0];
652       if (!preg_match("/w/",$this->ui->get_permissions($val['dn'],"users/password"))){
654         /* Missing permissions, show message */
655         msg_dialog::display(_("Password change"),_("You have no permission to change the lock status for this user!"),WARNING_DIALOG);
657       }else{
659         if(!preg_match("/^\{[^\}]/",$pwd)){
660           trigger_error("Can not deactivate user which is using clear password encryption.");
661         }else{
663           $locked = false;
664           if(preg_match("/^[^\}]*+\}!/",$pwd)){
665             $locked = true;
666           }
668           /* Create ldap array to update status */
669           $attrs = array("userPassword" => $pwd);
670           if($locked){
671             $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
672           }else{
673             $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
674           }
676           /* Write new status back to ldap */
677           $ldap = $this->config->get_ldap_link();
678           $ldap->cd($val['dn']);
679           $ldap->modify($attrs);
680           if (!$ldap->success()){
681             msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
682           }
683         }
684       }
685     }
688     /********************
689       Delete entry Canceled 
690      ********************/
692     /* Delete user canceled? */
693     if (isset($_POST['delete_cancel'])){
695       /* Remove lock file after successfull deletion */
696       $this->remove_lock();
697       $this->dns = array();
698     }
701     /********************
702       Edit entry finished (Save) 
703      ********************/
705     /* Finish user edit is triggered by the tabulator dialog, so
706        the user wants to save edited data. Check and save at this
707        point. */
708     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
710       /* Check tabs, will feed message array */
711       $this->usertab->last= $this->usertab->current;
712       $this->usertab->save_object();
713       $message= $this->usertab->check();
715       /* Save, or display error message? */
716       if (count($message) == 0){
718         /* No errors. Go ahead and prepare to ask for a password
719            in case we're creating a new user. 'dn' will be 'new'
720            in this case. It is set to the correct value later. */
721         if ($this->dn == "new"){
722           $set_pass= 1;
723         } else {
724           $set_pass= 0;
725         }
727         /* Save user data to ldap */
728         if($this->usertab->save() == 1){
729           return;
730         }
732         if (!isset($_POST['edit_apply'])){
733           /* User has been saved successfully, remove lock from LDAP. */
734           if ($this->dn != "new"){
735             $this->remove_lock();
736           }
738           /* In case of new users, ask for a password, skip this for templates */
739           if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
740             $this->dn = $this->usertab->dn;
741             return($smarty->fetch(get_template_path('password.tpl', TRUE)));
742           }
744           unset ($this->usertab);
745           $this->usertab= NULL;
746           session::un_set('objectinfo');
747         }else{
748         
749           /* Reinitialize tab */
750           if($this->usertab instanceof tabs){
751             $this->usertab->re_init();
752           }
753         }
754       } else {
755         /* Ok. There seem to be errors regarding to the tab data,
756            show message and continue as usual. */
757         msg_dialog::displayChecks($message);
758       }
759     }
763     /********************
764       Create a new user,template, user from template 
765      ********************/
767     /* Check selected options for template */
768     if (isset($_POST['template_continue'])){
769       $message = array();
770       if(!isset($_POST['template']) || (empty($_POST['template']))){
771         $message[]= msgPool::invalid(_("Template"));
772       }
773       if(!isset($_POST['sn']) || (empty($_POST['sn']))){
774         $message[]= msgPool::required(_("Name"));
775       }
776       if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
777         $message[]= msgPool::required(_("Given name"));
778       }
779     
780       /* Show error message / continue editing */
781       if (count($message) > 0){
782         msg_dialog::displayChecks($message);
784         foreach(array("sn", "givenName", "uid", "template") as $attr){
785           if(isset($_POST[$attr])){
786             $smarty->assign("$attr", $_POST[$attr]);
787           }else{
788             $smarty->assign("$attr", "");
789           }
790         }
791         $smarty->assign("templates",$this->templates);
792         $smarty->assign("got_uid",$this->got_uid);
793         $smarty->assign("edit_uid",false);
794         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
796       }
797     }
799     /* New user/template request */
800     if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
801       /* By default we set 'dn' to 'new', all relevant plugins will
802          react on this. */
803       $this->dn= "new";
804       
805       if (isset($this->config->current['IDGEN'])){
806         $this->got_uid= false;
807       } else {
808         $this->got_uid= true;
809       }
811       /* Create new usertab object */
812       $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
813       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
814       $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
816       /* Take care about templates */
817       if ($s_action=="new_tpl"){
818         $this->is_template= TRUE;
819         $this->usertab->set_template_mode ();
820       } else {
821         $this->is_template= FALSE;
822       }
824       /* Use template if there are any of them */
825       if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
826         foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
827           $smarty->assign("$attr", $this->$attr);
828         }
829         if ($s_action=="create_user_from_tpl"){
830           $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
831         } else {
832           $smarty->assign("template", "none");
833         }
834         $smarty->assign("edit_uid", "");
835         return($smarty->fetch(get_template_path('template.tpl', TRUE)));
836       }
837     }
839     /********************
840       Template selected continue edit
841      ********************/
843     /* Continue template editing */
844     if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
846       $this->sn             = $_POST['sn'];
847       $this->givenName      = $_POST['givenName'];
849       /* Check for requred values */
850       $message= array();
851       if ($this->sn == "") {
852         $message[]= msgPool::required(_("Name"));
853       }
854       if ($this->givenName == "") {
855         $message[]= msgPool::required(_("Given name"));
856       }
858       /* Check if dn is used */
859       $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
860       $ldap= $this->config->get_ldap_link();
861       $ldap->cd ($dn);
862       $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
863       if ($ldap->count () != 0){
864         msgPool::duplicated(_("Name"));
865       }
867       /* Show error message / continue editing */
868       if (count($message) > 0){
869         msg_dialog::displayChecks($message);
870       } else {
871         $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
872         if (isset($this->config->current['IDGEN']) &&
873             $this->config->current['IDGEN'] != ""){
874           $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
875           if (count($uids)){
876             $smarty->assign("edit_uid", "false");
877             $smarty->assign("uids", $uids);
878             $this->uid= current($uids);
879           }
880         } else {
881           $smarty->assign("edit_uid", "");
882           $this->uid= "";
883         }
884         $this->got_uid= true;
885       }
887       foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
888         $smarty->assign("$attr", $this->$attr);
889       }
890       if (isset($_POST['template'])){
891         $smarty->assign("template", $_POST['template']);
892       }
893       return($smarty->fetch(get_template_path('template.tpl', TRUE)));
894     }
896     /********************
897       No template selected continue edit
898      ********************/
900     /* No template. Ok. Lets fill data into the normal user dialog */
901     if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
902       foreach(array("sn", "givenName", "uid") as $attr){
903         if (isset($_POST[$attr])){
904           $this->usertab->by_object['user']->$attr= $_POST[$attr];
905         }
906       }
907     }
910     /********************
911       Template selected continue edit
912      ********************/
914     /* Finish template preamble */
915     if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
917       /* Might not be filled if IDGEN is unset */
918       $this->sn                 = $_POST['sn'];
919       $this->givenName          = $_POST['givenName'];
921       /* Move user supplied data to sub plugins */
922       $this->uid                = $_POST['uid'];
923       $this->usertab->uid       = $this->uid;
924       $this->usertab->sn        = $this->sn;
925       $this->usertab->givenName = $this->givenName;
926       $template_dn              = $_POST['template'];
927       $this->usertab->adapt_from_template($template_dn);
928       $template_base            = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
929       $this->usertab->by_object['user']->base= $template_base;
930     }
931    
932  
933     /********************
934       If no template was selected set base
935      ********************/
937     if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
938       $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
939     }
942     /********************
943       Display subdialog 
944      ********************/
946     /* Show tab dialog if object is present */
947     if(isset($this->usertab->config)){
949       $display= $this->usertab->execute();
951       /* Don't show buttons if tab dialog requests this */
952       
953         $dia = FALSE;
954         if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
955           $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
956         }
958         if(!is_object($dia) && $dia != TRUE){
959           $display.= "<p style=\"text-align:right\">\n";
960           $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
961           $display.= "&nbsp;\n";
962           if ($this->dn != "new"){
963             $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
964             $display.= "&nbsp;\n";
965           }
966           $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
967           $display.= "</p>";
968         }
969       return ($display);
970     }
971     
972     /* Check if there is a snapshot dialog open */
973     $base = $this->DivListUsers->selectedBase;
974     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
975       return($str);
976     }
977   
978     /* Return rendered main page */
979         /* Display dialog with system list */
980     $this->DivListUsers->parent = $this;
981     $this->DivListUsers->execute();
983     /* Add departments if subsearch is disabled */
984     if(!$this->DivListUsers->SubSearch){
985       $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
986     }
987     $this->reload();
988     $this->DivListUsers->setEntries($this->list);
989     return($this->DivListUsers->Draw());
990   }
993   /* Return departments, that will be included within snapshot detection */
994   function get_used_snapshot_bases()
995   {
996     return(array(get_people_ou().$this->DivListUsers->selectedBase));
997   }  
1000   function reload()
1001   {
1002     /* Set base for all searches */
1003     $base= $this->DivListUsers->selectedBase;
1004     $this->list =array();
1006     /* Get filter configuration */
1007     $Regex                = $this->DivListUsers->Regex;
1008     $SubSearch            = $this->DivListUsers->SubSearch;
1009     $ShowTemplates        = $this->DivListUsers->ShowTemplates;
1010     $ShowFunctionalUsers  = $this->DivListUsers->ShowFunctionalUsers;
1011     $ShowUnixUsers        = $this->DivListUsers->ShowUnixUsers;
1012     $ShowMailUsers        = $this->DivListUsers->ShowMailUsers;
1013     $ShowSambaUsers       = $this->DivListUsers->ShowSambaUsers;
1014     $ShowProxyUsers       = $this->DivListUsers->ShowProxyUsers;
1016     /* Setup filter depending on selection */
1017     $filter="";
1018     if ($this->config->current['SAMBAVERSION'] == 3){
1019       $samba= "sambaSamAccount";
1020     } else {
1021       $samba= "sambaAccount";
1022     }
1024     if ($ShowFunctionalUsers){
1025       $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
1026                 "(objectClass=gosaMailAccount)(objectClass=$samba)".
1027                 "(objectClass=gosaProxyAccount))))";
1028     }
1029     if ($ShowUnixUsers){
1030       $filter.= "(objectClass=posixAccount)";
1031     }
1032     if ($ShowMailUsers){
1033       $filter.= "(objectClass=gosaMailAccount)";
1034     }
1035     if ($ShowSambaUsers){
1036       $filter.= "(objectClass=$samba)";
1037     }
1038     if ($ShowProxyUsers){
1039       $filter.= "(objectClass=gosaProxyAccount)";
1040     }
1041     if ($ShowTemplates){
1042       $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1043     } else {
1044       $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1045     }
1046     $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1048     /* Generate userlist */
1049     $ldap= $this->config->get_ldap_link(TRUE);
1051     if ($SubSearch){
1052       $ListTemp =  get_sub_list($filter, "users", get_people_ou(),$base,
1053                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1054     } else {
1055       $base= get_people_ou().$base;
1056       $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base, 
1057                             array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1058     }
1059     $SortTemp = array();
1060     $List = array();
1061     foreach($ListTemp as $Key => $Entry){
1063       /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1064        * Else winstations will be listed too, if you use the subtree flag. 
1065        */
1066       if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1067         continue;
1068       }else{
1070         // Generate caption for rows
1071         if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1072           $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1073         } else {
1074           $display= "[".$Entry["uid"][0]."]";
1075         }
1077         $display = strtolower($display);
1078         $List[$display] = $Entry;
1079         $SortTemp[$display] = $display;
1080       }
1081     }
1082     natcasesort($SortTemp);
1083     reset($SortTemp);
1085     $this->list = array();
1086     foreach($SortTemp as $Key){
1087       $this->list[] = $List[$Key];
1088     }
1089   }
1092   function remove_lock()
1093   {
1094     /* Remove user lock if a DN is marked as "currently edited" */
1095     if (isset($this->usertab->dn)){
1096       del_lock ($this->usertab->dn);
1097     }
1098     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
1099       del_lock($this->dn);
1100     }
1101     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
1102       del_lock($this->dns);
1103     }
1104   }
1107   function copyPasteHandling_from_queue($s_action,$s_entry)
1108   {
1109     /* Check if Copy & Paste is disabled */
1110     if(!is_object($this->CopyPasteHandler)){
1111       return("");
1112     }
1113     
1114     $ui = get_userinfo();
1115   
1116     /* Add a single entry to queue */
1117     if($s_action == "cut" || $s_action == "copy"){
1119       /* Cleanup object queue */
1120       $this->CopyPasteHandler->cleanup_queue();
1121       $dn = $this->list[$s_entry]['dn'];
1122       if($s_action == "copy" && preg_match("/r/",$ui->has_complete_category_acls($dn,"users"))){
1123         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1124       }
1125       if($s_action == "cut" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"users"))){
1126         $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1127       }
1128     }
1130     /* Add entries to queue */
1131     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1133       /* Cleanup object queue */
1134       $this->CopyPasteHandler->cleanup_queue();
1136       /* Add new entries to CP queue */
1137       foreach($this->list_get_selected_items() as $id){
1138         $dn = $this->list[$id]['dn'];
1140         if($s_action == "copy_multiple" && preg_match("/r/",$ui->has_complete_category_acls($dn,"users"))){
1141           $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1142         }
1143         if($s_action == "cut_multiple" && preg_match("/(r.*d|d.*r)/",$ui->has_complete_category_acls($dn,"users"))){
1144           $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1145         }
1146       }
1147     }
1148     
1149     /* Start pasting entries */
1150     if($s_action == "editPaste"){
1151       $this->start_pasting_copied_objects = TRUE;
1152     }
1154     /* Return C&P dialog */ 
1155     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1156    
1157       /* Get dialog */
1158       $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase); 
1159       $data = $this->CopyPasteHandler->execute();
1161       /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1162       if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1163         $s_entry = $this->CopyPasteHandler->last_entry();
1164         $this->reload();
1165         foreach($this->list as $key => $entry){
1166           if($entry['dn'] == $s_entry){
1167             $this->CPPasswordChange = $key;
1168           }
1169         }
1170       }
1172       /* Return dialog data */
1173       if(!empty($data) && $this->CPPasswordChange == ""){
1174         return($data);
1175       }
1176     }
1178     /* Automatically disable status for pasting */ 
1179     if(!$this->CopyPasteHandler->entries_queued()){
1180       $this->start_pasting_copied_objects = FALSE;
1181     }
1182     return("");
1183   }
1186   function save_object()
1187   {
1188     /* Handle divlist filter && department selection*/
1189     if(!is_object($this->usertab)){
1190       $this->DivListUsers->save_object();
1191     }
1192     if(is_object($this->CopyPasteHandler)){
1193       $this->CopyPasteHandler->save_object();
1194     }
1195   }
1197     
1198   function list_get_selected_items()
1199   {
1200     $ids = array();
1201     foreach($_POST as $name => $value){
1202       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1203         $id   = preg_replace("/^item_selected_/","",$name);
1204         $ids[$id] = $id;
1205       }
1206     }
1207     return($ids);
1208   }
1209   
1211   /* A set of disabled and therefore overloaded functions. They are
1212      not needed in this class. */
1213   function remove_from_parent() { } 
1214   function check() { } 
1215   function save() { } 
1216   function adapt_from_template($dn, $skip= array()) { } 
1217   function password_change_needed() { } 
1219 } /* ... class userManagement */
1220 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1221 ?>