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 /* Call parent execute */
60 plugin::execute();
61 /* Save data */
62 $groupfilter= get_global("groupfilter");
63 $s_action = "";
64 $s_entry = "";
67 if (!isset($this->grouptab)){
68 foreach( array("depselect", "guser", "regex") as $type){
69 if (isset($_POST[$type])){
70 $groupfilter[$type]= $_POST[$type];
71 }
72 }
73 if (isset($_POST['regex'])){
74 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
75 "functionalgroups", "subsearch") as $type){
77 if (isset($_POST[$type])) {
78 $groupfilter[$type]= "checked";
79 } else {
80 $groupfilter[$type]= "";
81 }
82 }
83 }
84 if (isset($_GET['search'])){
85 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
86 if ($s == "**"){
87 $s= "*";
88 }
89 $groupfilter['regex']= $s;
90 }
91 register_global("groupfilter", $groupfilter);
92 }
95 /* Test Posts */
96 foreach($_POST as $key => $val){
97 // Post for delete
98 if(preg_match("/group_del.*/",$key)){
99 $s_action = "del";
100 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
101 // Post for edit
102 }elseif(preg_match("/group_edit_.*/",$key)){
103 $s_action="edit";
104 $s_entry = preg_replace("/group_".$s_action."_/i","",$key);
105 // Post for new
106 }elseif(preg_match("/dep_back.*/i",$key)){
107 $s_action="back";
108 }elseif(preg_match("/group_new.*/",$key)){
109 $s_action="new";
110 }elseif(preg_match("/dep_home.*/i",$key)){
111 $s_action="home";
112 }elseif(preg_match("/group_tplnew.*/i",$key)){
113 $s_action="new_tpl";
114 }elseif(preg_match("/group_chgpw.*/i",$key)){
115 $s_action="change_pw";
116 $s_entry = preg_replace("/group_chgpw_/i","",$key);
117 }elseif(preg_match("/dep_root.*/i",$key)){
118 $s_action="root";
119 }
120 }
121 $s_entry = preg_replace("/_.$/","",$s_entry);
123 /* Start for New List Managment */
124 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
125 $s_action="open";
126 $s_entry = base64_decode($_GET['dep_id']);
127 $groupfilter['depselect']= "".$this->config->departments[trim($s_entry)];
128 }
130 // Edit if
131 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
132 $s_action ="edit";
133 $s_entry = $_GET['id'];
134 }
136 /* Department changed? */
137 if(isset($_POST['depselect']) && $_POST['depselect']){
138 $groupfilter['depselect']= $_POST['depselect'];
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 }
147 if($s_action=="root"){
148 $groupfilter['depselect']=($this->config->current['BASE']);
149 }
151 /* If Backbutton is Posted */
152 if($s_action == "back"){
153 $base_back= preg_replace("/^[^,]+,/","",$groupfilter['depselect']);
154 $base_back= convert_department_dn($base_back);
156 if(isset($this->config->departments[trim($base_back)])){
157 $groupfilter['depselect']= $this->config->departments[trim($base_back)];
158 }else{
159 $groupfilter['depselect']= $this->config->departments["/"];
160 }
161 }
162 register_global("groupfilter", $groupfilter);
164 $smarty= get_smarty();
166 /* Prepare formular */
167 $this->reload();
169 /* Check for exeeded sizelimit */
170 if (($message= check_sizelimit()) != ""){
171 return($message);
172 }
174 /* New group? */
175 if ($s_action=="new"){
177 /* By default we set 'dn' to 'new', all relevant plugins will
178 react on this. */
179 $this->dn= "new";
181 /* Create new usertab object */
182 $this->grouptab= new grouptabs($this->config,
183 $this->config->data['TABS']['GROUPTABS'], $this->dn);
184 $this->grouptab->set_acl(array(':all'));
185 }
187 /* Cancel dialogs */
188 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
189 del_lock ($this->grouptab->dn);
190 unset ($this->grouptab);
191 $this->grouptab= NULL;
192 unset($_SESSION['objectinfo']);
193 }
195 /* Finish group edit is triggered by the tabulator dialog, so
196 the user wants to save edited data. Check and save at this
197 point. */
198 if (isset($_POST['edit_finish'])){
200 /* Check tabs, will feed message array */
201 $message= $this->grouptab->check();
203 /* Save, or display error message? */
204 if (count($message) == 0){
206 /* Save user data to ldap */
207 $this->grouptab->save();
208 gosa_log ("Group object'".$this->dn."' has been saved");
210 /* Group has been saved successfully, remove lock from
211 LDAP. */
212 if ($this->dn != "new"){
213 del_lock ($this->dn);
214 }
216 /* There's no page reload so we have to read new users at
217 this point. */
218 $this->reload ();
219 unset ($this->grouptab);
220 $this->grouptab= NULL;
221 unset ($_SESSION['objectinfo']);
223 } else {
224 /* Ok. There seem to be errors regarding to the tab data,
225 show message and continue as usual. */
226 show_errors($message);
227 }
228 }
230 /* User wants to edit data? */
231 if ($s_action=="edit"){
233 /* Get 'dn' from posted 'uid', must be unique */
234 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
236 /* Check locking, save current plugin in 'back_plugin', so
237 the dialog knows where to return. */
238 if (($user= get_lock($this->dn)) != ""){
239 return(gen_locked_message ($user, $this->dn));
240 }
242 /* Lock the current entry, so everyone will get the
243 above dialog */
244 add_lock ($this->dn, $this->ui->dn);
246 /* Set up the users ACL's for this 'dn' */
247 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
249 /* Register grouptab to trigger edit dialog */
250 $this->grouptab= new grouptabs($this->config,
251 $this->config->data['TABS']['GROUPTABS'], $this->dn);
252 $this->grouptab->set_acl($acl);
253 $_SESSION['objectinfo']= $this->dn;
254 }
256 /* Remove user was requested */
257 if ($s_action=="del"){
258 /* Get 'dn' from posted 'uid' */
259 $this->dn= $this->grouplist[trim($s_entry)]['dn'];
261 /* Load permissions for selected 'dn' and check if
262 we're allowed to remove this 'dn' */
263 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
264 $this->acl= get_module_permission($acl, "group", $this->dn);
265 if (chkacl($this->acl, "delete") == ""){
267 /* Check locking, save current plugin in 'back_plugin', so
268 the dialog knows where to return. */
269 if (($user= get_lock($this->dn)) != ""){
270 return(gen_locked_message ($user, $this->dn));
271 }
273 /* Lock the current entry, so nobody will edit it during deletion */
274 add_lock ($this->dn, $this->ui->dn);
275 $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), $this->dn));
276 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
277 } else {
279 /* Obviously the user isn't allowed to delete. Show message and
280 clean session. */
281 print_red (_("You are not allowed to delete this group!"));
282 }
283 }
285 /* Confirmation for deletion has been passed. Group should be deleted. */
286 if (isset($_POST['delete_group_confirm'])){
288 /* Some nice guy may send this as POST, so we've to check
289 for the permissions again. */
290 if (chkacl($this->acl, "delete") == ""){
292 /* Delete request is permitted, perform LDAP action */
293 $this->grouptab= new grouptabs($this->config,
294 $this->config->data['TABS']['GROUPTABS'], $this->dn);
295 $this->grouptab->set_acl(array($this->acl));
296 $this->grouptab->delete ();
297 gosa_log ("Group object'".$this->dn."' has been removed");
298 unset ($this->grouptab);
299 $this->grouptab= NULL;
301 /* Group list has changed, reload it. */
302 $this->reload ();
303 } else {
305 /* Normally this shouldn't be reached, send some extra
306 logs to notify the administrator */
307 print_red (_("You are not allowed to delete this group!"));
308 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
309 }
311 /* Remove lock file after successfull deletion */
312 del_lock ($this->dn);
313 unset($_SESSION['objectinfo']);
314 }
317 /* Delete group canceled? */
318 if (isset($_POST['delete_cancel'])){
319 del_lock ($this->dn);
320 unset($_SESSION['objectinfo']);
321 }
323 /* Show tab dialog if object is present */
324 if ($this->grouptab){
325 $display= $this->grouptab->execute();
327 /* Don't show buttons if tab dialog requests this */
328 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
329 $display.= "<p style=\"text-align:right\">\n";
330 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
331 $display.= " \n";
332 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
333 $display.= "</p>";
334 }
335 return ($display);
336 }
338 /* Prepare departments */
339 $options= "";
340 foreach ($this->config->idepartments as $key => $value){
341 if ($groupfilter['depselect'] == $key){
342 $options.= "<option selected value='$key'>$value</option>";
343 } else {
344 $options.= "<option value='$key'>$value</option>";
345 }
346 }
348 // Managment
349 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
350 " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
351 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
352 " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."'
353 alt='"._("Home")."' name='dep_home'> ".
354 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
355 " <input type='image' align='middle' src='images/list_new_group.png' title='"._("Create new group")."'
356 alt='"._("New")."' name='group_new'> ".
357 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
358 _("Current base")." <select name='depselect' onChange='mainform.submit()'>$options</select>".
359 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
360 "</div>";
363 $actions = "<input type='image' src='images/edit.png' alt='"._("edit")."' name='group_edit_%KEY%' title='"._("Edit this entry")."'>";
364 $actions.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='group_del_%KEY%' title='"._("Delete this entry")."'>";
366 // Defining Links
367 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
369 // image Buttons
370 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
371 $userimg = "<img src='images/select_groups.png' alt='User' title='%s'>";
373 // Extension images
374 $posiximg = "<img src='images/select_groups.png' alt='P' title='"._("Posix") ."'>";
375 $mailimg = "<img src='images/mailto.png' alt='M' title='"._("Mail") ."'>";
376 $sambaimg = "<img src='images/select_winstation.png' alt='S' title='"._("Samba") ."'>";
377 $applimg = "<img src='images/select_application.png' alt='A' title='"._("Application")."'>";
378 $phoneimg = "<img src='images/select_phone.png' alt='Ph' title='"._("Phone") ."'>";
379 $envimg = "<img src='images/network.png' alt='E' title='"._("Environment") ."'>";
380 // Space
381 $empty = "<img src='images/empty.png' style='width:16px;height:16px;' alt=''>";
384 $divlist = new divlist("groupstab");
385 $divlist->SetHeader(array(
386 array("string" => " "),
387 array("string" => _("Groupname")." / "._("Department")),
388 array("string" => _("Properties"), "attach" => "style='width:116px;'"),
389 array("string" => _("Actions") ,"attach" => "style='border:none'")
390 ));
393 $divlist->SetSummary(_("This table displays all groups, in the selected tree."));
394 $divlist->SetEntriesPerPage(0);
396 foreach($this->departments as $key=> $val){
398 if(!isset($this->config->departments[trim($key)])){
399 $this->config->departments[trim($key)]="";
400 }
402 $non_empty="";
403 $keys= str_replace("/","\/",$key);
404 foreach($this->config->departments as $keyd=>$vald ){
405 if(preg_match("/".$keys."\/.*/",$keyd)){
406 $non_empty="full";
407 }
408 }
411 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>");
412 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
413 $field3 = array("string" => " ");
414 $field4 = array("string" => " ","attach"=>"style='text-align:right;border:none'");
416 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
417 }
419 foreach($this->grouplist as $key => $val){
421 if(isset($val['objectClass'])){
422 if(in_array("posixGroup", $val['objectClass'])) $posix = $posiximg; else $posix = $empty;
423 if(in_array("gosaMailAccount", $val['objectClass'])) $mail = $mailimg; else $mail = $empty;
424 if(in_array("sambaGroupMapping", $val['objectClass'])) $samba = $sambaimg; else $samba = $empty;
425 if(in_array("gosaApplicationGroup", $val['objectClass'])) $appl = $applimg; else $appl = $empty;
426 if(in_array("goFonPickupGroup", $val['objectClass'])) $phone = $phoneimg; else $phone = $empty;
427 if(in_array("gotoEnvironment", $val['objectClass'])) $enviro= $envimg; else $enviro = $empty;
428 }else{
429 $posix=$mail=$samba=$appl=$phone=$empty;
430 }
432 $title = "title='gid : ".$key." - dn : ".$val['dn']."'";
434 if(!isset($val['description'][0])){
435 $desc = "";
436 }else{
437 $desc = " - [ ".$val['description'][0]." ]";
438 }
439 $field1 = array("string" => sprintf($userimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
440 $field2 = array("string" => sprintf($editlink,$key,($val['cn']['0'].$desc)),"attach"=>$title);
441 $field3 = array("string" => $posix." ".$enviro." ".$mail." ".$samba." ".$appl." ".$phone);
442 $field4 = array("string" => preg_replace("/%KEY%/", $key, $actions),"attach"=>"style='text-align:right;border:none;width:32px;'");
444 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
445 }
447 /* Show main page */
448 $smarty->assign("grouplisthead", $listhead);
449 $smarty->assign("grouplist", $divlist->DrawList());
450 $smarty->assign("search_image", get_template_path('images/search.png'));
451 $smarty->assign("searchu_image", get_template_path('images/search_user.png'));
452 $smarty->assign("tree_image", get_template_path('images/tree.png'));
453 $smarty->assign("infoimage", get_template_path('images/info.png'));
454 $smarty->assign("launchimage", get_template_path('images/launch.png'));
455 $smarty->assign("deplist", $this->config->idepartments);
456 foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups","appgroups", "sambagroups", "functionalgroups", "subsearch") as $type){
457 $smarty->assign("$type", $groupfilter[$type]);
458 }
460 /* Extend if we are not using javascript */
461 $smarty->assign("apply", apply_filter());
462 $smarty->assign("alphabet", generate_alphabet());
463 $smarty->assign("hint", print_sizelimit_warning());
465 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
466 }
468 function reload($CreatePosixsList=false)
469 {
470 /* Get config */
471 $groupfilter= get_global('groupfilter');
473 /* Set base for all searches */
474 $base= $groupfilter['depselect'];
476 /* Regex filter? */
477 if ($groupfilter['regex'] != ""){
478 $regex= $groupfilter['regex'];
479 } else {
480 $regex= "*";
481 }
483 /* User filter? */
484 $filter= "(objectclass=posixGroup)";
485 $error= "";
486 $error2= "";
487 $this->grouplist= array();
489 /* What are primary groups? */
490 $primaries= array();
491 $ldap= $this->config->get_ldap_link();
492 $ldap->cd($base);
493 $ldap->search("(&(uid=$regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))", array("gidNumber", "cn"));
494 show_ldap_error($ldap->get_error());
495 while ($attrs= $ldap->fetch()){
496 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
497 }
499 /* Set filter depending on selection */
500 $sfilter= "";
501 if ($groupfilter['appgroups'] == "checked"){
502 $filter.= "(objectClass=gosaApplicationGroup)";
503 }
504 if ($this->config->current['SAMBAVERSION'] == 3){
505 if ($groupfilter['primarygroups'] != "checked"){
506 $sfilter= "(objectClass=sambaGroupMapping)";
507 } elseif ($groupfilter['sambagroups'] == "checked"){
508 $filter.= "(objectClass=sambaGroupMapping)";
509 }
510 }
512 if ($groupfilter['mailgroups'] == "checked"){
513 $filter.= "(objectClass=gosaMailAccount)";
514 }
515 $functional= array();
516 $ldap->cd(get_groups_ou().$base);
517 $ldap->set_size_limit($_SESSION['size_limit']);
518 $ldap->search("(&(cn=$regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))", array("cn", "gidNumber", "description"));
519 $error2= $ldap->error;
520 while ($attrs= $ldap->fetch()){
521 if (!isset($primaries[$attrs['gidNumber'][0]])){
522 $functional[$attrs['gidNumber'][0]]= $attrs['gidNumber'][0];
523 }
524 }
526 /*
527 * We also need to search for the departments
528 * So we are able to navigate like in konquerer
529 */
531 /* Only needed if we are in GroupManagement */
532 if(!$CreatePosixsList){
533 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
534 FALSE, $base, array("ou", "description"), TRUE);
535 $this->departments= array();
536 $tmp = array();
537 foreach ($res3 as $value){
538 $tmp[strtolower($value['dn']).$value['dn']]=$value;
539 }
540 ksort($tmp);
541 foreach($tmp as $value){
542 if($value["description"][0]!=".."){
543 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
544 }else{
545 $this->departments[$value['dn']]=$value["description"][0];
546 }
547 }
548 }
549 /* Ende department generation
550 */
552 /* Generate grouplist */
553 if ($filter != ""){
554 $filter= "(&(cn=$regex)(objectClass=posixGroup)(|$filter))";
555 if ($groupfilter['guser'] != ""){
556 $filter= "(&(|(memberUID=" . $groupfilter['guser'] . ")(cn=" . $groupfilter['guser'] . "))$filter)";
557 }
558 }
560 /* Depending on $CreatePosixsList we have to create different lists */
562 if ($groupfilter['subsearch'] == "checked"){
563 $res= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("cn", "description", "gidNumber"), TRUE);
564 } else {
565 $base= get_groups_ou().$base;
566 $res= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("cn", "description", "gidNumber"), TRUE);
567 }
568 if (preg_match("/size limit/i", $error) || preg_match("/size limit/i", $error2)){
569 $_SESSION['limit_exceeded']= TRUE;
570 }
572 foreach ($res as $value){
573 if ($groupfilter['functionalgroups'] != "checked" && isset($functional[$value['gidNumber'][0]])){
574 continue;
575 }
576 if ($groupfilter['primarygroups'] == "checked"){
577 $this->grouplist[$value['gidNumber'][0]]= $value;
578 } else {
579 if (!isset($primaries[$value['gidNumber'][0]])){
580 $this->grouplist[$value['gidNumber'][0]]= $value;
581 }
582 }
583 }
585 $tmp=array();
587 /* If true ; this result is used for posix tab, group membership selection. */
588 if($CreatePosixsList){
590 /* Addd to array */
591 foreach($this->grouplist as $tkey => $val ){
592 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
593 }
595 /* Sort index */
596 ksort($tmp);
598 /* Recreate index array[dn]=cn[description]*/
599 $this->grouplist=array();
600 foreach($tmp as $val){
601 if(isset($val['description'])){
602 $this->grouplist[$val['dn']]=$val['cn'][0]." [".$val['description'][0]."]";
603 }else{
604 $this->grouplist[$val['dn']]=$val['cn'][0];
605 }
606 }
607 }else{
609 /* Create result for group management listbox*/
610 foreach($this->grouplist as $tkey => $val ){
611 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
612 }
613 ksort($tmp);
614 $this->grouplist=array();
615 foreach($tmp as $val){
616 $this->grouplist[]=$val;
617 }
618 reset ($this->grouplist);
619 }
620 }
622 function remove_from_parent()
623 {
624 /* Optionally execute a command after we're done */
625 $this->postremove();
626 }
629 /* Save data to object */
630 function save_object()
631 {
632 }
635 /* Check values */
636 function check()
637 {
638 }
641 /* Save to LDAP */
642 function save()
643 {
644 /* Optionally execute a command after we're done */
645 $this->postcreate();
646 }
648 function adapt_from_template($dn)
649 {
650 }
652 function password_change_needed()
653 {
654 }
656 function show_header($button_text, $text, $disabled= FALSE)
657 {
658 }
660 function remove_lock()
661 {
662 if (isset($this->grouptab->dn)){
663 del_lock ($this->grouptab->dn);
664 }
665 }
667 }
669 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
670 ?>