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 */
21 class groupManagement extends plugin
22 {
23 /* Definitions */
24 var $plHeadline= "Groups";
25 var $plDescription= "This does something";
27 /* Dialog attributes */
28 var $grouptab = NULL;
29 var $grouplist = array();
30 var $ui = NULL;
31 var $CopyPasteHandler = NULL;
32 var $DivListGroup = NULL;
33 var $ShowPrimaryCheckBox= false;
34 var $start_pasting_copied_objects = 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 }elseif(preg_match("/^multiple_copy_groups/",$key)){
112 $s_action = "copy_multiple";
113 }elseif(preg_match("/^multiple_cut_groups/",$key)){
114 $s_action = "cut_multiple";
115 }
116 }
117 $s_entry = preg_replace("/_.$/","",$s_entry);
119 /* Check for posted gets */
120 if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
121 $s_entry = $_GET['id'];
122 $s_action = "edit";
123 }
125 /* handle C&P from layers menu */
126 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
127 $s_action = "copy_multiple";
128 }
129 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
130 $s_action = "cut_multiple";
131 }
132 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
133 $s_action = "editPaste";
134 }
136 /* Create options */
137 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "group_new"){
138 $s_action = "new";
139 }
141 /* handle remove from layers menu */
142 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
143 $s_action = "del_multiple";
144 }
148 $smarty= get_smarty();
150 /********************
151 Copy & Paste Handling ...
152 ********************/
154 /* Display the copy & paste dialog, if it is currently open */
155 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
156 if($ret){
157 return($ret);
158 }
161 /********************
162 Create a new group ...
163 ********************/
165 /* New group? */
166 if ($s_action=="new"){
168 /* Check create permissions */
169 $acl = $this->ui->get_permissions($this->DivListGroup->selectedBase,"groups/group");
170 if(preg_match("/c/",$acl)){
172 /* By default we set 'dn' to 'new', all relevant plugins will
173 react on this. */
174 $this->dn= "new";
176 /* Create new usertab object */
177 $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
179 /* Set up the group ACL's for this 'dn' */
180 $this->grouptab->set_acl_base($this->DivListGroup->selectedBase);
181 }
182 }
185 /********************
186 Save Group Tab/Object Changes
187 ********************/
189 /* Finish group edit is triggered by the tabulator dialog, so
190 the user wants to save edited data. Check and save at this
191 point. */
192 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->grouptab->config)) ){
194 /* Check tabs, will feed message array
195 Save, or display error message? */
196 $message= $this->grouptab->check();
197 if (count($message) == 0){
199 /* Save user data to ldap */
200 $this->grouptab->save();
202 if (!isset($_POST['edit_apply'])){
203 /* Group has been saved successfully, remove lock from LDAP. */
204 if ($this->dn != "new"){
205 del_lock ($this->dn);
206 }
208 /* There's no page reload so we have to read new groups at this point. */
209 //$this->reload ();
210 unset ($this->grouptab);
211 $this->grouptab= NULL;
212 unset ($_SESSION['objectinfo']);
213 }
214 } else {
215 /* Ok. There seem to be errors regarding to the tab data,
216 show message and continue as usual. */
217 show_errors($message);
218 }
219 }
222 /********************
223 Edit existing group
224 ********************/
226 /* User wants to edit data? */
227 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
229 /* Get 'dn' from posted 'uid', must be unique */
230 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
232 /* Check locking & lock entry if required */
233 $user = get_lock($this->dn);
234 if ($user != ""){
235 return(gen_locked_message ($user, $this->dn));
236 }
237 add_lock ($this->dn, $this->ui->dn);
239 /* Register grouptab to trigger edit dialog */
240 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
241 $this->grouptab->set_acl_base($this->dn);
242 $_SESSION['objectinfo']= $this->dn;
243 }
246 /********************
247 Delete MULTIPLE entries requested, display confirm dialog
248 ********************/
250 if ($s_action=="del_multiple"){
251 $ids = $this->list_get_selected_items();
253 if(count($ids)){
255 foreach($ids as $id){
256 $dn = $this->grouplist[$id]['dn'];
257 if (($user= get_lock($dn)) != ""){
258 return(gen_locked_message ($user, $dn));
259 }
260 $this->dns[$id] = $dn;
261 }
263 $dns_names = "<br><pre>";
264 foreach($this->dns as $dn){
265 add_lock ($dn, $this->ui->dn);
266 $dns_names .= $dn."\n";
267 }
268 $dns_names .="</pre>";
270 /* Lock the current entry, so nobody will edit it during deletion */
271 if (count($this->dns) == 1){
272 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
273 } else {
274 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
275 }
276 $smarty->assign("multiple", true);
277 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
278 }
279 }
282 /********************
283 Delete MULTIPLE entries confirmed
284 ********************/
286 /* Confirmation for deletion has been passed. Groups should be deleted. */
287 if (isset($_POST['delete_multiple_groups_confirm'])){
289 /* Remove user by user and check acls before removeing them */
290 foreach($this->dns as $key => $dn){
292 /* Load permissions for selected 'dn' and check if
293 we're allowed to remove this 'dn' */
294 $acl = $this->ui->get_permissions($dn,"groups/group");
295 if(preg_match("/d/",$acl)){
297 /* Delete request is permitted, perform LDAP action */
298 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
299 $this->grouptab->set_acl_base($dn);
300 $this->grouptab->delete ();
301 unset ($this->grouptab);
302 $this->grouptab= NULL;
304 } else {
306 /* Normally this shouldn't be reached, send some extra
307 logs to notify the administrator */
308 print_red (_("You are not allowed to delete this group!"));
309 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
310 }
311 /* Remove lock file after successfull deletion */
312 del_lock ($dn);
313 unset($this->dns[$key]);
314 }
315 }
318 /********************
319 Delete MULTIPLE entries Canceled
320 ********************/
322 /* Remove lock */
323 if(isset($_POST['delete_multiple_user_cancel'])){
324 foreach($this->dns as $key => $dn){
325 del_lock ($dn);
326 unset($this->dns[$key]);
327 }
328 }
331 /********************
332 Delete group
333 ********************/
335 /* Remove group was requested */
336 if ($s_action=="del"){
338 /* Get 'dn' from posted 'uid' */
339 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
341 /* Load permissions for selected 'dn' and check if
342 we're allowed to remove this 'dn' */
343 $acl = $this->ui->get_permissions($this->dn,"groups/group");
344 if(preg_match("/d/",$acl)){
346 /* Check locking, save current plugin in 'back_plugin', so
347 the dialog knows where to return. */
348 if (($user= get_lock($this->dn)) != ""){
349 return(gen_locked_message ($user, $this->dn));
350 }
352 /* Lock the current entry, so nobody will edit it during deletion */
353 add_lock ($this->dn, $this->ui->dn);
354 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
355 $smarty->assign("multiple", false);
356 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
358 } else {
360 /* Obviously the user isn't allowed to delete. Show message and clean session. */
361 print_red (_("You are not allowed to delete this group!"));
362 }
363 }
366 /********************
367 Delete group confirmed
368 ********************/
370 /* Confirmation for deletion has been passed. Group should be deleted. */
371 if (isset($_POST['delete_group_confirm'])){
373 /* Some nice guy may send this as POST, so we've to check
374 for the permissions again. */
375 $acl = $this->ui->get_permissions($this->dn,"groups/group");
376 if(preg_match("/d/",$acl)){
378 /* Delete request is permitted, perform LDAP action */
379 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
380 $this->grouptab->set_acl_base($this->dn);
381 $this->grouptab->delete ();
382 unset ($this->grouptab);
383 $this->grouptab= NULL;
385 /* Group list has changed, reload it. */
386 //$this->reload ();
388 } else {
390 /* Normally this shouldn't be reached, send some extra
391 logs to notify the administrator */
392 print_red (_("You are not allowed to delete this group!"));
393 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
394 }
396 /* Remove lock file after successfull deletion */
397 del_lock ($this->dn);
398 unset($_SESSION['objectinfo']);
399 }
402 /********************
403 Delete group canceled
404 ********************/
406 /* Delete group canceled? */
407 if (isset($_POST['delete_cancel'])){
408 del_lock ($this->dn);
409 unset($_SESSION['objectinfo']);
410 }
413 /********************
414 A dialog was canceled
415 ********************/
417 /* Cancel dialogs */
418 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
419 if(isset($this->grouptab->dn)){
420 del_lock ($this->grouptab->dn);
421 }
422 unset ($this->grouptab);
423 $this->grouptab= NULL;
424 unset($_SESSION['objectinfo']);
425 }
428 /********************
429 If there is currently a dialog open, display it
430 ********************/
432 /* Show tab dialog if object is present */
433 if (isset($this->grouptab->config)){
434 $display= $this->grouptab->execute();
436 /* Don't show buttons if tab dialog requests this */
437 if(isset($this->grouptab->by_object)){
438 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
439 $display.= "<p style=\"text-align:right\">\n";
440 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
441 $display.= " \n";
442 if ($this->dn != "new"){
443 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
444 $display.= " \n";
445 }
446 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
447 $display.= "</p>";
448 }
449 }
450 return ($display);
451 }
454 /* Check if there is a snapshot dialog open */
455 $base = $this->DivListGroup->selectedBase;
456 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
457 return($str);
458 }
460 /* Display dialog with group list */
461 $this->DivListGroup->parent = $this;
462 $this->DivListGroup->execute();
464 /* Add departments if subsearch is disabled */
465 if(!$this->DivListGroup->SubSearch){
466 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4,1);
467 }
468 $this->reload ();
469 $this->DivListGroup->setEntries($this->grouplist);
470 return($this->DivListGroup->Draw());
471 }
474 /* Return departments, that will be included within snapshot detection */
475 function get_used_snapshot_bases()
476 {
477 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
478 }
481 function list_get_selected_items()
482 {
483 $ids = array();
484 foreach($_POST as $name => $value){
485 if(preg_match("/^item_selected_[0-9]*$/",$name)){
486 $id = preg_replace("/^item_selected_/","",$name);
487 $ids[$id] = $id;
488 }
489 }
490 return($ids);
491 }
494 function reload($CreatePosixsList=false)
495 {
496 $this->grouplist = array();
497 $primaries = array();
498 $functional = array();
499 $error= $error2 = "";
500 $filter = "(objectclass=posixGroup)";
502 $base = $this->DivListGroup->selectedBase;
503 $Regex = $this->DivListGroup->Regex;
504 $UserRegex = $this->DivListGroup->UserRegex;
505 $SubSearch = $this->DivListGroup->SubSearch;
506 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
507 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
508 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
509 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
510 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
512 /* Prepare ldap class */
513 $ldap= $this->config->get_ldap_link();
514 $ldap->cd($base);
515 $ldap->set_size_limit($_SESSION['size_limit']);
518 /********************
519 Create filter depending on selected checkboxes
520 ********************/
522 /* Add application groups */
523 if ($ShowApplicationGroups){
524 $filter.= "(objectClass=gosaApplicationGroup)";
525 }
527 /* Add Mail Groups */
528 if ($ShowMailGroups){
529 $filter.= "(objectClass=gosaMailAccount)";
530 }
532 $sfilter= "";
533 if ($this->config->current['SAMBAVERSION'] == 3){
534 if (!$ShowPrimaryGroups){
535 $sfilter= "(objectClass=sambaGroupMapping)";
536 } elseif ($ShowSambaGroups){
537 $filter.= "(objectClass=sambaGroupMapping)";
538 }
539 }
541 /* Prepare filter for given Regex && UserRegex */
542 if ($filter != ""){
543 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
544 if ($UserRegex != ""){
545 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
546 }
547 }
550 /********************
551 Collect some groupids to be able to skip primary & functional groups
552 ********************/
554 /* Collect primary groupIDs to show primary groups
555 if this option is enabled in gosa conf && the checkbox is checked */
556 if ($this->ShowPrimaryCheckBox){
557 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
558 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
559 foreach ($res as $attrs){
560 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
561 }
562 }
564 /* Collect all GroupIDs from those groups which are functional.
565 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
566 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
567 if ($SubSearch){
568 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
569 } else {
570 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_NONE);
571 }
572 foreach($res as $attrs){
573 if (!isset($primaries[$attrs['gidNumber'][0]])){
574 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
575 }
576 }
579 /********************
580 Search for the prepared filter
581 ********************/
583 /* Attributes to search for */
584 $attrs = array("cn", "description", "gidNumber", "objectClass");
586 /* If subsearch is activated search for subobjects too */
587 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
588 if(!empty($tmp)){
589 $attrs [] = "FAIrelease";
590 }
592 if ($SubSearch){
593 $res= get_sub_list($filter, "groups",get_groups_ou(), $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
594 } else {
595 $res= get_sub_list($filter, "groups",get_groups_ou(), get_groups_ou().$base, $attrs, GL_SIZELIMIT);
596 }
598 /* Sort values into grouplist*/
599 $tmp = $tmp2 = array();
600 foreach ($res as $value){
601 /* Skip functional groups if checkbox isn't checked */
602 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
603 continue;
604 }
606 /* If gidNumber is in $primaries skip this entry */
607 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
608 $tmp2[$value['cn'][0]] = $value;
609 $tmp [$value['cn'][0]] = $value['cn'][0];
610 }
611 }
612 natcasesort($tmp);
613 foreach($tmp as $name){
614 $this->grouplist[] = $tmp2[$name];
615 }
616 reset ($this->grouplist);
617 }
620 function copyPasteHandling_from_queue($s_action,$s_entry)
621 {
622 /* Check if Copy & Paste is disabled */
623 if(!is_object($this->CopyPasteHandler)){
624 return("");
625 }
627 /* Add a single entry to queue */
628 if($s_action == "cut" || $s_action == "copy"){
630 /* Cleanup object queue */
631 $this->CopyPasteHandler->cleanup_queue();
632 $dn = $this->grouplist[$s_entry]['dn'];
633 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"grouptabs","GROUPTABS","groups");
634 }
637 /* Add entries to queue */
638 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
640 /* Cleanup object queue */
641 $this->CopyPasteHandler->cleanup_queue();
643 /* Add new entries to CP queue */
644 foreach($this->list_get_selected_items() as $id){
645 $dn = $this->grouplist[$id]['dn'];
647 if($s_action == "copy_multiple"){
648 $this->CopyPasteHandler->add_to_queue($dn,"copy","grouptabs","GROUPTABS","groups");
649 }
650 if($s_action == "cut_multiple"){
651 $this->CopyPasteHandler->add_to_queue($dn,"cut","grouptabs","GROUPTABS","groups");
652 }
653 }
654 }
656 /* Start pasting entries */
657 if($s_action == "editPaste"){
658 $this->start_pasting_copied_objects = TRUE;
659 }
661 /* Return C&P dialog */
662 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
664 /* Load entry from queue and set base */
665 $this->CopyPasteHandler->load_entry_from_queue();
666 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
668 /* Get dialog */
669 $data = $this->CopyPasteHandler->execute();
671 /* Return dialog data */
672 if(!empty($data)){
673 return($data);
674 }
675 }
677 /* Automatically disable status for pasting */
678 if(!$this->CopyPasteHandler->entries_queued()){
679 $this->start_pasting_copied_objects = FALSE;
680 }
681 return("");
682 }
685 /* Save data to object */
686 function save_object()
687 {
688 $this->DivListGroup->save_object();
689 }
692 function remove_lock()
693 {
694 if (isset($this->grouptab->dn)){
695 del_lock ($this->grouptab->dn);
696 }
697 }
700 function remove_from_parent()
701 {
702 /* Optionally execute a command after we're done */
703 $this->postremove();
704 }
707 /* Save to LDAP */
708 function save()
709 {
710 /* Optionally execute a command after we're done */
711 $this->postcreate();
712 }
714 /* Unused functions */
715 function check() { }
716 function adapt_from_template($dn) { }
717 function password_change_needed() { }
718 }
719 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
720 ?>