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_/");
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 }
110 }
111 $s_entry = preg_replace("/_.$/","",$s_entry);
113 /* Check for posted gets */
114 if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
115 $s_entry = $_GET['id'];
116 $s_action = "edit";
117 }
119 $smarty= get_smarty();
121 /********************
122 Copy & Paste Handling ...
123 ********************/
125 /* Only perform copy&paste requests if it is enabled
126 */
127 if($this->CopyPasteHandler){
128 if($str = $this->copyPasteHandling($s_action,$s_entry)){
129 return $str;
130 }
131 }
134 /********************
135 Create a new group ...
136 ********************/
138 /* New group? */
139 if ($s_action=="new"){
141 /* By default we set 'dn' to 'new', all relevant plugins will
142 react on this. */
143 $this->dn= "new";
145 /* Create new usertab object */
146 $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
148 /* Set up the users ACL's for this 'dn' */
149 $this->grouptab->set_acl_base($this->DivListGroup->selectedBase);
150 }
153 /********************
154 Save Group Tab/Object Changes
155 ********************/
157 /* Finish group edit is triggered by the tabulator dialog, so
158 the user wants to save edited data. Check and save at this
159 point. */
160 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->grouptab->config)) ){
162 /* Check tabs, will feed message array
163 Save, or display error message? */
164 $message= $this->grouptab->check();
165 if (count($message) == 0){
167 /* Save user data to ldap */
168 $this->grouptab->save();
169 gosa_log ("Group object'".$this->dn."' has been saved");
171 if (!isset($_POST['edit_apply'])){
172 /* Group has been saved successfully, remove lock from LDAP. */
173 if ($this->dn != "new"){
174 del_lock ($this->dn);
175 }
177 /* There's no page reload so we have to read new users at this point. */
178 //$this->reload ();
179 unset ($this->grouptab);
180 $this->grouptab= NULL;
181 unset ($_SESSION['objectinfo']);
182 }
183 } else {
184 /* Ok. There seem to be errors regarding to the tab data,
185 show message and continue as usual. */
186 show_errors($message);
187 }
188 }
191 /********************
192 Edit existing group
193 ********************/
195 /* User wants to edit data? */
196 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
198 /* Get 'dn' from posted 'uid', must be unique */
199 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
201 /* Check locking & lock entry if required */
202 $user = get_lock($this->dn);
203 if ($user != ""){
204 return(gen_locked_message ($user, $this->dn));
205 }
206 add_lock ($this->dn, $this->ui->dn);
208 /* Register grouptab to trigger edit dialog */
209 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
210 $this->grouptab->set_acl_base($this->dn);
211 $_SESSION['objectinfo']= $this->dn;
212 }
215 /********************
216 Delete group
217 ********************/
219 /* Remove group was requested */
220 if ($s_action=="del"){
222 /* Get 'dn' from posted 'uid' */
223 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
225 /* Load permissions for selected 'dn' and check if
226 we're allowed to remove this 'dn' */
227 $acl = $this->ui->get_permissions($this->dn,"groups/group");
228 if(preg_match("/d/",$acl)){
230 /* Check locking, save current plugin in 'back_plugin', so
231 the dialog knows where to return. */
232 if (($user= get_lock($this->dn)) != ""){
233 return(gen_locked_message ($user, $this->dn));
234 }
236 /* Lock the current entry, so nobody will edit it during deletion */
237 add_lock ($this->dn, $this->ui->dn);
238 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
239 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
241 } else {
243 /* Obviously the user isn't allowed to delete. Show message and clean session. */
244 print_red (_("You are not allowed to delete this group!"));
245 }
246 }
249 /********************
250 Delete group confirmed
251 ********************/
253 /* Confirmation for deletion has been passed. Group should be deleted. */
254 if (isset($_POST['delete_group_confirm'])){
256 /* Some nice guy may send this as POST, so we've to check
257 for the permissions again. */
258 $acl = $this->ui->get_permissions($this->dn,"groups/group");
259 if(preg_match("/d/",$acl)){
261 /* Delete request is permitted, perform LDAP action */
262 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
263 $this->grouptab->set_acl_base($this->dn);
264 $this->grouptab->delete ();
265 gosa_log ("Group object'".$this->dn."' has been removed");
266 unset ($this->grouptab);
267 $this->grouptab= NULL;
269 /* Group list has changed, reload it. */
270 //$this->reload ();
272 } else {
274 /* Normally this shouldn't be reached, send some extra
275 logs to notify the administrator */
276 print_red (_("You are not allowed to delete this group!"));
277 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
278 }
280 /* Remove lock file after successfull deletion */
281 del_lock ($this->dn);
282 unset($_SESSION['objectinfo']);
283 }
286 /********************
287 Delete group canceled
288 ********************/
290 /* Delete group canceled? */
291 if (isset($_POST['delete_cancel'])){
292 del_lock ($this->dn);
293 unset($_SESSION['objectinfo']);
294 }
297 /********************
298 A dialog was canceled
299 ********************/
301 /* Cancel dialogs */
302 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
303 if(isset($this->grouptab->dn)){
304 del_lock ($this->grouptab->dn);
305 }
306 unset ($this->grouptab);
307 $this->grouptab= NULL;
308 unset($_SESSION['objectinfo']);
309 }
312 /********************
313 If there is currently a dialog open, display it
314 ********************/
316 /* Show tab dialog if object is present */
317 if (isset($this->grouptab->config)){
318 $display= $this->grouptab->execute();
320 /* Don't show buttons if tab dialog requests this */
321 if(isset($this->grouptab->by_object)){
322 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
323 $display.= "<p style=\"text-align:right\">\n";
324 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
325 $display.= " \n";
326 if ($this->dn != "new"){
327 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
328 $display.= " \n";
329 }
330 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
331 $display.= "</p>";
332 }
333 }
334 return ($display);
335 }
338 /* Check if there is a snapshot dialog open */
339 $base = $this->DivListGroup->selectedBase;
340 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
341 return($str);
342 }
344 /* Display dialog with group list */
345 $this->DivListGroup->parent = $this;
346 $this->DivListGroup->execute();
348 /* Add departments if subsearch is disabled */
349 if(!$this->DivListGroup->SubSearch){
350 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4);
351 }
352 $this->reload ();
353 $this->DivListGroup->setEntries($this->grouplist);
354 return($this->DivListGroup->Draw());
355 }
358 /* Return departments, that will be included within snapshot detection */
359 function get_used_snapshot_bases()
360 {
361 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
362 }
366 function reload($CreatePosixsList=false)
367 {
368 $this->grouplist = array();
369 $primaries = array();
370 $functional = array();
371 $error= $error2 = "";
372 $filter = "(objectclass=posixGroup)";
374 $base = $this->DivListGroup->selectedBase;
375 $Regex = $this->DivListGroup->Regex;
376 $UserRegex = $this->DivListGroup->UserRegex;
377 $SubSearch = $this->DivListGroup->SubSearch;
378 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
379 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
380 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
381 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
382 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
384 /* Prepare ldap class */
385 $ldap= $this->config->get_ldap_link();
386 $ldap->cd($base);
387 $ldap->set_size_limit($_SESSION['size_limit']);
390 /********************
391 Create filter depending on selected checkboxes
392 ********************/
394 /* Add application groups */
395 if ($ShowApplicationGroups){
396 $filter.= "(objectClass=gosaApplicationGroup)";
397 }
399 /* Add Mail Groups */
400 if ($ShowMailGroups){
401 $filter.= "(objectClass=gosaMailAccount)";
402 }
404 $sfilter= "";
405 if ($this->config->current['SAMBAVERSION'] == 3){
406 if (!$ShowPrimaryGroups){
407 $sfilter= "(objectClass=sambaGroupMapping)";
408 } elseif ($ShowSambaGroups){
409 $filter.= "(objectClass=sambaGroupMapping)";
410 }
411 }
413 /* Prepare filter for given Regex && UserRegex */
414 if ($filter != ""){
415 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
416 if ($UserRegex != ""){
417 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
418 }
419 }
422 /********************
423 Collect some groupids to be able to skip primary & functional groups
424 ********************/
426 /* Collect primary groupIDs to show primary groups
427 if this option is enabled in gosa conf && the checkbox is checked */
428 if ($this->ShowPrimaryCheckBox){
429 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
430 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
431 foreach ($res as $attrs){
432 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
433 }
434 }
436 /* Collect all GroupIDs from those groups which are functional.
437 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
438 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
439 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
440 foreach($res as $attrs){
441 if (!isset($primaries[$attrs['gidNumber'][0]])){
442 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
443 }
444 }
447 /********************
448 Search for the prepared filter
449 ********************/
451 /* If subsearch is activated search for subobjects too */
452 $attrs = array("cn", "description", "gidNumber", "objectClass","FAIrelease");
454 if ($SubSearch){
455 $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
456 } else {
457 $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
458 }
460 /* Sort values into grouplist*/
461 $tmp = $tmp2 = array();
462 foreach ($res as $value){
463 /* Skip functional groups if checkbox isn't checked */
464 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
465 continue;
466 }
468 /* If gidNumber is in $primaries skip this entry */
469 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
470 $tmp2[$value['cn'][0]] = $value;
471 $tmp [$value['cn'][0]] = $value['cn'][0];
472 }
473 }
474 natcasesort($tmp);
475 foreach($tmp as $name){
476 $this->grouplist[] = $tmp2[$name];
477 }
478 reset ($this->grouplist);
479 }
483 /* Perform copy & paste requests
484 If copy&paste is in progress this returns a dialog to fix required attributes
485 */
486 function copyPasteHandling($s_action,$s_entry)
487 {
488 /* Paste copied/cutted object in here
489 */
490 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
491 $this->CopyPasteHandler->save_object();
492 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
493 if($str = $this->CopyPasteHandler->execute()){
494 return( $str);
495 };
496 /* Ensure that the new object is shown in the list now */
497 $this->reload();
498 }
501 /* Copy current object to CopyHandler
502 */
503 if($s_action == "copy"){
504 $this->CopyPasteHandler->Clear();
506 $dn = $this->grouplist[trim($s_entry)]['dn'];
507 $acl = get_permissions ($dn, $this->ui->subtreeACL);
509 $obj = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
510 $obj->set_acl_base($dn);
511 $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
512 $obj->set_acl_base($dn);
514 $this->CopyPasteHandler->Copy($obj,$objNew);
515 }
518 /* Copy current object to CopyHandler
519 */
520 if($s_action == "cut"){
521 $this->CopyPasteHandler->Clear();
522 $dn = $this->grouplist[trim($s_entry)]['dn'];
523 $acl= get_permissions ($dn, $this->ui->subtreeACL);
525 $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
526 $obj->set_acl_base($dn);
528 $this->CopyPasteHandler->Cut($obj);
529 }
530 }
532 /* Save data to object */
533 function save_object()
534 {
535 $this->DivListGroup->save_object();
536 }
539 function remove_lock()
540 {
541 if (isset($this->grouptab->dn)){
542 del_lock ($this->grouptab->dn);
543 }
544 }
547 function remove_from_parent()
548 {
549 /* Optionally execute a command after we're done */
550 $this->postremove();
551 }
554 /* Save to LDAP */
555 function save()
556 {
557 /* Optionally execute a command after we're done */
558 $this->postcreate();
559 }
561 /* Unused functions */
562 function check() { }
563 function adapt_from_template($dn) { }
564 function password_change_needed() { }
565 }
566 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
567 ?>