1 <?php
2 /*
3 This code is part of GOsa (https://gosa.gonicus.de)
4 Copyright (C) 2003 Cajus Pollmeier
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 */
20 require "tabs_group.inc";
22 class groupManagement extends plugin
23 {
24 /* Definitions */
25 var $plHeadline= "Groups";
26 var $plDescription= "This does something";
28 /* Dialog attributes */
29 var $grouptab = NULL;
30 var $grouplist = array();
31 var $ui = NULL;
32 var $CopyPasteHandler = NULL;
33 var $DivListGroup = NULL;
34 var $ShowPrimaryCheckBox= false;
36 function groupManagement ($config, $ui)
37 {
38 /* Save configuration for internal use */
39 $this->config = $config;
40 $this->ui = $ui;
42 /* Copy & Paste enabled ?*/
43 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
44 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
45 }
47 /* Detect if we have to display the primary group checkbox */
48 $tmp = "";
49 if (isset($this->config->data['MAIN']['NOPRIMARYGROUP'])){
50 $tmp = $this->config->data['MAIN']['NOPRIMARYGROUP'];
51 }
52 if(preg_match("/true/i",$tmp)|| (preg_match("/yes/",$tmp))){
53 $this->ShowPrimaryCheckBox = false;
54 } else {
55 $this->ShowPrimaryCheckBox = true;
56 }
58 /* Create dialog object */
59 $this->DivListGroup = new divListGroup($this->config,$this);
60 $this->DivListGroup->DisableCheckBox("ShowPrimaryGroups",$this->ShowPrimaryCheckBox);
61 }
64 function execute()
65 {
66 /* Call parent execute */
67 plugin::execute();
69 /* Store these posts if the current object is locked (used by somebody else)*/
70 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_groups/");
72 /* Save data */
73 $s_action = "";
74 $s_entry = "";
76 /* Test Posts */
77 foreach($_POST as $key => $val){
78 // Post for delete
79 if(preg_match("/^group_del.*/",$key)){
80 $s_action = "del";
81 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
82 // Post for edit
83 }elseif(preg_match("/^group_edit_.*/",$key)){
84 $s_action="edit";
85 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
86 // Post for new
87 }elseif(preg_match("/^group_new.*/",$key)){
88 $s_action="new";
89 }elseif(preg_match("/^dep_home.*/i",$key)){
90 $s_action="home";
91 }elseif(preg_match("/^group_tplnew.*/i",$key)){
92 $s_action="new_tpl";
93 }elseif(preg_match("/^group_chgpw.*/i",$key)){
94 $s_action="change_pw";
95 $s_entry = preg_replace("/group_chgpw_/i","",$key);
96 }elseif(preg_match("/_group_edit_/",$key)){
97 $type = preg_replace("/_group_edit_.*$/","",$key);
98 $s_action="edit";
99 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
100 $_POST['arg'] = $type;
101 }elseif(preg_match("/^editPaste.*/i",$key)){
102 $s_action="editPaste";
103 }elseif(preg_match("/^copy_.*/",$key)){
104 $s_action="copy";
105 $s_entry = preg_replace("/^copy_/i","",$key);
106 }elseif(preg_match("/^cut_.*/",$key)){
107 $s_action="cut";
108 $s_entry = preg_replace("/^cut_/i","",$key);
109 }elseif(preg_match("/^remove_multiple_groups/",$key)){
110 $s_action="del_multiple";
111 }
112 }
113 $s_entry = preg_replace("/_.$/","",$s_entry);
115 /* Check for posted gets */
116 if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
117 $s_entry = $_GET['id'];
118 $s_action = "edit";
119 }
121 $smarty= get_smarty();
123 /********************
124 Copy & Paste Handling ...
125 ********************/
127 /* Only perform copy&paste requests if it is enabled
128 */
129 if($this->CopyPasteHandler){
130 if($str = $this->copyPasteHandling($s_action,$s_entry)){
131 return $str;
132 }
133 }
136 /********************
137 Create a new group ...
138 ********************/
140 /* New group? */
141 if ($s_action=="new"){
143 /* Check create permissions */
144 $acl = $this->ui->get_permissions($this->DivListGroup->selectedBase,"groups/group");
145 if(preg_match("/c/",$acl)){
147 /* By default we set 'dn' to 'new', all relevant plugins will
148 react on this. */
149 $this->dn= "new";
151 /* Create new usertab object */
152 $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
154 /* Set up the users ACL's for this 'dn' */
155 $this->grouptab->set_acl_base($this->DivListGroup->selectedBase);
156 }
157 }
160 /********************
161 Save Group Tab/Object Changes
162 ********************/
164 /* Finish group edit is triggered by the tabulator dialog, so
165 the user wants to save edited data. Check and save at this
166 point. */
167 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->grouptab->config)) ){
169 /* Check tabs, will feed message array
170 Save, or display error message? */
171 $message= $this->grouptab->check();
172 if (count($message) == 0){
174 /* Save user data to ldap */
175 $this->grouptab->save();
176 gosa_log ("Group object '".$this->dn."' has been saved");
178 if (!isset($_POST['edit_apply'])){
179 /* Group has been saved successfully, remove lock from LDAP. */
180 if ($this->dn != "new"){
181 del_lock ($this->dn);
182 }
184 /* There's no page reload so we have to read new users at this point. */
185 //$this->reload ();
186 unset ($this->grouptab);
187 $this->grouptab= NULL;
188 unset ($_SESSION['objectinfo']);
189 }
190 } else {
191 /* Ok. There seem to be errors regarding to the tab data,
192 show message and continue as usual. */
193 show_errors($message);
194 }
195 }
198 /********************
199 Edit existing group
200 ********************/
202 /* User wants to edit data? */
203 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
205 /* Get 'dn' from posted 'uid', must be unique */
206 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
208 /* Check locking & lock entry if required */
209 $user = get_lock($this->dn);
210 if ($user != ""){
211 return(gen_locked_message ($user, $this->dn));
212 }
213 add_lock ($this->dn, $this->ui->dn);
215 /* Register grouptab to trigger edit dialog */
216 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
217 $this->grouptab->set_acl_base($this->dn);
218 $_SESSION['objectinfo']= $this->dn;
219 }
222 /********************
223 Delete MULTIPLE entries requested, display confirm dialog
224 ********************/
226 if ($s_action=="del_multiple"){
227 $ids = $this->list_get_selected_items();
229 if(count($ids)){
231 foreach($ids as $id){
232 $dn = $this->grouplist[$id]['dn'];
233 if (($user= get_lock($dn)) != ""){
234 return(gen_locked_message ($user, $dn));
235 }
236 $this->dns[$id] = $dn;
237 }
239 $dns_names = "<br><pre>";
240 foreach($this->dns as $dn){
241 add_lock ($dn, $this->ui->dn);
242 $dns_names .= $dn."\n";
243 }
244 $dns_names .="</pre>";
246 /* Lock the current entry, so nobody will edit it during deletion */
247 if (count($this->dns) == 1){
248 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
249 } else {
250 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
251 }
252 $smarty->assign("multiple", true);
253 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
254 }
255 }
258 /********************
259 Delete MULTIPLE entries confirmed
260 ********************/
262 /* Confirmation for deletion has been passed. Users should be deleted. */
263 if (isset($_POST['delete_multiple_group_confirm'])){
265 /* Remove user by user and check acls before removeing them */
266 foreach($this->dns as $key => $dn){
268 $acl = $this->ui->get_permissions($dn, "users/user");
269 if (preg_match('/d/', $acl)){
271 /* Delete request is permitted, perform LDAP action */
272 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
273 $this->usertab->set_acl_base();
274 $this->usertab->delete ();
275 gosa_log ("User object '".$dn."' has been removed");
276 unset ($this->usertab);
277 $this->usertab= NULL;
278 } else {
279 print_red (sprintf(_("You are not allowed to delete the user '%s'!"),$dn));
280 if(isset($this->ui->uid)){
281 gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
282 }
283 }
284 /* Remove lock file after successfull deletion */
285 del_lock ($dn);
286 unset($this->dns[$key]);
287 }
288 }
291 /********************
292 Delete MULTIPLE entries confirmed
293 ********************/
295 /* Confirmation for deletion has been passed. Users should be deleted. */
296 if (isset($_POST['delete_multiple_groups_confirm'])){
298 /* Remove user by user and check acls before removeing them */
299 foreach($this->dns as $key => $dn){
301 /* Load permissions for selected 'dn' and check if
302 we're allowed to remove this 'dn' */
303 $acl = $this->ui->get_permissions($this->dn,"groups/group");
304 if(preg_match("/d/",$acl)){
306 /* Delete request is permitted, perform LDAP action */
307 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
308 $this->grouptab->set_acl_base($this->dn);
309 $this->grouptab->delete ();
310 gosa_log ("Group object'".$this->dn."' has been removed");
311 unset ($this->grouptab);
312 $this->grouptab= NULL;
314 } else {
316 /* Normally this shouldn't be reached, send some extra
317 logs to notify the administrator */
318 print_red (_("You are not allowed to delete this group!"));
319 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
320 }
321 /* Remove lock file after successfull deletion */
322 del_lock ($dn);
323 unset($this->dns[$key]);
324 }
325 }
328 /********************
329 Delete MULTIPLE entries Canceled
330 ********************/
332 /* Remove lock */
333 if(isset($_POST['delete_multiple_user_cancel'])){
334 foreach($this->dns as $key => $dn){
335 del_lock ($dn);
336 unset($this->dns[$key]);
337 }
338 }
341 /********************
342 Delete group
343 ********************/
345 /* Remove group was requested */
346 if ($s_action=="del"){
348 /* Get 'dn' from posted 'uid' */
349 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
351 /* Load permissions for selected 'dn' and check if
352 we're allowed to remove this 'dn' */
353 $acl = $this->ui->get_permissions($this->dn,"groups/group");
354 if(preg_match("/d/",$acl)){
356 /* Check locking, save current plugin in 'back_plugin', so
357 the dialog knows where to return. */
358 if (($user= get_lock($this->dn)) != ""){
359 return(gen_locked_message ($user, $this->dn));
360 }
362 /* Lock the current entry, so nobody will edit it during deletion */
363 add_lock ($this->dn, $this->ui->dn);
364 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
365 $smarty->assign("multiple", false);
366 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
368 } else {
370 /* Obviously the user isn't allowed to delete. Show message and clean session. */
371 print_red (_("You are not allowed to delete this group!"));
372 }
373 }
376 /********************
377 Delete group confirmed
378 ********************/
380 /* Confirmation for deletion has been passed. Group should be deleted. */
381 if (isset($_POST['delete_group_confirm'])){
383 /* Some nice guy may send this as POST, so we've to check
384 for the permissions again. */
385 $acl = $this->ui->get_permissions($this->dn,"groups/group");
386 if(preg_match("/d/",$acl)){
388 /* Delete request is permitted, perform LDAP action */
389 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
390 $this->grouptab->set_acl_base($this->dn);
391 $this->grouptab->delete ();
392 gosa_log ("Group object'".$this->dn."' has been removed");
393 unset ($this->grouptab);
394 $this->grouptab= NULL;
396 /* Group list has changed, reload it. */
397 //$this->reload ();
399 } else {
401 /* Normally this shouldn't be reached, send some extra
402 logs to notify the administrator */
403 print_red (_("You are not allowed to delete this group!"));
404 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
405 }
407 /* Remove lock file after successfull deletion */
408 del_lock ($this->dn);
409 unset($_SESSION['objectinfo']);
410 }
413 /********************
414 Delete group canceled
415 ********************/
417 /* Delete group canceled? */
418 if (isset($_POST['delete_cancel'])){
419 del_lock ($this->dn);
420 unset($_SESSION['objectinfo']);
421 }
424 /********************
425 A dialog was canceled
426 ********************/
428 /* Cancel dialogs */
429 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
430 if(isset($this->grouptab->dn)){
431 del_lock ($this->grouptab->dn);
432 }
433 unset ($this->grouptab);
434 $this->grouptab= NULL;
435 unset($_SESSION['objectinfo']);
436 }
439 /********************
440 If there is currently a dialog open, display it
441 ********************/
443 /* Show tab dialog if object is present */
444 if (isset($this->grouptab->config)){
445 $display= $this->grouptab->execute();
447 /* Don't show buttons if tab dialog requests this */
448 if(isset($this->grouptab->by_object)){
449 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
450 $display.= "<p style=\"text-align:right\">\n";
451 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
452 $display.= " \n";
453 if ($this->dn != "new"){
454 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
455 $display.= " \n";
456 }
457 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
458 $display.= "</p>";
459 }
460 }
461 return ($display);
462 }
465 /* Check if there is a snapshot dialog open */
466 $base = $this->DivListGroup->selectedBase;
467 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
468 return($str);
469 }
471 /* Display dialog with group list */
472 $this->DivListGroup->parent = $this;
473 $this->DivListGroup->execute();
475 /* Add departments if subsearch is disabled */
476 if(!$this->DivListGroup->SubSearch){
477 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4,1);
478 }
479 $this->reload ();
480 $this->DivListGroup->setEntries($this->grouplist);
481 return($this->DivListGroup->Draw());
482 }
485 /* Return departments, that will be included within snapshot detection */
486 function get_used_snapshot_bases()
487 {
488 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
489 }
492 function list_get_selected_items()
493 {
494 $ids = array();
495 foreach($_POST as $name => $value){
496 if(preg_match("/^item_selected_[0-9]*$/",$name)){
497 $id = preg_replace("/^item_selected_/","",$name);
498 $ids[$id] = $id;
499 }
500 }
501 return($ids);
502 }
505 function reload($CreatePosixsList=false)
506 {
507 $this->grouplist = array();
508 $primaries = array();
509 $functional = array();
510 $error= $error2 = "";
511 $filter = "(objectclass=posixGroup)";
513 $base = $this->DivListGroup->selectedBase;
514 $Regex = $this->DivListGroup->Regex;
515 $UserRegex = $this->DivListGroup->UserRegex;
516 $SubSearch = $this->DivListGroup->SubSearch;
517 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
518 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
519 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
520 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
521 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
523 /* Prepare ldap class */
524 $ldap= $this->config->get_ldap_link();
525 $ldap->cd($base);
526 $ldap->set_size_limit($_SESSION['size_limit']);
529 /********************
530 Create filter depending on selected checkboxes
531 ********************/
533 /* Add application groups */
534 if ($ShowApplicationGroups){
535 $filter.= "(objectClass=gosaApplicationGroup)";
536 }
538 /* Add Mail Groups */
539 if ($ShowMailGroups){
540 $filter.= "(objectClass=gosaMailAccount)";
541 }
543 $sfilter= "";
544 if ($this->config->current['SAMBAVERSION'] == 3){
545 if (!$ShowPrimaryGroups){
546 $sfilter= "(objectClass=sambaGroupMapping)";
547 } elseif ($ShowSambaGroups){
548 $filter.= "(objectClass=sambaGroupMapping)";
549 }
550 }
552 /* Prepare filter for given Regex && UserRegex */
553 if ($filter != ""){
554 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
555 if ($UserRegex != ""){
556 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
557 }
558 }
561 /********************
562 Collect some groupids to be able to skip primary & functional groups
563 ********************/
565 /* Collect primary groupIDs to show primary groups
566 if this option is enabled in gosa conf && the checkbox is checked */
567 if ($this->ShowPrimaryCheckBox){
568 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
569 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
570 foreach ($res as $attrs){
571 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
572 }
573 }
575 /* Collect all GroupIDs from those groups which are functional.
576 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
577 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
578 if ($SubSearch){
579 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
580 } else {
581 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_NONE);
582 }
583 foreach($res as $attrs){
584 if (!isset($primaries[$attrs['gidNumber'][0]])){
585 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
586 }
587 }
590 /********************
591 Search for the prepared filter
592 ********************/
594 /* Attributes to search for */
595 $attrs = array("cn", "description", "gidNumber", "objectClass");
597 /* If subsearch is activated search for subobjects too */
598 $tmp = search_config($this->config->data,"faiManagement","CLASS");
599 if(!empty($tmp)){
600 $attrs [] = "FAIrelease";
601 }
603 if ($SubSearch){
604 $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
605 } else {
606 $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
607 }
609 /* Sort values into grouplist*/
610 $tmp = $tmp2 = array();
611 foreach ($res as $value){
612 /* Skip functional groups if checkbox isn't checked */
613 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
614 continue;
615 }
617 /* If gidNumber is in $primaries skip this entry */
618 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
619 $tmp2[$value['cn'][0]] = $value;
620 $tmp [$value['cn'][0]] = $value['cn'][0];
621 }
622 }
623 natcasesort($tmp);
624 foreach($tmp as $name){
625 $this->grouplist[] = $tmp2[$name];
626 }
627 reset ($this->grouplist);
628 }
632 /* Perform copy & paste requests
633 If copy&paste is in progress this returns a dialog to fix required attributes
634 */
635 function copyPasteHandling($s_action,$s_entry)
636 {
637 /* Paste copied/cutted object in here
638 */
639 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
640 $this->CopyPasteHandler->save_object();
641 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
642 if($str = $this->CopyPasteHandler->execute()){
643 return( $str);
644 };
645 /* Ensure that the new object is shown in the list now */
646 $this->reload();
647 }
650 /* Copy current object to CopyHandler
651 */
652 if($s_action == "copy"){
654 $dn = $this->grouplist[trim($s_entry)]['dn'];
655 $ui = get_userinfo();
656 $acl_all = $ui->has_complete_category_acls($this->DivListGroup->selectedBase,"groups") ;
657 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
659 $this->CopyPasteHandler->Clear();
660 $obj = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
661 $obj->set_acl_base($dn);
662 $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
663 $objNew->set_acl_base($dn);
665 $this->CopyPasteHandler->Copy($obj,$objNew);
666 }else{
667 print_red("You are not allowed to copy this entry.");
668 }
669 }
672 /* Copy current object to CopyHandler
673 */
674 if($s_action == "cut"){
676 $dn = $this->grouplist[trim($s_entry)]['dn'];
677 $acl_all = $ui->has_complete_category_acls($this->selectedBase,"groups") ;
678 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
680 $this->CopyPasteHandler->Clear();
681 $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
682 $obj->set_acl_base($dn);
683 $this->CopyPasteHandler->Cut($obj);
684 }else{
685 print_red("You are not allowed to cut this entry.");
686 }
687 }
688 }
690 /* Save data to object */
691 function save_object()
692 {
693 $this->DivListGroup->save_object();
694 }
697 function remove_lock()
698 {
699 if (isset($this->grouptab->dn)){
700 del_lock ($this->grouptab->dn);
701 }
702 }
705 function remove_from_parent()
706 {
707 /* Optionally execute a command after we're done */
708 $this->postremove();
709 }
712 /* Save to LDAP */
713 function save()
714 {
715 /* Optionally execute a command after we're done */
716 $this->postcreate();
717 }
719 /* Unused functions */
720 function check() { }
721 function adapt_from_template($dn) { }
722 function password_change_needed() { }
723 }
724 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
725 ?>