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;
35 var $start_pasting_copied_objects = FALSE;
37 function groupManagement ($config, $ui)
38 {
39 /* Save configuration for internal use */
40 $this->config = $config;
41 $this->ui = $ui;
43 /* Copy & Paste enabled ?*/
44 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
45 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
46 }
48 /* Detect if we have to display the primary group checkbox */
49 $tmp = "";
50 if (isset($this->config->data['MAIN']['NOPRIMARYGROUP'])){
51 $tmp = $this->config->data['MAIN']['NOPRIMARYGROUP'];
52 }
53 if(preg_match("/true/i",$tmp)|| (preg_match("/yes/",$tmp))){
54 $this->ShowPrimaryCheckBox = false;
55 } else {
56 $this->ShowPrimaryCheckBox = true;
57 }
59 /* Create dialog object */
60 $this->DivListGroup = new divListGroup($this->config,$this);
61 $this->DivListGroup->DisableCheckBox("ShowPrimaryGroups",$this->ShowPrimaryCheckBox);
62 }
65 function execute()
66 {
67 /* Call parent execute */
68 plugin::execute();
70 /* Store these posts if the current object is locked (used by somebody else)*/
71 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/","/^item_selected/","/^remove_multiple_groups/");
73 /* Save data */
74 $s_action = "";
75 $s_entry = "";
77 /* Test Posts */
78 foreach($_POST as $key => $val){
79 // Post for delete
80 if(preg_match("/^group_del.*/",$key)){
81 $s_action = "del";
82 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
83 // Post for edit
84 }elseif(preg_match("/^group_edit_.*/",$key)){
85 $s_action="edit";
86 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
87 // Post for new
88 }elseif(preg_match("/^group_new.*/",$key)){
89 $s_action="new";
90 }elseif(preg_match("/^dep_home.*/i",$key)){
91 $s_action="home";
92 }elseif(preg_match("/^group_tplnew.*/i",$key)){
93 $s_action="new_tpl";
94 }elseif(preg_match("/^group_chgpw.*/i",$key)){
95 $s_action="change_pw";
96 $s_entry = preg_replace("/group_chgpw_/i","",$key);
97 }elseif(preg_match("/_group_edit_/",$key)){
98 $type = preg_replace("/_group_edit_.*$/","",$key);
99 $s_action="edit";
100 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
101 $_POST['arg'] = $type;
102 }elseif(preg_match("/^editPaste.*/i",$key)){
103 $s_action="editPaste";
104 }elseif(preg_match("/^copy_.*/",$key)){
105 $s_action="copy";
106 $s_entry = preg_replace("/^copy_/i","",$key);
107 }elseif(preg_match("/^cut_.*/",$key)){
108 $s_action="cut";
109 $s_entry = preg_replace("/^cut_/i","",$key);
110 }elseif(preg_match("/^remove_multiple_groups/",$key)){
111 $s_action="del_multiple";
112 }elseif(preg_match("/^multiple_copy_groups/",$key)){
113 $s_action = "copy_multiple";
114 }elseif(preg_match("/^multiple_cut_groups/",$key)){
115 $s_action = "cut_multiple";
116 }
117 }
118 $s_entry = preg_replace("/_.$/","",$s_entry);
120 /* Check for posted gets */
121 if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
122 $s_entry = $_GET['id'];
123 $s_action = "edit";
124 }
126 $smarty= get_smarty();
128 /********************
129 Copy & Paste Handling ...
130 ********************/
132 /* Display the copy & paste dialog, if it is currently open */
133 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
134 if($ret){
135 return($ret);
136 }
139 /********************
140 Create a new group ...
141 ********************/
143 /* New group? */
144 if ($s_action=="new"){
146 /* Check create permissions */
147 $acl = $this->ui->get_permissions($this->DivListGroup->selectedBase,"groups/group");
148 if(preg_match("/c/",$acl)){
150 /* By default we set 'dn' to 'new', all relevant plugins will
151 react on this. */
152 $this->dn= "new";
154 /* Create new usertab object */
155 $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
157 /* Set up the group ACL's for this 'dn' */
158 $this->grouptab->set_acl_base($this->DivListGroup->selectedBase);
159 }
160 }
163 /********************
164 Save Group Tab/Object Changes
165 ********************/
167 /* Finish group edit is triggered by the tabulator dialog, so
168 the user wants to save edited data. Check and save at this
169 point. */
170 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->grouptab->config)) ){
172 /* Check tabs, will feed message array
173 Save, or display error message? */
174 $message= $this->grouptab->check();
175 if (count($message) == 0){
177 /* Save user data to ldap */
178 $this->grouptab->save();
180 if (!isset($_POST['edit_apply'])){
181 /* Group has been saved successfully, remove lock from LDAP. */
182 if ($this->dn != "new"){
183 del_lock ($this->dn);
184 }
186 /* There's no page reload so we have to read new groups at this point. */
187 //$this->reload ();
188 unset ($this->grouptab);
189 $this->grouptab= NULL;
190 unset ($_SESSION['objectinfo']);
191 }
192 } else {
193 /* Ok. There seem to be errors regarding to the tab data,
194 show message and continue as usual. */
195 show_errors($message);
196 }
197 }
200 /********************
201 Edit existing group
202 ********************/
204 /* User wants to edit data? */
205 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
207 /* Get 'dn' from posted 'uid', must be unique */
208 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
210 /* Check locking & lock entry if required */
211 $user = get_lock($this->dn);
212 if ($user != ""){
213 return(gen_locked_message ($user, $this->dn));
214 }
215 add_lock ($this->dn, $this->ui->dn);
217 /* Register grouptab to trigger edit dialog */
218 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
219 $this->grouptab->set_acl_base($this->dn);
220 $_SESSION['objectinfo']= $this->dn;
221 }
224 /********************
225 Delete MULTIPLE entries requested, display confirm dialog
226 ********************/
228 if ($s_action=="del_multiple"){
229 $ids = $this->list_get_selected_items();
231 if(count($ids)){
233 foreach($ids as $id){
234 $dn = $this->grouplist[$id]['dn'];
235 if (($user= get_lock($dn)) != ""){
236 return(gen_locked_message ($user, $dn));
237 }
238 $this->dns[$id] = $dn;
239 }
241 $dns_names = "<br><pre>";
242 foreach($this->dns as $dn){
243 add_lock ($dn, $this->ui->dn);
244 $dns_names .= $dn."\n";
245 }
246 $dns_names .="</pre>";
248 /* Lock the current entry, so nobody will edit it during deletion */
249 if (count($this->dns) == 1){
250 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
251 } else {
252 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
253 }
254 $smarty->assign("multiple", true);
255 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
256 }
257 }
260 /********************
261 Delete MULTIPLE entries confirmed
262 ********************/
264 /* Confirmation for deletion has been passed. Groups should be deleted. */
265 if (isset($_POST['delete_multiple_groups_confirm'])){
267 /* Remove user by user and check acls before removeing them */
268 foreach($this->dns as $key => $dn){
270 /* Load permissions for selected 'dn' and check if
271 we're allowed to remove this 'dn' */
272 $acl = $this->ui->get_permissions($this->dn,"groups/group");
273 if(preg_match("/d/",$acl)){
275 /* Delete request is permitted, perform LDAP action */
276 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
277 $this->grouptab->set_acl_base($this->dn);
278 $this->grouptab->delete ();
279 unset ($this->grouptab);
280 $this->grouptab= NULL;
282 } else {
284 /* Normally this shouldn't be reached, send some extra
285 logs to notify the administrator */
286 print_red (_("You are not allowed to delete this group!"));
287 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
288 }
289 /* Remove lock file after successfull deletion */
290 del_lock ($dn);
291 unset($this->dns[$key]);
292 }
293 }
296 /********************
297 Delete MULTIPLE entries Canceled
298 ********************/
300 /* Remove lock */
301 if(isset($_POST['delete_multiple_user_cancel'])){
302 foreach($this->dns as $key => $dn){
303 del_lock ($dn);
304 unset($this->dns[$key]);
305 }
306 }
309 /********************
310 Delete group
311 ********************/
313 /* Remove group was requested */
314 if ($s_action=="del"){
316 /* Get 'dn' from posted 'uid' */
317 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
319 /* Load permissions for selected 'dn' and check if
320 we're allowed to remove this 'dn' */
321 $acl = $this->ui->get_permissions($this->dn,"groups/group");
322 if(preg_match("/d/",$acl)){
324 /* Check locking, save current plugin in 'back_plugin', so
325 the dialog knows where to return. */
326 if (($user= get_lock($this->dn)) != ""){
327 return(gen_locked_message ($user, $this->dn));
328 }
330 /* Lock the current entry, so nobody will edit it during deletion */
331 add_lock ($this->dn, $this->ui->dn);
332 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
333 $smarty->assign("multiple", false);
334 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
336 } else {
338 /* Obviously the user isn't allowed to delete. Show message and clean session. */
339 print_red (_("You are not allowed to delete this group!"));
340 }
341 }
344 /********************
345 Delete group confirmed
346 ********************/
348 /* Confirmation for deletion has been passed. Group should be deleted. */
349 if (isset($_POST['delete_group_confirm'])){
351 /* Some nice guy may send this as POST, so we've to check
352 for the permissions again. */
353 $acl = $this->ui->get_permissions($this->dn,"groups/group");
354 if(preg_match("/d/",$acl)){
356 /* Delete request is permitted, perform LDAP action */
357 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
358 $this->grouptab->set_acl_base($this->dn);
359 $this->grouptab->delete ();
360 unset ($this->grouptab);
361 $this->grouptab= NULL;
363 /* Group list has changed, reload it. */
364 //$this->reload ();
366 } else {
368 /* Normally this shouldn't be reached, send some extra
369 logs to notify the administrator */
370 print_red (_("You are not allowed to delete this group!"));
371 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
372 }
374 /* Remove lock file after successfull deletion */
375 del_lock ($this->dn);
376 unset($_SESSION['objectinfo']);
377 }
380 /********************
381 Delete group canceled
382 ********************/
384 /* Delete group canceled? */
385 if (isset($_POST['delete_cancel'])){
386 del_lock ($this->dn);
387 unset($_SESSION['objectinfo']);
388 }
391 /********************
392 A dialog was canceled
393 ********************/
395 /* Cancel dialogs */
396 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
397 if(isset($this->grouptab->dn)){
398 del_lock ($this->grouptab->dn);
399 }
400 unset ($this->grouptab);
401 $this->grouptab= NULL;
402 unset($_SESSION['objectinfo']);
403 }
406 /********************
407 If there is currently a dialog open, display it
408 ********************/
410 /* Show tab dialog if object is present */
411 if (isset($this->grouptab->config)){
412 $display= $this->grouptab->execute();
414 /* Don't show buttons if tab dialog requests this */
415 if(isset($this->grouptab->by_object)){
416 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
417 $display.= "<p style=\"text-align:right\">\n";
418 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
419 $display.= " \n";
420 if ($this->dn != "new"){
421 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
422 $display.= " \n";
423 }
424 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
425 $display.= "</p>";
426 }
427 }
428 return ($display);
429 }
432 /* Check if there is a snapshot dialog open */
433 $base = $this->DivListGroup->selectedBase;
434 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
435 return($str);
436 }
438 /* Display dialog with group list */
439 $this->DivListGroup->parent = $this;
440 $this->DivListGroup->execute();
442 /* Add departments if subsearch is disabled */
443 if(!$this->DivListGroup->SubSearch){
444 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4,1);
445 }
446 $this->reload ();
447 $this->DivListGroup->setEntries($this->grouplist);
448 return($this->DivListGroup->Draw());
449 }
452 /* Return departments, that will be included within snapshot detection */
453 function get_used_snapshot_bases()
454 {
455 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
456 }
459 function list_get_selected_items()
460 {
461 $ids = array();
462 foreach($_POST as $name => $value){
463 if(preg_match("/^item_selected_[0-9]*$/",$name)){
464 $id = preg_replace("/^item_selected_/","",$name);
465 $ids[$id] = $id;
466 }
467 }
468 return($ids);
469 }
472 function reload($CreatePosixsList=false)
473 {
474 $this->grouplist = array();
475 $primaries = array();
476 $functional = array();
477 $error= $error2 = "";
478 $filter = "(objectclass=posixGroup)";
480 $base = $this->DivListGroup->selectedBase;
481 $Regex = $this->DivListGroup->Regex;
482 $UserRegex = $this->DivListGroup->UserRegex;
483 $SubSearch = $this->DivListGroup->SubSearch;
484 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
485 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
486 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
487 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
488 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
490 /* Prepare ldap class */
491 $ldap= $this->config->get_ldap_link();
492 $ldap->cd($base);
493 $ldap->set_size_limit($_SESSION['size_limit']);
496 /********************
497 Create filter depending on selected checkboxes
498 ********************/
500 /* Add application groups */
501 if ($ShowApplicationGroups){
502 $filter.= "(objectClass=gosaApplicationGroup)";
503 }
505 /* Add Mail Groups */
506 if ($ShowMailGroups){
507 $filter.= "(objectClass=gosaMailAccount)";
508 }
510 $sfilter= "";
511 if ($this->config->current['SAMBAVERSION'] == 3){
512 if (!$ShowPrimaryGroups){
513 $sfilter= "(objectClass=sambaGroupMapping)";
514 } elseif ($ShowSambaGroups){
515 $filter.= "(objectClass=sambaGroupMapping)";
516 }
517 }
519 /* Prepare filter for given Regex && UserRegex */
520 if ($filter != ""){
521 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
522 if ($UserRegex != ""){
523 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
524 }
525 }
528 /********************
529 Collect some groupids to be able to skip primary & functional groups
530 ********************/
532 /* Collect primary groupIDs to show primary groups
533 if this option is enabled in gosa conf && the checkbox is checked */
534 if ($this->ShowPrimaryCheckBox){
535 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
536 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
537 foreach ($res as $attrs){
538 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
539 }
540 }
542 /* Collect all GroupIDs from those groups which are functional.
543 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
544 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
545 if ($SubSearch){
546 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
547 } else {
548 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_NONE);
549 }
550 foreach($res as $attrs){
551 if (!isset($primaries[$attrs['gidNumber'][0]])){
552 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
553 }
554 }
557 /********************
558 Search for the prepared filter
559 ********************/
561 /* Attributes to search for */
562 $attrs = array("cn", "description", "gidNumber", "objectClass");
564 /* If subsearch is activated search for subobjects too */
565 $tmp = search_config($this->config->data,"faiManagement","CLASS");
566 if(!empty($tmp)){
567 $attrs [] = "FAIrelease";
568 }
570 if ($SubSearch){
571 $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
572 } else {
573 $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
574 }
576 /* Sort values into grouplist*/
577 $tmp = $tmp2 = array();
578 foreach ($res as $value){
579 /* Skip functional groups if checkbox isn't checked */
580 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
581 continue;
582 }
584 /* If gidNumber is in $primaries skip this entry */
585 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
586 $tmp2[$value['cn'][0]] = $value;
587 $tmp [$value['cn'][0]] = $value['cn'][0];
588 }
589 }
590 natcasesort($tmp);
591 foreach($tmp as $name){
592 $this->grouplist[] = $tmp2[$name];
593 }
594 reset ($this->grouplist);
595 }
598 function copyPasteHandling_from_queue($s_action,$s_entry)
599 {
600 /* Check if Copy & Paste is disabled */
601 if(!is_object($this->CopyPasteHandler)){
602 return("");
603 }
605 /* Add a single entry to queue */
606 if($s_action == "cut" || $s_action == "copy"){
608 /* Cleanup object queue */
609 $this->CopyPasteHandler->cleanup_queue();
610 $dn = $this->grouplist[$s_entry]['dn'];
611 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"grouptabs","GROUPTABS","groups");
612 }
615 /* Add entries to queue */
616 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
618 /* Cleanup object queue */
619 $this->CopyPasteHandler->cleanup_queue();
621 /* Add new entries to CP queue */
622 foreach($this->list_get_selected_items() as $id){
623 $dn = $this->grouplist[$id]['dn'];
625 if($s_action == "copy_multiple"){
626 $this->CopyPasteHandler->add_to_queue($dn,"copy","grouptabs","GROUPTABS","groups");
627 }
628 if($s_action == "cut_multiple"){
629 $this->CopyPasteHandler->add_to_queue($dn,"cut","grouptabs","GROUPTABS","groups");
630 }
631 }
632 }
634 /* Start pasting entries */
635 if($s_action == "editPaste"){
636 $this->start_pasting_copied_objects = TRUE;
637 }
639 /* Return C&P dialog */
640 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
642 /* Load entry from queue and set base */
643 $this->CopyPasteHandler->load_entry_from_queue();
644 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
646 /* Get dialog */
647 $data = $this->CopyPasteHandler->execute();
649 /* Return dialog data */
650 if(!empty($data)){
651 return($data);
652 }
653 }
655 /* Automatically disable status for pasting */
656 if(!$this->CopyPasteHandler->entries_queued()){
657 $this->start_pasting_copied_objects = FALSE;
658 }
659 return("");
660 }
663 /* Save data to object */
664 function save_object()
665 {
666 $this->DivListGroup->save_object();
667 }
670 function remove_lock()
671 {
672 if (isset($this->grouptab->dn)){
673 del_lock ($this->grouptab->dn);
674 }
675 }
678 function remove_from_parent()
679 {
680 /* Optionally execute a command after we're done */
681 $this->postremove();
682 }
685 /* Save to LDAP */
686 function save()
687 {
688 /* Optionally execute a command after we're done */
689 $this->postcreate();
690 }
692 /* Unused functions */
693 function check() { }
694 function adapt_from_template($dn) { }
695 function password_change_needed() { }
696 }
697 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
698 ?>