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";
27 var $departments = array();
29 /* Dialog attributes */
30 var $grouptab= NULL;
31 var $grouplist= array();
32 var $ui= NULL;
33 var $acl= "";
35 function groupManagement ($config, $ui)
36 {
37 /* Save configuration for internal use */
38 $this->config= $config;
39 $this->ui= $ui;
41 /* Get global filter config */
42 if (!is_global("groupfilter")){
43 $base= get_base_from_people($ui->dn);
44 $groupfilter= array("primarygroups" => "checked",
45 "mailgroups" => "checked",
46 "sambagroups" => "checked",
47 "appgroups" => "checked",
48 "functionalgroups" => "checked",
49 "guser" => "*",
50 "subsearch" => "",
51 "depselect" => $base,
52 "regex" => "*");
53 register_global("groupfilter", $groupfilter);
54 }
55 }
57 function execute()
58 {
59 /* Save data */
60 $groupfilter= get_global("groupfilter");
61 $s_action = "";
62 $s_entry = "";
65 if (!isset($this->grouptab)){
66 foreach( array("depselect", "guser", "regex") as $type){
67 if (isset($_POST[$type])){
68 $groupfilter[$type]= $_POST[$type];
69 }
70 }
71 if (isset($_POST['regex'])){
72 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
73 "functionalgroups", "subsearch") as $type){
75 if (isset($_POST[$type])) {
76 $groupfilter[$type]= "checked";
77 } else {
78 $groupfilter[$type]= "";
79 }
80 }
81 }
82 if (isset($_GET['search'])){
83 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
84 if ($s == "**"){
85 $s= "*";
86 }
87 $groupfilter['regex']= $s;
88 }
89 register_global("groupfilter", $groupfilter);
90 }
93 /* Test Posts */
94 foreach($_POST as $key => $val){
95 // Post for delete
96 if(preg_match("/group_del.*/",$key)){
97 $s_action = "del";
98 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
99 // Post for edit
100 }elseif(preg_match("/group_edit_.*/",$key)){
101 $s_action="edit";
102 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
103 // Post for new
104 }elseif(preg_match("/dep_back.*/i",$key)){
105 $s_action="back";
106 }elseif(preg_match("/group_new.*/",$key)){
107 $s_action="new";
108 }elseif(preg_match("/dep_home.*/i",$key)){
109 $s_action="home";
110 }elseif(preg_match("/group_tplnew.*/i",$key)){
111 $s_action="new_tpl";
112 }elseif(preg_match("/group_chgpw.*/i",$key)){
113 $s_action="change_pw";
114 $s_entry = preg_replace("/group_chgpw_/i","",$key);
115 }elseif(preg_match("/dep_root.*/i",$key)){
116 $s_action="root";
117 }
118 }
119 $s_entry = preg_replace("/_.$/","",$s_entry);
121 /* Start for New List Managment */
122 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
123 $s_action="open";
124 $s_entry = base64_decode($_GET['dep_id']);
125 $groupfilter['depselect']= "".$this->config->departments[trim($s_entry)];
126 $this->reload();
127 }
129 // Edit if
130 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
131 $s_action ="edit";
132 $s_entry = $_GET['id'];
133 }
135 /* Department changed? */
136 if(isset($_POST['depselect']) && $_POST['depselect']){
137 $groupfilter['depselect']= $_POST['depselect'];
138 $this->reload();
139 }
141 /* Homebutton is posted */
142 if($s_action=="home"){
143 $groupfilter['depselect']= (preg_replace("/^[^,]+,/","",$this->ui->dn));
144 $groupfilter['depselect']= (preg_replace("/^[^,]+,/","",$groupfilter['depselect']));
145 $this->reload();
146 }
148 if($s_action=="root"){
149 $groupfilter['depselect']=($this->config->current['BASE']);
150 $this->reload();
151 }
153 /* If Backbutton is Posted */
154 if($s_action == "back"){
155 $base_back= preg_replace("/^[^,]+,/","",$groupfilter['depselect']);
156 $base_back= convert_department_dn($base_back);
158 if(isset($this->config->departments[trim($base_back)])){
159 $groupfilter['depselect']= $this->config->departments[trim($base_back)];
160 }else{
161 $groupfilter['depselect']= $this->config->departments["/"];
162 }
163 $this->reload();
164 }
165 register_global("groupfilter", $groupfilter);
167 $smarty= get_smarty();
169 /* Prepare formular */
170 if (!isset($this->grouptab) &&
171 !isset($_POST['new_group']) &&
172 !isset($_POST['delete_group']) &&
173 !isset($_POST['select_group'])){
174 $this->reload();
175 }
177 /* Check for exeeded sizelimit */
178 if (($message= check_sizelimit()) != ""){
179 return($message);
180 }
182 /* New group? */
183 if ($s_action=="new"){
185 /* By default we set 'dn' to 'new', all relevant plugins will
186 react on this. */
187 $this->dn= "new";
189 /* Create new usertab object */
190 $this->grouptab= new grouptabs($this->config,
191 $this->config->data['TABS']['GROUPTABS'], $this->dn);
192 $this->grouptab->set_acl(array(':all'));
193 }
195 /* Cancel dialogs */
196 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
197 del_lock ($this->grouptab->dn);
198 unset ($this->grouptab);
199 $this->grouptab= NULL;
200 unset($_SESSION['objectinfo']);
201 }
203 /* Finish group edit is triggered by the tabulator dialog, so
204 the user wants to save edited data. Check and save at this
205 point. */
206 if (isset($_POST['edit_finish'])){
208 /* Check tabs, will feed message array */
209 $message= $this->grouptab->check();
211 /* Save, or display error message? */
212 if (count($message) == 0){
214 /* Save user data to ldap */
215 $this->grouptab->save();
216 gosa_log ("Group object'".$this->dn."' has been saved");
218 /* Group has been saved successfully, remove lock from
219 LDAP. */
220 if ($this->dn != "new"){
221 del_lock ($this->dn);
222 }
224 /* There's no page reload so we have to read new users at
225 this point. */
226 $this->reload ();
227 unset ($this->grouptab);
228 $this->grouptab= NULL;
229 unset ($_SESSION['objectinfo']);
231 } else {
232 /* Ok. There seem to be errors regarding to the tab data,
233 show message and continue as usual. */
234 show_errors($message);
235 }
236 }
238 /* User wants to edit data? */
239 if ($s_action=="edit"){
241 /* Get 'dn' from posted 'uid', must be unique */
242 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
244 /* Check locking, save current plugin in 'back_plugin', so
245 the dialog knows where to return. */
246 if (($user= get_lock($this->dn)) != ""){
247 return(gen_locked_message ($user, $this->dn));
248 }
250 /* Lock the current entry, so everyone will get the
251 above dialog */
252 add_lock ($this->dn, $this->ui->dn);
254 /* Set up the users ACL's for this 'dn' */
255 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
257 /* Register grouptab to trigger edit dialog */
258 $this->grouptab= new grouptabs($this->config,
259 $this->config->data['TABS']['GROUPTABS'], $this->dn);
260 $this->grouptab->set_acl($acl);
261 $_SESSION['objectinfo']= $this->dn;
262 }
264 /* Remove user was requested */
265 if ($s_action=="del"){
266 /* Get 'dn' from posted 'uid' */
267 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
269 /* Load permissions for selected 'dn' and check if
270 we're allowed to remove this 'dn' */
271 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
272 $this->acl= get_module_permission($acl, "group", $this->dn);
273 if (chkacl($this->acl, "delete") == ""){
275 /* Check locking, save current plugin in 'back_plugin', so
276 the dialog knows where to return. */
277 if (($user= get_lock($this->dn)) != ""){
278 return(gen_locked_message ($user, $this->dn));
279 }
281 /* Lock the current entry, so nobody will edit it during deletion */
282 add_lock ($this->dn, $this->ui->dn);
283 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), $this->dn));
284 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
285 } else {
287 /* Obviously the user isn't allowed to delete. Show message and
288 clean session. */
289 print_red (_("You are not allowed to delete this group!"));
290 }
291 }
293 /* Confirmation for deletion has been passed. Group should be deleted. */
294 if (isset($_POST['delete_group_confirm'])){
296 /* Some nice guy may send this as POST, so we've to check
297 for the permissions again. */
298 if (chkacl($this->acl, "delete") == ""){
300 /* Delete request is permitted, perform LDAP action */
301 $this->grouptab= new grouptabs($this->config,
302 $this->config->data['TABS']['GROUPTABS'], $this->dn);
303 $this->grouptab->set_acl(array($this->acl));
304 $this->grouptab->delete ();
305 gosa_log ("Group object'".$this->dn."' has been removed");
306 unset ($this->grouptab);
307 $this->grouptab= NULL;
309 /* Group list has changed, reload it. */
310 $this->reload ();
311 } else {
313 /* Normally this shouldn't be reached, send some extra
314 logs to notify the administrator */
315 print_red (_("You are not allowed to delete this group!"));
316 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
317 }
319 /* Remove lock file after successfull deletion */
320 del_lock ($this->dn);
321 unset($_SESSION['objectinfo']);
322 }
325 /* Delete group canceled? */
326 if (isset($_POST['delete_cancel'])){
327 del_lock ($this->dn);
328 unset($_SESSION['objectinfo']);
329 }
331 /* Show tab dialog if object is present */
332 if ($this->grouptab){
333 $display= $this->grouptab->execute();
335 /* Don't show buttons if tab dialog requests this */
336 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
337 $display.= "<p style=\"text-align:right\">\n";
338 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
339 $display.= " \n";
340 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
341 $display.= "</p>";
342 }
343 return ($display);
344 }
346 /* Prepare departments */
347 $options= "";
348 foreach ($this->config->idepartments as $key => $value){
349 if ($groupfilter['depselect'] == $key){
350 $options.= "<option selected value='$key'>$value</option>";
351 } else {
352 $options.= "<option value='$key'>$value</option>";
353 }
354 }
356 // Managment
357 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
358 " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
359 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
360 " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."'
361 alt='"._("Home")."' name='dep_home'> ".
362 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
363 " <input type='image' align='middle' src='images/list_new_group.png' title='"._("Create new group")."'
364 alt='"._("New")."' name='group_new'> ".
365 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
366 _("Current base")." <select name='depselect' onChange='mainform.submit()'>$options</select>".
367 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
368 "</div>";
371 $actions = "<input type='image' src='images/edit.png' alt='"._("edit")."' name='group_edit_%KEY%' title='"._("Edit this entry")."'>";
372 $actions.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='group_del_%KEY%' title='"._("Delete this entry")."'>";
374 // Defining Links
375 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
377 // image Buttons
378 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
379 $userimg = "<img src='images/select_groups.png' alt='User' title='%s'>";
381 // Extension images
382 $posiximg = "<img src='images/select_groups.png' alt='P' title='"._("Posix") ."'>";
383 $mailimg = "<img src='images/mailto.png' alt='M' title='"._("Mail") ."'>";
384 $sambaimg = "<img src='images/select_winstation.png' alt='S' title='"._("Samba") ."'>";
385 $applimg = "<img src='images/select_application.png' alt='A' title='"._("Application")."'>";
386 $phoneimg = "<img src='images/select_phone.png' alt='Ph' title='"._("Phone") ."'>";
387 // Space
388 $empty = "<img src='images/empty.png' style='width:16px;height:16px;' alt=''>";
391 $divlist = new divlist();
392 $divlist->SetHeader(array(
393 array("string" => " "),
394 array("string" => _("Groupname")." / "._("Department")),
395 array("string" => _("Properties"), "attach" => "style='width:100px;'"),
396 array("string" => _("Actions") ,"attach" => "style='border:none'")
397 ));
400 $divlist->SetSummary(_("This table displays all groups, in the selected tree."));
401 $divlist->SetEntriesPerPage(20);
403 foreach($this->departments as $key=> $val){
405 $field1 = array("string" => "<img src='images/folder.png' alt='department'>");
406 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
407 $field3 = array("string" => " ");
408 $field4 = array("string" => " ","attach"=>"style='text-align:right;border:none'");
410 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
411 }
413 foreach($this->grouplist as $key => $val){
415 if(isset($val['objectClass'])){
416 if(in_array("posixGroup", $val['objectClass'])) $posix = $posiximg; else $posix = $empty;
417 if(in_array("gosaMailAccount", $val['objectClass'])) $mail = $mailimg; else $mail = $empty;
418 if(in_array("sambaGroupMapping", $val['objectClass'])) $samba = $sambaimg; else $samba = $empty;
419 if(in_array("gosaApplicationGroup", $val['objectClass'])) $appl = $applimg; else $appl = $empty;
420 if(in_array("goFonPickupGroup", $val['objectClass'])) $phone = $phoneimg; else $phone = $empty;
421 }else{
422 $posix=$mail=$samba=$appl=$phone=$empty;
423 }
425 $title = "title='gid : ".$key." - dn : ".$val['dn']."'";
427 if(!isset($val['description'][0])){
428 $desc = "";
429 }else{
430 $desc = " - [ ".$val['description'][0]." ]";
431 }
432 $field1 = array("string" => sprintf($userimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
433 $field2 = array("string" => sprintf($editlink,$key,($val['cn']['0'].$desc)),"attach"=>$title);
434 $field3 = array("string" => $posix." ".$mail." ".$samba." ".$appl." ".$phone);
435 $field4 = array("string" => preg_replace("/%KEY%/", $key, $actions),"attach"=>"style='text-align:right;border:none;width:32px;'");
437 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
438 }
440 /* Show main page */
441 $smarty->assign("grouplisthead", $listhead);
442 $smarty->assign("grouplist", $divlist->DrawList());
443 $smarty->assign("search_image", get_template_path('images/search.png'));
444 $smarty->assign("tree_image", get_template_path('images/tree.png'));
445 $smarty->assign("infoimage", get_template_path('images/info.png'));
446 $smarty->assign("launchimage", get_template_path('images/launch.png'));
447 $smarty->assign("deplist", $this->config->idepartments);
448 foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups","appgroups", "sambagroups", "functionalgroups", "subsearch") as $type){
449 $smarty->assign("$type", $groupfilter[$type]);
450 }
452 /* Extend if we are not using javascript */
453 $smarty->assign("apply", apply_filter());
454 $smarty->assign("alphabet", generate_alphabet());
455 $smarty->assign("hint", print_sizelimit_warning());
457 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
458 }
461 function reload()
462 {
463 /* Get config */
464 $groupfilter= get_global('groupfilter');
466 /* Set base for all searches */
467 $base= $groupfilter['depselect'];
469 /* Regex filter? */
470 if ($groupfilter['regex'] != ""){
471 $regex= $groupfilter['regex'];
472 } else {
473 $regex= "*";
474 }
476 /* User filter? */
477 $filter= "";
478 $error= "";
479 $error2= "";
480 $this->grouplist= array();
482 /* What are primary groups? */
483 $primaries= array();
484 $ldap= $this->config->get_ldap_link(TRUE);
485 $ldap->cd($base);
486 $ldap->search("(&(uid=$regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))", array("gidNumber", "cn"));
487 $error= $ldap->error;
488 while ($attrs= $ldap->fetch()){
489 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
490 }
492 if ($groupfilter['primarygroups'] == "checked"){
493 $filter.= "(&(objectClass=posixGroup)(|";
494 foreach ($primaries as $gidNumber => $cn){
495 $filter.= "(gidNumber=$gidNumber)";
496 }
497 $filter.= "))";
498 } else {
499 $filter.= "(&(objectClass=posixGroup)(!(|";
500 foreach ($primaries as $gidNumber => $cn){
501 $filter.= "(gidNumber=$gidNumber)";
502 }
503 $filter.= ")))";
504 }
507 /* Set filter depending on selection */
508 $sfilter= "";
509 if ($groupfilter['appgroups'] == "checked"){
510 $filter.= "(objectClass=gosaApplicationGroup)";
511 }
512 if ($this->config->current['SAMBAVERSION'] == 3){
513 if ($groupfilter['primarygroups'] != "checked"){
514 $sfilter= "(objectClass=sambaGroupMapping)";
515 } elseif ($groupfilter['sambagroups'] == "checked"){
516 $filter.= "(objectClass=sambaGroupMapping)";
517 }
518 }
520 if ($groupfilter['mailgroups'] == "checked"){
521 $filter.= "(objectClass=gosaMailAccount)";
522 }
523 if ($groupfilter['functionalgroups'] == "checked"){
524 $ldap->cd($base);
525 $ldap->set_size_limit($_SESSION['size_limit']);
526 $ldap->search("(&(cn=$regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))", array("cn", "gidNumber", "description"));
527 $error2= $ldap->error;
528 while ($attrs= $ldap->fetch()){
529 if (!isset($primaries[$attrs['gidNumber'][0]])){
530 $this->grouplist[$attrs['gidNumber'][0]]= $attrs;
531 }
532 }
533 }
535 /*
536 * We also need to search for the departments
537 * So we are able to navigate like in konquerer
538 */
539 $base2 = preg_replace("/ou=people,/i","",$base);
541 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
542 TRUE, $base2, array("ou", "description"), TRUE);
544 $this->departments= array();
545 $tmp = array();
546 foreach ($res3 as $value){
547 $tmp[strtolower($value['dn']).$value['dn']]=$value;
548 }
549 ksort($tmp);
550 foreach($tmp as $value){
551 if($value["description"][0]!=".."){
552 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
553 }else{
554 $this->departments[$value['dn']]=$value["description"][0];
555 }
556 }
558 /* Generate grouplist */
559 if ($filter != ""){
560 $filter= "(&(cn=$regex)(objectClass=posixGroup)(|$filter))";
561 if ($groupfilter['guser'] != ""){
562 $filter= "(&(|(memberUID=" . $groupfilter['guser'] . ")(cn=" . $groupfilter['guser'] . "))$filter)";
563 }
564 }
565 if ($groupfilter['subsearch'] == "checked"){
566 $res= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("cn", "description", "gidNumber"), TRUE);
567 } else {
568 $base= get_groups_ou().$base;
569 $res= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("cn", "description", "gidNumber"), TRUE);
570 }
571 if (preg_match("/size limit/i", $error) || preg_match("/size limit/i", $error2)){
572 $_SESSION['limit_exceeded']= TRUE;
573 }
575 foreach ($res as $value){
576 $this->grouplist[$value['gidNumber'][0]]= $value;
577 }
581 $tmp=array();
582 foreach($this->grouplist as $tkey => $val ){
583 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
584 }
585 ksort($tmp);
586 $this->grouplist=array();
587 foreach($tmp as $val){
588 $this->grouplist[]=$val;
589 }
590 reset ($this->grouplist);
591 }
593 function remove_from_parent()
594 {
595 /* Optionally execute a command after we're done */
596 $this->postremove();
597 }
600 /* Save data to object */
601 function save_object()
602 {
603 }
606 /* Check values */
607 function check()
608 {
609 }
612 /* Save to LDAP */
613 function save()
614 {
615 /* Optionally execute a command after we're done */
616 $this->postcreate();
617 }
619 function adapt_from_template($dn)
620 {
621 }
623 function password_change_needed()
624 {
625 }
627 function show_header($button_text, $text, $disabled= FALSE)
628 {
629 }
631 function remove_lock()
632 {
633 if (isset($this->grouptab->dn)){
634 del_lock ($this->grouptab->dn);
635 }
636 }
638 }
640 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
641 ?>