Code

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