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 }
155 if(isset($_POST['menu_action']) && preg_match("/^templatize_multiple/",$_POST['menu_action'])){
156 $s_action = "templatize_multiple";
157 }
159 if(isset($_POST['menu_action']) && preg_match("/^event/",$_POST['menu_action'])){
160 $s_action = $_POST['menu_action'];
161 }
164 /********************
165 Create notification event
166 ********************/
168 if(preg_match("/^event_/",$s_action) && class_available("DaemonEvent")){
169 $ids = $this->list_get_selected_items();
170 $uids = array();
171 foreach($ids as $id){
172 $uids[] = $this->list[$id]['uid'][0];
173 }
174 if(count($uids)){
175 $events = DaemonEvent::get_event_types(USER_EVENT);
176 $event = preg_replace("/^event_/","",$s_action);
177 if(isset($events['BY_CLASS'][$event])){
178 $type = $events['BY_CLASS'][$event];
179 $this->usertab = new $type['CLASS_NAME']($this->config);
180 $this->usertab->add_users($uids);
181 $this->usertab->set_type(TRIGGERED_EVENT);
182 }
183 }
184 }
186 /* Abort event dialog */
187 if(isset($_POST['abort_event_dialog'])){
188 $this->usertab = FALSE;
189 }
191 /* Save event */
192 if(isset($_POST['save_event_dialog'])){
193 $this->usertab->save_object();
194 $msgs = $this->usertab->check();
195 if(count($msgs)){
196 msg_dialog::displayChecks($msgs);
197 }else{
199 $o_queue = new gosaSupportDaemon();
200 $o_queue->append($this->usertab);
201 if($o_queue->is_error()){
202 msg_dialog::display(_("Infrastructure error"), msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
203 }else{
204 $this->usertab = FALSE;
205 }
206 }
207 }
209 /* Display event */
210 if($this->usertab instanceof DaemonEvent){
211 $this->usertab->save_object();
212 return($this->usertab->execute());
213 }
216 /********************
217 Copy & Paste
218 ********************/
220 /* Display the copy & paste dialog, if it is currently open */
221 if($this->CPPasswordChange == ""){
222 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
223 if($ret){
224 return($ret);
225 }
226 }
229 /********************
230 Change password confirmed
231 ********************/
233 /* Perform password change */
234 if (isset($_POST['password_finish'])){
236 /* For security reasons, check if user is allowed to set password again */
237 $dn = $this->dn;
238 $acl = $this->ui->get_permissions($dn, "users/password");
239 $cacl= $this->ui->get_permissions($dn, "users/user");
241 if (preg_match('/w/', $acl) || preg_match('/c/', $cacl)){
243 /* Check input and feed errors into 'message' */
244 $message= array();
246 /* Sanity checks... */
247 if ($_POST['new_password'] != $_POST['repeated_password']){
249 /* Matching passwords in new and repeated? */
250 $message[]= _("The passwords you've entered as 'New password' and 'Repeated new password' do not match.");
251 } else {
253 /* Empty password is not permitted by default. */
254 if ($_POST['new_password'] == ""){
255 msgPool::required(_("New password"));
256 }
257 }
259 /* Errors, or password change? */
260 if (count($message) != 0){
262 /* Show error message and continue editing */
263 msg_dialog::displayChecks($message);
264 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
265 }
267 $config= $this->config;
268 $ldap_ui= $this->config->get_ldap_link();
269 if(isset($this->usertab->dn)){
270 $ldap_ui->cat($this->usertab->dn,array("uid"));
271 $user = $ldap_ui->fetch();
272 }else{
273 $ldap_ui->cat($this->dn,array("uid"));
274 $user = $ldap_ui->fetch();
275 }
276 if((is_array($user))&&(isset($user['uid']))){
277 $username= $user['uid'][0];
278 }
280 /* Set password, perform required steps */
281 if ($this->usertab){
282 if ($this->usertab->password_change_needed()){
283 $obj= $this->usertab->by_object['user'];
284 change_password ($this->usertab->dn, $_POST['new_password'],0, $obj->pw_storage);
285 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
286 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
287 }
288 new log("modify","users/".get_class($this),$this->usertab->dn,array(),"Password has been changed");
289 unset($this->usertab);
290 $this->usertab= NULL;
291 }
292 } else {
293 change_password ($this->dn, $_POST['new_password']);
294 if (isset($config->data['MAIN']['EXTERNALPWDHOOK'])){
295 exec($config->data['MAIN']['EXTERNALPWDHOOK']." ".$username." ".$_POST['new_password'], $resarr);
296 }
297 new log("modify","users/".get_class($this),$this->dn,array(),"Password has been changed");
298 }
299 } else {
301 /* Missing permissions, show message */
302 msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
303 }
304 /* Clean session, delete lock */
305 del_lock ($this->dn);
306 unset ($this->usertab);
307 $this->usertab= NULL;
308 $this->lognames= array();;
309 $this->sn= "";
310 $this->givenName= "";
311 $this->uid= "";
312 session::un_set('objectinfo');
313 }
316 /********************
317 Change multiple passwords requested
318 ********************/
320 if($s_action == "multiple_password_change"){
321 $this->pwd_change_queue = $this->list_get_selected_items();
322 }
325 /********************
326 Change password requested
327 ********************/
329 /* Password change requested */
330 if (($s_action == "change_pw") || (!empty($this->CPPasswordChange)) || count($this->pwd_change_queue)){
332 /* Get users whose passwords should be changed. */
333 if(count($this->pwd_change_queue)){
334 $s_entry= array_pop($this->pwd_change_queue);
335 }
337 if(!empty($this->CPPasswordChange)){
338 $s_entry = $this->CPPasswordChange;
339 $this->CPPasswordChange = "";
340 }
342 /* Get 'dn' from posted 'uid' */
343 $this->dn= $this->list[trim($s_entry)]['dn'];
345 /* Load permissions for selected 'dn' and check if
346 we're allowed to remove this 'dn' */
347 if (preg_match("/w/",$this->ui->get_permissions($this->dn,"users/password"))){
349 /* User is allowed to change passwords, save 'dn' and 'acl' for next
350 dialog. */
351 session::set('objectinfo',$this->dn);
352 return ($smarty->fetch(get_template_path('password.tpl', TRUE)));
355 } else {
356 /* User is not allowed. Show message and cancel. */
357 msg_dialog::display(_("Password change"),_("You have no permission to change this users password!"),WARNING_DIALOG);
358 }
359 }
363 /********************
364 Edit existing entry
365 ********************/
367 /* User wants to edit data? */
368 if (($s_action=="edit") && (!isset($this->usertab->config))){
370 /* Get 'dn' from posted 'uid', must be unique */
371 $this->dn= $this->list[trim($s_entry)]['dn'];
373 /* Check locking, save current plugin in 'back_plugin', so
374 the dialog knows where to return. */
375 if (($user= get_lock($this->dn)) != ""){
376 return(gen_locked_message ($user, $this->dn));
377 }
379 /* Lock the current entry, so everyone will get the
380 above dialog */
381 add_lock ($this->dn, $this->ui->dn);
383 /* Register usertab to trigger edit dialog */
384 $this->usertab= new usertabs($this->config,
385 $this->config->data['TABS']['USERTABS'], $this->dn);
387 /* Switch tab, if it was requested by the user */
388 $this->usertab->current = $s_tab;
390 /* Set ACL and move DN to the headline */
391 $this->usertab->set_acl_base($this->dn);
392 session::set('objectinfo',$this->dn);
393 }
396 /********************
397 Edit multiple entries
398 ********************/
400 /* User wants to edit data? */
401 if ($s_action == "multiple_edit" && !isset($this->usertab->config)){
403 $this->dn = array();
404 foreach($this->list_get_selected_items() as $id){
405 $this->dn[] = $this->list[$id]['dn'];;
406 }
407 $tmp = new multi_plug($this->config,"usertabs",$this->config->data['TABS']['USERTABS'],
408 $this->dn,$this->DivListUsers->selectedBase,"user");
409 if ($tmp->entries_locked()){
410 return($tmp->display_lock_message());
411 }
412 $tmp->lock_entries($this->ui->dn);
413 if($tmp->multiple_available()){
414 $this->usertab = $tmp;
415 $this->usertab->set_active_tab($s_tab);
416 session::set('objectinfo',$this->usertab->get_object_info());
417 }
418 }
421 /********************
422 Edit canceled
423 ********************/
425 /* Reset all relevant data, if we get a _cancel request */
426 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
427 if (isset($this->usertab)){
428 del_lock ($this->usertab->dn);
429 unset ($this->usertab);
430 }
431 $this->usertab= NULL;
432 $this->lognames= array();;
433 $this->sn= "";
434 $this->givenName= "";
435 $this->uid= "";
436 session::un_set('objectinfo');
437 }
440 /********************
441 Apply template to multiple entries requested, display confirm dialog
442 ********************/
445 # TODO: show dialog to choose
446 # * template
447 # * when the template is choosen refresh the list of
448 # available attributes with a checkbox - everything checked
449 # * when pressing apply do the stuff below
450 if ($s_action=="templatize_multiple"){
451 $ids = $this->list_get_selected_items();
452 $this->dns = array();
453 if(count($ids)){
455 foreach($ids as $id){
456 $dn = $this->list[$id]['dn'];
457 if (($user= get_lock($dn)) != ""){
458 return(gen_locked_message ($user, $dn));
459 }
460 $this->dns[$id] = $dn;
461 }
462 }
464 return($smarty->fetch(get_template_path('templatize.tpl', TRUE)));
465 }
467 # TODO: apply after we pressed some button
468 # foreach ($this->dns as $dn){
469 #
470 # echo "Template test with '$dn'<br>";
471 # $template_dn= "cn=super44 super44,ou=people,dc=gonicus,dc=de";
472 # $usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'], $dn);
473 # $usertab->adapt_from_template($template_dn, array("sn", "givenName", "uid"));
474 # $usertab->save();
475 # unset ($usertab);
476 # $usertab= NULL;
477 # }
480 /********************
481 Delete MULTIPLE entries requested, display confirm dialog
482 ********************/
484 if ($s_action=="del_multiple"){
485 $ids = $this->list_get_selected_items();
486 $this->dns = array();
487 if(count($ids)){
489 foreach($ids as $id){
490 $dn = $this->list[$id]['dn'];
491 if (($user= get_lock($dn)) != ""){
492 return(gen_locked_message ($user, $dn));
493 }
494 $this->dns[$id] = $dn;
495 }
497 $dns_names = array();
498 foreach($this->dns as $dn){
499 $dns_names[] = @LDAP::fix($dn);
500 }
502 /* Lock the current entry, so nobody will edit it during deletion */
503 $info = sprintf(msgPool::deleteInfo($dns_names,_("user")));
504 $this->msg_dialog = new msg_dialog(_("Delete users"),$info,CONFIRM_DIALOG);
505 $this->current_action = $s_action;
506 }
507 }
510 /********************
511 Delete MULTIPLE entries confirmed
512 ********************/
514 /* Confirmation for deletion has been passed. Users should be deleted. */
515 if ($this->current_action == "del_multiple" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
517 $this->current_action = "";
519 /* Remove user by user and check acls before removeing them */
520 foreach($this->dns as $key => $dn){
522 $acl = $this->ui->get_permissions($dn, "users/user");
523 if (preg_match('/d/', $acl)){
525 /* Delete request is permitted, perform LDAP action */
526 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
527 $this->usertab->set_acl_base();
528 $this->usertab->delete ();
529 unset ($this->usertab);
530 $this->usertab= NULL;
531 } else {
532 msg_dialog::display(_("Warning"),msgPool::permDelete($dn),WARNING_DIALOG);
533 if(isset($this->ui->uid)){
534 new log("security","users/".get_class($this),$dn,array(),"Tried to trick deletion.");
535 }
536 }
537 /* Remove lock file after successfull deletion */
538 del_lock ($dn);
539 unset($this->dns[$key]);
540 }
541 }
544 /********************
545 Delete MULTIPLE entries Canceled
546 ********************/
548 /* Remove lock */
549 if(isset($_POST['delete_multiple_user_cancel'])){
550 foreach($this->dns as $key => $dn){
551 del_lock ($dn);
552 unset($this->dns[$key]);
553 }
554 }
557 /********************
558 Toggle lock status for user
559 ********************/
561 if($s_action == "toggle_lock_status" && isset($this->list[$s_entry])){
563 /* Get entry check current status */
564 $val = $this->list[$s_entry];
565 $pwd = $val['userPassword'][0];
567 if(!preg_match("/^\{[^\}]/",$pwd)){
568 trigger_error("Can not deactivate user which is using clear password encryption.");
569 }else{
571 $locked = false;
572 if(preg_match("/^[^\}]*+\}!/",$pwd)){
573 $locked = true;
574 }
576 /* Create ldap array to update status */
577 $attrs = array("userPassword" => $pwd);
578 if($locked){
579 $attrs['userPassword'] = preg_replace("/(^[^\}]+\})!(.*$)/","\\1\\2",$attrs['userPassword']);
580 }else{
581 $attrs['userPassword'] = preg_replace("/(^[^\}]+\})(.*$)/","\\1!\\2",$attrs['userPassword']);
582 }
584 /* Write new status back to ldap */
585 $ldap = $this->config->get_ldap_link();
586 $ldap->cd($val['dn']);
587 $ldap->modify($attrs);
588 if (!$ldap->success()){
589 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $val['dn'], 0, get_class()));
590 }
591 }
592 }
595 /********************
596 Delete entry requested, display confirm dialog
597 ********************/
599 /* Remove user was requested */
600 if ($s_action=="del"){
602 /* Get 'dn' from posted 'uid' */
603 $this->dn= $this->list[trim($s_entry)]['dn'];
605 /* Load permissions for selected 'dn' and check if
606 we're allowed to remove this 'dn' */
608 /* Check locking, save current plugin in 'back_plugin', so
609 the dialog knows where to return. */
610 if (($user= get_lock($this->dn)) != ""){
611 return(gen_locked_message ($user, $this->dn));
612 }
614 $this->msg_dialog = new msg_dialog( _("Delete user"),msgPool::deleteInfo(@LDAP::fix($this->dn),_("user")),CONFIRM_DIALOG);
615 $this->current_action = $s_action;
616 }
619 /********************
620 Delete entry confirmed
621 ********************/
623 /* Confirmation for deletion has been passed. User should be deleted. */
624 if ($this->current_action == "del" && is_object($this->msg_dialog) && $this->msg_dialog->is_confirmed()){
626 $this->current_action = "";
628 /* Some nice guy may send this as POST, so we've to check
629 for the permissions again. */
631 $acl = $this->ui->get_permissions($this->dn, "users/user");
633 if (preg_match('/d/', $acl)){
635 /* Delete request is permitted, perform LDAP action */
636 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$this->dn);
637 $this->usertab->set_acl_base();
638 $this->usertab->delete ();
639 unset ($this->usertab);
640 $this->usertab= NULL;
641 msg_dialog::display(_("Information"),_("User successfully removed."),INFO_DIALOG);
642 } else {
644 /* Normally this shouldn't be reached, send some extra
645 logs to notify the administrator */
646 msg_dialog::display(_("Warning"),msgPool::permDelete(),WARNING_DIALOG);
648 if(isset($this->ui->uid)){
649 new log("security","users/".get_class($this),$this->dn,array(),"Tried to trick deletion.");
650 }
651 }
653 /* Remove lock file after successfull deletion */
654 del_lock ($this->dn);
655 }
658 /********************
659 Delete entry Canceled
660 ********************/
662 /* Delete user canceled? */
663 if (isset($_POST['delete_cancel'])){
664 del_lock ($this->dn);
665 }
668 /********************
669 Edit entry finished (Save)
670 ********************/
672 /* Finish user edit is triggered by the tabulator dialog, so
673 the user wants to save edited data. Check and save at this
674 point. */
675 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->usertab->config))){
677 /* Check tabs, will feed message array */
678 $this->usertab->last= $this->usertab->current;
679 $this->usertab->save_object();
680 $message= $this->usertab->check();
682 /* Save, or display error message? */
683 if (count($message) == 0){
685 /* No errors. Go ahead and prepare to ask for a password
686 in case we're creating a new user. 'dn' will be 'new'
687 in this case. It is set to the correct value later. */
688 if ($this->dn == "new"){
689 $set_pass= 1;
690 } else {
691 $set_pass= 0;
692 }
694 /* Save user data to ldap */
695 if($this->usertab->save() == 1){
696 return;
697 }
699 if (!isset($_POST['edit_apply'])){
700 /* User has been saved successfully, remove lock from LDAP. */
701 if ($this->dn != "new"){
702 del_lock ($this->dn);
703 }
705 /* In case of new users, ask for a password, skip this for templates */
706 if (($set_pass || $this->usertab->password_change_needed()) && !$this->is_template){
707 $this->dn = $this->usertab->dn;
708 return($smarty->fetch(get_template_path('password.tpl', TRUE)));
709 }
711 unset ($this->usertab);
712 $this->usertab= NULL;
713 session::un_set('objectinfo');
714 }
715 } else {
716 /* Ok. There seem to be errors regarding to the tab data,
717 show message and continue as usual. */
718 msg_dialog::displayChecks($message);
719 }
720 }
723 /********************
724 We want to create a new user, so fetch all available user templates
725 ********************/
727 /* Generate template list */
728 if (($s_action=="new")||($s_action=="create_user_from_tpl")){
730 $this->templates= array();
731 $ldap= $this->config->get_ldap_link();
733 /* Create list of templates */
734 foreach ($this->config->departments as $key => $value){
736 /* Get acls from different ou's */
737 $acl = $this->ui->get_permissions("cn=dummy,".get_people_ou().$value,"users/user") ;
739 /* If creation of a new user is allowed, append this template */
740 if (preg_match("/c/",$acl)){
742 /* Search all templates from the current dn */
743 $ldap->cd (get_people_ou().$value);
744 $ldap->search ("(objectClass=gosaUserTemplate)", array("uid"));
746 /* Append */
747 if ($ldap->count() != 0){
748 while ($attrs= $ldap->fetch()){
749 $this->templates[$ldap->getDN()]=
750 $attrs['uid'][0]." - ".@LDAP::fix($key);
751 }
752 $this->templates['none']= _("none");
753 }
754 }
755 }
757 /* Sort templates */
758 natcasesort ($this->templates);
759 reset ($this->templates);
760 }
763 /********************
764 Create a new user,template, user from template
765 ********************/
767 /* Check selected options for template */
768 if (isset($_POST['template_continue'])){
769 $message = array();
770 if(!isset($_POST['template']) || (empty($_POST['template']))){
771 $message[]= msgPool::invalid(_("Template"));
772 }
773 if(!isset($_POST['sn']) || (empty($_POST['sn']))){
774 $message[]= msgPool::required(_("Name"));
775 }
776 if(!isset($_POST['givenName']) || (empty($_POST['givenName']))){
777 $message[]= msgPool::required(_("Given name"));
778 }
780 /* Show error message / continue editing */
781 if (count($message) > 0){
782 msg_dialog::displayChecks($message);
784 foreach(array("sn", "givenName", "uid", "template") as $attr){
785 if(isset($_POST[$attr])){
786 $smarty->assign("$attr", $_POST[$attr]);
787 }else{
788 $smarty->assign("$attr", "");
789 }
790 }
791 $smarty->assign("templates",$this->templates);
792 $smarty->assign("got_uid",$this->got_uid);
793 $smarty->assign("edit_uid",false);
794 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
796 }
797 }
799 /* New user/template request */
800 if (($s_action=="create_user_from_tpl")||($s_action=="new") || ($s_action=="new_tpl")){
801 /* By default we set 'dn' to 'new', all relevant plugins will
802 react on this. */
803 $this->dn= "new";
805 if (isset($this->config->current['IDGEN'])){
806 $this->got_uid= false;
807 } else {
808 $this->got_uid= true;
809 }
811 /* Create new usertab object */
812 $this->usertab= new usertabs($this->config,$this->config->data['TABS']['USERTABS'], $this->dn);
813 $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
814 $this->usertab->set_acl_base('dummy,'.$this->DivListUsers->selectedBase);
816 /* Take care about templates */
817 if ($s_action=="new_tpl"){
818 $this->is_template= TRUE;
819 $this->usertab->set_template_mode ();
820 } else {
821 $this->is_template= FALSE;
822 }
824 /* Use template if there are any of them */
825 if ((count($this->templates) && ($s_action!='new_tpl'))||($s_action=="create_user_from_tpl")){
826 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
827 $smarty->assign("$attr", $this->$attr);
828 }
829 if ($s_action=="create_user_from_tpl"){
830 $smarty->assign("template", $this->dn= $this->list[trim($s_entry)]['dn']);
831 } else {
832 $smarty->assign("template", "none");
833 }
834 $smarty->assign("edit_uid", "");
835 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
836 }
837 }
839 /********************
840 Template selected continue edit
841 ********************/
843 /* Continue template editing */
844 if ((isset($_POST['template_continue'])) && ($_POST['template'] != 'none') && (!isset($_POST['uid']))){
846 $this->sn = $_POST['sn'];
847 $this->givenName = $_POST['givenName'];
849 /* Check for requred values */
850 $message= array();
851 if ($this->sn == "") {
852 $message[]= msgPool::required(_("Name"));
853 }
854 if ($this->givenName == "") {
855 $message[]= msgPool::required(_("Given name"));
856 }
858 /* Check if dn is used */
859 $dn= preg_replace("/^[^,]+,/i", "", $_POST['template']);
860 $ldap= $this->config->get_ldap_link();
861 $ldap->cd ($dn);
862 $ldap->search ("(&(sn=".normalizeLdap($this->sn).")(givenName=".normalizeLdap($this->givenName)."))", array("givenName"));
863 if ($ldap->count () != 0){
864 msgPool::duplicated(_("Name"));
865 }
867 /* Show error message / continue editing */
868 if (count($message) > 0){
869 msg_dialog::displayChecks($message);
870 } else {
871 $attributes= array('sn' => $this->sn, 'givenName' => $this->givenName);
872 if (isset($this->config->current['IDGEN']) &&
873 $this->config->current['IDGEN'] != ""){
874 $uids= gen_uids ($this->config->current['IDGEN'], $attributes);
875 if (count($uids)){
876 $smarty->assign("edit_uid", "false");
877 $smarty->assign("uids", $uids);
878 $this->uid= current($uids);
879 }
880 } else {
881 $smarty->assign("edit_uid", "");
882 $this->uid= "";
883 }
884 $this->got_uid= true;
885 }
887 foreach(array("sn", "givenName", "uid", "got_uid", "templates") as $attr){
888 $smarty->assign("$attr", $this->$attr);
889 }
890 if (isset($_POST['template'])){
891 $smarty->assign("template", $_POST['template']);
892 }
893 return($smarty->fetch(get_template_path('template.tpl', TRUE)));
894 }
896 /********************
897 No template selected continue edit
898 ********************/
900 /* No template. Ok. Lets fill data into the normal user dialog */
901 if (isset($_POST['template_continue']) && $_POST['template'] == 'none'){
902 foreach(array("sn", "givenName", "uid") as $attr){
903 if (isset($_POST[$attr])){
904 $this->usertab->by_object['user']->$attr= $_POST[$attr];
905 }
906 }
907 }
910 /********************
911 Template selected continue edit
912 ********************/
914 /* Finish template preamble */
915 if (isset($_POST['template_continue']) && $_POST['template'] != 'none' && (isset($_POST['uid']))){
917 /* Might not be filled if IDGEN is unset */
918 $this->sn = $_POST['sn'];
919 $this->givenName = $_POST['givenName'];
921 /* Move user supplied data to sub plugins */
922 $this->uid = $_POST['uid'];
923 $this->usertab->uid = $this->uid;
924 $this->usertab->sn = $this->sn;
925 $this->usertab->givenName = $this->givenName;
926 $template_dn = $_POST['template'];
927 $this->usertab->adapt_from_template($template_dn);
928 $template_base = preg_replace("/^[^,]+,".normalizePreg(get_people_ou())."/", '', $template_dn);
929 $this->usertab->by_object['user']->base= $template_base;
930 }
933 /********************
934 If no template was selected set base
935 ********************/
937 if (isset($_POST['template_continue']) && ($_POST['template'] == 'none')){
938 $this->usertab->by_object['user']->base= $this->DivListUsers->selectedBase;
939 }
942 /********************
943 Display subdialog
944 ********************/
946 /* Show tab dialog if object is present */
947 if(isset($this->usertab->config)){
949 $display= $this->usertab->execute();
951 /* Don't show buttons if tab dialog requests this */
953 $dia = FALSE;
954 if(isset($this->usertab->by_object[$this->usertab->current]->dialog)){
955 $dia = $this->usertab->by_object[$this->usertab->current]->dialog;
956 }
958 if(!is_object($dia) && $dia != TRUE){
959 $display.= "<p style=\"text-align:right\">\n";
960 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
961 $display.= " \n";
962 if ($this->dn != "new"){
963 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
964 $display.= " \n";
965 }
966 $display.= "<input type=submit name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
967 $display.= "</p>";
968 }
969 return ($display);
970 }
972 /* Check if there is a snapshot dialog open */
973 $base = $this->DivListUsers->selectedBase;
974 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
975 return($str);
976 }
978 /* Return rendered main page */
979 /* Display dialog with system list */
980 $this->DivListUsers->parent = $this;
981 $this->DivListUsers->execute();
983 /* Add departments if subsearch is disabled */
984 if(!$this->DivListUsers->SubSearch){
985 $this->DivListUsers->AddDepartments($this->DivListUsers->selectedBase,4,1);
986 }
987 $this->reload();
988 $this->DivListUsers->setEntries($this->list);
989 return($this->DivListUsers->Draw());
990 }
993 /* Return departments, that will be included within snapshot detection */
994 function get_used_snapshot_bases()
995 {
996 return(array(get_people_ou().$this->DivListUsers->selectedBase));
997 }
1000 function reload()
1001 {
1002 /* Set base for all searches */
1003 $base= $this->DivListUsers->selectedBase;
1004 $this->list =array();
1006 /* Get filter configuration */
1007 $Regex = $this->DivListUsers->Regex;
1008 $SubSearch = $this->DivListUsers->SubSearch;
1009 $ShowTemplates = $this->DivListUsers->ShowTemplates;
1010 $ShowFunctionalUsers = $this->DivListUsers->ShowFunctionalUsers;
1011 $ShowUnixUsers = $this->DivListUsers->ShowUnixUsers;
1012 $ShowMailUsers = $this->DivListUsers->ShowMailUsers;
1013 $ShowSambaUsers = $this->DivListUsers->ShowSambaUsers;
1014 $ShowProxyUsers = $this->DivListUsers->ShowProxyUsers;
1016 /* Setup filter depending on selection */
1017 $filter="";
1018 if ($this->config->current['SAMBAVERSION'] == 3){
1019 $samba= "sambaSamAccount";
1020 } else {
1021 $samba= "sambaAccount";
1022 }
1024 if ($ShowFunctionalUsers){
1025 $filter.= "(&(objectClass=gosaAccount)(!(|(objectClass=posixAccount)".
1026 "(objectClass=gosaMailAccount)(objectClass=$samba)".
1027 "(objectClass=gosaProxyAccount))))";
1028 }
1029 if ($ShowUnixUsers){
1030 $filter.= "(objectClass=posixAccount)";
1031 }
1032 if ($ShowMailUsers){
1033 $filter.= "(objectClass=gosaMailAccount)";
1034 }
1035 if ($ShowSambaUsers){
1036 $filter.= "(objectClass=$samba)";
1037 }
1038 if ($ShowProxyUsers){
1039 $filter.= "(objectClass=gosaProxyAccount)";
1040 }
1041 if ($ShowTemplates){
1042 $filter= "(|(objectClass=gosaUserTemplate)(&(objectClass=gosaAccount)(|$filter)))";
1043 } else {
1044 $filter= "(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(|$filter))";
1045 }
1046 $filter= "(&(|(uid=".normalizeLdap($Regex).")(sn=".normalizeLdap($Regex).")(givenName=".normalizeLdap($Regex)."))$filter)";
1048 /* Generate userlist */
1049 $ldap= $this->config->get_ldap_link(TRUE);
1051 if ($SubSearch){
1052 $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base,
1053 array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SUBSEARCH | GL_SIZELIMIT);
1054 } else {
1055 $base= get_people_ou().$base;
1056 $ListTemp = get_sub_list($filter, "users", get_people_ou(),$base,
1057 array("uid", "givenName", "sn", "objectClass","userPassword"), GL_SIZELIMIT);
1058 }
1059 $SortTemp = array();
1060 $List = array();
1061 foreach($ListTemp as $Key => $Entry){
1063 /* Skip entries that are not located under the people ou (normaly 'ou=people,')
1064 * Else winstations will be listed too, if you use the subtree flag.
1065 */
1066 if(!preg_match("/".normalizePreg(get_people_ou())."/i",$Entry['dn'])){
1067 continue;
1068 }else{
1070 // Generate caption for rows
1071 if (isset($Entry["sn"]) && isset($Entry["givenName"])){
1072 $display= $Entry["sn"][0].", ".$Entry["givenName"][0]." [".$Entry["uid"][0]."]";
1073 } else {
1074 $display= "[".$Entry["uid"][0]."]";
1075 }
1077 $display = strtolower($display);
1078 $List[$display] = $Entry;
1079 $SortTemp[$display] = $display;
1080 }
1081 }
1082 natcasesort($SortTemp);
1083 reset($SortTemp);
1085 $this->list = array();
1086 foreach($SortTemp as $Key){
1087 $this->list[] = $List[$Key];
1088 }
1089 }
1091 function remove_lock()
1092 {
1093 /* Remove user lock if a DN is marked as "currently edited" */
1094 if (isset($this->usertab->dn)){
1095 del_lock ($this->usertab->dn);
1096 }
1097 }
1100 function copyPasteHandling_from_queue($s_action,$s_entry)
1101 {
1102 /* Check if Copy & Paste is disabled */
1103 if(!is_object($this->CopyPasteHandler)){
1104 return("");
1105 }
1107 /* Add a single entry to queue */
1108 if($s_action == "cut" || $s_action == "copy"){
1109 /* Cleanup object queue */
1110 $this->CopyPasteHandler->cleanup_queue();
1111 $dn = $this->list[$s_entry]['dn'];
1112 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"usertabs","USERTABS","users");
1113 }
1115 /* Add entries to queue */
1116 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1118 /* Cleanup object queue */
1119 $this->CopyPasteHandler->cleanup_queue();
1121 /* Add new entries to CP queue */
1122 foreach($this->list_get_selected_items() as $id){
1123 $dn = $this->list[$id]['dn'];
1125 if($s_action == "copy_multiple"){
1126 $this->CopyPasteHandler->add_to_queue($dn,"copy","usertabs","USERTABS","users");
1127 }
1128 if($s_action == "cut_multiple"){
1129 $this->CopyPasteHandler->add_to_queue($dn,"cut","usertabs","USERTABS","users");
1130 }
1131 }
1132 }
1134 /* Start pasting entries */
1135 if($s_action == "editPaste"){
1136 $this->start_pasting_copied_objects = TRUE;
1137 }
1139 /* Return C&P dialog */
1140 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1142 /* Get dialog */
1143 $data = $this->CopyPasteHandler->execute();
1144 $this->CopyPasteHandler->SetVar("base",$this->DivListUsers->selectedBase);
1146 /* Set CPPasswordChange to s_entry which indicates that this entry requires a new password. */
1147 if(isset($_POST['passwordTodo']) && ($_POST['passwordTodo'] == "new")){
1148 $s_entry = $this->CopyPasteHandler->last_entry();
1149 $this->reload();
1150 foreach($this->list as $key => $entry){
1151 if($entry['dn'] == $s_entry){
1152 $this->CPPasswordChange = $key;
1153 }
1154 }
1155 }
1157 /* Return dialog data */
1158 if(!empty($data) && $this->CPPasswordChange == ""){
1159 return($data);
1160 }
1161 }
1163 /* Automatically disable status for pasting */
1164 if(!$this->CopyPasteHandler->entries_queued()){
1165 $this->start_pasting_copied_objects = FALSE;
1166 }
1167 return("");
1168 }
1171 function save_object()
1172 {
1173 /* Handle divlist filter && department selection*/
1174 if(!is_object($this->usertab)){
1175 $this->DivListUsers->save_object();
1176 }
1177 if(is_object($this->CopyPasteHandler)){
1178 $this->CopyPasteHandler->save_object();
1179 }
1180 }
1183 function list_get_selected_items()
1184 {
1185 $ids = array();
1186 foreach($_POST as $name => $value){
1187 if(preg_match("/^item_selected_[0-9]*$/",$name)){
1188 $id = preg_replace("/^item_selected_/","",$name);
1189 $ids[$id] = $id;
1190 }
1191 }
1192 return($ids);
1193 }
1196 /* A set of disabled and therefore overloaded functions. They are
1197 not needed in this class. */
1198 function remove_from_parent() { }
1199 function check() { }
1200 function save() { }
1201 function adapt_from_template($dn, $skip= array()) { }
1202 function password_change_needed() { }
1204 } /* ... class userManagement */
1205 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1206 ?>