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 $tmp = "";
52 if (isset($this->config->data['MAIN']['NOPRIMARYGROUP'])){
53 $tmp = $this->config->data['MAIN']['NOPRIMARYGROUP'];
54 }
55 if(preg_match("/true/i",$tmp)|| (preg_match("/yes/",$tmp))){
56 $this->ShowPrimaryCheckBox = false;
57 } else {
58 $this->ShowPrimaryCheckBox = true;
59 }
61 /* Create dialog object */
62 $this->DivListGroup = new divListGroup($this->config,$this);
63 $this->DivListGroup->DisableCheckBox("ShowPrimaryGroups",$this->ShowPrimaryCheckBox);
64 }
66 function execute()
67 {
68 /* Call parent execute */
69 plugin::execute();
71 /* Store these posts if the current object is locked (used by somebody else)*/
72 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^group_edit_/","/^group_del_/");
74 /* Save data */
75 $s_action = "";
76 $s_entry = "";
78 /* Test Posts */
79 foreach($_POST as $key => $val){
80 // Post for delete
81 if(preg_match("/^group_del.*/",$key)){
82 $s_action = "del";
83 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
84 // Post for edit
85 }elseif(preg_match("/^group_edit_.*/",$key)){
86 $s_action="edit";
87 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
88 // Post for new
89 }elseif(preg_match("/^group_new.*/",$key)){
90 $s_action="new";
91 }elseif(preg_match("/^dep_home.*/i",$key)){
92 $s_action="home";
93 }elseif(preg_match("/^group_tplnew.*/i",$key)){
94 $s_action="new_tpl";
95 }elseif(preg_match("/^group_chgpw.*/i",$key)){
96 $s_action="change_pw";
97 $s_entry = preg_replace("/group_chgpw_/i","",$key);
98 }elseif(preg_match("/_group_edit_/",$key)){
99 $type = preg_replace("/_group_edit_.*$/","",$key);
100 $s_action="edit";
101 $s_entry = preg_replace("/".$type."_group_edit_/i","",$key);
102 $_POST['arg'] = $type;
103 }elseif(preg_match("/^editPaste.*/i",$key)){
104 $s_action="editPaste";
105 }elseif(preg_match("/^copy_.*/",$key)){
106 $s_action="copy";
107 $s_entry = preg_replace("/^copy_/i","",$key);
108 }elseif(preg_match("/^cut_.*/",$key)){
109 $s_action="cut";
110 $s_entry = preg_replace("/^cut_/i","",$key);
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 /* By default we set 'dn' to 'new', all relevant plugins will
144 react on this. */
145 $this->dn= "new";
147 /* Create new usertab object */
148 $this->grouptab= new grouptabs($this->config, $this->config->data['TABS']['GROUPTABS'], $this->dn);
150 /* Set up the users ACL's for this 'dn' */
151 $this->grouptab->set_acl_base($this->DivListGroup->selectedBase);
152 }
155 /********************
156 Save Group Tab/Object Changes
157 ********************/
159 /* Finish group edit is triggered by the tabulator dialog, so
160 the user wants to save edited data. Check and save at this
161 point. */
162 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->grouptab->config)) ){
164 /* Check tabs, will feed message array
165 Save, or display error message? */
166 $message= $this->grouptab->check();
167 if (count($message) == 0){
169 /* Save user data to ldap */
170 $this->grouptab->save();
171 gosa_log ("Group object'".$this->dn."' has been saved");
173 if (!isset($_POST['edit_apply'])){
174 /* Group has been saved successfully, remove lock from LDAP. */
175 if ($this->dn != "new"){
176 del_lock ($this->dn);
177 }
179 /* There's no page reload so we have to read new users at this point. */
180 //$this->reload ();
181 unset ($this->grouptab);
182 $this->grouptab= NULL;
183 unset ($_SESSION['objectinfo']);
184 }
185 } else {
186 /* Ok. There seem to be errors regarding to the tab data,
187 show message and continue as usual. */
188 show_errors($message);
189 }
190 }
193 /********************
194 Edit existing group
195 ********************/
197 /* User wants to edit data? */
198 if (($s_action=="edit") && (!isset($this->grouptab-> config))){
200 /* Get 'dn' from posted 'uid', must be unique */
201 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
203 /* Check locking & lock entry if required */
204 $user = get_lock($this->dn);
205 if ($user != ""){
206 return(gen_locked_message ($user, $this->dn));
207 }
208 add_lock ($this->dn, $this->ui->dn);
210 /* Register grouptab to trigger edit dialog */
211 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
212 $this->grouptab->set_acl_base($this->dn);
213 $_SESSION['objectinfo']= $this->dn;
214 }
217 /********************
218 Delete group
219 ********************/
221 /* Remove group was requested */
222 if ($s_action=="del"){
224 /* Get 'dn' from posted 'uid' */
225 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
227 /* Load permissions for selected 'dn' and check if
228 we're allowed to remove this 'dn' */
229 $acl = $this->ui->get_permission($this->dn,"groups/group");
230 if(preg_match("/d/",$acl)){
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 $acl = $this->ui->get_permission($this->dn,"groups/group");
261 if(preg_match("/d/",$acl)){
263 /* Delete request is permitted, perform LDAP action */
264 $this->grouptab= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $this->dn);
265 $this->grouptab->set_acl(array($this->acl));
266 $this->grouptab->delete ();
267 gosa_log ("Group object'".$this->dn."' has been removed");
268 unset ($this->grouptab);
269 $this->grouptab= NULL;
271 /* Group list has changed, reload it. */
272 //$this->reload ();
274 } else {
276 /* Normally this shouldn't be reached, send some extra
277 logs to notify the administrator */
278 print_red (_("You are not allowed to delete this group!"));
279 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
280 }
282 /* Remove lock file after successfull deletion */
283 del_lock ($this->dn);
284 unset($_SESSION['objectinfo']);
285 }
288 /********************
289 Delete group canceled
290 ********************/
292 /* Delete group canceled? */
293 if (isset($_POST['delete_cancel'])){
294 del_lock ($this->dn);
295 unset($_SESSION['objectinfo']);
296 }
299 /********************
300 A dialog was canceled
301 ********************/
303 /* Cancel dialogs */
304 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
305 if(isset($this->grouptab->dn)){
306 del_lock ($this->grouptab->dn);
307 }
308 unset ($this->grouptab);
309 $this->grouptab= NULL;
310 unset($_SESSION['objectinfo']);
311 }
314 /********************
315 If there is currently a dialog open, display it
316 ********************/
318 /* Show tab dialog if object is present */
319 if (isset($this->grouptab->config)){
320 $display= $this->grouptab->execute();
322 /* Don't show buttons if tab dialog requests this */
323 if(isset($this->grouptab->by_object)){
324 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
325 $display.= "<p style=\"text-align:right\">\n";
326 $display.= "<input type=submit name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
327 $display.= " \n";
328 if ($this->dn != "new"){
329 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
330 $display.= " \n";
331 }
332 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
333 $display.= "</p>";
334 }
335 }
336 return ($display);
337 }
340 /* Check if there is a snapshot dialog open */
341 $base = $this->DivListGroup->selectedBase;
342 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
343 return($str);
344 }
346 /* Display dialog with group list */
347 $this->DivListGroup->parent = $this;
348 $this->DivListGroup->execute();
350 /* Add departments if subsearch is disabled */
351 if(!$this->DivListGroup->SubSearch){
352 $this->DivListGroup->AddDepartments($this->DivListGroup->selectedBase,4);
353 }
354 $this->reload ();
355 $this->DivListGroup->setEntries($this->grouplist);
356 return($this->DivListGroup->Draw());
357 }
360 /* Return departments, that will be included within snapshot detection */
361 function get_used_snapshot_bases()
362 {
363 return(array(get_groups_ou().$this->DivListGroup->selectedBase));
364 }
368 function reload($CreatePosixsList=false)
369 {
370 $this->grouplist = array();
371 $primaries = array();
372 $functional = array();
373 $error= $error2 = "";
374 $filter = "(objectclass=posixGroup)";
376 $base = $this->DivListGroup->selectedBase;
377 $Regex = $this->DivListGroup->Regex;
378 $UserRegex = $this->DivListGroup->UserRegex;
379 $SubSearch = $this->DivListGroup->SubSearch;
380 $ShowPrimaryGroups = $this->DivListGroup->ShowPrimaryGroups;
381 $ShowSambaGroups = $this->DivListGroup->ShowSambaGroups;
382 $ShowApplicationGroups = $this->DivListGroup->ShowApplicationGroups;
383 $ShowMailGroups = $this->DivListGroup->ShowMailGroups;
384 $ShowFunctionalGroups = $this->DivListGroup->ShowFunctionalGroups;
386 /* Prepare ldap class */
387 $ldap= $this->config->get_ldap_link();
388 $ldap->cd($base);
389 $ldap->set_size_limit($_SESSION['size_limit']);
392 /********************
393 Create filter depending on selected checkboxes
394 ********************/
396 /* Add application groups */
397 if ($ShowApplicationGroups){
398 $filter.= "(objectClass=gosaApplicationGroup)";
399 }
401 /* Add Mail Groups */
402 if ($ShowMailGroups){
403 $filter.= "(objectClass=gosaMailAccount)";
404 }
406 $sfilter= "";
407 if ($this->config->current['SAMBAVERSION'] == 3){
408 if (!$ShowPrimaryGroups){
409 $sfilter= "(objectClass=sambaGroupMapping)";
410 } elseif ($ShowSambaGroups){
411 $filter.= "(objectClass=sambaGroupMapping)";
412 }
413 }
415 /* Prepare filter for given Regex && UserRegex */
416 if ($filter != ""){
417 $filter= "(&(cn=$Regex)(objectClass=posixGroup)(|$filter))";
418 if ($UserRegex != ""){
419 $filter= "(&(|(memberUID=".$UserRegex.")(cn=".$UserRegex."))$filter)";
420 }
421 }
424 /********************
425 Collect some groupids to be able to skip primary & functional groups
426 ********************/
428 /* Collect primary groupIDs to show primary groups
429 if this option is enabled in gosa conf && the checkbox is checked */
430 if ($this->ShowPrimaryCheckBox){
431 $res = get_list("(&(uid=$Regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))",
432 "groups", $base,array("gidNumber", "cn"), GL_SUBSEARCH);
433 foreach ($res as $attrs){
434 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
435 }
436 }
438 /* Collect all GroupIDs from those groups which are functional.
439 Only perfrom this search if ShowFunctionalGroups is unchecked, else leave arre empty */
440 $ff = "(&(cn=$Regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))";
441 $res = get_list($ff, "groups", $base,array("gidNumber", "cn", "description"), GL_SUBSEARCH);
442 foreach($res as $attrs){
443 if (!isset($primaries[$attrs['gidNumber'][0]])){
444 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
445 }
446 }
449 /********************
450 Search for the prepared filter
451 ********************/
453 /* If subsearch is activated search for subobjects too */
454 $attrs = array("cn", "description", "gidNumber", "objectClass","FAIrelease");
456 if ($SubSearch){
457 $res= get_list($filter, "groups", $base, $attrs, GL_SIZELIMIT| GL_SUBSEARCH);
458 } else {
459 $res= get_list($filter, "groups", get_groups_ou().$base, $attrs, GL_SIZELIMIT);
460 }
462 /* Sort values into grouplist*/
463 $tmp = $tmp2 = array();
464 foreach ($res as $value){
465 /* Skip functional groups if checkbox isn't checked */
466 if (!$ShowFunctionalGroups && isset($functional[$value['gidNumber'][0]])){
467 continue;
468 }
470 /* If gidNumber is in $primaries skip this entry */
471 if (($ShowPrimaryGroups) || (!$ShowPrimaryGroups && !isset($primaries[$value['gidNumber'][0]]))){
472 $tmp2[$value['cn'][0]] = $value;
473 $tmp [$value['cn'][0]] = $value['cn'][0];
474 }
475 }
476 natcasesort($tmp);
477 foreach($tmp as $name){
478 $this->grouplist[] = $tmp2[$name];
479 }
480 reset ($this->grouplist);
481 }
485 /* Perform copy & paste requests
486 If copy&paste is in progress this returns a dialog to fix required attributes
487 */
488 function copyPasteHandling($s_action,$s_entry)
489 {
490 /* Paste copied/cutted object in here
491 */
492 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
493 $this->CopyPasteHandler->save_object();
494 $this->CopyPasteHandler->SetVar("base",$this->DivListGroup->selectedBase);
495 if($str = $this->CopyPasteHandler->execute()){
496 return( $str);
497 };
498 /* Ensure that the new object is shown in the list now */
499 $this->reload();
500 }
503 /* Copy current object to CopyHandler
504 */
505 if($s_action == "copy"){
506 $this->CopyPasteHandler->Clear();
508 $dn = $this->grouplist[trim($s_entry)]['dn'];
509 $acl = get_permissions ($dn, $this->ui->subtreeACL);
511 $obj = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
512 $objNew = new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], "new");
513 $obj->set_acl($acl);
514 $objNew->set_acl($acl);
515 $this->CopyPasteHandler->Copy($obj,$objNew);
516 }
519 /* Copy current object to CopyHandler
520 */
521 if($s_action == "cut"){
522 $this->CopyPasteHandler->Clear();
523 $dn = $this->grouplist[trim($s_entry)]['dn'];
524 $acl= get_permissions ($dn, $this->ui->subtreeACL);
526 $obj= new grouptabs($this->config,$this->config->data['TABS']['GROUPTABS'], $dn);
527 $obj->set_acl($acl);
529 $this->CopyPasteHandler->Cut($obj);
530 }
531 }
533 /* Save data to object */
534 function save_object()
535 {
536 $this->DivListGroup->save_object();
537 }
540 function remove_lock()
541 {
542 if (isset($this->grouptab->dn)){
543 del_lock ($this->grouptab->dn);
544 }
545 }
548 function remove_from_parent()
549 {
550 /* Optionally execute a command after we're done */
551 $this->postremove();
552 }
555 /* Save to LDAP */
556 function save()
557 {
558 /* Optionally execute a command after we're done */
559 $this->postcreate();
560 }
562 /* Unused functions */
563 function check() { }
564 function adapt_from_template($dn) { }
565 function password_change_needed() { }
566 }
567 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
568 ?>