Code

Moved to trunk/branches/tags structure
[gosa.git] / plugins / admin / groups / class_groupManagement.inc
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   function groupManagement ($config, $ui)
35   {
36         /* Save configuration for internal use */
37         $this->config= $config;
38         $this->ui= $ui;
40         /* Get global filter config */
41         if (!is_global("groupfilter")){
42                 $base= get_base_from_people($ui->dn);
43                 $groupfilter= array("primarygroups" => "checked",
44                                         "mailgroups" => "checked",
45                                         "sambagroups" => "checked",
46                                         "appgroups" => "checked",
47                                         "functionalgroups" => "checked",
48                                         "guser" => "*",
49                                         "subsearch" => "",
50                                         "depselect" => $base,
51                                         "regex" => "*");
52                 register_global("groupfilter", $groupfilter);
53         }
54   }
56   function execute()
57   {
58         /* Save data */
59         $groupfilter= get_global("groupfilter");
60         foreach( array("depselect", "guser", "regex") as $type){
61                 if (isset($_POST[$type])){
62                         $groupfilter[$type]= $_POST[$type];
63                 } 
64         }
65         if (isset($_POST['depselect'])){
66                 foreach( array("primarygroups", "sambagroups", "mailgroups", "appgroups",
67                                 "functionalgroups", "subsearch") as $type){
69                         if (isset($_POST[$type])) {
70                                 $groupfilter[$type]= "checked";
71                         } else {
72                                 $groupfilter[$type]= "";
73                         }
74                 }
75         }
76         if (isset($_GET['search'])){
77                 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
78                 if ($s == "**"){
79                         $s= "*";
80                 }
81                 $groupfilter['regex']= $s;
82         }
83         register_global("groupfilter", $groupfilter);
85         $smarty= get_smarty();
87         /* Prepare formular */
88         if (!isset($this->grouptab) &&
89             !isset($_POST['new_group']) &&
90             !isset($_POST['delete_group']) &&
91             !isset($_POST['select_group'])){
92                 $this->reload();
93         }
95         /* Check for exeeded sizelimit */
96         if (($message= check_sizelimit()) != ""){
97                 return($message);
98         }
100         /* New group? */
101         if (isset($_POST['new_group'])){
103                 /* By default we set 'dn' to 'new', all relevant plugins will
104                    react on this. */
105                 $this->dn= "new";
107                 /* Create new usertab object */
108                 $this->grouptab= new grouptabs($this->config,
109                                 $this->config->data['TABS']['GROUPTABS'], $this->dn);
110                 $this->grouptab->set_acl(array(':all'));
111         }
113         /* Cancel dialogs */
114         if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
115                 del_lock ($this->grouptab->dn);
116                 unset ($this->grouptab);
117                 $this->grouptab= NULL;
118                 unset($_SESSION['objectinfo']);
119         }
121         /* Finish group edit is triggered by the tabulator dialog, so
122            the user wants to save edited data. Check and save at this
123            point. */
124         if (isset($_POST['edit_finish'])){
126                 /* Check tabs, will feed message array */
127                 $message= $this->grouptab->check();
129                 /* Save, or display error message? */
130                 if (count($message) == 0){
132                         /* Save user data to ldap */
133                         $this->grouptab->save();
134                         gosa_log ("Group object'".$this->dn."' has been saved");
136                         /* Group has been saved successfully, remove lock from
137                            LDAP. */
138                         if ($this->dn != "new"){
139                                 del_lock ($this->dn);
140                         }
142                         /* There's no page reload so we have to read new users at
143                            this point. */
144                         $this->reload ();
145                         unset ($this->grouptab);
146                         $this->grouptab= NULL;
147                         unset ($_SESSION['objectinfo']);
149                 } else {
150                         /* Ok. There seem to be errors regarding to the tab data,
151                            show message and continue as usual. */
152                         show_errors($message);
153                 }
154         }
156         /* User wants to edit data? */
157         if ((isset($_POST['select_group']) || (isset($_POST['edit_helper']) && $_POST['edit_helper'] == "1")) && isset($_POST['grouplist']) && $_POST['grouplist'] != ""){
159                 /* Get 'dn' from posted 'uid', must be unique */
160                 $this->dn= trim($_POST['grouplist']);
162                 /* Check locking, save current plugin in 'back_plugin', so
163                    the dialog knows where to return. */
164                 if (($user= get_lock($this->dn)) != ""){
165                         return(gen_locked_message ($user, $this->dn));
166                 }
168                 /* Lock the current entry, so everyone will get the
169                    above dialog */
170                 add_lock ($this->dn, $this->ui->dn);
172                 /* Set up the users ACL's for this 'dn' */
173                 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
175                 /* Register grouptab to trigger edit dialog */
176                 $this->grouptab= new grouptabs($this->config,
177                                 $this->config->data['TABS']['GROUPTABS'], $this->dn);
178                 $this->grouptab->set_acl($acl);
179                 $_SESSION['objectinfo']= $this->dn;
180         }
182         /* Remove user was requested */
183         if (isset($_POST['delete_group']) && isset($_POST['grouplist'])){
185                 /* Get 'dn' from posted 'uid' */
186                 $this->dn= trim($_POST['grouplist']);
188                 /* Load permissions for selected 'dn' and check if
189                    we're allowed to remove this 'dn' */
190                 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
191                 $this->acl= get_module_permission($acl, "group", $this->dn);
192                 if (chkacl($this->acl, "delete") == ""){
194                         /* Check locking, save current plugin in 'back_plugin', so
195                            the dialog knows where to return. */
196                         if (($user= get_lock($this->dn)) != ""){
197                                 return(gen_locked_message ($user, $this->dn));
198                         }
200                         /* Lock the current entry, so nobody will edit it during deletion */
201                         add_lock ($this->dn, $this->ui->dn);
202                         $smarty->assign("info", sprintf(_("You're about to delete the group '%s'."), $this->dn));
203                         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
204                 } else {
206                         /* Obviously the user isn't allowed to delete. Show message and
207                            clean session. */
208                         print_red (_("You are not allowed to delete this group!"));
209                 }
210         }
212         /* Confirmation for deletion has been passed. Group should be deleted. */
213         if (isset($_POST['delete_group_confirm'])){
215                 /* Some nice guy may send this as POST, so we've to check
216                    for the permissions again. */
217                 if (chkacl($this->acl, "delete") == ""){
219                         /* Delete request is permitted, perform LDAP action */
220                         $this->grouptab= new grouptabs($this->config,
221                                         $this->config->data['TABS']['GROUPTABS'], $this->dn);
222                         $this->grouptab->set_acl(array($this->acl));
223                         $this->grouptab->delete ();
224                         gosa_log ("Group object'".$this->dn."' has been removed");
225                         unset ($this->grouptab);
226                         $this->grouptab= NULL;
228                         /* Group list has changed, reload it. */
229                         $this->reload ();
230                 } else {
232                         /* Normally this shouldn't be reached, send some extra
233                            logs to notify the administrator */
234                         print_red (_("You are not allowed to delete this group!"));
235                         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
236                 }
238                 /* Remove lock file after successfull deletion */
239                 del_lock ($this->dn);
240                 unset($_SESSION['objectinfo']);
241         }
244         /* Delete group canceled? */
245         if (isset($_POST['delete_cancel'])){
246                 del_lock ($this->dn);
247                 unset($_SESSION['objectinfo']);
248         }
250         /* Show tab dialog if object is present */
251         if ($this->grouptab){
252                 $display= $this->grouptab->execute();
254                 /* Don't show buttons if tab dialog requests this */
255                 if (!$this->grouptab->by_object[$this->grouptab->current]->dialog){
256                         $display.= "<p style=\"text-align:right\">\n";
257                         $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
258                         $display.= "&nbsp;\n";
259                         $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
260                         $display.= "</p>";
261                 }
262                 return ($display);
263         }
265         /* Show main page */
266         $smarty->assign("grouplist", $this->grouplist);
267         $smarty->assign("search_image", get_template_path('images/search.png'));
268         $smarty->assign("tree_image", get_template_path('images/tree.png'));
269         $smarty->assign("infoimage", get_template_path('images/info.png'));
270         $smarty->assign("launchimage", get_template_path('images/launch.png'));
271         $smarty->assign("deplist", $this->config->idepartments);
272         foreach( array("depselect", "guser", "regex", "primarygroups", "mailgroups",
273                         "appgroups", "sambagroups", "functionalgroups", "subsearch") as $type){
275                 $smarty->assign("$type", $groupfilter[$type]);
276         }
278         /* Extend if we are not using javascript */
279         $smarty->assign("apply", apply_filter());
280         $smarty->assign("alphabet", generate_alphabet());
281         $smarty->assign("hint", print_sizelimit_warning());
282         
283         return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
284   }
287   function reload()
288   {
289         /* Get config */
290         $groupfilter= get_global('groupfilter');
292         /* Set base for all searches */
293         $base= $groupfilter['depselect'];
295         /* Regex filter? */
296         if ($groupfilter['regex'] != ""){
297                 $regex= $groupfilter['regex'];
298         } else {
299                 $regex= "*";
300         }
302         /* User filter? */
303         $filter= "";
304         $error= "";
305         $error2= "";
306         $this->grouplist= array();
308         /* What are primary groups? */
309         $primaries= array();
310         $ldap= $this->config->get_ldap_link(TRUE);
311         $ldap->cd($base);
312         $ldap->search("(&(uid=$regex)(!(uid=*$))(objectClass=posixAccount)(gidNumber=*))", array("gidNumber", "cn"));
313         $error= $ldap->error;
314         while ($attrs= $ldap->fetch()){
315                 $primaries[$attrs['gidNumber'][0]]= $attrs['cn'][0];
316         }
317         if ($groupfilter['primarygroups'] == "checked"){
318                 $filter.= "(&(objectClass=posixGroup)(|";
319                 foreach ($primaries as $gidNumber => $cn){
320                         $filter.= "(gidNumber=$gidNumber)";
321                 }
322                 $filter.= "))";
323         }
325         /* Set filter depending on selection */
326         if ($groupfilter['appgroups'] == "checked"){
327                 $filter.= "(objectClass=gosaApplicationGroup)";
328         }
329         if ($this->config->current['SAMBAVERSION'] == 3){
330                 $sfilter= "(objectClass=sambaGroupMapping)";
331                 if ($groupfilter['sambagroups'] == "checked"){
332                         $filter.= "(objectClass=sambaGroupMapping)";
333                 }
334         } else {
335                 $sfilter= "";
336         }
337         if ($groupfilter['mailgroups'] == "checked"){
338                 $filter.= "(objectClass=gosaMailAccount)";
339         }
340         if ($groupfilter['functionalgroups'] == "checked"){
341                 $ldap->cd($base);
342                 $ldap->set_size_limit($_SESSION['size_limit']);
343                 $ldap->search("(&(cn=$regex)(objectClass=posixGroup)(!(|(objectClass=gosaMailAccount)(objectClass=gosaApplicationGroup)$sfilter)))", array("cn", "gidNumber", "description"));
344                 $error2= $ldap->error;
345                 while ($attrs= $ldap->fetch()){
346                         if (!isset($primaries[$attrs['gidNumber'][0]])){
347                                 if (isset($attrs["description"][0])){
348                                         $this->grouplist[$attrs["dn"]]= $attrs["cn"][0]." [".
349                                                         $attrs["description"][0]."]";
350                                 } else {
351                                         $this->grouplist[$attrs["dn"]]= $attrs["cn"][0];
352                                 }
353                         }
354                 }
355         }
357         /* Generate grouplist */
358         if ($filter != ""){
359                 $filter= "(&(cn=$regex)(objectClass=posixGroup)(|$filter))";
360                 if ($groupfilter['guser'] != ""){
361                         $filter= "(&(|(memberUID=" . $groupfilter['guser'] . ")(cn=" . $groupfilter['guser'] . "))$filter)";
362                 }
363         }
364         if ($groupfilter['subsearch'] == "checked"){
365                 $res= get_list($this->ui->subtreeACL, "$filter", TRUE, $base, array("cn", "description", "gidNumber"), TRUE);
366         } else {
367                 $base= get_groups_ou().$base;
368                 $res= get_list($this->ui->subtreeACL, "$filter", FALSE, $base, array("cn", "description", "gidNumber"), TRUE);
369         }
370         if (preg_match("/size limit/i", $error) || preg_match("/size limit/i", $error2)){
371                 $_SESSION['limit_exceeded']= TRUE;
372         }
374         foreach ($res as $value){
375                 /* Is primary group? Change description */
376                 if (isset($primaries[$value['gidNumber'][0]])){
377                         $dsc= _("Group of user")." ".$primaries[$value['gidNumber'][0]];
378                         $this->grouplist[$value["dn"]]= $value["cn"][0]." [".$dsc."]";
379                         continue;
380                 }
382                 if (isset($value["description"][0])){
383                         $this->grouplist[$value["dn"]]= $value["cn"][0]." [".
384                                         $value["description"][0]."]";
385                 } else {
386                         $this->grouplist[$value["dn"]]= $value["cn"][0];
387                 }
388         }
389         natcasesort ($this->grouplist);
390         reset ($this->grouplist);
391   }
393   function remove_from_parent()
394   {
395         /* Optionally execute a command after we're done */
396         $this->postremove();
397   }
400   /* Save data to object */
401   function save_object()
402   {
403   }
406   /* Check values */
407   function check()
408   {
409   }
412   /* Save to LDAP */
413   function save()
414   {
415         /* Optionally execute a command after we're done */
416         $this->postcreate();
417   }
419   function adapt_from_template($dn)
420   {
421   }
423   function password_change_needed()
424   {
425   }
427   function show_header($button_text, $text, $disabled= FALSE)
428   {
429   }
431   function remove_lock()
432   {
433         if (isset($this->grouptab->dn)){
434                 del_lock ($this->grouptab->dn);
435         }
436   }
440 ?>