Code

Moved to trunk/branches/tags structure
[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           "emptygroups" => "checked",
64           "tcgroups" => "checked",
65           "fongroups" => "checked",
66           "depselect" => $base,
67           "regex" => "*");
68       register_global("ogroupfilter", $ogroupfilter);
69     }
70   }
72   function execute()
73   {
74     /* Prepare template */
75     $smarty= get_smarty();
77     /* Save data */
78     if (is_global("ogroupfilter")){
79       $ogroupfilter= get_global("ogroupfilter");
80     } else {
81       $ogroupfilter= array();
82     }
83     foreach( array("depselect", "regex") as $type){
84       if (isset($_POST[$type])){
85         $ogroupfilter[$type]= $_POST[$type];
86       }
87     }
88     if (isset($_POST['depselect'])){
89       foreach( array("usergroups", "groupgroups", "appgroups", "depgroups", "emptygroups",
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     }
106     register_global("ogroupfilter", $ogroupfilter);
108     /* Prepare formular */
109     if (!isset($this->ogroup) &&
110         !isset($_POST['new_group']) &&
111         !isset($_POST['delete_group']) &&
112         !isset($_POST['select_group'])){
113       $this->reload();
114     }
116     /* Check for exeeded sizelimit */
117     if (($message= check_sizelimit()) != ""){
118       return($message);
119     }
121     /* New group? */
122     if (isset($_POST['new_group'])){
124       /* By default we set 'dn' to 'new', all relevant plugins will
125          react on this. */
126       $this->dn= "new";
128       /* Create new usertab object */
129       $this->ogroup= new ogrouptabs($this->config,
130           $this->config->data['TABS']['OGROUPTABS'], $this->dn);
131       $this->ogroup->set_acl(array(':all'));
132     }
134     if (isset($_POST['delete_group']) && isset($_POST['grouplist'])){
136       /* Get 'dn' from posted 'uid' */
137       $this->dn= trim($_POST['grouplist']);
139       /* Load permissions for selected 'dn' and check if
140          we're allowed to remove this 'dn' */
141       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
142       $this->acl= get_module_permission($acl, "ogroup", $this->dn);
143       if (chkacl($this->acl, "delete") == ""){
145         /* Check locking, save current plugin in 'back_plugin', so
146            the dialog knows where to return. */
147         if (($user= get_lock($this->dn)) != ""){
148           return(gen_locked_message ($user, $this->dn));
149         }
151         /* Lock the current entry, so nobody will edit it during deletion */
152         add_lock ($this->dn, $this->ui->dn);
153         $smarty->assign("info", sprintf(_("You're about to delete the object group '%s'."), $this->dn));
154         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
155       } else {
157         /* Obviously the user isn't allowed to delete. Show message and
158            clean session. */
159         print_red (_("You are not allowed to delete this object group!"));
160       }
161     }
163     /* Confirmation for deletion has been passed. Group should be deleted. */
164     if (isset($_POST['delete_group_confirm'])){
166       /* Some nice guy may send this as POST, so we've to check
167          for the permissions again. */
168       if (chkacl($this->acl, "delete") == ""){
170         /* Delete request is permitted, perform LDAP action */
171         $this->ogroup= new ogrouptabs($this->config,
172             $this->config->data['TABS']['OGROUPTABS'], $this->dn);
173         $this->ogroup->set_acl(array($this->acl));
174         $this->ogroup->delete ();
175         gosa_log ("Object group'".$this->dn."' has been removed");
176         unset ($this->ogroup);
177         $this->ogroup= NULL;
179         /* Group list has changed, reload it. */
180         $this->reload ();
181       } else {
183         /* Normally this shouldn't be reached, send some extra
184            logs to notify the administrator */
185         print_red (_("You are not allowed to delete this object group!"));
186         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
187       }
189       /* Remove lock file after successfull deletion */
190       del_lock ($this->dn);
191       unset($_SESSION['objectinfo']);
192     }
195     /* Delete group canceled? */
196     if (isset($_POST['delete_cancel'])){
197       del_lock ($this->dn);
198       unset($_SESSION['objectinfo']);
199     }
201     if ((isset($_POST['select_group']) || (isset($_POST['edit_helper']) && $_POST['edit_helper'] == "1")) && isset($_POST['grouplist']) && $_POST['grouplist'] != ""){
203       /* Get 'dn' from posted 'uid', must be unique */
204       $this->dn= trim($_POST['grouplist']);
206       /* Check locking, save current plugin in 'back_plugin', so
207          the dialog knows where to return. */
208       if (($user= get_lock($this->dn)) != ""){
209         return(gen_locked_message ($user, $this->dn));
210       }
212       /* Lock the current entry, so everyone will get the
213          above dialog */
214       add_lock ($this->dn, $this->ui->dn);
216       /* Set up the users ACL's for this 'dn' */
217       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
219       /* Register grouptab to trigger edit dialog */
220       $this->ogroup= new ogrouptabs($this->config, $this->config->data['TABS']['OGROUPTABS'],
221           $this->dn);
222       $this->ogroup->set_acl($acl);
223       $_SESSION['objectinfo']= $this->dn;
224     }
226     /* Finish button has been pressed */
227     if (isset($_POST['edit_finish'])){
229       /* Check tabs, will feed message array */
230       $message= $this->ogroup->check();
232       /* Save, or display error message? */
233       if (count($message) == 0){
235         /* Save user data to ldap */
236         $this->ogroup->save();
237         gosa_log ("Object group'".$this->dn."' has been saved");
239         /* Group has been saved successfully, remove lock from
240            LDAP. */
241         if ($this->dn != "new"){
242           del_lock ($this->dn);
243         }
245         /* There's no page reload so we have to read new users at
246            this point. */
247         $this->reload ();
248         unset ($this->ogroup);
249         $this->ogroup= NULL;
250         unset ($_SESSION['objectinfo']);
252       } else {
253         /* Ok. There seem to be errors regarding to the tab data,
254            show message and continue as usual. */
255         show_errors($message);
256       }
257     }
259     /* Cancel dialogs */
260     if (isset($_POST['edit_cancel'])){
261       del_lock ($this->ogroup->dn);
262       unset ($this->ogroup);
263       $this->ogroup= NULL;
264       unset($_SESSION['objectinfo']);
265     }
267     /* Show dialog if object is present */
268     if ($this->ogroup){
269       $display= $this->ogroup->execute();
271       /* Don't show buttons if tab dialog requests this */
272       if (!$this->ogroup->by_object[$this->ogroup->current]->dialog){
273         $display.= "<p style=\"text-align:right\">\n";
274         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
275         $display.= "&nbsp;\n";
276         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
277         $display.= "</p>";
278       }
279       return ($display);
280     }
282     /* Show main page */
283     $smarty->assign("search_image", get_template_path('images/search.png'));
284     $smarty->assign("tree_image", get_template_path('images/tree.png'));
285     $smarty->assign("infoimage", get_template_path('images/info.png'));
286     $smarty->assign("launchimage", get_template_path('images/launch.png'));
287     $smarty->assign("deplist", $this->config->idepartments);
289     $smarty->assign("groups", $this->convert_list($this->grouplist));
291     foreach( array("usergroups", "groupgroups", "appgroups", "depgroups", "emptygroups",
292           "servergroups", "wsgroups", "prtgroups", "tcgroups", "fongroups", "regex", "depselect") as $type){
293       $smarty->assign("$type", $ogroupfilter[$type]);
294     }
296     $smarty->assign("hint", print_sizelimit_warning());
297     $smarty->assign("apply", apply_filter());
298     return ($smarty->fetch (get_template_path('headpage.tpl', TRUE)));
299   }
302   function convert_list($input)
303   {
304     $temp= "";
305     $conv= array(   "U" => "select_user.png",
306         "G" => "select_groups.png",
307         "A" => "select_application.png",
308         "D" => "select_department.png",
309         "S" => "select_server.png",
310         "F" => "select_phone.png",
311         "W" => "select_workstation.png",
312         "T" => "select_terminal.png",
313         "P" => "select_printer.png");
315     foreach ($input as $key => $value){
317       /* Assemble picture */
318       $type= $value['type'];
319       if (isset($type[0])){
320         $p1= $conv[$type[0]];
321       } else {
322         $p1= "empty.png";
323       }
324       if (isset($type[1])){
325         $p2= $conv[$type[1]];
326       } else {
327         $p2= "empty.png";
328       }
330       /* Generate output */
331       $temp= "<option value=\"$key\" class=\"select\" style=\"background-image:url('combine.php?image1=$p1&image2=$p2');padding-left:35px;\">".$value['text']."</option>\n".$temp;
332     }
334     return ($temp);
335   }
338   function reload()
339   {
340     /* Get config */
341     $ogroupfilter= get_global('ogroupfilter');
343     /* Set base for all searches */
344     $base= $ogroupfilter['depselect'];
346     /* Regex filter? */
347     if ($ogroupfilter['regex'] != ""){
348       $regex= $ogroupfilter['regex'];
349     } else {
350       $regex= "*";
351     }
353     /* User filter? */
354     $this->grouplist= array();
356     /* Generate grouplist filter */
357     $filter= "";
359     if ($ogroupfilter['usergroups'] == "checked"){
360       $filter.= "(gosaGroupObjects=*U*)";
361     }
362     if ($ogroupfilter['groupgroups'] == "checked"){
363       $filter.= "(gosaGroupObjects=*G*)";
364     }
365     if ($ogroupfilter['appgroups'] == "checked"){
366       $filter.= "(gosaGroupObjects=*A*)";
367     }
368     if ($ogroupfilter['depgroups'] == "checked"){
369       $filter.= "(gosaGroupObjects=*D*)";
370     }
371     if ($ogroupfilter['servergroups'] == "checked"){
372       $filter.= "(gosaGroupObjects=*S*)";
373     }
374     if ($ogroupfilter['wsgroups'] == "checked"){
375       $filter.= "(gosaGroupObjects=*W*)";
376     }
377     if ($ogroupfilter['prtgroups'] == "checked"){
378       $filter.= "(gosaGroupObjects=*P*)";
379     }
380     if ($ogroupfilter['tcgroups'] == "checked"){
381       $filter.= "(gosaGroupObjects=*T*)";
382     }
383     if ($ogroupfilter['fongroups'] == "checked"){
384       $filter.= "(gosaGroupObjects=*F*)";
385     }
386     if ($ogroupfilter['emptygroups'] == "checked"){
387       $filter.= "(gosaGroupObjects=[])";
388     }
390     if ($filter != ""){
391       $filter= "(&(cn=$regex)(objectClass=gosaGroupOfNames)(|$filter))";
392     } else {
393       $filter= "(&(cn=$regex)(objectClass=gosaGroupOfNames))";
394     }
395     $res= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("cn", "description", "gosaGroupObjects"), TRUE);
396     foreach ($res as $value){
397       $pictype= preg_replace('/[\[\]]/', '', $value["gosaGroupObjects"][0]);
398       $sortstring= preg_split('//', $pictype, -1, PREG_SPLIT_NO_EMPTY);
399       sort($sortstring);
400       $pictype= implode("", $sortstring);
401       if (isset($value["description"][0])){
402         $this->grouplist[$value["dn"]]= array("text" => $value["cn"][0]." [".
403             $value["description"][0]."]", "type" => $pictype);
404       } else {
405         $this->grouplist[$value["dn"]]= array("text" => $value["cn"][0],
406             "type" => $pictype);
407       }
408     }
409     ksort ($this->grouplist);
410     reset ($this->grouplist);
411   }
415 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
416 ?>