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 group 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();
177 if (!isset($_POST['edit_apply'])){
178 /* Group has been saved successfully, remove lock from LDAP. */
179 if ($this->dn != "new"){
180 del_lock ($this->dn);
181 }
183 /* There's no page reload so we have to read new groups at this point. */
184 //$this->reload ();
185 unset ($this->grouptab);
186 $this->grouptab= NULL;
187 unset ($_SESSION['objectinfo']);
188 }
189 } else {
190 /* Ok. There seem to be errors regarding to the tab data,
191 show message and continue as usual. */
192 show_errors($message);
193 }
194 }
197 /********************
198 Edit existing group
199 ********************/
201 /* User wants to edit data? */
202 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
204 /* Get 'dn' from posted 'uid', must be unique */
205 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
207 /* Check locking & lock entry if required */
208 $user = get_lock($this->dn);
209 if ($user != ""){
210 return(gen_locked_message ($user, $this->dn));
211 }
212 add_lock ($this->dn, $this->ui->dn);
214 /* Register grouptab to trigger edit dialog */
215 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
216 $this->grouptab->set_acl_base($this->dn);
217 $_SESSION['objectinfo']= $this->dn;
218 }
221 /********************
222 Delete MULTIPLE entries requested, display confirm dialog
223 ********************/
225 if ($s_action=="del_multiple"){
226 $ids = $this->list_get_selected_items();
228 if(count($ids)){
230 foreach($ids as $id){
231 $dn = $this->grouplist[$id]['dn'];
232 if (($user= get_lock($dn)) != ""){
233 return(gen_locked_message ($user, $dn));
234 }
235 $this->dns[$id] = $dn;
236 }
238 $dns_names = "<br><pre>";
239 foreach($this->dns as $dn){
240 add_lock ($dn, $this->ui->dn);
241 $dns_names .= $dn."\n";
242 }
243 $dns_names .="</pre>";
245 /* Lock the current entry, so nobody will edit it during deletion */
246 if (count($this->dns) == 1){
247 $smarty->assign("info", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
248 } else {
249 $smarty->assign("info", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
250 }
251 $smarty->assign("multiple", true);
252 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
253 }
254 }
257 /********************
258 Delete MULTIPLE entries confirmed
259 ********************/
261 /* Confirmation for deletion has been passed. Groups should be deleted. */
262 if (isset($_POST['delete_multiple_groups_confirm'])){
264 /* Remove user by user and check acls before removeing them */
265 foreach($this->dns as $key => $dn){
267 /* Load permissions for selected 'dn' and check if
268 we're allowed to remove this 'dn' */
269 $acl = $this->ui->get_permissions($this->dn,"groups/group");
270 if(preg_match("/d/",$acl)){
272 /* Delete request is permitted, perform LDAP action */
273 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
274 $this->grouptab->set_acl_base($this->dn);
275 $this->grouptab->delete ();
276 unset ($this->grouptab);
277 $this->grouptab= NULL;
279 } else {
281 /* Normally this shouldn't be reached, send some extra
282 logs to notify the administrator */
283 print_red (_("You are not allowed to delete this group!"));
284 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
285 }
286 /* Remove lock file after successfull deletion */
287 del_lock ($dn);
288 unset($this->dns[$key]);
289 }
290 }
293 /********************
294 Delete MULTIPLE entries Canceled
295 ********************/
297 /* Remove lock */
298 if(isset($_POST['delete_multiple_user_cancel'])){
299 foreach($this->dns as $key => $dn){
300 del_lock ($dn);
301 unset($this->dns[$key]);
302 }
303 }
306 /********************
307 Delete group
308 ********************/
310 /* Remove group was requested */
311 if ($s_action=="del"){
313 /* Get 'dn' from posted 'uid' */
314 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
316 /* Load permissions for selected 'dn' and check if
317 we're allowed to remove this 'dn' */
318 $acl = $this->ui->get_permissions($this->dn,"groups/group");
319 if(preg_match("/d/",$acl)){
321 /* Check locking, save current plugin in 'back_plugin', so
322 the dialog knows where to return. */
323 if (($user= get_lock($this->dn)) != ""){
324 return(gen_locked_message ($user, $this->dn));
325 }
327 /* Lock the current entry, so nobody will edit it during deletion */
328 add_lock ($this->dn, $this->ui->dn);
329 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
330 $smarty->assign("multiple", false);
331 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
333 } else {
335 /* Obviously the user isn't allowed to delete. Show message and clean session. */
336 print_red (_("You are not allowed to delete this group!"));
337 }
338 }
341 /********************
342 Delete group confirmed
343 ********************/
345 /* Confirmation for deletion has been passed. Group should be deleted. */
346 if (isset($_POST['delete_group_confirm'])){
348 /* Some nice guy may send this as POST, so we've to check
349 for the permissions again. */
350 $acl = $this->ui->get_permissions($this->dn,"groups/group");
351 if(preg_match("/d/",$acl)){
353 /* Delete request is permitted, perform LDAP action */
354 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
355 $this->grouptab->set_acl_base($this->dn);
356 $this->grouptab->delete ();
357 unset ($this->grouptab);
358 $this->grouptab= NULL;
360 /* Group list has changed, reload it. */
361 //$this->reload ();
363 } else {
365 /* Normally this shouldn't be reached, send some extra
366 logs to notify the administrator */
367 print_red (_("You are not allowed to delete this group!"));
368 new log("security","groups/".get_class($this),$dn,array(),"Tried to trick deletion.");
369 }
371 /* Remove lock file after successfull deletion */
372 del_lock ($this->dn);
373 unset($_SESSION['objectinfo']);
374 }
377 /********************
378 Delete group canceled
379 ********************/
381 /* Delete group canceled? */
382 if (isset($_POST['delete_cancel'])){
383 del_lock ($this->dn);
384 unset($_SESSION['objectinfo']);
385 }
388 /********************
389 A dialog was canceled
390 ********************/
392 /* Cancel dialogs */
393 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
394 if(isset($this->grouptab->dn)){
395 del_lock ($this->grouptab->dn);
396 }
397 unset ($this->grouptab);
398 $this->grouptab= NULL;
399 unset($_SESSION['objectinfo']);
400 }
403 /********************
404 If there is currently a dialog open, display it
405 ********************/
407 /* Show tab dialog if object is present */
408 if (isset($this->grouptab->config)){
409 $display= $this->grouptab->execute();
411 /* Don't show buttons if tab dialog requests this */
412 if(isset($this->grouptab->by_object)){
413 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
414 $display.= "<p style=\"text-align:right\">\n";
415 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
416 $display.= " \n";
417 if ($this->dn != "new"){
418 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
419 $display.= " \n";
420 }
421 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
422 $display.= "</p>";
423 }
424 }
425 return ($display);
426 }
429 /* Check if there is a snapshot dialog open */
430 $base = $this->DivListGroup->selectedBase;
431 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
432 return($str);
433 }
435 /* Display dialog with group list */
436 $this->DivListGroup->parent = $this;
437 $this->DivListGroup->execute();
439 /* Add departments if subsearch is disabled */
440 if(!$this->DivListGroup->SubSearch){
441 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4,1);
442 }
443 $this->reload ();
444 $this->DivListGroup->setEntries($this->grouplist);
445 return($this->DivListGroup->Draw());
446 }
449 /* Return departments, that will be included within snapshot detection */
450 function get_used_snapshot_bases()
451 {
452 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
453 }
456 function list_get_selected_items()
457 {
458 $ids = array();
459 foreach($_POST as $name => $value){
460 if(preg_match("/^item_selected_[0-9]*$/",$name)){
461 $id = preg_replace("/^item_selected_/","",$name);
462 $ids[$id] = $id;
463 }
464 }
465 return($ids);
466 }
469 function reload($CreatePosixsList=false)
470 {
471 $this->grouplist = array();
472 $primaries = array();
473 $functional = array();
474 $error= $error2 = "";
475 $filter = "(objectclass=posixGroup)";
477 $base = $this->DivListGroup->selectedBase;
478 $Regex = $this->DivListGroup->Regex;
479 $UserRegex = $this->DivListGroup->UserRegex;
480 $SubSearch = $this->DivListGroup->SubSearch;
481 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
482 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
483 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
484 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
485 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
487 /* Prepare ldap class */
488 $ldap= $this->config->get_ldap_link();
489 $ldap->cd($base);
490 $ldap->set_size_limit($_SESSION['size_limit']);
493 /********************
494 Create filter depending on selected checkboxes
495 ********************/
497 /* Add application groups */
498 if ($ShowApplicationGroups){
499 $filter.= "(objectClass=gosaApplicationGroup)";
500 }
502 /* Add Mail Groups */
503 if ($ShowMailGroups){
504 $filter.= "(objectClass=gosaMailAccount)";
505 }
507 $sfilter= "";
508 if ($this->config->current['SAMBAVERSION'] == 3){
509 if (!$ShowPrimaryGroups){
510 $sfilter= "(objectClass=sambaGroupMapping)";
511 } elseif ($ShowSambaGroups){
512 $filter.= "(objectClass=sambaGroupMapping)";
513 }
514 }
516 /* Prepare filter for given Regex && UserRegex */
517 if ($filter != ""){
518 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
519 if ($UserRegex != ""){
520 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
521 }
522 }
525 /********************
526 Collect some groupids to be able to skip primary & functional groups
527 ********************/
529 /* Collect primary groupIDs to show primary groups
530 if this option is enabled in gosa conf && the checkbox is checked */
531 if ($this->ShowPrimaryCheckBox){
532 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
533 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
534 foreach ($res as $attrs){
535 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
536 }
537 }
539 /* Collect all GroupIDs from those groups which are functional.
540 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
541 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
542 if ($SubSearch){
543 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
544 } else {
545 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_NONE);
546 }
547 foreach($res as $attrs){
548 if (!isset($primaries[$attrs['gidNumber'][0]])){
549 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
550 }
551 }
554 /********************
555 Search for the prepared filter
556 ********************/
558 /* Attributes to search for */
559 $attrs = array("cn", "description", "gidNumber", "objectClass");
561 /* If subsearch is activated search for subobjects too */
562 $tmp = search_config($this->config->data,"faiManagement","CLASS");
563 if(!empty($tmp)){
564 $attrs [] = "FAIrelease";
565 }
567 if ($SubSearch){
568 $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
569 } else {
570 $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
571 }
573 /* Sort values into grouplist*/
574 $tmp = $tmp2 = array();
575 foreach ($res as $value){
576 /* Skip functional groups if checkbox isn't checked */
577 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
578 continue;
579 }
581 /* If gidNumber is in $primaries skip this entry */
582 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
583 $tmp2[$value['cn'][0]] = $value;
584 $tmp [$value['cn'][0]] = $value['cn'][0];
585 }
586 }
587 natcasesort($tmp);
588 foreach($tmp as $name){
589 $this->grouplist[] = $tmp2[$name];
590 }
591 reset ($this->grouplist);
592 }
596 /* Perform copy & paste requests
597 If copy&paste is in progress this returns a dialog to fix required attributes
598 */
599 function copyPasteHandling($s_action,$s_entry)
600 {
601 /* Paste copied/cutted object in here
602 */
603 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
604 $this->CopyPasteHandler->save_object();
605 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
606 if($str = $this->CopyPasteHandler->execute()){
607 return( $str);
608 };
609 /* Ensure that the new object is shown in the list now */
610 $this->reload();
611 }
614 /* Copy current object to CopyHandler
615 */
616 if($s_action == "copy"){
618 $dn = $this->grouplist[trim($s_entry)]['dn'];
619 $ui = get_userinfo();
620 $acl_all = $ui->has_complete_category_acls($this->DivListGroup->selectedBase,"groups") ;
621 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
623 $this->CopyPasteHandler->Clear();
624 $obj = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
625 $obj->set_acl_base($dn);
626 $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
627 $objNew->set_acl_base($dn);
629 $this->CopyPasteHandler->Copy($obj,$objNew);
630 }else{
631 print_red("You are not allowed to copy this entry.");
632 }
633 }
636 /* Copy current object to CopyHandler
637 */
638 if($s_action == "cut"){
640 $dn = $this->grouplist[trim($s_entry)]['dn'];
641 $acl_all = $ui->has_complete_category_acls($this->selectedBase,"groups") ;
642 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
644 $this->CopyPasteHandler->Clear();
645 $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
646 $obj->set_acl_base($dn);
647 $this->CopyPasteHandler->Cut($obj);
648 }else{
649 print_red("You are not allowed to cut this entry.");
650 }
651 }
652 }
654 /* Save data to object */
655 function save_object()
656 {
657 $this->DivListGroup->save_object();
658 }
661 function remove_lock()
662 {
663 if (isset($this->grouptab->dn)){
664 del_lock ($this->grouptab->dn);
665 }
666 }
669 function remove_from_parent()
670 {
671 /* Optionally execute a command after we're done */
672 $this->postremove();
673 }
676 /* Save to LDAP */
677 function save()
678 {
679 /* Optionally execute a command after we're done */
680 $this->postcreate();
681 }
683 /* Unused functions */
684 function check() { }
685 function adapt_from_template($dn) { }
686 function password_change_needed() { }
687 }
688 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
689 ?>