Code

cd7004384d31ac89aec92209e5f76146d50b6e88
[gosa.git] / plugins / admin / systems / class_componentGeneric.inc
1 <?php
3 class componentGeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage component base objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $interfaces= array();
12   var $ignore_account= TRUE;
14   /* Needed values and lists */
15   var $base= "";
16   var $cn= "";
17   var $description= "";
18   var $orig_dn= "";
20   /* attribute list for save action */
21   var $attributes= array("cn", "description");
22   var $objectclasses= array("top", "device", "ipHost", "ieee802Device");
23   var $netConfigDNS;
25   function componentgeneric ($config, $dn= NULL, $parent= NULL)
26   {
27     plugin::plugin ($config, $dn, $parent);
29     /* Set base */
30     if ($this->dn == "new"){
31       $ui= get_userinfo();
32       $this->base= dn2base($ui->dn);
33       $this->cn= "";
34     } else {
35       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
36     }
37     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses, true);
38     /* Save dn for later references */
39     $this->orig_dn= $this->dn;
40   }
42   function execute()
43   {
44         /* Call parent execute */
45         plugin::execute();
47     /* Do we represent a valid phone? */
48     if (!$this->is_account && $this->parent == NULL){
49       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
50         _("This 'dn' has no network features.")."</b>";
51       return($display);
52     }
54     /* Base select dialog */
55     $once = true;
56     foreach($_POST as $name => $value){
57       if(preg_match("/^chooseBase/",$name) && $once){
58         $once = false;
59         $this->dialog = new baseSelectDialog($this->config,$this);
60         $this->dialog->setCurrentBase($this->base);
61       }
62     }
64     /* Dialog handling */
65     if(is_object($this->dialog)){
66       /* Must be called before save_object */
67       $this->dialog->save_object();
69       if($this->dialog->isClosed()){
70         $this->dialog = false;
71       }elseif($this->dialog->isSelected()){
72         $this->base = $this->dialog->isSelected();
73         $this->dialog= false;
74       }else{
75         return($this->dialog->execute());
76       }
77     }
79     /* Fill templating stuff */
80     $smarty= get_smarty();
81     $smarty->assign("bases", $this->config->idepartments);
83     /* Create base acls */
84     $baseACL = $this->getacl("base");
85     if(!$this->acl_is_moveable()) {
86       $baseACL = preg_replace("/w/","",$baseACL);
87     }
88     $smarty->assign("baseACL",          $baseACL);
90     /* Set acls */
91     $tmp = $this->plInfo();
92     foreach($tmp['plProvidedAcls'] as $name => $translation){
93       $smarty->assign($name."ACL",$this->getacl($name));
94     }
96     $smarty->assign("bases", $this->get_allowed_bases());
99     /* Assign attributes */
100     foreach ($this->attributes as $attr){
101       $smarty->assign("$attr", $this->$attr);
102     }
103     $smarty->assign("base_select", $this->base);
105     /* Show main page */
106     $smarty->assign("netconfig", $this->netConfigDNS->execute());
107     return($smarty->fetch (get_template_path('component.tpl', TRUE)));
108   }
110   function set_acl_base($base)
111   {
112     plugin::set_acl_base($base);
113     $this->netConfigDNS->set_acl_base($base);
114   }
116   function set_acl_category($cat)
117   {
118     plugin::set_acl_category($cat);
119     $this->netConfigDNS->set_acl_category($cat);
120   }
122   function remove_from_parent()
123   {
124     $ldap= $this->config->get_ldap_link();
125     $this->netConfigDNS->remove_from_parent();
126     $ldap->rmdir($this->dn);
127     show_ldap_error($ldap->get_error(), sprintf(_("Removing of system component/generic with dn '%s' failed."),$this->dn));
128     $this->handle_post_events("remove");
130     /* Delete references to object groups */
131     $ldap->cd ($this->config->current['BASE']);
132     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
133     while ($ldap->fetch()){
134       $og= new ogroup($this->config, $ldap->getDN());
135       unset($og->member[$this->dn]);
136       $og->save ();
137     }
138   }
141   /* Save data to object */
142   function save_object()
143   {
144     plugin::save_object();
146     $this->netConfigDNS->save_object();
148     /* Save base, since this is no LDAP attribute */
149     if (isset($_POST['base']) && $this->acl_is_moveable()){
150       $this->base= $_POST['base'];
151     }
152   }
155   /* Check supplied data */
156   function check()
157   {
158     /* Call common method to give check the hook */
159     $message= plugin::check();
160     $message= array_merge($message,$this->netConfigDNS->check());
162     $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
164     if ($this->cn == "" ){
165       $message[]= _("The required field 'Component name' is not set.");
166     }
168     /* To check for valid ip*/
169     if($this->netConfigDNS->ipHostNumber == ""){
170        $message[]= _("The required field IP address is empty.");
171     } else {
172       if (!is_ip($this->netConfigDNS->ipHostNumber)){
173         $message[]= _("The field IP address contains an invalid address.");
174       }
175     }
177     /* Set new acl base */
178     if($this->dn == "new") {
179       $this->set_acl_base($this->base);
180     }
182     $ui= get_userinfo();
183     if ($this->dn == "new" && !$this->acl_is_createable()){
184       $message[]= _("You have no permissions to create a component on this 'Base'.");
185     }
187     if ($this->orig_dn != $this->dn){
188       $ldap= $this->config->get_ldap_link();
189       $ldap->cd ($this->base);
190       $ldap->search ("(cn=".$this->cn.")", array("cn"));
191       if ($ldap->count() != 0){
192         while ($attrs= $ldap->fetch()){
193           if ($attrs['dn'] != $this->orig_dn){
194             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
195             break;
196           }
197         }
198       }
199     }
201     return ($message);
202   }
205   /* Save to LDAP */
206   function save()
207   {
208     plugin::save();
210     /* Remove all empty values */
211     if ($this->orig_dn == 'new'){
212       $attrs= array();
213       foreach ($this->attrs as $key => $val){
214         if (is_array($val) && count($val) == 0){
215           continue;
216         }
217         $attrs[$key]= $val;
218       }
219       $this->attrs= $attrs;
220     }
222     /* If this is a new Object IP & Mac aren't set.
223          IP & Mac are msut attributes, so we set this values by here. */
224     if($this->orig_dn == 'new'){
225       $this->attrs['ipHostNumber'] = $this->netConfigDNS->ipHostNumber;
226       $this->attrs['macAddress']  = $this->netConfigDNS->macAddress;
227     }
229     /* Write back to ldap */
230     $ldap= $this->config->get_ldap_link();
231     if ($this->orig_dn == 'new'){
232       $ldap->cd($this->config->current['BASE']);
233       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
234       $ldap->cd($this->dn);
235       $ldap->add($this->attrs);
236       $this->handle_post_events("add");
237     } else {
238       if ($this->orig_dn != $this->dn){
239         $this->move($this->orig_dn, $this->dn);
240       }
242       $ldap->cd($this->dn);
243       $this->cleanup();
244       $ldap->modify ($this->attrs); 
246       $this->handle_post_events("modify");
247     }
249     $this->netConfigDNS->cn = $this->cn;
250     $this->netConfigDNS->save($this->dn);
252     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system component/generic with dn '%s' failed."),$this->dn));
254     /* Optionally execute a command after we're done */
255     $this->postcreate();
256   }
258   /* Return plugin informations for acl handling */
259   function plInfo()
260   {
261     return (array(
262           "plShortName"   => _("Generic"),
263           "plDescription" => _("Component generic"),
264           "plSelfModify"  => FALSE,
265           "plDepends"     => array(),
266           "plPriority"    => 1,
267           "plSection"     => array("administration"),
268           "plCategory"    => array("component" => array("description"  => _("Network device"),
269                                                         "objectClass"  => array("device", "ipHost", "ieee802Device"))),
270           "plProvidedAcls"=> array(
271             "cn"                  => _("Name"),
272             "base"                => _("Base"),
273             "description"         => _("Description"))
274           ));
275   }
280 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
281 ?>