Code

Updated hot plug ACLs.
[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;
18   public $attributes      = array("cn","description","devID","serial","vendor", "type");
19   public $objectclasses   = array("top","gotoDevice");
21   public $CopyPasteVars   = array("orig_cn","description","vendor","devID","serial","base");
23   public function deviceGeneric(&$config,$dn = NULL)
24   {
25     plugin::plugin($config,$dn);
27     $this->is_account = TRUE;
29     $this->types= array("camera" => _("Digital camera"),
30                         "disc" => _("USB stick"),
31                         "cdrom" => _("CD/DVD drive"),);
32     asort($this->types);
34     /* Set class values */
35     if(isset($this->attrs['gotoHotplugDevice'][0])){
36       $tmp = preg_split("/\|/",$this->attrs['gotoHotplugDevice'][0]);
37       $this->cn         = $this->attrs['cn'][0];
38       $this->description= $tmp[0];
39       $this->devID     = $tmp[1];
40       $this->serial     = $tmp[2];
41       $this->vendor     = $tmp[3];
42       if (isset($tmp[4])){
43         $this->type= $tmp[4];
44       }
45     }     
47     $this->orig_cn = $this->cn;
49     /* Set Base */
50     if ($this->dn == "new"){
51       if(session::is_set('CurrentMainBase')){
52         $this->base = session::get('CurrentMainBase');
53       }else{
54         $ui= get_userinfo();
55         $this->base= dn2base($ui->dn);
56       }
57     } else {
58       $this->base =preg_replace ("/^[^,]+,".get_ou('deviceou')."/","",$this->dn);
59     }
60     $this->orig_base = $this->base;
61     $this->orig_dn   = $this->dn;
62   }
65   public function execute()
66   {
67     $smarty = get_smarty();
68     $smarty->assign("base",$this->base);
69     $smarty->assign("bases",$this->get_allowed_bases());
70     foreach($this->attributes as $attr){
71       $smarty->assign($attr,$this->$attr);
72     }
73   
74     $tmp = $this->plInfo();
75     foreach($tmp['plProvidedAcls'] as $attr => $desc){
76       $smarty->assign($attr."ACL", $this->getacl($attr));
77     }
79     $smarty->assign("type",$this->type);
80     $smarty->assign ("types", $this->types);
81     return($smarty->fetch(get_template_path("deviceGeneric.tpl",TRUE,dirname(__FILE__))));
82   }
84   
85   public function check()
86   {
87     $message = plugin::check();
89     if(empty($this->cn)||(preg_match("/[^a-z0-9]/i",$this->cn))){
90       $message[]= msgPool::invalid(_("Name"),$this->cn,"/^[a-z0-9]*$/i");
91     }
92     if(preg_match("/[^a-z0-9!\"?.,;:-_\(\) ]/i",$this->description)){
93       $message[]= msgPool::invalid(_("Description"),$this->cn,"/^[a-z0-9!\"?.,;:-_\(\) ]*$/i");
94     }
96     /* Skip serial check if vendor and product id are given */
97     if(preg_match("/^\s+$/i",$this->devID)){
98       $message[]= msgPool::invalid(_("iSerial"),"","01234");
99     }
100     if(empty($this->serial) || !$this->is_2byteHex($this->serial)){
101       $message[]= msgPool::invalid(_("Serial"),"","","0x1234");
102     }
103     if(empty($this->vendor) || !$this->is_2byteHex($this->vendor)){
104       $message[]= msgPool::invalid(_("Vendor ID"),"","","0x1234");
105     }
106   
107     /* Check if entry already exists */ 
108     if($this->cn != $this->orig_cn || $this->dn == "new"){
109       $ldap = $this->config->get_ldap_link();
110       $ldap->search("(&(objectClass=gotoDevice)(cn=".$this->cn."*))",array("cn"));
111       if($ldap->count()){
112         $message[]= msgPool::duplicated(_("Name"));
113       }
114     }
116     /* Check if we are allowed to create or move this object
117      */
118     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
119       $message[] = msgPool::permCreate();
120     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
121       $message[] = msgPool::permMove();
122     }
124     return($message);
125   }
128   public function save_object()
129   {
130     if(isset($_POST['deviceGeneric_posted'])){
131       plugin::save_object();
132   
133       if(isset($_POST['base'])){
134         $tmp = $this->get_allowed_bases();
135         if(isset($tmp[get_post("base")])){
136           $this->base = get_post("base");
137         }
138       }
139     }
140   }
142   
143   public function remove_from_parent()
144   {
145     plugin::remove_from_parent();
146     $ldap = $this->config->get_ldap_link();
147     $ldap->cd($this->config->current['BASE']);
149     $ldap->search("(&(objectClass=gotoEnvironment)(gotoHotplugDeviceDN=".$this->dn."))",array("cn","gotoHotplugDeviceDN"));
150     $skip = FALSE;
151     $obj = array();
152     while($attrs = $ldap->fetch()){
153       $obj[$ldap->getDN()]= $attrs['cn'][0];
154       $skip =TRUE;
155     }
156     if($skip){
157       msg_dialog::display(_("Error"), msgPool::stillInUse(_("Device"), $obj), INFO_DIALOG);
158     }else{
159       $ldap->rmdir_recursive($this->dn);
160     }
161   }
164   public function save()
165   {
166     plugin::save();
168     $this->attrs = array();
169     $this->attrs['cn'] = $this->cn;
170     $this->attrs['gotoHotplugDevice'] = "";
171     foreach(array("description","devID","serial","vendor", "type") as $post){
172       $this->attrs['gotoHotplugDevice'] .= $this->$post."|"; 
173     }
174     $this->attrs['gotoHotplugDevice'] = preg_replace("/\|$/","",$this->attrs['gotoHotplugDevice']);
176     $ldap = $this->config->get_ldap_link();
177     $ldap->cd($this->config->current['BASE']);
178     $ldap->cat($this->dn);
179     if($ldap->count()){
180       $ldap->cd($this->dn);
181       $ldap->modify($this->attrs);
182     }else{
183       $ldap->create_missing_trees(preg_replace("/^[^,]+,/","",$this->dn));
184       $ldap->cd($this->dn);
185       $ldap->add($this->attrs);
186     }
187     if (!$ldap->success()){
188       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
189     }
190   }
193   /* check if given str in like this 0xffff*/
194   function is_2byteHex($str)
195   {
196     return !strlen($str) || preg_match("/^(0x|x|)[a-f0-9][a-f0-9][a-f0-9][a-f0-9]/i",$str);
197   }
200   function PrepareForCopyPaste($source)
201   {
202     plugin::PrepareForCopyPaste($source);
203     $source_o = new deviceGeneric($this->config,$source['dn']);
204     foreach($this->CopyPasteVars as $post){
205       $this->$post = $source_o->$post;
206     }
207   }
210   /* Return a dialog with all fields that must be changed,
211      if we want to copy this entry */
212   function getCopyDialog()
213   {
214     $str = "";
215     $smarty = get_smarty();
216     $smarty->assign("cn",         $this->cn);
217     $str = $smarty->fetch(get_template_path("paste_deviceGeneric.tpl",TRUE,dirname(__FILE__)));
219     $ret = array();
220     $ret['string'] = $str;
221     $ret['status'] = "";
222     return($ret);
223   }
226   /* Save all */
227   function saveCopyDialog()
228   {
229     $attrs = array("cn");
230     foreach($attrs as $attr){
231       if(isset($_POST[$attr])){
232         $this->$attr = $_POST[$attr];
233       }
234     }
235   }
239   /* Return plugin informations for acl handling  */
240   public static function plInfo()
241   {
242     return (array(
243           "plShortName"   => _("Generic"),
244           "plDescription" => _("Device generic")." ("._("Hotplug").")",
245           "plSelfModify"  => FALSE,
246           "plDepends"     => array(),
247           "plPriority"    => 0,
248           "plSection"     => array("administration"),
249           "plCategory"    => array("devices" => array("description"  => _("Devices"),
250                                                         "objectClass"  => "gotoHotplugDevice")),
251           "plProvidedAcls"=> array(
252             "cn"            => _("Name"),
253             "base"          => _("Base"),
254             "description"   => _("Description"),
255             "type"          => _("Tpye"),
256             "serial"        => _("Serial"),
257             "vendor"        => _("Vendor"), 
258             "devID"        => _("Devive ID"))
259           ));
261   }
262         
264 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
265 ?>