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 $current_action ="";
42 var $msg_dialog= NULL;
44 function userManagement(&$config, $ui)
45 {
46 /* Save configuration for internal use */
47 $this->config= &$config;
48 $this->ui= &$ui;
50 /* Copy & Paste handler */
51 if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
52 $this->CopyPasteHandler= new CopyPasteHandler($this->config);
53 }
55 /* Creat dialog object */
56 $this->DivListUsers = new divListUsers($this->config,$this);
58 }
61 function execute()
62 {
63 /* Call parent execute */
64 plugin::execute();
66 /* LOCK MESSAGE Vars */
67 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/","/^item_selected/","/^remove_multiple_users/","/^multiple_edit/","/menu_action/"));
69 $smarty = get_smarty(); // Smarty instance
70 $s_action = ""; // Contains the action to be taken
71 $s_entry = ""; // The value for s_action
73 /* Edit entry button pressed? */
74 if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
75 $s_action= "edit";
76 $s_entry= validate($_GET['id']);
77 }
79 /* Test relevant POST values */
80 foreach($_POST as $key => $val){
82 /* Get every possible POST combination and set s_action/s_entry accordingly */
83 foreach(array("del" => "user_del",
84 "edit" => "user_edit",
85 "new" => "user_new",
86 "new_tpl" => "user_tplnew",
87 "del_multiple" => "^remove_multiple_users",
88 "create_user_from_tpl" => "userfrom_tpl",
89 "change_pw" => "user_chgpw",
90 "editPaste" => "editPaste",
91 "copy_multiple" => "multiple_copy_users",
92 "multiple_edit" => "multiple_edit",
93 "cut_multiple" => "multiple_cut_users",
94 "multiple_password_change" => "multiple_password_change",
95 "copy" => "^copy",
96 "toggle_lock_status" => "toggle_lock_status",
97 "cut" => "^cut") as $act => $name){
99 if (preg_match("/".$name.".*/", $key)){
100 $s_action= $act;
101 $s_entry= preg_replace("/".$name."_/i", "", $key);
102 break;
103 }
104 }
106 } /* ...Test POST */
108 /* Remove coordinate prefix from POST, required by some browsers */
109 $s_entry= preg_replace("/_.$/", "", $s_entry);
111 /* Seperate possibly encoded tab and entry, default to tab "user" */
112 if(preg_match("/.*-.*/", $s_entry)){
113 $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
114 $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
115 }else{
116 $s_tab= "user";
117 }
119 if(!$this->config->search($s_tab, 'class',array('tabs'))){
120 $s_tab = "user";
121 }
124 /* handle C&P from layers menu */
125 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
126 $s_action = "copy_multiple";
127 }
128 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
129 $s_action = "cut_multiple";
130 }
131 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
132 $s_action = "editPaste";
133 }
135 /* Create options */
136 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_new"){
137 $s_action = "new";
138 }
139 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_tplnew"){
140 $s_action = "new_tpl";
141 }
142 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "multiple_edit"){
143 $s_action = "multiple_edit";
144 }
146 /* handle remove from layers menu */
147 if(isset($_POST['menu_action']) && preg_match("/^multiple_password_change/",$_POST['menu_action'])){
148 $s_action = "multiple_password_change";
149 }
151 /* handle remove from layers menu */
152 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
153 $s_action = "del_multiple";
154 }
156 if(isset($_POST['menu_action']) && preg_match("/^event/",$_POST['menu_action'])){
157 $s_action = $_POST['menu_action'];
158 }
161 /********************
162 Create notification event
163 ********************/
165 if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
166 $ids = $this->list_get_selected_items();
167 $uids = array();
168 foreach($ids as $id){
169 $uids[] = $this->list[$id]['uid'][0];
170 }
171 if(count($uids)){
172 $events = DaemonEvent::get_event_types(USER_EVENT);
173 $event = preg_replace("/^event_/","",$s_action);
174 if(isset($events['BY_CLASS'][$event])){
175 $type = $events['BY_CLASS'][$event];
176 $this->usertab = new $type['CLASS_NAME']($this->config);
177 $this->usertab->add_targets($uids);
178 $this->usertab->set_type(TRIGGERED_EVENT);
179 }
180 }
181 }
183 /* Abort event dialog */
184 if(isset($_POST['abort_event_dialog'])){
185 $this->usertab = FALSE;
186 }
188 /* Save event */
189 if(isset($_POST['save_event_dialog'])){
190 $o_queue = new gosaSupportDaemon();
191 $o_queue->append($this->usertab);
192 if($o_queue->is_error()){
193 msg_dialog::display(_("Service infrastructure"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
194 }else{
195 $this->usertab = FALSE;
196 }
197 }
199 /* Display event */
200 if($this->usertab instanceof DaemonEvent){
201 $this->usertab->save_object();
202 return($this->usertab->execute());
203 }
206 /********************
207 Copy & Paste
208 ********************/
210 /* Display the copy & paste dialog, if it is currently open */
211 if($this->CPPasswordChange == ""){
212 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
213 if($ret){
214 return($ret);
215 }
216 }
219 /********************
220 Change password confirmed
221 ********************/
223 /* Perform password change */
224 if (isset($_POST['password_finish'])){
226 /* For security reasons, check if user is allowed to set password again */
227 $dn = $this->dn;
228 $acl = $this->ui->get_permissions($dn, "users/password");
229 $cacl= $this->ui->get_permissions($dn, "users/user");
231 if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
233 /* Check input and feed errors into 'message' */
234 $message= array();
236 /* Sanity checks... */
237 if ($_POST['new_password'] != $_POST['repeated_password']){
239 /* Matching passwords in new and repeated? */
240 $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
241 } else {
243 /* Empty password is not permitted by default. */
244 if ($_POST['new_password'] == ""){
245 msgPool::required(_("New password"));
246 }
247 }
249 /* Errors, or password change? */
250 if (count($message) != 0){
252 /* Show error message and continue editing */
253 msg_dialog::displayChecks($message);
254 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
255 }
257 $config= $this->config;
258 $ldap_ui= $this->config->get_ldap_link();
259 if(isset($this->usertab->dn)){
260 $ldap_ui->cat($this->usertab->dn,array("uid"));
261 $user = $ldap_ui->fetch();
262 }else{
263 $ldap_ui->cat($this->dn,array("uid"));
264 $user = $ldap_ui->fetch();
265 }
266 if((is_array($user))&&(isset($user['uid']))){
267 $username= $user['uid'][0];
268 }
270 /* Set password, perform required steps */
271 if ($this->usertab){
272 if ($this->usertab->password_change_needed()){
273 $obj= $this->usertab->by_object['user'];
274 change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
275 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
276 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
277 }
278 new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
279 unset($this->usertab);
280 $this->usertab= NULL;
281 }
282 } else {
283 change_password ($this->dn, $_POST['new_password']);
284 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
285 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
286 }
287 new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
288 }
289 } else {
291 /* Missing permissions, show message */
292 msg_dialog::display(_("Password change failed."),_("You are not allowed to set this users password!"),WARNING_DIALOG);
293 }
294 /* Clean session, delete lock */
295 del_lock ($this->dn);
296 unset ($this->usertab);
297 $this->usertab= NULL;
298 $this->lognames= array();;
299 $this->sn= "";
300 $this->givenName= "";
301 $this->uid= "";
302 session::un_set('objectinfo');
303 }
306 /********************
307 Change multiple passwords requested
308 ********************/
310 if($s_action == "multiple_password_change"){
311 $this->pwd_change_queue = $this->list_get_selected_items();
312 }
315 /********************
316 Change password requested
317 ********************/
319 /* Password change requested */
320 if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
322 /* Get users whose passwords should be changed. */
323 if(count($this->pwd_change_queue)){
324 $s_entry= array_pop($this->pwd_change_queue);
325 }
327 if(!empty($this->CPPasswordChange)){
328 $s_entry = $this->CPPasswordChange;
329 $this->CPPasswordChange = "";
330 }
332 /* Get 'dn' from posted 'uid' */
333 $this->dn= $this->list[trim($s_entry)]['dn'];
335 /* Load permissions for selected 'dn' and check if
336 we're allowed to remove this 'dn' */
337 if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
339 /* User is allowed to change passwords, save 'dn' and 'acl' for next
340 dialog. */
341 session::set('objectinfo',$this->dn);
342 return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
345 } else {
346 /* User is not allowed. Show message and cancel. */
347 msg_dialog::display(_("Password change"),_("You are not allowed to set this users password!"),WARNING_DIALOG);
348 }
349 }
353 /********************
354 Edit existing entry
355 ********************/
357 /* User wants to edit data? */
358 if (($s_action=="edit") && (!isset($this->usertab->config))){
360 /* Get 'dn' from posted 'uid', must be unique */
361 $this->dn= $this->list[trim($s_entry)]['dn'];
363 /* Check locking, save current plugin in 'back_plugin', so
364 the dialog knows where to return. */
365 if (($user= get_lock($this->dn)) != ""){
366 return(gen_locked_message ($user, $this->dn));
367 }
369 /* Lock the current entry, so everyone will get the
370 above dialog */
371 add_lock ($this->dn, $this->ui->dn);
373 /* Register usertab to trigger edit dialog */
374 $this->usertab= new usertabs($this->config,
375 $this->config->data['TABS']['USERTABS'], $this->dn);
377 /* Switch tab, if it was requested by the user */
378 $this->usertab->current = $s_tab;
380 /* Set ACL and move DN to the headline */
381 $this->usertab->set_acl_base($this->dn);
382 session::set('objectinfo',$this->dn);
383 }
386 /********************
387 Edit multiple entries
388 ********************/
390 /* User wants to edit data? */
391 if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
393 $this->dn = array();
394 foreach($this->list_get_selected_items() as $id){
395 $this->dn[] = $this->list[$id]['dn'];;
396 }
397 $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
398 $this->dn,$this->DivListUsers->selectedBase,"user");
399 if ($tmp->entries_locked()){
400 return($tmp->display_lock_message());
401 }
402 $tmp->lock_entries($this->ui->dn);
403 if($tmp->multiple_available()){
404 $this->usertab = $tmp;
405 $this->usertab->set_active_tab($s_tab);
406 session::set('objectinfo',$this->usertab->get_object_info());
407 }
408 }
411 /********************
412 Edit canceled
413 ********************/
415 /* Reset all relevant data, if we get a _cancel request */
416 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
417 if (isset($this->usertab)){
418 del_lock ($this->usertab->dn);
419 unset ($this->usertab);
420 }
421 $this->usertab= NULL;
422 $this->lognames= array();;
423 $this->sn= "";
424 $this->givenName= "";
425 $this->uid= "";
426 session::un_set('objectinfo');
427 }
430 /********************
431 Delete MULTIPLE entries requested, display confirm dialog
432 ********************/
434 if ($s_action=="del_multiple"){
435 $ids = $this->list_get_selected_items();
436 $this->dns = array();
437 if(count($ids)){
439 foreach($ids as $id){
440 $dn = $this->list[$id]['dn'];
441 if (($user= get_lock($dn)) != ""){
442 return(gen_locked_message ($user, $dn));
443 }
444 $this->dns[$id] = $dn;
445 }
447 $dns_names = array();
448 foreach($this->dns as $dn){
449 $dns_names[] = @LDAP::fix($dn);
450 }
452 /* Lock the current entry, so nobody will edit it during deletion */
453 $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
454 $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
455 $this->current_action = $s_action;
456 }
457 }
460 /********************
461 Delete MULTIPLE entries confirmed
462 ********************/
464 /* Confirmation for deletion has been passed. Users should be deleted. */
465 if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
467 $this->current_action = "";
469 /* Remove user by user and check acls before removeing them */
470 foreach($this->dns as $key => $dn){
472 $acl = $this->ui->get_permissions($dn, "users/user");
473 if (preg_match('/d/', $acl)){
475 /* Delete request is permitted, perform LDAP action */
476 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
477 $this->usertab->set_acl_base();
478 $this->usertab->delete ();
479 unset ($this->usertab);
480 $this->usertab= NULL;
481 } else {
482 msg_dialog::display(_("User delete"),msgPool::permDelete($dn),WARNING_DIALOG);
483 if(isset($this->ui->uid)){
484 new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
485 }
486 }
487 /* Remove lock file after successfull deletion */
488 del_lock ($dn);
489 unset($this->dns[$key]);
490 }
491 }
494 /********************
495 Delete MULTIPLE entries Canceled
496 ********************/
498 /* Remove lock */
499 if(isset($_POST['delete_multiple_user_cancel'])){
500 foreach($this->dns as $key => $dn){
501 del_lock ($dn);
502 unset($this->dns[$key]);
503 }
504 }
507 /********************
508 Toggle lock status for user
509 ********************/
511 if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
513 /* Get entry check current status */
514 $val = $this->list[$s_entry];
515 $pwd = $val['userPassword'][0];
517 if(!preg_match("/^\{[^\}]/",$pwd)){
518 trigger_error("Can not deactivate user which is using clear password encryption.");
519 }else{
521 $locked = false;
522 if(preg_match("/^[^\}]*+\}!/",$pwd)){
523 $locked = true;
524 }
526 /* Create ldap array to update status */
527 $attrs = array("userPassword" => $pwd);
528 if($locked){
529 $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
530 }else{
531 $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
532 }
534 /* Write new status back to ldap */
535 $ldap = $this->config->get_ldap_link();
536 $ldap->cd($val['dn']);
537 $ldap->modify($attrs);
538 if (!$ldap->success()){
539 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
540 }
541 }
542 }
545 /********************
546 Delete entry requested, display confirm dialog
547 ********************/
549 /* Remove user was requested */
550 if ($s_action=="del"){
552 /* Get 'dn' from posted 'uid' */
553 $this->dn= $this->list[trim($s_entry)]['dn'];
555 /* Load permissions for selected 'dn' and check if
556 we're allowed to remove this 'dn' */
558 /* Check locking, save current plugin in 'back_plugin', so
559 the dialog knows where to return. */
560 if (($user= get_lock($this->dn)) != ""){
561 return(gen_locked_message ($user, $this->dn));
562 }
564 $this->msg_dialog = new msg_dialog( _("Delete user"),msgPool::deleteInfo(@LDAP::fix($this->dn),_("user")),CONFIRM_DIALOG);
565 $this->current_action = $s_action;
566 }
569 /********************
570 Delete entry confirmed
571 ********************/
573 /* Confirmation for deletion has been passed. User should be deleted. */
574 if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
576 $this->current_action = "";
578 /* Some nice guy may send this as POST, so we've to check
579 for the permissions again. */
581 $acl = $this->ui->get_permissions($this->dn, "users/user");
583 if (preg_match('/d/', $acl)){
585 /* Delete request is permitted, perform LDAP action */
586 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
587 $this->usertab->set_acl_base();
588 $this->usertab->delete ();
589 unset ($this->usertab);
590 $this->usertab= NULL;
591 msg_dialog::display(_("User deleted"),_("User successfully removed."),INFO_DIALOG);
592 } else {
594 /* Normally this shouldn't be reached, send some extra
595 logs to notify the administrator */
596 msg_dialog::display(_("User delete"),msgPool::permDelete(),WARNING_DIALOG);
598 if(isset($this->ui->uid)){
599 new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
600 }
601 }
603 /* Remove lock file after successfull deletion */
604 del_lock ($this->dn);
605 }
608 /********************
609 Delete entry Canceled
610 ********************/
612 /* Delete user canceled? */
613 if (isset($_POST['delete_cancel'])){
614 del_lock ($this->dn);
615 }
618 /********************
619 Edit entry finished (Save)
620 ********************/
622 /* Finish user edit is triggered by the tabulator dialog, so
623 the user wants to save edited data. Check and save at this
624 point. */
625 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
627 /* Check tabs, will feed message array */
628 $this->usertab->last= $this->usertab->current;
629 $this->usertab->save_object();
630 $message= $this->usertab->check();
632 /* Save, or display error message? */
633 if (count($message) == 0){
635 /* No errors. Go ahead and prepare to ask for a password
636 in case we're creating a new user. 'dn' will be 'new'
637 in this case. It is set to the correct value later. */
638 if ($this->dn == "new"){
639 $set_pass= 1;
640 } else {
641 $set_pass= 0;
642 }
644 /* Save user data to ldap */
645 if($this->usertab->save() == 1){
646 return;
647 }
649 if (!isset($_POST['edit_apply'])){
650 /* User has been saved successfully, remove lock from LDAP. */
651 if ($this->dn != "new"){
652 del_lock ($this->dn);
653 }
655 /* In case of new users, ask for a password, skip this for templates */
656 if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
657 $this->dn = $this->usertab->dn;
658 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
659 }
661 unset ($this->usertab);
662 $this->usertab= NULL;
663 session::un_set('objectinfo');
664 }
665 } else {
666 /* Ok. There seem to be errors regarding to the tab data,
667 show message and continue as usual. */
668 msg_dialog::displayChecks($message);
669 }
670 }
673 /********************
674 We want to create a new user, so fetch all available user templates
675 ********************/
677 /* Generate template list */
678 if (($s_action=="new")||($s_action=="create_user_from_tpl")){
680 $this->templates= array();
681 $ldap= $this->config->get_ldap_link();
683 /* Create list of templates */
684 foreach ($this->config->departments as $key => $value){
686 /* Get acls from different ou's */
687 $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user") ;
689 /* If creation of a new user is allowed, append this template */
690 if (preg_match("/c/",$acl)){
692 /* Search all templates from the current dn */
693 $ldap->cd (get_people_ou().$value);
694 $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
696 /* Append */
697 if ($ldap->count() != 0){
698 while ($attrs= $ldap->fetch()){
699 $this->templates[$ldap->getDN()]=
700 $attrs['uid'][0]." - ".@LDAP::fix($key);
701 }
702 $this->templates['none']= _("none");
703 }
704 }
705 }
707 /* Sort templates */
708 natcasesort ($this->templates);
709 reset ($this->templates);
710 }
713 /********************
714 Create a new user,template, user from template
715 ********************/
717 /* Check selected options for template */
718 if (isset($_POST['template_continue'])){
719 $message = array();
720 if(!isset($_POST['template']) || (empty($_POST['template']))){
721 $message[]= msgPool::invalid(_("Template"));
722 }
723 if(!isset($_POST['sn']) || (empty($_POST['sn']))){
724 $message[]= msgPool::required(_("Name"));
725 }
726 if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
727 $message[]= msgPool::required(_("Given name"));
728 }
730 /* Show error message / continue editing */
731 if (count($message) > 0){
732 msg_dialog::displayChecks($message);
734 foreach(array("sn", "givenName", "uid", "template") as $attr){
735 if(isset($_POST[$attr])){
736 $smarty->assign("$attr", $_POST[$attr]);
737 }else{
738 $smarty->assign("$attr", "");
739 }
740 }
741 $smarty->assign("templates",$this->templates);
742 $smarty->assign("got_uid",$this->got_uid);
743 $smarty->assign("edit_uid",false);
744 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
746 }
747 }
749 /* New user/template request */
750 if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
751 /* By default we set 'dn' to 'new', all relevant plugins will
752 react on this. */
753 $this->dn= "new";
755 if (isset($this->config->current['IDGEN'])){
756 $this->got_uid= false;
757 } else {
758 $this->got_uid= true;
759 }
761 /* Create new usertab object */
762 $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
763 $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
764 $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
766 /* Take care about templates */
767 if ($s_action=="new_tpl"){
768 $this->is_template= TRUE;
769 $this->usertab->set_template_mode ();
770 } else {
771 $this->is_template= FALSE;
772 }
774 /* Use template if there are any of them */
775 if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
776 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
777 $smarty->assign("$attr", $this->$attr);
778 }
779 if ($s_action=="create_user_from_tpl"){
780 $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
781 } else {
782 $smarty->assign("template", "none");
783 }
784 $smarty->assign("edit_uid", "");
785 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
786 }
787 }
789 /********************
790 Template selected continue edit
791 ********************/
793 /* Continue template editing */
794 if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
796 $this->sn = $_POST['sn'];
797 $this->givenName = $_POST['givenName'];
799 /* Check for requred values */
800 $message= array();
801 if ($this->sn == "") {
802 $message[]= msgPool::required(_("Name"));
803 }
804 if ($this->givenName == "") {
805 $message[]= msgPool::required(_("Given name"));
806 }
808 /* Check if dn is used */
809 $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
810 $ldap= $this->config->get_ldap_link();
811 $ldap->cd ($dn);
812 $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
813 if ($ldap->count () != 0){
814 msgPool::duplicated(_("Name"));
815 }
817 /* Show error message / continue editing */
818 if (count($message) > 0){
819 msg_dialog::displayChecks($message);
820 } else {
821 $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
822 if (isset($this->config->current['IDGEN']) &&
823 $this->config->current['IDGEN'] != ""){
824 $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
825 if (count($uids)){
826 $smarty->assign("edit_uid", "false");
827 $smarty->assign("uids", $uids);
828 $this->uid= current($uids);
829 }
830 } else {
831 $smarty->assign("edit_uid", "");
832 $this->uid= "";
833 }
834 $this->got_uid= true;
835 }
837 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
838 $smarty->assign("$attr", $this->$attr);
839 }
840 if (isset($_POST['template'])){
841 $smarty->assign("template", $_POST['template']);
842 }
843 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
844 }
846 /********************
847 No template selected continue edit
848 ********************/
850 /* No template. Ok. Lets fill data into the normal user dialog */
851 if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
852 foreach(array("sn", "givenName", "uid") as $attr){
853 if (isset($_POST[$attr])){
854 $this->usertab->by_object['user']->$attr= $_POST[$attr];
855 }
856 }
857 }
860 /********************
861 Template selected continue edit
862 ********************/
864 /* Finish template preamble */
865 if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
867 /* Might not be filled if IDGEN is unset */
868 $this->sn = $_POST['sn'];
869 $this->givenName = $_POST['givenName'];
871 /* Move user supplied data to sub plugins */
872 $this->uid = $_POST['uid'];
873 $this->usertab->uid = $this->uid;
874 $this->usertab->sn = $this->sn;
875 $this->usertab->givenName = $this->givenName;
876 $template_dn = $_POST['template'];
877 $this->usertab->adapt_from_template($template_dn);
878 $template_base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
879 $this->usertab->by_object['user']->base= $template_base;
880 }
883 /********************
884 If no template was selected set base
885 ********************/
887 if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
888 $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
889 }
892 /********************
893 Display subdialog
894 ********************/
896 /* Show tab dialog if object is present */
897 if(isset($this->usertab->config)){
899 $display= $this->usertab->execute();
901 /* Don't show buttons if tab dialog requests this */
903 $dia = FALSE;
904 if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
905 $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
906 }
908 if(!is_object($dia) && $dia != TRUE){
909 $display.= "<p style=\"text-align:right\">\n";
910 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
911 $display.= " \n";
912 if ($this->dn != "new"){
913 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
914 $display.= " \n";
915 }
916 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
917 $display.= "</p>";
918 }
919 return ($display);
920 }
922 /* Check if there is a snapshot dialog open */
923 $base = $this->DivListUsers->selectedBase;
924 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
925 return($str);
926 }
928 /* Return rendered main page */
929 /* Display dialog with system list */
930 $this->DivListUsers->parent = $this;
931 $this->DivListUsers->execute();
933 /* Add departments if subsearch is disabled */
934 if(!$this->DivListUsers->SubSearch){
935 $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
936 }
937 $this->reload();
938 $this->DivListUsers->setEntries($this->list);
939 return($this->DivListUsers->Draw());
940 }
943 /* Return departments, that will be included within snapshot detection */
944 function get_used_snapshot_bases()
945 {
946 return(array(get_people_ou().$this->DivListUsers->selectedBase));
947 }
950 function reload()
951 {
952 /* Set base for all searches */
953 $base= $this->DivListUsers->selectedBase;
954 $this->list =array();
956 /* Get filter configuration */
957 $Regex = $this->DivListUsers->Regex;
958 $SubSearch = $this->DivListUsers->SubSearch;
959 $ShowTemplates = $this->DivListUsers->ShowTemplates;
960 $ShowFunctionalUsers = $this->DivListUsers->ShowFunctionalUsers;
961 $ShowUnixUsers = $this->DivListUsers->ShowUnixUsers;
962 $ShowMailUsers = $this->DivListUsers->ShowMailUsers;
963 $ShowSambaUsers = $this->DivListUsers->ShowSambaUsers;
964 $ShowProxyUsers = $this->DivListUsers->ShowProxyUsers;
966 /* Setup filter depending on selection */
967 $filter="";
968 if ($this->config->current['SAMBAVERSION'] == 3){
969 $samba= "sambaSamAccount";
970 } else {
971 $samba= "sambaAccount";
972 }
974 if ($ShowFunctionalUsers){
975 $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
976 "(objectClass=gosaMailAccount)(objectClass=$samba)".
977 "(objectClass=gosaProxyAccount))))";
978 }
979 if ($ShowUnixUsers){
980 $filter.= "(objectClass=posixAccount)";
981 }
982 if ($ShowMailUsers){
983 $filter.= "(objectClass=gosaMailAccount)";
984 }
985 if ($ShowSambaUsers){
986 $filter.= "(objectClass=$samba)";
987 }
988 if ($ShowProxyUsers){
989 $filter.= "(objectClass=gosaProxyAccount)";
990 }
991 if ($ShowTemplates){
992 $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
993 } else {
994 $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
995 }
996 $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
998 /* Generate userlist */
999 $ldap= $this->config->get_ldap_link(TRUE);
1001 if ($SubSearch){
1002 $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base,
1003 array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1004 } else {
1005 $base= get_people_ou().$base;
1006 $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base,
1007 array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1008 }
1009 $SortTemp = array();
1010 $List = array();
1011 foreach($ListTemp as $Key => $Entry){
1013 /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1014 * Else winstations will be listed too, if you use the subtree flag.
1015 */
1016 if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1017 continue;
1018 }else{
1020 // Generate caption for rows
1021 if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1022 $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1023 } else {
1024 $display= "[".$Entry["uid"][0]."]";
1025 }
1027 $display = strtolower($display);
1028 $List[$display] = $Entry;
1029 $SortTemp[$display] = $display;
1030 }
1031 }
1032 natcasesort($SortTemp);
1033 reset($SortTemp);
1035 $this->list = array();
1036 foreach($SortTemp as $Key){
1037 $this->list[] = $List[$Key];
1038 }
1039 }
1041 function remove_lock()
1042 {
1043 /* Remove user lock if a DN is marked as "currently edited" */
1044 if (isset($this->usertab->dn)){
1045 del_lock ($this->usertab->dn);
1046 }
1047 }
1050 function copyPasteHandling_from_queue($s_action,$s_entry)
1051 {
1052 /* Check if Copy & Paste is disabled */
1053 if(!is_object($this->CopyPasteHandler)){
1054 return("");
1055 }
1057 /* Add a single entry to queue */
1058 if($s_action == "cut" || $s_action == "copy"){
1059 /* Cleanup object queue */
1060 $this->CopyPasteHandler->cleanup_queue();
1061 $dn = $this->list[$s_entry]['dn'];
1062 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1063 }
1065 /* Add entries to queue */
1066 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1068 /* Cleanup object queue */
1069 $this->CopyPasteHandler->cleanup_queue();
1071 /* Add new entries to CP queue */
1072 foreach($this->list_get_selected_items() as $id){
1073 $dn = $this->list[$id]['dn'];
1075 if($s_action == "copy_multiple"){
1076 $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1077 }
1078 if($s_action == "cut_multiple"){
1079 $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1080 }
1081 }
1082 }
1084 /* Start pasting entries */
1085 if($s_action == "editPaste"){
1086 $this->start_pasting_copied_objects = TRUE;
1087 }
1089 /* Return C&P dialog */
1090 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1092 /* Get dialog */
1093 $data = $this->CopyPasteHandler->execute();
1094 $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase);
1096 /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1097 if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1098 $s_entry = $this->CopyPasteHandler->last_entry();
1099 $this->reload();
1100 foreach($this->list as $key => $entry){
1101 if($entry['dn'] == $s_entry){
1102 $this->CPPasswordChange = $key;
1103 }
1104 }
1105 }
1107 /* Return dialog data */
1108 if(!empty($data) && $this->CPPasswordChange == ""){
1109 return($data);
1110 }
1111 }
1113 /* Automatically disable status for pasting */
1114 if(!$this->CopyPasteHandler->entries_queued()){
1115 $this->start_pasting_copied_objects = FALSE;
1116 }
1117 return("");
1118 }
1121 function save_object()
1122 {
1123 /* Handle divlist filter && department selection*/
1124 if(!is_object($this->usertab)){
1125 $this->DivListUsers->save_object();
1126 }
1127 }
1130 function list_get_selected_items()
1131 {
1132 $ids = array();
1133 foreach($_POST as $name => $value){
1134 if(preg_match("/^item_selected_[0-9]*$/",$name)){
1135 $id = preg_replace("/^item_selected_/","",$name);
1136 $ids[$id] = $id;
1137 }
1138 }
1139 return($ids);
1140 }
1143 /* A set of disabled and therefore overloaded functions. They are
1144 not needed in this class. */
1145 function remove_from_parent() { }
1146 function check() { }
1147 function save() { }
1148 function adapt_from_template($dn) { }
1149 function password_change_needed() { }
1151 } /* ... class userManagement */
1152 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1153 ?>