04a17bc485c896e04848d0dd389d6663d682e18b
1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003-2006 - Cajus Pollmeier <pollmeier@gonicus.de>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
21 class userManagement extends plugin
22 {
23 /* Plugin definitions */
24 var $plHeadline= "Users";
25 var $plDescription= "Manage users";
27 /* Dialog attributes */
28 var $usertab = NULL;
29 var $ui = NULL;
30 var $templates = array();
31 var $got_uid = false;
32 var $CopyPasteHandler = NULL;
33 var $CPPasswordChange = ""; // Contains the entry id which should get a new password
34 var $DivListUsers;
36 var $pwd_change_queue = array();
38 var $start_pasting_copied_objects = FALSE;
39 var $current_action ="";
40 var $msg_dialog= NULL;
42 function userManagement(&$config, $ui)
43 {
44 /* Save configuration for internal use */
45 $this->config= &$config;
46 $this->ui= &$ui;
48 /* Copy & Paste handler */
49 if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
50 $this->CopyPasteHandler= new CopyPasteHandler($this->config);
51 }
53 /* Creat dialog object */
54 $this->DivListUsers = new divListUsers($this->config,$this);
56 }
59 function execute()
60 {
61 /* Call parent execute */
62 plugin::execute();
64 /* LOCK MESSAGE Vars */
65 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^user_edit_/","/^user_del_/","/^item_selected/","/^remove_multiple_users/","/^multiple_edit/","/menu_action/"));
67 $smarty = get_smarty(); // Smarty instance
68 $s_action = ""; // Contains the action to be taken
69 $s_entry = ""; // The value for s_action
71 /* Edit entry button pressed? */
72 if( isset($_GET['act']) && $_GET['act'] == "edit_entry" ){
73 $s_action= "edit";
74 $s_entry= validate($_GET['id']);
75 }
77 /* Test relevant POST values */
78 foreach($_POST as $key => $val){
80 /* Get every possible POST combination and set s_action/s_entry accordingly */
81 foreach(array("del" => "user_del",
82 "edit" => "user_edit",
83 "new" => "user_new",
84 "new_tpl" => "user_tplnew",
85 "del_multiple" => "^remove_multiple_users",
86 "create_user_from_tpl" => "userfrom_tpl",
87 "change_pw" => "user_chgpw",
88 "editPaste" => "editPaste",
89 "copy_multiple" => "multiple_copy_users",
90 "multiple_edit" => "multiple_edit",
91 "cut_multiple" => "multiple_cut_users",
92 "multiple_password_change" => "multiple_password_change",
93 "copy" => "^copy",
94 "toggle_lock_status" => "toggle_lock_status",
95 "cut" => "^cut") as $act => $name){
97 if (preg_match("/".$name.".*/", $key)){
98 $s_action= $act;
99 $s_entry= preg_replace("/".$name."_/i", "", $key);
100 break;
101 }
102 }
104 } /* ...Test POST */
106 /* Remove coordinate prefix from POST, required by some browsers */
107 $s_entry= preg_replace("/_.$/", "", $s_entry);
109 /* Seperate possibly encoded tab and entry, default to tab "user" */
110 if(preg_match("/.*-.*/", $s_entry)){
111 $s_tab= preg_replace("/^[^-]*-/i", "" ,$s_entry);
112 $s_entry= preg_replace("/-[^-]*$/i", "", $s_entry);
113 }else{
114 $s_tab= "user";
115 }
117 if(!$this->config->search($s_tab, 'class',array('tabs'))){
118 $s_tab = "user";
119 }
122 /* handle C&P from layers menu */
123 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
124 $s_action = "copy_multiple";
125 }
126 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
127 $s_action = "cut_multiple";
128 }
129 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
130 $s_action = "editPaste";
131 }
133 /* Create options */
134 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_new"){
135 $s_action = "new";
136 }
137 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "user_tplnew"){
138 $s_action = "new_tpl";
139 }
140 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "multiple_edit"){
141 $s_action = "multiple_edit";
142 }
144 /* handle remove from layers menu */
145 if(isset($_POST['menu_action']) && preg_match("/^multiple_password_change/",$_POST['menu_action'])){
146 $s_action = "multiple_password_change";
147 }
149 /* handle remove from layers menu */
150 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
151 $s_action = "del_multiple";
152 }
154 if(isset($_POST['menu_action']) && preg_match("/^trigger_event_DaemonEvent_notify/",$_POST['menu_action'])){
155 $s_action = "notify";
156 }
158 /********************
159 Create notification event
160 ********************/
162 if($s_action == "notify"){
164 $ids = $this->list_get_selected_items();
165 $uids = array();
166 foreach($ids as $id){
167 $uids[] = $this->list[$id]['uid'][0];
168 }
169 if(count($uids)){
170 $this->usertab = new DaemonEvent_notify($this->config);
171 $this->usertab->add_targets($uids);
172 }
174 }
176 /* Abort event dialog */
177 if(isset($_POST['abort_event_dialog'])){
178 $this->usertab = FALSE;
179 }
181 /* Save event */
182 if(isset($_POST['save_event_dialog'])){
183 $o_queue = new gosaSupportDaemon();
184 $o_queue->append($this->usertab);
185 if($o_queue->is_error()){
186 msg_dialog::display(_("Daemon"),sprintf(_("Something went wrong while talking to the daemon: %s."),
187 $o_queue->get_error()),ERROR_DIALOG);
188 }else{
189 $this->usertab = FALSE;
190 }
191 }
193 /* Display event */
194 if($this->usertab instanceof DaemonEvent){
195 $this->usertab->save_object();
196 return($this->usertab->execute());
197 }
200 /********************
201 Copy & Paste
202 ********************/
204 /* Display the copy & paste dialog, if it is currently open */
205 if($this->CPPasswordChange == ""){
206 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
207 if($ret){
208 return($ret);
209 }
210 }
213 /********************
214 Change password confirmed
215 ********************/
217 /* Perform password change */
218 if (isset($_POST['password_finish'])){
220 /* For security reasons, check if user is allowed to set password again */
221 $dn = $this->dn;
222 $acl = $this->ui->get_permissions($dn, "users/password");
223 $cacl= $this->ui->get_permissions($dn, "users/user");
225 if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
227 /* Check input and feed errors into 'message' */
228 $message= array();
230 /* Sanity checks... */
231 if ($_POST['new_password'] != $_POST['repeated_password']){
233 /* Matching passwords in new and repeated? */
234 $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
235 } else {
237 /* Empty password is not permitted by default. */
238 if ($_POST['new_password'] == ""){
239 $message[]= _("The password you've entered as 'New password' is empty.");
240 }
241 }
243 /* Errors, or password change? */
244 if (count($message) != 0){
246 /* Show error message and continue editing */
247 show_errors ($message);
248 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
249 }
251 $config= $this->config;
252 $ldap_ui= $this->config->get_ldap_link();
253 if(isset($this->usertab->dn)){
254 $ldap_ui->cat($this->usertab->dn,array("uid"));
255 $user = $ldap_ui->fetch();
256 }else{
257 $ldap_ui->cat($this->dn,array("uid"));
258 $user = $ldap_ui->fetch();
259 }
260 if((is_array($user))&&(isset($user['uid']))){
261 $username= $user['uid'][0];
262 }
264 /* Set password, perform required steps */
265 if ($this->usertab){
266 if ($this->usertab->password_change_needed()){
267 $obj= $this->usertab->by_object['user'];
268 change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
269 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
270 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
271 }
272 new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
273 unset($this->usertab);
274 $this->usertab= NULL;
275 }
276 } else {
277 change_password ($this->dn, $_POST['new_password']);
278 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
279 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
280 }
281 new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
282 }
283 } else {
285 /* Missing permissions, show message */
286 msg_dialog::display(_("Password change failed."),_("You are not allowed to set this users password!"),WARNING_DIALOG);
287 }
288 /* Clean session, delete lock */
289 del_lock ($this->dn);
290 unset ($this->usertab);
291 $this->usertab= NULL;
292 $this->lognames= array();;
293 $this->sn= "";
294 $this->givenName= "";
295 $this->uid= "";
296 session::un_set('objectinfo');
297 }
300 /********************
301 Change multiple passwords requested
302 ********************/
304 if($s_action == "multiple_password_change"){
305 $this->pwd_change_queue = $this->list_get_selected_items();
306 }
309 /********************
310 Change password requested
311 ********************/
313 /* Password change requested */
314 if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
316 /* Get users whose passwords should be changed. */
317 if(count($this->pwd_change_queue)){
318 $s_entry= array_pop($this->pwd_change_queue);
319 }
321 if(!empty($this->CPPasswordChange)){
322 $s_entry = $this->CPPasswordChange;
323 $this->CPPasswordChange = "";
324 }
326 /* Get 'dn' from posted 'uid' */
327 $this->dn= $this->list[trim($s_entry)]['dn'];
329 /* Load permissions for selected 'dn' and check if
330 we're allowed to remove this 'dn' */
331 if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
333 /* User is allowed to change passwords, save 'dn' and 'acl' for next
334 dialog. */
335 session::set('objectinfo',$this->dn);
336 return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
339 } else {
340 /* User is not allowed. Show message and cancel. */
341 msg_dialog::display(_("Password change"),_("You are not allowed to set this users password!"),WARNING_DIALOG);
342 }
343 }
347 /********************
348 Edit existing entry
349 ********************/
351 /* User wants to edit data? */
352 if (($s_action=="edit") && (!isset($this->usertab->config))){
354 /* Get 'dn' from posted 'uid', must be unique */
355 $this->dn= $this->list[trim($s_entry)]['dn'];
357 /* Check locking, save current plugin in 'back_plugin', so
358 the dialog knows where to return. */
359 if (($user= get_lock($this->dn)) != ""){
360 return(gen_locked_message ($user, $this->dn));
361 }
363 /* Lock the current entry, so everyone will get the
364 above dialog */
365 add_lock ($this->dn, $this->ui->dn);
367 /* Register usertab to trigger edit dialog */
368 $this->usertab= new usertabs($this->config,
369 $this->config->data['TABS']['USERTABS'], $this->dn);
371 /* Switch tab, if it was requested by the user */
372 $this->usertab->current = $s_tab;
374 /* Set ACL and move DN to the headline */
375 $this->usertab->set_acl_base($this->dn);
376 session::set('objectinfo',$this->dn);
377 }
380 /********************
381 Edit multiple entries
382 ********************/
384 /* User wants to edit data? */
385 if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
387 $this->dn = array();
388 foreach($this->list_get_selected_items() as $id){
389 $this->dn[] = $this->list[$id]['dn'];;
390 }
391 $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
392 $this->dn,$this->DivListUsers->selectedBase,"user");
393 if ($tmp->entries_locked()){
394 return($tmp->display_lock_message());
395 }
396 $tmp->lock_entries($this->ui->dn);
397 if($tmp->multiple_available()){
398 $this->usertab = $tmp;
399 $this->usertab->set_active_tab($s_tab);
400 session::set('objectinfo',$this->usertab->get_object_info());
401 }
402 }
405 /********************
406 Edit canceled
407 ********************/
409 /* Reset all relevant data, if we get a _cancel request */
410 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
411 if (isset($this->usertab)){
412 del_lock ($this->usertab->dn);
413 unset ($this->usertab);
414 }
415 $this->usertab= NULL;
416 $this->lognames= array();;
417 $this->sn= "";
418 $this->givenName= "";
419 $this->uid= "";
420 session::un_set('objectinfo');
421 }
424 /********************
425 Delete MULTIPLE entries requested, display confirm dialog
426 ********************/
428 if ($s_action=="del_multiple"){
429 $ids = $this->list_get_selected_items();
430 $this->dns = array();
431 if(count($ids)){
433 foreach($ids as $id){
434 $dn = $this->list[$id]['dn'];
435 if (($user= get_lock($dn)) != ""){
436 return(gen_locked_message ($user, $dn));
437 }
438 $this->dns[$id] = $dn;
439 }
441 $dns_names = "<br><pre>";
442 foreach($this->dns as $dn){
443 $dns_names .= $dn."\n";
444 }
445 $dns_names .="</pre>";
447 /* Lock the current entry, so nobody will edit it during deletion */
448 if (count($this->dns) == 1){
449 $info = sprintf(_("You're about to delete the following entry: %s"), @LDAP::fix($dns_names));
450 } else {
451 $info = sprintf(_("You're about to delete the following entries: %s"), @LDAP::fix($dns_names));
452 }
453 $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
454 $this->current_action = $s_action;
455 }
456 }
459 /********************
460 Delete MULTIPLE entries confirmed
461 ********************/
463 /* Confirmation for deletion has been passed. Users should be deleted. */
464 if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
466 $this->current_action = "";
468 /* Remove user by user and check acls before removeing them */
469 foreach($this->dns as $key => $dn){
471 $acl = $this->ui->get_permissions($dn, "users/user");
472 if (preg_match('/d/', $acl)){
474 /* Delete request is permitted, perform LDAP action */
475 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
476 $this->usertab->set_acl_base();
477 $this->usertab->delete ();
478 unset ($this->usertab);
479 $this->usertab= NULL;
480 } else {
481 msg_dialog::display(_("User delete"),sprintf(_("You are not allowed to delete the user '%s'!"),$dn),WARNING_DIALOG);
482 if(isset($this->ui->uid)){
483 new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
484 }
485 }
486 /* Remove lock file after successfull deletion */
487 del_lock ($dn);
488 unset($this->dns[$key]);
489 }
490 }
493 /********************
494 Delete MULTIPLE entries Canceled
495 ********************/
497 /* Remove lock */
498 if(isset($_POST['delete_multiple_user_cancel'])){
499 foreach($this->dns as $key => $dn){
500 del_lock ($dn);
501 unset($this->dns[$key]);
502 }
503 }
506 /********************
507 Toggle lock status for user
508 ********************/
510 if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
512 /* Get entry check current status */
513 $val = $this->list[$s_entry];
514 $pwd = $val['userPassword'][0];
516 if(!preg_match("/^\{[^\}]/",$pwd)){
517 trigger_error("Can not deactivate user which is using clear password encryption.");
518 }else{
520 $locked = false;
521 if(preg_match("/^[^\}]*+\}!/",$pwd)){
522 $locked = true;
523 }
525 /* Create ldap array to update status */
526 $attrs = array("userPassword" => $pwd);
527 if($locked){
528 $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
529 }else{
530 $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
531 }
533 /* Write new status back to ldap */
534 $ldap = $this->config->get_ldap_link();
535 $ldap->cd($val['dn']);
536 $ldap->modify($attrs);
537 if($locked){
538 show_ldap_error($ldap->get_error(),_("Could not set user status from locked to unlocked."));
539 }else{
540 show_ldap_error($ldap->get_error(),_("Could not set user status from unlocked to locked."));
541 }
542 }
543 }
546 /********************
547 Delete entry requested, display confirm dialog
548 ********************/
550 /* Remove user was requested */
551 if ($s_action=="del"){
553 /* Get 'dn' from posted 'uid' */
554 $this->dn= $this->list[trim($s_entry)]['dn'];
556 /* Load permissions for selected 'dn' and check if
557 we're allowed to remove this 'dn' */
559 /* Check locking, save current plugin in 'back_plugin', so
560 the dialog knows where to return. */
561 if (($user= get_lock($this->dn)) != ""){
562 return(gen_locked_message ($user, $this->dn));
563 }
566 /* Lock the current entry, so nobody will edit it during deletion */
567 # add_lock ($this->dn, $this->ui->dn);
568 # $smarty->assign("info", sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)));
569 # $smarty->assign("multiple", false);
570 # return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
571 $this->msg_dialog = new msg_dialog( _("Delete user"),sprintf(_("You're about to delete the user %s."), @LDAP::fix($this->dn)),CONFIRM_DIALOG);
572 $this->current_action = $s_action;
573 }
576 /********************
577 Delete entry confirmed
578 ********************/
580 /* Confirmation for deletion has been passed. User should be deleted. */
581 if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
583 $this->current_action = "";
585 /* Some nice guy may send this as POST, so we've to check
586 for the permissions again. */
588 $acl = $this->ui->get_permissions($this->dn, "users/user");
590 if (preg_match('/d/', $acl)){
592 /* Delete request is permitted, perform LDAP action */
593 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
594 $this->usertab->set_acl_base();
595 $this->usertab->delete ();
596 unset ($this->usertab);
597 $this->usertab= NULL;
598 msg_dialog::display(_("User deleted"),_("User successfully removed."),INFO_DIALOG);
599 } else {
601 /* Normally this shouldn't be reached, send some extra
602 logs to notify the administrator */
603 msg_dialog::display(_("User delete"),_("You are not allowed to delete this user!"),WARNING_DIALOG);
605 if(isset($this->ui->uid)){
606 new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
607 }
608 }
610 /* Remove lock file after successfull deletion */
611 del_lock ($this->dn);
612 }
615 /********************
616 Delete entry Canceled
617 ********************/
619 /* Delete user canceled? */
620 if (isset($_POST['delete_cancel'])){
621 del_lock ($this->dn);
622 }
625 /********************
626 Edit entry finished (Save)
627 ********************/
629 /* Finish user edit is triggered by the tabulator dialog, so
630 the user wants to save edited data. Check and save at this
631 point. */
632 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
634 /* Check tabs, will feed message array */
635 $this->usertab->last= $this->usertab->current;
636 $this->usertab->save_object();
637 $message= $this->usertab->check();
639 /* Save, or display error message? */
640 if (count($message) == 0){
642 /* No errors. Go ahead and prepare to ask for a password
643 in case we're creating a new user. 'dn' will be 'new'
644 in this case. It is set to the correct value later. */
645 if ($this->dn == "new"){
646 $set_pass= 1;
647 } else {
648 $set_pass= 0;
649 }
651 /* Save user data to ldap */
652 if($this->usertab->save() == 1){
653 return;
654 }
656 if (!isset($_POST['edit_apply'])){
657 /* User has been saved successfully, remove lock from LDAP. */
658 if ($this->dn != "new"){
659 del_lock ($this->dn);
660 }
662 /* In case of new users, ask for a password, skip this for templates */
663 if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
664 $this->dn = $this->usertab->dn;
665 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
666 }
668 unset ($this->usertab);
669 $this->usertab= NULL;
670 session::un_set('objectinfo');
671 }
672 } else {
673 /* Ok. There seem to be errors regarding to the tab data,
674 show message and continue as usual. */
675 show_errors($message);
676 }
677 }
680 /********************
681 We want to create a new user, so fetch all available user templates
682 ********************/
684 /* Generate template list */
685 if (($s_action=="new")||($s_action=="create_user_from_tpl")){
687 $this->templates= array();
688 $ldap= $this->config->get_ldap_link();
690 /* Create list of templates */
691 foreach ($this->config->departments as $key => $value){
693 /* Get acls from different ou's */
694 $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user") ;
696 /* If creation of a new user is allowed, append this template */
697 if (preg_match("/c/",$acl)){
699 /* Search all templates from the current dn */
700 $ldap->cd (get_people_ou().$value);
701 $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
703 /* Append */
704 if ($ldap->count() != 0){
705 while ($attrs= $ldap->fetch()){
706 $this->templates[$ldap->getDN()]=
707 $attrs['uid'][0]." - ".@LDAP::fix($key);
708 }
709 $this->templates['none']= _("none");
710 }
711 }
712 }
714 /* Sort templates */
715 natcasesort ($this->templates);
716 reset ($this->templates);
717 }
720 /********************
721 Create a new user,template, user from template
722 ********************/
724 /* Check selected options for template */
725 if (isset($_POST['template_continue'])){
726 $message = array();
727 if(!isset($_POST['template']) || (empty($_POST['template']))){
728 $message[] = _("Please select a valid template.");
729 }
730 if(!isset($_POST['sn']) || (empty($_POST['sn']))){
731 $message[]= _("The required field 'Name' is not set.");
732 }
733 if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
734 $message[]= _("The required field 'Given name' is not set.");
735 }
737 /* Show error message / continue editing */
738 if (count($message) > 0){
739 show_errors ($message);
741 foreach(array("sn", "givenName", "uid", "template") as $attr){
742 if(isset($_POST[$attr])){
743 $smarty->assign("$attr", $_POST[$attr]);
744 }else{
745 $smarty->assign("$attr", "");
746 }
747 }
748 $smarty->assign("templates",$this->templates);
749 $smarty->assign("got_uid",$this->got_uid);
750 $smarty->assign("edit_uid",false);
751 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
753 }
754 }
756 /* New user/template request */
757 if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
758 /* By default we set 'dn' to 'new', all relevant plugins will
759 react on this. */
760 $this->dn= "new";
762 if (isset($this->config->current['IDGEN'])){
763 $this->got_uid= false;
764 } else {
765 $this->got_uid= true;
766 }
768 /* Create new usertab object */
769 $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
770 $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
771 $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
773 /* Take care about templates */
774 if ($s_action=="new_tpl"){
775 $this->is_template= TRUE;
776 $this->usertab->set_template_mode ();
777 } else {
778 $this->is_template= FALSE;
779 }
781 /* Use template if there are any of them */
782 if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
783 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
784 $smarty->assign("$attr", $this->$attr);
785 }
786 if ($s_action=="create_user_from_tpl"){
787 $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
788 } else {
789 $smarty->assign("template", "none");
790 }
791 $smarty->assign("edit_uid", "");
792 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
793 }
794 }
796 /********************
797 Template selected continue edit
798 ********************/
800 /* Continue template editing */
801 if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
803 $this->sn = $_POST['sn'];
804 $this->givenName = $_POST['givenName'];
806 /* Check for requred values */
807 $message= array();
808 if ($this->sn == "") {
809 $message[]= _("The required field 'Name' is not set.");
810 }
811 if ($this->givenName == "") {
812 $message[]= _("The required field 'Given name' is not set.");
813 }
815 /* Check if dn is used */
816 $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
817 $ldap= $this->config->get_ldap_link();
818 $ldap->cd ($dn);
819 $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
820 if ($ldap->count () != 0){
821 $message[]= _("A person with the choosen name is already used in this tree.");
822 }
824 /* Show error message / continue editing */
825 if (count($message) > 0){
826 show_errors ($message);
827 } else {
828 $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
829 if (isset($this->config->current['IDGEN']) &&
830 $this->config->current['IDGEN'] != ""){
831 $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
832 if (count($uids)){
833 $smarty->assign("edit_uid", "false");
834 $smarty->assign("uids", $uids);
835 $this->uid= current($uids);
836 }
837 } else {
838 $smarty->assign("edit_uid", "");
839 $this->uid= "";
840 }
841 $this->got_uid= true;
842 }
844 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
845 $smarty->assign("$attr", $this->$attr);
846 }
847 if (isset($_POST['template'])){
848 $smarty->assign("template", $_POST['template']);
849 }
850 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
851 }
853 /********************
854 No template selected continue edit
855 ********************/
857 /* No template. Ok. Lets fill data into the normal user dialog */
858 if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
859 foreach(array("sn", "givenName", "uid") as $attr){
860 if (isset($_POST[$attr])){
861 $this->usertab->by_object['user']->$attr= $_POST[$attr];
862 }
863 }
864 }
867 /********************
868 Template selected continue edit
869 ********************/
871 /* Finish template preamble */
872 if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
874 /* Might not be filled if IDGEN is unset */
875 $this->sn = $_POST['sn'];
876 $this->givenName = $_POST['givenName'];
878 /* Move user supplied data to sub plugins */
879 $this->uid = $_POST['uid'];
880 $this->usertab->uid = $this->uid;
881 $this->usertab->sn = $this->sn;
882 $this->usertab->givenName = $this->givenName;
883 $template_dn = $_POST['template'];
884 $this->usertab->adapt_from_template($template_dn);
885 $template_base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
886 $this->usertab->by_object['user']->base= $template_base;
887 }
890 /********************
891 If no template was selected set base
892 ********************/
894 if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
895 $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
896 }
899 /********************
900 Display subdialog
901 ********************/
903 /* Show tab dialog if object is present */
904 if(isset($this->usertab->config)){
906 $display= $this->usertab->execute();
908 /* Don't show buttons if tab dialog requests this */
910 $dia = FALSE;
911 if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
912 $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
913 }
915 if(!is_object($dia) && $dia != TRUE){
916 $display.= "<p style=\"text-align:right\">\n";
917 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
918 $display.= " \n";
919 if ($this->dn != "new"){
920 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
921 $display.= " \n";
922 }
923 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
924 $display.= "</p>";
925 }
926 return ($display);
927 }
929 /* Check if there is a snapshot dialog open */
930 $base = $this->DivListUsers->selectedBase;
931 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
932 return($str);
933 }
935 /* Return rendered main page */
936 /* Display dialog with system list */
937 $this->DivListUsers->parent = $this;
938 $this->DivListUsers->execute();
940 /* Add departments if subsearch is disabled */
941 if(!$this->DivListUsers->SubSearch){
942 $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
943 }
944 $this->reload();
945 $this->DivListUsers->setEntries($this->list);
946 return($this->DivListUsers->Draw());
947 }
950 /* Return departments, that will be included within snapshot detection */
951 function get_used_snapshot_bases()
952 {
953 return(array(get_people_ou().$this->DivListUsers->selectedBase));
954 }
957 function reload()
958 {
959 /* Set base for all searches */
960 $base= $this->DivListUsers->selectedBase;
961 $this->list =array();
963 /* Get filter configuration */
964 $Regex = $this->DivListUsers->Regex;
965 $SubSearch = $this->DivListUsers->SubSearch;
966 $ShowTemplates = $this->DivListUsers->ShowTemplates;
967 $ShowFunctionalUsers = $this->DivListUsers->ShowFunctionalUsers;
968 $ShowUnixUsers = $this->DivListUsers->ShowUnixUsers;
969 $ShowMailUsers = $this->DivListUsers->ShowMailUsers;
970 $ShowSambaUsers = $this->DivListUsers->ShowSambaUsers;
971 $ShowProxyUsers = $this->DivListUsers->ShowProxyUsers;
973 /* Setup filter depending on selection */
974 $filter="";
975 if ($this->config->current['SAMBAVERSION'] == 3){
976 $samba= "sambaSamAccount";
977 } else {
978 $samba= "sambaAccount";
979 }
981 if ($ShowFunctionalUsers){
982 $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
983 "(objectClass=gosaMailAccount)(objectClass=$samba)".
984 "(objectClass=gosaProxyAccount))))";
985 }
986 if ($ShowUnixUsers){
987 $filter.= "(objectClass=posixAccount)";
988 }
989 if ($ShowMailUsers){
990 $filter.= "(objectClass=gosaMailAccount)";
991 }
992 if ($ShowSambaUsers){
993 $filter.= "(objectClass=$samba)";
994 }
995 if ($ShowProxyUsers){
996 $filter.= "(objectClass=gosaProxyAccount)";
997 }
998 if ($ShowTemplates){
999 $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1000 } else {
1001 $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1002 }
1003 $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1005 /* Generate userlist */
1006 $ldap= $this->config->get_ldap_link(TRUE);
1008 if ($SubSearch){
1009 $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base,
1010 array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1011 } else {
1012 $base= get_people_ou().$base;
1013 $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base,
1014 array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1015 }
1016 $SortTemp = array();
1017 $List = array();
1018 foreach($ListTemp as $Key => $Entry){
1020 /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1021 * Else winstations will be listed too, if you use the subtree flag.
1022 */
1023 if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1024 continue;
1025 }else{
1027 // Generate caption for rows
1028 if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1029 $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1030 } else {
1031 $display= "[".$Entry["uid"][0]."]";
1032 }
1034 $display = strtolower($display);
1035 $List[$display] = $Entry;
1036 $SortTemp[$display] = $display;
1037 }
1038 }
1039 natcasesort($SortTemp);
1040 reset($SortTemp);
1042 $this->list = array();
1043 foreach($SortTemp as $Key){
1044 $this->list[] = $List[$Key];
1045 }
1046 }
1048 function remove_lock()
1049 {
1050 /* Remove user lock if a DN is marked as "currently edited" */
1051 if (isset($this->usertab->dn)){
1052 del_lock ($this->usertab->dn);
1053 }
1054 }
1057 function copyPasteHandling_from_queue($s_action,$s_entry)
1058 {
1059 /* Check if Copy & Paste is disabled */
1060 if(!is_object($this->CopyPasteHandler)){
1061 return("");
1062 }
1064 /* Add a single entry to queue */
1065 if($s_action == "cut" || $s_action == "copy"){
1066 /* Cleanup object queue */
1067 $this->CopyPasteHandler->cleanup_queue();
1068 $dn = $this->list[$s_entry]['dn'];
1069 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1070 }
1072 /* Add entries to queue */
1073 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1075 /* Cleanup object queue */
1076 $this->CopyPasteHandler->cleanup_queue();
1078 /* Add new entries to CP queue */
1079 foreach($this->list_get_selected_items() as $id){
1080 $dn = $this->list[$id]['dn'];
1082 if($s_action == "copy_multiple"){
1083 $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1084 }
1085 if($s_action == "cut_multiple"){
1086 $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1087 }
1088 }
1089 }
1091 /* Start pasting entries */
1092 if($s_action == "editPaste"){
1093 $this->start_pasting_copied_objects = TRUE;
1094 }
1096 /* Return C&P dialog */
1097 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1099 /* Get dialog */
1100 $data = $this->CopyPasteHandler->execute();
1101 $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase);
1103 /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1104 if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1105 $s_entry = $this->CopyPasteHandler->last_entry();
1106 $this->reload();
1107 foreach($this->list as $key => $entry){
1108 if($entry['dn'] == $s_entry){
1109 $this->CPPasswordChange = $key;
1110 }
1111 }
1112 }
1114 /* Return dialog data */
1115 if(!empty($data) && $this->CPPasswordChange == ""){
1116 return($data);
1117 }
1118 }
1120 /* Automatically disable status for pasting */
1121 if(!$this->CopyPasteHandler->entries_queued()){
1122 $this->start_pasting_copied_objects = FALSE;
1123 }
1124 return("");
1125 }
1128 function save_object()
1129 {
1130 /* Handle divlist filter && department selection*/
1131 if(!is_object($this->usertab)){
1132 $this->DivListUsers->save_object();
1133 }
1134 }
1137 function list_get_selected_items()
1138 {
1139 $ids = array();
1140 foreach($_POST as $name => $value){
1141 if(preg_match("/^item_selected_[0-9]*$/",$name)){
1142 $id = preg_replace("/^item_selected_/","",$name);
1143 $ids[$id] = $id;
1144 }
1145 }
1146 return($ids);
1147 }
1150 /* A set of disabled and therefore overloaded functions. They are
1151 not needed in this class. */
1152 function remove_from_parent() { }
1153 function check() { }
1154 function save() { }
1155 function adapt_from_template($dn) { }
1156 function password_change_needed() { }
1158 } /* ... class userManagement */
1159 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1160 ?>