Code

Added support for general check hooks
[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)
26   {
27     plugin::plugin ($config, $dn);
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);
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);
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     /* Assign attributes */
84     foreach ($this->attributes as $attr){
85       $smarty->assign($attr."ACL", chkacl($this->acl, $attr));
86       $smarty->assign("$attr", $this->$attr);
87     }
88     $smarty->assign("base_select", $this->base);
90     /* Show main page */
91     $smarty->assign("netconfig", $this->netConfigDNS->execute());
92     return($smarty->fetch (get_template_path('component.tpl', TRUE)));
93   }
95   function remove_from_parent()
96   {
97     $ldap= $this->config->get_ldap_link();
98     $this->netConfigDNS->remove_from_parent();
99     $ldap->rmdir($this->dn);
100     show_ldap_error($ldap->get_error());
101     $this->handle_post_events("remove");
103     /* Delete references to object groups */
104     $ldap->cd ($this->config->current['BASE']);
105     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
106     while ($ldap->fetch()){
107       $og= new ogroup($this->config, $ldap->getDN());
108       unset($og->member[$this->dn]);
109       $og->save ();
110     }
111   }
114   /* Save data to object */
115   function save_object()
116   {
117     plugin::save_object();
119     $this->netConfigDNS->save_object();
121     /* Save base, since this is no LDAP attribute */
122     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
123       $this->base= $_POST['base'];
124     }
125   }
128   /* Check supplied data */
129   function check()
130   {
131     /* Call common method to give check the hook */
132     $message= plugin::check();
133     $message= array_merge($message,$this->netConfigDNS->check());
135     $this->dn= "cn=".$this->cn.",ou=netdevices,ou=systems,".$this->base;
137     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
138       $message[]= _("The required field 'Component name' is not set.");
139     }
141     $ui= get_userinfo();
142     $acl= get_permissions ($this->dn, $ui->subtreeACL);
143     $acl= get_module_permission($acl, "component", $this->dn);
144     if (chkacl($acl, "create") != ""){
145       $message[]= _("You have no permissions to create a component on this 'Base'.");
146     }
148     if ($this->orig_dn != $this->dn){
149       $ldap= $this->config->get_ldap_link();
150       $ldap->cd ($this->base);
151       $ldap->search ("(cn=".$this->cn.")", array("cn"));
152       if ($ldap->count() != 0){
153         while ($attrs= $ldap->fetch()){
154           if ($attrs['dn'] != $this->orig_dn){
155             $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
156             break;
157           }
158         }
159       }
160     }
162     return ($message);
163   }
166   /* Save to LDAP */
167   function save()
168   {
169     plugin::save();
171     /* Remove all empty values */
172     if ($this->orig_dn == 'new'){
173       $attrs= array();
174       foreach ($this->attrs as $key => $val){
175         if (is_array($val) && count($val) == 0){
176           continue;
177         }
178         $attrs[$key]= $val;
179       }
180       $this->attrs= $attrs;
181     }
183     /* Write back to ldap */
184     $ldap= $this->config->get_ldap_link();
185     if ($this->orig_dn == 'new'){
186       $ldap->cd($this->config->current['BASE']);
187       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
188       $ldap->cd($this->dn);
189       $ldap->add($this->attrs);
190       $this->handle_post_events("add");
191     } else {
192       if ($this->orig_dn != $this->dn){
193         $this->move($this->orig_dn, $this->dn);
194       }
196       $ldap->cd($this->dn);
197       $this->cleanup();
198 $ldap->modify ($this->attrs); 
200       $this->handle_post_events("modify");
201     }
203     $this->netConfigDNS->cn = $this->cn;
204     $this->netConfigDNS->save($this->dn);
206     show_ldap_error($ldap->get_error());
208     /* Optionally execute a command after we're done */
209     $this->postcreate();
210   }
214 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
215 ?>