Code

Added support for f*cking colons in the dn
[gosa.git] / plugins / admin / departments / class_departmentGeneric.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 */
21 class department extends plugin
22 {
23   /* department attributes */
24   var $ou= "";
25   var $description= "";
26   var $base= "";
27   var $st= "";
28   var $l= "";
29   var $postalAddress= "";
30   var $businessCategory= "";
31   var $telephoneNumber= "";
32   var $facsimileTelephoneNumber= "";
33   var $orig_dn= "";
35   /* Headpage attributes */
36   var $last_dep_sorting= "invalid";
37   var $departments= array();
39   /* attribute list for save action */
40   var $attributes= array("ou", "description", "businessCategory", "st", "l", "postalAddress",
41                         "telephoneNumber", "facsimileTelephoneNumber");
42   var $objectclasses= array("top", "gosaDepartment", "organizationalUnit");
44   function department ($config, $dn)
45   {
47           plugin::plugin($config, $dn);
48           $this->is_account= TRUE;
49           $this->ui= get_userinfo();
50           $this->dn= $dn;
51           $this->orig_dn= $dn;
52           $this->config= $config;
54           /* Set base */
55           if ($this->dn == "new"){
56                   $ui= get_userinfo();
57                   if(isset($_SESSION['depfilter']['depselect'])){
58                           $this->base = $_SESSION['depfilter']['depselect'];
59                   }else{
60                           $this->base= dn2base($ui->dn);
61                   }
62           } else {
63                   $this->base= preg_replace ("/^[^,]+,/", "", $this->dn);
64           }
65           /* set permissions */
66           $ui= get_userinfo();
67           $acl= get_permissions ($ui->dn, $ui->subtreeACL);
68           $this->acl= get_module_permission($acl, "department", $ui->dn);
69   }
71   function execute()
72   {
73           /* Call parent execute */
74           plugin::execute();
76           /* Reload departments */
77           $this->config->departments= get_departments($this->dn);
78           $this->config->make_idepartments();
79           $smarty= get_smarty();
81           /* Hide all departments, that are subtrees of this department */
82           $bases        = $this->config->idepartments;
83           if(($this->dn == "new")||($this->dn == "")){
84                 $tmp = $bases;
85           }else{
86                   $tmp  = array();      
87                   foreach($bases as $dn=>$base){
88                           $fixed = str_replace("/","\\",$this->dn);
89                           /* Only attach departments which are not a subtree of this one */
90                           if(!preg_match("/".$fixed."/",$dn)){
91                                   $tmp[$dn]=$base;
92                           }
93                   }
94                 }
95           $smarty->assign("bases", $tmp);
97           foreach ($this->attributes as $val){
98                   $smarty->assign("$val", $this->$val);
99                   $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
100           }
101           $smarty->assign("base_select", $this->base);
102           return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
103   }
105   function clear_fields()
106   {
107           $this->dn= "";
108           $this->base= "";
109           $this->acl= "#none#";
111           foreach ($this->attributes as $val){
112                   $this->$val= "";
113           }
114   }
117   function remove_from_parent()
118   {
119           $ldap= $this->config->get_ldap_link();
120           $ldap->cd ($this->dn);
121           $ldap->recursive_remove();
123           /* Optionally execute a command after we're done */
124         $this->handle_post_events('remove');
125   }
128   /* Save data to object */
129   function save_object()
130   {
131         if (isset($_POST['base'])){
132                 plugin::save_object();
134                 /* Save base, since this is no LDAP attribute */
135                 if (chkacl($this->acl, "create") == ""){
136                         $this->base= $_POST['base'];
137                 }
138         }
139   }
142   /* Check values */
143   function check()
144   {
145         $message= array();
147         /* Permissions for that base? */
148 //      $this->dn= "ou=$this->ou,".$this->base;
149         if (chkacl($this->acl, "create") != ""){
150                 $message[]= _("You have no permissions to create a department on this 'Base'.");
151         }
153         /* Check for presence of this department */
154         $ldap= $this->config->get_ldap_link();
155         $attrs= $ldap->cat ($this->dn);
156         if ($this->orig_dn == "new" && !($attrs === FALSE)){
157                 $message[]= _("Department with that 'Name' already exists.");
158         } elseif ($this->orig_dn != $this->dn && !($attrs === FALSE)){
159                 $message[]= _("Department with that 'Name' already exists.");
160         }
162         /* All required fields are set? */
163         if ($this->ou == ""){
164                 $message[]= _("Required field 'Name' is not set.");
165         }
166         if ($this->description == ""){
167                 $message[]= _("Required field 'Description' is not set.");
168         }
170         /* Validate and modify - or: spaghetti rules! */
171         if ($this->ou == "incoming"){
172                 $message[]= _("The field 'Name' contains the reserved word 'incoming'.".
173                                 " Please choose another name.");
174         }
175         if (preg_match ('/[#+:=>\\\\\/]/', $this->ou)){
176                 $message[]= _("The field 'Name' contains invalid characters.");
177         }
178         if (!is_phone_nr($this->telephoneNumber)){
179                 $message[]= _("The field 'Phone' contains an invalid phone number.");
180         }
181         if (!is_phone_nr($this->facsimileTelephoneNumber)){
182                 $message[]= _("The field 'Fax' contains an invalid phone number.");
183         }
185         return $message;
186   }
189   /* Save to LDAP */
190   function save()
191   {
192         plugin::save();
194         /* Write back to ldap */
195         $ldap= $this->config->get_ldap_link();
196         $ldap->cat($this->dn);
197         $a= $ldap->fetch();
198         $ldap->cd($this->dn);
199         if (count($a)){
200                 $ldap->modify($this->attrs);
201                 $this->handle_post_events('modify');
202         } else {
203                 $ldap->add($this->attrs);
204                 $this->handle_post_events('add');
205         }
206         show_ldap_error($ldap->get_error());
208         /* Optionally execute a command after we're done */
209         $this->postcreate();
210   }
214 ?>