Code

Added last base check
[gosa.git] / gosa-plugins / goto / admin / devices / class_deviceGeneric.inc
1 <?php
3 class deviceGeneric extends plugin
4 {
5   public $dn            = "";
6   public $cn            = "";
7   public $orig_cn       = "";
8   public $orig_base     = "";
9   public $orig_dn       = "";
10   public $description   = "";
11   public $vendor        = "";
12   public $devID        = "";
13   public $serial        = "";
14   public $base          = "";   
15   public $types;
16   public $type;
17   public $baseSelector;
19   public $attributes      = array("cn","description","devID","serial","vendor", "type");
20   public $objectclasses   = array("top","gotoDevice");
22   public $CopyPasteVars   = array("orig_cn","description","vendor","devID","serial","base");
24   public function deviceGeneric(&$config,$dn = NULL)
25   {
26     plugin::plugin($config,$dn);
28     $this->is_account = TRUE;
30     $this->types= array("camera" => _("Digital camera"),
31                         "disc" => _("USB stick"),
32                         "cdrom" => _("CD/DVD drive"),);
33     asort($this->types);
35     /* Set class values */
36     if(isset($this->attrs['gotoHotplugDevice'][0])){
37       $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
38       $this->cn         = $this->attrs['cn'][0];
39       $this->description= $tmp[0];
40       $this->devID     = $tmp[1];
41       $this->serial     = $tmp[2];
42       $this->vendor     = $tmp[3];
43       if (isset($tmp[4])){
44         $this->type= $tmp[4];
45       }
46     }     
48     $this->orig_cn = $this->cn;
50     /* Set Base */
51     if ($this->dn == "new"){
52       if(session::is_set('CurrentMainBase')){
53         $this->base = session::get('CurrentMainBase');
54       }else{
55         $ui= get_userinfo();
56         $this->base= dn2base($ui->dn);
57       }
58     } else {
59       $this->base =preg_replace ("/^[^,]+,".preg_quote(get_ou('deviceRDN'), '/')."/i","",$this->dn);
60     }
61     $this->orig_base = $this->base;
62     $this->orig_dn   = $this->dn;
64     /* Instanciate base selector */
65     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
66     $this->baseSelector->setSubmitButton(false);
67     $this->baseSelector->setHeight(300);
68     $this->baseSelector->update(true);
69   }
72   public function execute()
73   {
74     $smarty = get_smarty();
75     $smarty->assign("usePrototype", "true");
76     $smarty->assign("base", $this->baseSelector->render());
77     foreach($this->attributes as $attr){
78       $smarty->assign($attr,$this->$attr);
79     }
80   
81     $tmp = $this->plInfo();
82     foreach($tmp['plProvidedAcls'] as $attr => $desc){
83       $smarty->assign($attr."ACL", $this->getacl($attr));
84     }
86     $smarty->assign("type",$this->type);
87     $smarty->assign ("types", $this->types);
88     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
89   }
91   
92   public function check()
93   {
94     $message = plugin::check();
96     if($this->cn == "" ||(preg_match("/[^a-z0-9]/i",$this->cn))){
97       $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
98     }
99     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
100       $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
101     }
103     /* Skip serial check if vendor and product id are given */
104     if(preg_match("/^\s+$/i",$this->devID)){
105       $message[]= msgPool::invalid(_("iSerial"),"","01234");
106     }elseif(empty($this->devID) && $this->devID!="0"){
107       $message[]= msgPool::required(_("iSerial"));
108     }
109     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
110       $message[]= msgPool::invalid(_("Product-ID"),"","","0x1234");
111     }
112     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
113       $message[]= msgPool::invalid(_("Vendor-ID"),"","","0x1234");
114     }
116     // Check if a wrong base was supplied
117     if(!$this->baseSelector->checkLastBaseUpdate()){
118       $message[]= msgPool::check_base();
119     }
120   
121     /* Check if entry already exists */ 
122     if($this->cn != $this->orig_cn || $this->dn == "new"){
123       $ldap = $this->config->get_ldap_link();
124       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
125       if($ldap->count()){
126         $message[]= msgPool::duplicated(_("Name"));
127       }
128     }
130     /* Check if we are allowed to create or move this object
131      */
132     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
133       $message[] = msgPool::permCreate();
134     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
135       $message[] = msgPool::permMove();
136     }
138     return($message);
139   }
142   public function save_object()
143   {
144     if(isset($_POST['deviceGeneric_posted'])){
145       plugin::save_object();
147       /* Refresh base */
148       if ($this->acl_is_moveable($this->base)){
149         if (!$this->baseSelector->update()) {
150           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
151         }
152         if ($this->base != $this->baseSelector->getBase()) {
153           $this->base= $this->baseSelector->getBase();
154           $this->is_modified= TRUE;
155         }
156       }
158     }
159   }
161   
162   public function remove_from_parent()
163   {
164     plugin::remove_from_parent();
165     $ldap = $this->config->get_ldap_link();
166     $ldap->cd($this->config->current['BASE']);
168     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
169     $skip = FALSE;
170     $obj = array();
171     while($attrs = $ldap->fetch()){
172       $obj[$ldap->getDN()]= $attrs['cn'][0];
173       $skip =TRUE;
174     }
175     if($skip){
176       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
177     }else{
178       $ldap->rmdir_recursive($this->dn);
179     }
180   }
183   public function save()
184   {
185     plugin::save();
187     /* Unset non ldap attributes 
188      */
189     foreach(array("devID","serial","vendor", "type", "description") as $name){
190       if(isset($this->attrs[$name])){
191         unset($this->attrs[$name]);
192       }
193     }
195     $this->attrs['gotoHotplugDevice'] = "";
196     foreach(array("description","devID","serial","vendor", "type") as $post){
197       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
198     }
199     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
201     $ldap = $this->config->get_ldap_link();
202     $ldap->cd($this->config->current['BASE']);
203     $ldap->cat($this->dn);
204     if($ldap->count()){
205       $ldap->cd($this->dn);
206       $ldap->modify($this->attrs);
207     }else{
208       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
209       $ldap->cd($this->dn);
210       $ldap->add($this->attrs);
211     }
212     if (!$ldap->success()){
213       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
214     }
215   }
218   /* check if given str in like this 0xffff*/
219   function is_2byteHex($str)
220   {
221     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9]{4}$/i",$str);
222   }
225   function PrepareForCopyPaste($source)
226   {
227     plugin::PrepareForCopyPaste($source);
228     $source_o = new deviceGeneric($this->config,$source['dn']);
229     foreach($this->CopyPasteVars as $post){
230       $this->$post = $source_o->$post;
231     }
232   }
235   /* Return a dialog with all fields that must be changed,
236      if we want to copy this entry */
237   function getCopyDialog()
238   {
239     $str = "";
240     $smarty = get_smarty();
241     $smarty->assign("cn",         $this->cn);
242     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
244     $ret = array();
245     $ret['string'] = $str;
246     $ret['status'] = "";
247     return($ret);
248   }
251   /* Save all */
252   function saveCopyDialog()
253   {
254     $attrs = array("cn");
255     foreach($attrs as $attr){
256       if(isset($_POST[$attr])){
257         $this->$attr = $_POST[$attr];
258       }
259     }
260   }
264   /* Return plugin informations for acl handling  */
265   public static function plInfo()
266   {
267     return (array(
268           "plShortName"   => _("Generic"),
269           "plDescription" => _("Device generic")." ("._("Hotplug").")",
270           "plSelfModify"  => FALSE,
271           "plDepends"     => array(),
272           "plPriority"    => 0,
273           "plSection"     => array("administration"),
274           "plCategory"    => array("devices" => array("description"  => _("Devices"),
275                                                         "objectClass"  => "gotoHotplugDevice")),
276           "plProvidedAcls"=> array(
277             "cn"            => _("Name"),
278             "base"          => _("Base"),
279             "description"   => _("Description"),
280             "type"          => _("Type"),
281             "serial"        => _("Serial"),
282             "vendor"        => _("Vendor"), 
283             "devID"        => _("Device ID"))
284           ));
286   }
287         
289 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
290 ?>