7bfc5c32784f9f6ff0e82be494dc9473e3dedc30
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 $acl= "";
34 var $CopyPasteHandler = NULL;
35 var $DivListGroup = NULL;
37 var $ShowPrimaryCheckBox = false;
39 function groupManagement ($config, $ui)
40 {
41 /* Save configuration for internal use */
42 $this->config= $config;
43 $this->ui= $ui;
45 /* Copy & Paste enabled ?*/
46 if((isset($this->config->data['MAIN']['ENABLECOPYPASTE']))&&(preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE']))){
47 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48 }
50 /* Detect if we have to display the primary group checkbox */
51 if (isset($this->config->data['MAIN']['NOPRIMARYGROUP'])){
52 $this->ShowPrimaryCheckBox = false;
53 } else {
54 $this->ShowPrimaryCheckBox = true;
55 }
57 /* Create dialog object */
58 $this->DivListGroup = new divListGroup($this->config,$this);
59 $this->DivListGroup->DisableCheckBox("ShowPrimaryGroups",$this->ShowPrimaryCheckBox);
60 }
62 function execute()
63 {
64 /* Call parent execute */
65 plugin::execute();
67 /* Store these posts if the current object is locked (used by somebody else)*/
68 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/");
70 /* Save data */
71 $s_action = "";
72 $s_entry = "";
74 /* Test Posts */
75 foreach($_POST as $key => $val){
76 // Post for delete
77 if(preg_match("/^group_del.*/",$key)){
78 $s_action = "del";
79 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
80 // Post for edit
81 }elseif(preg_match("/^group_edit_.*/",$key)){
82 $s_action="edit";
83 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
84 // Post for new
85 }elseif(preg_match("/^group_new.*/",$key)){
86 $s_action="new";
87 }elseif(preg_match("/^dep_home.*/i",$key)){
88 $s_action="home";
89 }elseif(preg_match("/^group_tplnew.*/i",$key)){
90 $s_action="new_tpl";
91 }elseif(preg_match("/^group_chgpw.*/i",$key)){
92 $s_action="change_pw";
93 $s_entry = preg_replace("/group_chgpw_/i","",$key);
94 }elseif(preg_match("/_group_edit_/",$key)){
95 $type = preg_replace("/_group_edit_.*$/","",$key);
96 $s_action="edit";
97 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
98 $_POST['arg'] = $type;
99 }elseif(preg_match("/^editPaste.*/i",$key)){
100 $s_action="editPaste";
101 }elseif(preg_match("/^copy_.*/",$key)){
102 $s_action="copy";
103 $s_entry = preg_replace("/^copy_/i","",$key);
104 }elseif(preg_match("/^cut_.*/",$key)){
105 $s_action="cut";
106 $s_entry = preg_replace("/^cut_/i","",$key);
107 }
108 }
109 $s_entry = preg_replace("/_.$/","",$s_entry);
111 /* Check for posted gets */
112 if((isset($_GET['act'])) && ($_GET['act'] == "edit_entry")){
113 $s_entry = $_GET['id'];
114 $s_action = "edit";
115 }
117 $smarty= get_smarty();
119 /********************
120 Copy & Paste Handling ...
121 ********************/
123 /* Only perform copy&paste requests if it is enabled
124 */
125 if($this->CopyPasteHandler){
126 if($str = $this->copyPasteHandling($s_action,$s_entry)){
127 return $str;
128 }
129 }
132 /********************
133 Create a new group ...
134 ********************/
136 /* New group? */
137 if ($s_action=="new"){
139 /* By default we set 'dn' to 'new', all relevant plugins will
140 react on this. */
141 $this->dn= "new";
143 /* Create new usertab object */
144 $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
146 /* Set up the users ACL's for this 'dn' */
147 $acl= get_permissions ($this->DivListGroup->selectedBase, $this->ui->subtreeACL);
148 $this->grouptab->set_acl($acl);
149 }
152 /********************
153 Save Group Tab/Object Changes
154 ********************/
156 /* Finish group edit is triggered by the tabulator dialog, so
157 the user wants to save edited data. Check and save at this
158 point. */
159 if ((isset($_POST['edit_finish'])) && (isset($this->grouptab->config)) ){
161 /* Check tabs, will feed message array
162 Save, or display error message? */
163 $message= $this->grouptab->check();
164 if (count($message) == 0){
166 /* Save user data to ldap */
167 $this->grouptab->save();
168 gosa_log ("Group object'".$this->dn."' has been saved");
170 /* Group has been saved successfully, remove lock from LDAP. */
171 if ($this->dn != "new"){
172 del_lock ($this->dn);
173 }
175 /* There's no page reload so we have to read new users at this point. */
176 //$this->reload ();
177 unset ($this->grouptab);
178 $this->grouptab= NULL;
179 unset ($_SESSION['objectinfo']);
181 } else {
182 /* Ok. There seem to be errors regarding to the tab data,
183 show message and continue as usual. */
184 show_errors($message);
185 }
186 }
189 /********************
190 Edit existing group
191 ********************/
193 /* User wants to edit data? */
194 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
196 /* Get 'dn' from posted 'uid', must be unique */
197 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
199 /* Check locking & lock entry if required */
200 $user = get_lock($this->dn);
201 if ($user != ""){
202 return(gen_locked_message ($user, $this->dn));
203 }
204 add_lock ($this->dn, $this->ui->dn);
206 /* Set up the users ACL's for this 'dn' */
207 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
209 /* Register grouptab to trigger edit dialog */
210 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
211 $this->grouptab->set_acl($acl);
212 $_SESSION['objectinfo']= $this->dn;
213 }
216 /********************
217 Delete group
218 ********************/
220 /* Remove group was requested */
221 if ($s_action=="del"){
223 /* Get 'dn' from posted 'uid' */
224 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
226 /* Load permissions for selected 'dn' and check if
227 we're allowed to remove this 'dn' */
228 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
229 $this->acl= get_module_permission($acl, "group", $this->dn);
230 if (chkacl($this->acl, "delete") == ""){
232 /* Check locking, save current plugin in 'back_plugin', so
233 the dialog knows where to return. */
234 if (($user= get_lock($this->dn)) != ""){
235 return(gen_locked_message ($user, $this->dn));
236 }
238 /* Lock the current entry, so nobody will edit it during deletion */
239 add_lock ($this->dn, $this->ui->dn);
240 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), @LDAP::fix($this->dn)));
241 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
243 } else {
245 /* Obviously the user isn't allowed to delete. Show message and clean session. */
246 print_red (_("You are not allowed to delete this group!"));
247 }
248 }
251 /********************
252 Delete group confirmed
253 ********************/
255 /* Confirmation for deletion has been passed. Group should be deleted. */
256 if (isset($_POST['delete_group_confirm'])){
258 /* Some nice guy may send this as POST, so we've to check
259 for the permissions again. */
260 if (chkacl($this->acl, "delete") == ""){
262 /* Delete request is permitted, perform LDAP action */
263 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
264 $this->grouptab->set_acl(array($this->acl));
265 $this->grouptab->delete ();
266 gosa_log ("Group object'".$this->dn."' has been removed");
267 unset ($this->grouptab);
268 $this->grouptab= NULL;
270 /* Group list has changed, reload it. */
271 //$this->reload ();
273 } else {
275 /* Normally this shouldn't be reached, send some extra
276 logs to notify the administrator */
277 print_red (_("You are not allowed to delete this group!"));
278 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
279 }
281 /* Remove lock file after successfull deletion */
282 del_lock ($this->dn);
283 unset($_SESSION['objectinfo']);
284 }
287 /********************
288 Delete group canceled
289 ********************/
291 /* Delete group canceled? */
292 if (isset($_POST['delete_cancel'])){
293 del_lock ($this->dn);
294 unset($_SESSION['objectinfo']);
295 }
298 /********************
299 A dialog was canceled
300 ********************/
302 /* Cancel dialogs */
303 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
304 del_lock ($this->grouptab->dn);
305 unset ($this->grouptab);
306 $this->grouptab= NULL;
307 unset($_SESSION['objectinfo']);
308 }
311 /********************
312 If there is currently a dialog open, display it
313 ********************/
315 /* Show tab dialog if object is present */
316 if (isset($this->grouptab->config)){
317 $display= $this->grouptab->execute();
319 /* Don't show buttons if tab dialog requests this */
320 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
321 $display.= "<p style=\"text-align:right\">\n";
322 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Save")."\">\n";
323 $display.= " \n";
324 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
325 $display.= "</p>";
326 }
327 return ($display);
328 }
330 /* Display dialog with group list */
331 $this->DivListGroup->execute();
333 /* Add departments if subsearch is disabled */
334 if(!$this->DivListGroup->SubSearch){
335 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4);
336 }
337 $this->reload ();
338 $this->DivListGroup->setEntries($this->grouplist);
339 return($this->DivListGroup->Draw());
340 }
342 function reload($CreatePosixsList=false)
343 {
344 $this->grouplist = array();
345 $primaries = array();
346 $functional = array();
347 $error= $error2 = "";
348 $filter = "(objectclass=posixGroup)";
350 $base = $this->DivListGroup->selectedBase;
351 $Regex = $this->DivListGroup->Regex;
352 $UserRegex = $this->DivListGroup->UserRegex;
353 $SubSearch = $this->DivListGroup->SubSearch;
354 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
355 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
356 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
357 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
358 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
360 /* Prepare ldap class */
361 $ldap= $this->config->get_ldap_link();
362 $ldap->cd($base);
363 $ldap->set_size_limit($_SESSION['size_limit']);
366 /********************
367 Create filter depending on selected checkboxes
368 ********************/
370 /* Add Mail Groups */
371 if ($ShowMailGroups){
372 $filter.= "(objectClass=gosaMailAccount)";
373 }
375 /* Add application groups */
376 if ($ShowApplicationGroups){
377 $filter.= "(objectClass=gosaApplicationGroup)";
378 }
380 /*
381 #FIXME Why is "sfilter" or "filter" set ?
382 Please verify && add comment
383 */
384 $sfilter= "";
385 if ($this->config->current['SAMBAVERSION'] == 3){
386 if (!$ShowPrimaryGroups){
387 $sfilter= "(objectClass=sambaGroupMapping)";
388 } elseif ($ShowSambaGroups){
389 $filter.= "(objectClass=sambaGroupMapping)";
390 }
391 }
393 /* Prepare filter for given Regex && UserRegex */
394 if ($filter != ""){
395 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
396 if ($UserRegex != ""){
397 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
398 }
399 }
402 /********************
403 Collect some groupids to be able to skip primary & functional groups
404 ********************/
406 /* Collect primary groupIDs to show primary groups
407 if this option is enabled in gosa conf && the checkbox is checked */
408 if(($this->ShowPrimaryCheckBox) && ($ShowPrimaryGroups)){
409 $ldap->search("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))", array("gidNumber", "cn"));
410 while ($attrs= $ldap->fetch()){
411 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
412 }
413 }
415 /* Collect all GroupIDs from those groups which are functional.
416 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
417 if(!$ShowFunctionalGroups){
418 $ldap->cd(get_groups_ou().$base);
419 $ldap->search("(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))", array("cn", "gidNumber", "description"));
420 $error2= $ldap->error;
421 while ($attrs= $ldap->fetch()){
422 if (!isset($primaries[$attrs['gidNumber'][0]])){
423 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
424 }
425 }
426 }
429 /********************
430 Search for the prepared filter
431 ********************/
433 /* If subsearch is activated search for subobjects too */
434 $attrs = array("cn", "description", "gidNumber", "objectClass");
436 if ($SubSearch){
437 $res= get_list($filter, $this->ui->subtreeACL, $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
438 } else {
439 $res= get_list($filter, $this->ui->subtreeACL, get_groups_ou().$base, $attrs, GL_SIZELIMIT);
440 }
442 /* Sort values into grouplist*/
443 $tmp = $tmp2 = array();
444 foreach ($res as $value){
446 /* Skip functional groups if checkbox isn't checked */
447 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
448 continue;
449 }
451 /* If gidNumber is in $primaries skip this entry */
452 if (!isset($primaries[$value['gidNumber'][0]])){
453 $tmp2[$value['cn'][0]] = $value;
454 $tmp [$value['cn'][0]] = $value['cn'][0];
455 }
456 }
457 natcasesort($tmp);
458 foreach($tmp as $name){
459 $this->grouplist[] = $tmp2[$name];
460 }
461 reset ($this->grouplist);
462 }
466 /* Perform copy & paste requests
467 If copy&paste is in progress this returns a dialog to fix required attributes
468 */
469 function copyPasteHandling($s_action,$s_entry)
470 {
471 /* Paste copied/cutted object in here
472 */
473 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
474 $this->CopyPasteHandler->save_object();
475 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
476 if($str = $this->CopyPasteHandler->execute()){
477 return( $str);
478 };
479 /* Ensure that the new object is shown in the list now */
480 $this->relaod();
481 }
484 /* Copy current object to CopyHandler
485 */
486 if($s_action == "copy"){
487 $this->CopyPasteHandler->Clear();
489 $dn = $this->grouplist[trim($s_entry)]['dn'];
490 $acl = get_permissions ($dn, $this->ui->subtreeACL);
492 $obj = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
493 $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
494 $obj->set_acl($acl);
495 $objNew->set_acl($acl);
496 $this->CopyPasteHandler->Copy($obj,$objNew);
497 }
500 /* Copy current object to CopyHandler
501 */
502 if($s_action == "cut"){
503 $this->CopyPasteHandler->Clear();
504 $dn = $this->grouplist[trim($s_entry)]['dn'];
505 $acl= get_permissions ($dn, $this->ui->subtreeACL);
507 $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
508 $obj->set_acl($acl);
510 $this->CopyPasteHandler->Cut($obj);
511 }
512 }
514 /* Save data to object */
515 function save_object()
516 {
517 $this->DivListGroup->save_object();
518 }
521 function remove_lock()
522 {
523 if (isset($this->grouptab->dn)){
524 del_lock ($this->grouptab->dn);
525 }
526 }
529 function remove_from_parent()
530 {
531 /* Optionally execute a command after we're done */
532 $this->postremove();
533 }
536 /* Save to LDAP */
537 function save()
538 {
539 /* Optionally execute a command after we're done */
540 $this->postcreate();
541 }
543 /* Unused functions */
544 function check() { }
545 function adapt_from_template($dn) { }
546 function password_change_needed() { }
547 function show_header($button_text, $text, $disabled= FALSE) { }
548 }
549 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
550 ?>