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 $smarty->assign("info", sprintf(_("You're about to delete the following group(s) %s"), @LDAP::fix($dns_names)));
248 $smarty->assign("multiple", true);
249 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
250 }
251 }
254 /********************
255 Delete MULTIPLE entries confirmed
256 ********************/
258 /* Confirmation for deletion has been passed. Users should be deleted. */
259 if (isset($_POST['delete_multiple_group_confirm'])){
261 /* Remove user by user and check acls before removeing them */
262 foreach($this->dns as $key => $dn){
264 $acl = $this->ui->get_permissions($dn, "users/user");
265 if (preg_match('/d/', $acl)){
267 /* Delete request is permitted, perform LDAP action */
268 $this->usertab= new usertabs($this->config, $this->config->data['TABS']['USERTABS'],$dn);
269 $this->usertab->set_acl_base();
270 $this->usertab->delete ();
271 gosa_log ("User object '".$dn."' has been removed");
272 unset ($this->usertab);
273 $this->usertab= NULL;
274 } else {
275 print_red (sprintf(_("You are not allowed to delete the user '%s'!"),$dn));
276 if(isset($this->ui->uid)){
277 gosa_log ("Warning: '".$this->ui->uid."' tried to trick user deletion.");
278 }
279 }
280 /* Remove lock file after successfull deletion */
281 del_lock ($dn);
282 unset($this->dns[$key]);
283 }
284 }
287 /********************
288 Delete MULTIPLE entries confirmed
289 ********************/
291 /* Confirmation for deletion has been passed. Users should be deleted. */
292 if (isset($_POST['delete_multiple_groups_confirm'])){
294 /* Remove user by user and check acls before removeing them */
295 foreach($this->dns as $key => $dn){
297 /* Load permissions for selected 'dn' and check if
298 we're allowed to remove this 'dn' */
299 $acl = $this->ui->get_permissions($this->dn,"groups/group");
300 if(preg_match("/d/",$acl)){
302 /* Delete request is permitted, perform LDAP action */
303 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
304 $this->grouptab->set_acl_base($this->dn);
305 $this->grouptab->delete ();
306 gosa_log ("Group object'".$this->dn."' has been removed");
307 unset ($this->grouptab);
308 $this->grouptab= NULL;
310 } else {
312 /* Normally this shouldn't be reached, send some extra
313 logs to notify the administrator */
314 print_red (_("You are not allowed to delete this group!"));
315 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
316 }
317 /* Remove lock file after successfull deletion */
318 del_lock ($dn);
319 unset($this->dns[$key]);
320 }
321 }
324 /********************
325 Delete MULTIPLE entries Canceled
326 ********************/
328 /* Remove lock */
329 if(isset($_POST['delete_multiple_user_cancel'])){
330 foreach($this->dns as $key => $dn){
331 del_lock ($dn);
332 unset($this->dns[$key]);
333 }
334 }
337 /********************
338 Delete group
339 ********************/
341 /* Remove group was requested */
342 if ($s_action=="del"){
344 /* Get 'dn' from posted 'uid' */
345 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
347 /* Load permissions for selected 'dn' and check if
348 we're allowed to remove this 'dn' */
349 $acl = $this->ui->get_permissions($this->dn,"groups/group");
350 if(preg_match("/d/",$acl)){
352 /* Check locking, save current plugin in 'back_plugin', so
353 the dialog knows where to return. */
354 if (($user= get_lock($this->dn)) != ""){
355 return(gen_locked_message ($user, $this->dn));
356 }
358 /* Lock the current entry, so nobody will edit it during deletion */
359 add_lock ($this->dn, $this->ui->dn);
360 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
361 $smarty->assign("multiple", false);
362 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
364 } else {
366 /* Obviously the user isn't allowed to delete. Show message and clean session. */
367 print_red (_("You are not allowed to delete this group!"));
368 }
369 }
372 /********************
373 Delete group confirmed
374 ********************/
376 /* Confirmation for deletion has been passed. Group should be deleted. */
377 if (isset($_POST['delete_group_confirm'])){
379 /* Some nice guy may send this as POST, so we've to check
380 for the permissions again. */
381 $acl = $this->ui->get_permissions($this->dn,"groups/group");
382 if(preg_match("/d/",$acl)){
384 /* Delete request is permitted, perform LDAP action */
385 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
386 $this->grouptab->set_acl_base($this->dn);
387 $this->grouptab->delete ();
388 gosa_log ("Group object'".$this->dn."' has been removed");
389 unset ($this->grouptab);
390 $this->grouptab= NULL;
392 /* Group list has changed, reload it. */
393 //$this->reload ();
395 } else {
397 /* Normally this shouldn't be reached, send some extra
398 logs to notify the administrator */
399 print_red (_("You are not allowed to delete this group!"));
400 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
401 }
403 /* Remove lock file after successfull deletion */
404 del_lock ($this->dn);
405 unset($_SESSION['objectinfo']);
406 }
409 /********************
410 Delete group canceled
411 ********************/
413 /* Delete group canceled? */
414 if (isset($_POST['delete_cancel'])){
415 del_lock ($this->dn);
416 unset($_SESSION['objectinfo']);
417 }
420 /********************
421 A dialog was canceled
422 ********************/
424 /* Cancel dialogs */
425 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
426 if(isset($this->grouptab->dn)){
427 del_lock ($this->grouptab->dn);
428 }
429 unset ($this->grouptab);
430 $this->grouptab= NULL;
431 unset($_SESSION['objectinfo']);
432 }
435 /********************
436 If there is currently a dialog open, display it
437 ********************/
439 /* Show tab dialog if object is present */
440 if (isset($this->grouptab->config)){
441 $display= $this->grouptab->execute();
443 /* Don't show buttons if tab dialog requests this */
444 if(isset($this->grouptab->by_object)){
445 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
446 $display.= "<p style=\"text-align:right\">\n";
447 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
448 $display.= " \n";
449 if ($this->dn != "new"){
450 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
451 $display.= " \n";
452 }
453 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
454 $display.= "</p>";
455 }
456 }
457 return ($display);
458 }
461 /* Check if there is a snapshot dialog open */
462 $base = $this->DivListGroup->selectedBase;
463 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
464 return($str);
465 }
467 /* Display dialog with group list */
468 $this->DivListGroup->parent = $this;
469 $this->DivListGroup->execute();
471 /* Add departments if subsearch is disabled */
472 if(!$this->DivListGroup->SubSearch){
473 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4,1);
474 }
475 $this->reload ();
476 $this->DivListGroup->setEntries($this->grouplist);
477 return($this->DivListGroup->Draw());
478 }
481 /* Return departments, that will be included within snapshot detection */
482 function get_used_snapshot_bases()
483 {
484 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
485 }
488 function list_get_selected_items()
489 {
490 $ids = array();
491 foreach($_POST as $name => $value){
492 if(preg_match("/^item_selected_[0-9]*$/",$name)){
493 $id = preg_replace("/^item_selected_/","",$name);
494 $ids[$id] = $id;
495 }
496 }
497 return($ids);
498 }
501 function reload($CreatePosixsList=false)
502 {
503 $this->grouplist = array();
504 $primaries = array();
505 $functional = array();
506 $error= $error2 = "";
507 $filter = "(objectclass=posixGroup)";
509 $base = $this->DivListGroup->selectedBase;
510 $Regex = $this->DivListGroup->Regex;
511 $UserRegex = $this->DivListGroup->UserRegex;
512 $SubSearch = $this->DivListGroup->SubSearch;
513 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
514 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
515 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
516 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
517 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
519 /* Prepare ldap class */
520 $ldap= $this->config->get_ldap_link();
521 $ldap->cd($base);
522 $ldap->set_size_limit($_SESSION['size_limit']);
525 /********************
526 Create filter depending on selected checkboxes
527 ********************/
529 /* Add application groups */
530 if ($ShowApplicationGroups){
531 $filter.= "(objectClass=gosaApplicationGroup)";
532 }
534 /* Add Mail Groups */
535 if ($ShowMailGroups){
536 $filter.= "(objectClass=gosaMailAccount)";
537 }
539 $sfilter= "";
540 if ($this->config->current['SAMBAVERSION'] == 3){
541 if (!$ShowPrimaryGroups){
542 $sfilter= "(objectClass=sambaGroupMapping)";
543 } elseif ($ShowSambaGroups){
544 $filter.= "(objectClass=sambaGroupMapping)";
545 }
546 }
548 /* Prepare filter for given Regex && UserRegex */
549 if ($filter != ""){
550 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
551 if ($UserRegex != ""){
552 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
553 }
554 }
557 /********************
558 Collect some groupids to be able to skip primary & functional groups
559 ********************/
561 /* Collect primary groupIDs to show primary groups
562 if this option is enabled in gosa conf && the checkbox is checked */
563 if ($this->ShowPrimaryCheckBox){
564 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
565 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
566 foreach ($res as $attrs){
567 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
568 }
569 }
571 /* Collect all GroupIDs from those groups which are functional.
572 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
573 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
574 if ($SubSearch){
575 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
576 } else {
577 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_NONE);
578 }
579 foreach($res as $attrs){
580 if (!isset($primaries[$attrs['gidNumber'][0]])){
581 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
582 }
583 }
586 /********************
587 Search for the prepared filter
588 ********************/
590 /* Attributes to search for */
591 $attrs = array("cn", "description", "gidNumber", "objectClass");
593 /* If subsearch is activated search for subobjects too */
594 $tmp = search_config($this->config->data,"faiManagement","CLASS");
595 if(!empty($tmp)){
596 $attrs [] = "FAIrelease";
597 }
599 if ($SubSearch){
600 $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
601 } else {
602 $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
603 }
605 /* Sort values into grouplist*/
606 $tmp = $tmp2 = array();
607 foreach ($res as $value){
608 /* Skip functional groups if checkbox isn't checked */
609 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
610 continue;
611 }
613 /* If gidNumber is in $primaries skip this entry */
614 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
615 $tmp2[$value['cn'][0]] = $value;
616 $tmp [$value['cn'][0]] = $value['cn'][0];
617 }
618 }
619 natcasesort($tmp);
620 foreach($tmp as $name){
621 $this->grouplist[] = $tmp2[$name];
622 }
623 reset ($this->grouplist);
624 }
628 /* Perform copy & paste requests
629 If copy&paste is in progress this returns a dialog to fix required attributes
630 */
631 function copyPasteHandling($s_action,$s_entry)
632 {
633 /* Paste copied/cutted object in here
634 */
635 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
636 $this->CopyPasteHandler->save_object();
637 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
638 if($str = $this->CopyPasteHandler->execute()){
639 return( $str);
640 };
641 /* Ensure that the new object is shown in the list now */
642 $this->reload();
643 }
646 /* Copy current object to CopyHandler
647 */
648 if($s_action == "copy"){
650 $dn = $this->grouplist[trim($s_entry)]['dn'];
651 $ui = get_userinfo();
652 $acl_all = $ui->has_complete_category_acls($this->DivListGroup->selectedBase,"groups") ;
653 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
655 $this->CopyPasteHandler->Clear();
656 $obj = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
657 $obj->set_acl_base($dn);
658 $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
659 $objNew->set_acl_base($dn);
661 $this->CopyPasteHandler->Copy($obj,$objNew);
662 }else{
663 print_red("You are not allowed to copy this entry.");
664 }
665 }
668 /* Copy current object to CopyHandler
669 */
670 if($s_action == "cut"){
672 $dn = $this->grouplist[trim($s_entry)]['dn'];
673 $acl_all = $ui->has_complete_category_acls($this->selectedBase,"groups") ;
674 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
676 $this->CopyPasteHandler->Clear();
677 $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
678 $obj->set_acl_base($dn);
679 $this->CopyPasteHandler->Cut($obj);
680 }else{
681 print_red("You are not allowed to cut this entry.");
682 }
683 }
684 }
686 /* Save data to object */
687 function save_object()
688 {
689 $this->DivListGroup->save_object();
690 }
693 function remove_lock()
694 {
695 if (isset($this->grouptab->dn)){
696 del_lock ($this->grouptab->dn);
697 }
698 }
701 function remove_from_parent()
702 {
703 /* Optionally execute a command after we're done */
704 $this->postremove();
705 }
708 /* Save to LDAP */
709 function save()
710 {
711 /* Optionally execute a command after we're done */
712 $this->postcreate();
713 }
715 /* Unused functions */
716 function check() { }
717 function adapt_from_template($dn) { }
718 function password_change_needed() { }
719 }
720 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
721 ?>