Code

fixed style
[gosa.git] / plugins / admin / ogroups / class_ogroupManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2004 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_ogroups.inc";
22 class ogroupManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline= "Object groups";
26   var $plDescription= "This does something";
28   /* attribute list for save action */
29   var $attributes= array();
30   var $objectclasses= array();
31   var $obtypes= array();
32   var $ogroup;
34   function ogroupManagement ($config, $dn= NULL)
35   {
36     /* Include config object */
37     $this->config= $config;
38     $this->ui= get_userinfo();
40     /* Fill translation array */
41     $this->obtypes= array(      "posixAccount" => _("UNIX accounts"),
42         "posixGroup" => _("Groups"),
43         "gosaDepartment" => _("Departments"),
44         "gosaApplication" => _("Applications"),
45         "goServer" => _("Servers"),
46         "gotoTerminal" => _("Thin Clients"),
47         "gotoWorkstation" => _("Workstations"),
48         "goFonHardware" => _("Phone"),
49         "gotoPrinter" => _("Printer"));
50     asort($this->obtypes);
52     /* Get global filter config */
53     if (!is_global("ogroupfilter")){
54       $ui= get_userinfo();
55       $base= get_base_from_people($ui->dn);
56       $ogroupfilter= array(     "usergroups" => "checked",
57           "groupgroups" => "checked",
58           "appgroups" => "checked",
59           "depgroups" => "checked",
60           "servergroups" => "checked",
61           "wsgroups" => "checked",
62           "prtgroups" => "checked",
63           "tcgroups" => "checked",
64           "fongroups" => "checked",
65           "depselect" => $base,
66           "regex" => "*");
67       register_global("ogroupfilter", $ogroupfilter);
68     }
69   }
71   function execute()
72   {
73         /* Call parent execute */
74         plugin::execute();
76     /* Prepare template */
77     $smarty= get_smarty();
79     $ogroupfilter= get_global("ogroupfilter");
80     $s_action   = "";
81     $s_entry    = "";
83     foreach( array("depselect", "regex") as $type){
84       if (isset($_POST[$type])){
85         $ogroupfilter[$type]= $_POST[$type];
86       }
87     }
88     if (isset($_POST['Ogroup_dialog_posted'])){
89       foreach( array("usergroups", "groupgroups", "appgroups", "depgroups",
90             "servergroups", "wsgroups", "prtgroups", "tcgroups", "fongroups") as $type){
92         if (isset($_POST[$type])) {
93           $ogroupfilter[$type]= "checked";
94         } else {
95           $ogroupfilter[$type]= "";
96         }
97       }
98     }
99     if (isset($_GET['search'])){
100       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
101       if ($s == "**"){
102         $s= "*";
103       }
104       $ogroupfilter['regex']= $s;
105     }
107     /* Test Posts */
108     foreach($_POST as $key => $val){
109       // Post for delete
110       if(preg_match("/group_del.*/",$key)){
111         $s_action = "del";
112         $s_entry  = preg_replace("/group_".$s_action."_/i","",$key);
113         // Post for edit
114       }elseif(preg_match("/group_edit_.*/",$key)){
115         $s_action="edit";
116         $s_entry  = preg_replace("/group_".$s_action."_/i","",$key);
117         // Post for new
118       }elseif(preg_match("/dep_back.*/i",$key)){
119         $s_action="back";
120       }elseif(preg_match("/group_new.*/",$key)){
121         $s_action="new";
122       }elseif(preg_match("/dep_home.*/i",$key)){
123         $s_action="home";
124       }elseif(preg_match("/group_tplnew.*/i",$key)){
125         $s_action="new_tpl";
126       }elseif(preg_match("/group_chgpw.*/i",$key)){
127         $s_action="change_pw";
128         $s_entry  = preg_replace("/group_chgpw_/i","",$key);
129       }elseif(preg_match("/dep_root.*/i",$key)){
130         $s_action="root";
131       }
132     }
133     $s_entry  = preg_replace("/_.$/","",$s_entry);
135     /* Department changed? */
136     if(isset($_POST['depselect']) && $_POST['depselect']){
137       $ogroupfilter['depselect']= $_POST['depselect'];
138     }
140     /* Start for New List Managment */
141     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
142       $s_action="open";
143       $s_entry = base64_decode($_GET['dep_id']);
144       $ogroupfilter['depselect']= "".$this->config->departments[trim($s_entry)];
145     }
147     /* If Backbutton is Posted */
148     if($s_action=="back"){
149       $base_back          = preg_replace("/^[^,]+,/","",$ogroupfilter['depselect']);
150       $base_back          = convert_department_dn($base_back);
152       if(isset($this->config->departments[trim($base_back)])){
153         $ogroupfilter['depselect']= $this->config->departments[trim($base_back)];
154       }else{
155         $ogroupfilter['depselect']= $this->config->departments["/"];
156       }
157       register_global("ogroupfilter", $ogroupfilter);
158     }
160     /* Homebutton is posted */
161     if($s_action=="home"){
162       $ogroupfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
163       $ogroupfilter['depselect']=(preg_replace("/^[^,]+,/","",$ogroupfilter['depselect']));
164     }
166     /* root posted */ 
167     if($s_action=="root"){
168       $ogroupfilter['depselect']=($this->config->current['BASE']);
169     }
171     // Edit if
172     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
173       $s_action ="edit";
174       $s_entry  = $_GET['id'];
175     }
177     register_global("ogroupfilter", $ogroupfilter);
179     $this->reload();
181     /* Check for exeeded sizelimit */
182     if (($message= check_sizelimit()) != ""){
183       return($message);
184     }
186     /* New group? */
187     if ($s_action=="new"){
189       /* By default we set 'dn' to 'new', all relevant plugins will
190          react on this. */
191       $this->dn= "new";
193       /* Create new usertab object */
194       $this->ogroup= new ogrouptabs($this->config,
195           $this->config->data['TABS']['OGROUPTABS'], $this->dn);
196       $this->ogroup->set_acl(array(':all'));
197     }
199     if ($s_action=="del"){
201       /* Get 'dn' from posted 'uid' */
202       $this->dn= $this->ogrouplist[$s_entry]['dn'];
204       /* Load permissions for selected 'dn' and check if
205          we're allowed to remove this 'dn' */
206       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
207       $this->acl= get_module_permission($acl, "ogroup", $this->dn);
208       if (chkacl($this->acl, "delete") == ""){
210         /* Check locking, save current plugin in 'back_plugin', so
211            the dialog knows where to return. */
212         if (($user= get_lock($this->dn)) != ""){
213           return(gen_locked_message ($user, $this->dn));
214         }
216         /* Lock the current entry, so nobody will edit it during deletion */
217         add_lock ($this->dn, $this->ui->dn);
218         $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), $this->dn));
219         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
220       } else {
222         /* Obviously the user isn't allowed to delete. Show message and
223            clean session. */
224         print_red (_("You are not allowed to delete this object group!"));
225       }
226     }
228     /* Confirmation for deletion has been passed. Group should be deleted. */
229     if (isset($_POST['delete_group_confirm'])){
231       /* Some nice guy may send this as POST, so we've to check
232          for the permissions again. */
233       if (chkacl($this->acl, "delete") == ""){
235         /* Delete request is permitted, perform LDAP action */
236         $this->ogroup= new ogrouptabs($this->config,
237             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
238         $this->ogroup->set_acl(array($this->acl));
239         $this->ogroup->delete ();
240         gosa_log ("Object group'".$this->dn."' has been removed");
241         unset ($this->ogroup);
242         $this->ogroup= NULL;
244         /* Group list has changed, reload it. */
245         $this->reload ();
246       } else {
248         /* Normally this shouldn't be reached, send some extra
249            logs to notify the administrator */
250         print_red (_("You are not allowed to delete this object group!"));
251         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
252       }
254       /* Remove lock file after successfull deletion */
255       del_lock ($this->dn);
256       unset($_SESSION['objectinfo']);
257     }
260     /* Delete group canceled? */
261     if (isset($_POST['delete_cancel'])){
262       del_lock ($this->dn);
263       unset($_SESSION['objectinfo']);
264     }
266     if ($s_action=="edit"){
268       /* Get 'dn' from posted 'uid', must be unique */
269       $this->dn= $this->ogrouplist[$s_entry]['dn'];
271       /* Check locking, save current plugin in 'back_plugin', so
272          the dialog knows where to return. */
273       if (($user= get_lock($this->dn)) != ""){
274         return(gen_locked_message ($user, $this->dn));
275       }
277       /* Lock the current entry, so everyone will get the
278          above dialog */
279       add_lock ($this->dn, $this->ui->dn);
281       /* Set up the users ACL's for this 'dn' */
282       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
284       /* Register grouptab to trigger edit dialog */
285       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],
286           $this->dn);
287       $this->ogroup->set_acl($acl);
288       $_SESSION['objectinfo']= $this->dn;
289     }
291     /* Finish button has been pressed */
292     if (isset($_POST['edit_finish'])){
294       /* Check tabs, will feed message array */
295       $message= $this->ogroup->check();
297       /* Save, or display error message? */
298       if (count($message) == 0){
300         /* Save user data to ldap */
301         $this->ogroup->save();
302         gosa_log ("Object group'".$this->dn."' has been saved");
304         /* Group has been saved successfully, remove lock from
305            LDAP. */
306         if ($this->dn != "new"){
307           del_lock ($this->dn);
308         }
310         /* There's no page reload so we have to read new users at
311            this point. */
312         $this->reload ();
313         unset ($this->ogroup);
314         $this->ogroup= NULL;
315         unset ($_SESSION['objectinfo']);
317       } else {
318         /* Ok. There seem to be errors regarding to the tab data,
319            show message and continue as usual. */
320         show_errors($message);
321       }
322     }
324     /* Cancel dialogs */
325     if (isset($_POST['edit_cancel'])){
326       del_lock ($this->ogroup->dn);
327       unset ($this->ogroup);
328       $this->ogroup= NULL;
329       unset($_SESSION['objectinfo']);
330     }
332     /* Show dialog if object is present */
333     if ($this->ogroup){
334       $display= $this->ogroup->execute();
336       /* Don't show buttons if tab dialog requests this */
337       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
338         $display.= "<p style=\"text-align:right\">\n";
339         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
340         $display.= "&nbsp;\n";
341         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
342         $display.= "</p>";
343       }
344       return ($display);
345     }
347     /* Prepare departments */
348     $options= "";
349     foreach ($this->config->idepartments as $key => $value){
350             if ($ogroupfilter['depselect'] == $key){
351                     $options.= "<option selected value='$key'>$value</option>";
352             } else {
353                     $options.= "<option value='$key'>$value</option>";
354             }
355     }
357     // Managment
358     $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
359       " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
360       " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."'
361 name='dep_root' alt='"._("Root")."'>&nbsp;".
362       " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."'
363  alt='"._("Home")."' name='dep_home'>&nbsp;".
364       " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
365       " <input type='image' align='middle' src='images/list_new_ogroup.png' title='"._("Create new object group")."' alt='"._("new")."' name='group_new'>".
366       " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
367       _("Current base")."&nbsp;<select name='depselect' onChange='mainform.submit()'>$options</select>".
368       " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
369       "</div>";
372     $actions = "<input type='image' src='images/edit.png' alt='"._("edit")."' name='group_edit_%KEY%' title='"._("Edit this entry")."'>";
373     $actions.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='group_del_%KEY%' title='"._("Delete this entry")."'>";
375     // Defining Links
376     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
378     // image Buttons
379     $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
380     $userimg  = "<img src='images/select_groups.png' alt='User'    title='%s'>";
382     // Extension images
383     $mailimg  = "<img src='images/mailto.png'             alt='M'  title='"._("Mail")   ."'>";
385     // Space
386     $empty    = "<img src='images/empty.png' style='width:16px;height:16px;' alt=''>";
388     // List Setup
389     $divlist = new divlist("ogroubstab");
390     $divlist->SetHeader(array(
391           array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
392           array("string" => _("Name of object groups")." / "._("Departments"), "attach" => "style=''"),
393           array("string" => _("Properties"), "attach" => "style='width:136px;'"),
394           array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'")
395           ));
398     $divlist->SetSummary(_("This table displays all groups, in the selected tree."));
399     $divlist->SetEntriesPerPage(0);
401     foreach($this->departments as $key=> $val){
403       if(!isset($this->config->departments[trim($key)])){
404         $this->config->departments[trim($key)]="";
405       }
407       $non_empty="";
408       $keys= str_replace("/","\/",$key);
409       foreach($this->config->departments as $keyd=>$vald ){
410         if(preg_match("/".$keys."\/.*/",$keyd)){
411           $non_empty="full";
412         }
413       }
417       $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='"._("Department")."'>", "attach" => "style='text-align:center;width:20px;'");
418       $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
419       $field3 = array("string" => "&nbsp;", "attach" => "style='width:136px;'");
420       $field4 = array("string" => "&nbsp;", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
422       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
423     }
425     // Assigning ogroups 
426     foreach($this->ogrouplist as $key => $val){
428       if(isset($val['mail'])){
429         $mail = $mailimg;
430       }else{
431         $mail = $empty;
432       }
434       $title = "title='dn : ".$val['dn']."'";
436       if(!isset($val['description'][0])){
437         $desc = "";
438       }else{
439         $desc = " - [ ".$val['description'][0]." ]";
440       }
441       $field1 = array("string" => "<img src='images/list_ogroup.png' alt='"._("Object group")."' ".$title.">", "attach" => "style='text-align:center;width:20px;'");
442       $field2 = array("string" => sprintf($editlink,$key,($val['cn']['0'].$desc)), "attach" => "style='' ".$title);
443       $field3 = array("string" => $this->convert_list($val)."&nbsp;".$mail, "attach" => "style='width:136px;'");
444       $field4 = array("string" => preg_replace("/%KEY%/", $key, $actions), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
446       $divlist->AddEntry(array($field1,$field2,$field3,$field4));
447     }
454     /* Show main page */
455     $smarty->assign("search_image", get_template_path('images/search.png'));
456     $smarty->assign("tree_image", get_template_path('images/tree.png'));
457     $smarty->assign("infoimage", get_template_path('images/info.png'));
458     $smarty->assign("launchimage", get_template_path('images/launch.png'));
459     $smarty->assign("deplist", $this->config->idepartments);
461     $smarty->assign("groupshead", $listhead);
462     $smarty->assign("groups", $divlist->DrawList());
464     foreach( array("usergroups", "groupgroups", "appgroups", "depgroups",
465           "servergroups", "wsgroups", "prtgroups", "tcgroups", "fongroups", "regex", "depselect") as $type){
466       $smarty->assign("$type", $ogroupfilter[$type]);
467     }
469     $smarty->assign("hint", print_sizelimit_warning());
470     $smarty->assign("apply", apply_filter());
471     $smarty->assign("alphabet", generate_alphabet());
472     return ($smarty->fetch (get_template_path('headpage.tpl', TRUE)));
473   }
476   function convert_list($input)
477   {
478     $temp= "";
481     $conv= array(   
482         "U" => array("select_user.png",_("User")),
483         "G" => array("select_groups.png",_("Group")),
484         "A" => array("select_application.png",_("Application")),
485         "D" => array("select_department.png",_("Department")),
486         "S" => array("select_server.png",_("Server")),
487         "F" => array("select_phone.png",_("Phone")),
488         "W" => array("select_workstation.png",_("Workstation")),
489         "T" => array("select_terminal.png",_("Terminal")),
490         "P" => array("select_printer.png",_("Printer")));
492     /* Assemble picture */
493     $type= $input['gosaGroupObjects'][0];
494     $type= preg_replace("/[^A-Z]/i","",$type);
495     if (isset($type[0])){
496       $p1['pic']= $conv[$type[0]][0];
497       $p1['tit']= $conv[$type[0]][1];
498       $p1['alt']= $type[0];
499     } else {
500       $p1['pic']= "empty.png";
501       $p1['tit']= "";
502       $p1['alt']= "";
503     }
504     if (isset($type[1])){
505       $p2['pic']= $conv[$type[1]][0];
506       $p2['alt']= $type[1];
507       $p2['tit']= $conv[$type[1]][1];
508     } else {
509       $p2['pic']= "empty.png";
510       $p2['alt']= "";
511       $p2['tit']= "";
512     }
513     $temp = "<img src='images/".$p1['pic']."' alt='".$p1['alt']."' title='".$p1['tit']."'>";
514     $temp.= "<img src='images/".$p2['pic']."' alt='".$p2['alt']."' title='".$p2['tit']."'>";
515     return ($temp);
516   }
519   function reload()
520   {
521     /* Get config */
522     $ogroupfilter= get_global('ogroupfilter');
524     /* Set base for all searches */
525     $base= $ogroupfilter['depselect'];
527     /* Regex filter? */
528     if ($ogroupfilter['regex'] != ""){
529       $regex= $ogroupfilter['regex'];
530     } else {
531       $regex= "*";
532     }
534     /* User filter? */
535     $this->oogrouplist= array();
537     /* Generate oogrouplist filter */
538     $filter= "";
540     if ($ogroupfilter['usergroups'] == "checked"){
541       $filter.= "(gosaGroupObjects=*U*)";
542     }
543     if ($ogroupfilter['groupgroups'] == "checked"){
544       $filter.= "(gosaGroupObjects=*G*)";
545     }
546     if ($ogroupfilter['appgroups'] == "checked"){
547       $filter.= "(gosaGroupObjects=*A*)";
548     }
549     if ($ogroupfilter['depgroups'] == "checked"){
550       $filter.= "(gosaGroupObjects=*D*)";
551     }
552     if ($ogroupfilter['servergroups'] == "checked"){
553       $filter.= "(gosaGroupObjects=*S*)";
554     }
555     if ($ogroupfilter['wsgroups'] == "checked"){
556       $filter.= "(gosaGroupObjects=*W*)";
557     }
558     if ($ogroupfilter['prtgroups'] == "checked"){
559       $filter.= "(gosaGroupObjects=*P*)";
560     }
561     if ($ogroupfilter['tcgroups'] == "checked"){
562       $filter.= "(gosaGroupObjects=*T*)";
563     }
564     if ($ogroupfilter['fongroups'] == "checked"){
565       $filter.= "(gosaGroupObjects=*F*)";
566     }
568     if ($filter != ""){
569       $filter= "(&(cn=$regex)(objectClass=gosaGroupOfNames)(|$filter))";
570     } else {
571       $filter= "(&(cn=$regex)(objectClass=gosaGroupOfNames))";
572     }
573     
574    $groups = ($this->config->current['GROUPS']);
576     $res= get_list($this->ui->subtreeACL, "$filter",FALSE, $groups.",".$base, array("*"), TRUE);
577     $this->ogrouplist= $res;
578     ksort ($this->ogrouplist);
579     reset ($this->ogrouplist);
582     /* NEW LIST MANAGMENT
583      * We also need to search for the departments
584      * So we are able to navigate like in konquerer
585      */
586     $base2 = preg_replace("/ou=people,/i","",$base);
588     $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
589                               TRUE, $base2, array("ou", "description"), TRUE);
591    
593     $this->departments= array();
594     $tmp = array();
595     foreach ($res3 as $value){
596       $tmp[strtolower($value['dn']).$value['dn']]=$value;
597     }
598     ksort($tmp);
599     foreach($tmp as $value){
600       if($value["description"][0]!=".."){
601         $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
602       }else{
603         $this->departments[$value['dn']]=$value["description"][0];
604       }
605     }
606     /* END NEW LIST MANAGMENT
607      */
609     $tmp=array();
610     foreach($this->ogrouplist as $tkey => $val ){
611       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
612     }
613     ksort($tmp);
614     $this->ogrouplist=array();
615     foreach($tmp as $val){
616       $this->ogrouplist[]=$val;
617     }
618     reset ($this->ogrouplist);
622   }
626 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
627 ?>