Code

Added servrepository acls
[gosa.git] / plugins / admin / systems / class_servRepository.inc
1 <?php
3 class servrepository extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary          = "Manage server basic 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   /* attribute list for save action */
11   //  var $ignore_account         = TRUE;
12   var $attributes             = array("FAIrepository");
13   var $objectclasses          = array("FAIrepositoryServer");
14   
15   /* Search filter */
16   var $regex                  = "*";
18   /* Configurationdialog for repositories */
19   var $dialog                 = NULL;
21   /* Repositories */
22   var $repositories           = array();
23   var $FAIrepository           = array();
25   function servrepository ($config, $dn= NULL, $parent= NULL)
26   {
27     plugin::plugin ($config, $dn, $parent);
29     $ui = get_userinfo();
30     $tmp= get_permissions ($this->dn, $ui->subtreeACL);
31     $this->acl= get_module_permission($tmp, "FAIclass", $this->dn);
32     
33     $this->repositories = array();
34     if(isset($this->attrs['FAIrepository'])){
35       for($i = 0; $i < $this->attrs['FAIrepository']['count']; $i++){
36         $tmp = split("\|",$this->attrs['FAIrepository'][$i]);
37         $tmp2 = array();  
38         $tmp3 = array();   
40         if(isset($tmp[1])){
41           $tmp2['ParentServer'] = $tmp[1];
42           if(empty($tmp[1])){
43             $tmp2['ParentServer'] = "none";
44           }
45         }else{
46           $tmp2['ParentServer'] = "none";
47         }
49         if(isset($tmp[0])){
50           $tmp2['Url']          = $tmp[0];
51         }else{
52           $tmp2['Url']          = "";
53         }
54   
55         if(isset($tmp[2])){
56           $tmp2['Release']      = $tmp[2];
57         }else{
58           $tmp2['Release']      = "";
59         }
61         if(isset($tmp[3])){
62           $tmp3 = split(",",$tmp[3]);
63           foreach($tmp3 as $sec){
64             $tmp2['Sections'][$sec]=$sec;
65           }    
66         }else{
67           $tmp['Section']=array();
68         }
70         $this->repositories[$tmp[2]]=$tmp2;      
71       }
72     }
73   }
75   function execute()
76   {
77     /* Call parent execute */
78     plugin::execute();
80     /* Fill templating stuff */
81     $smarty= get_smarty();
82     $display= "";
84     /* Smarty vars*/
85     $smarty->assign("infoimage",    get_template_path('images/info.png'));
86     $smarty->assign("search_image", get_template_path('images/search.png'));
87     $smarty->assign("launchimage",  get_template_path('images/small_filter.png'));
88     $smarty->assign("tree_image",   get_template_path('images/tree.png'));
89     $smarty->assign("alphabet",     generate_alphabet());
90     $smarty->assign("apply",        apply_filter());
91     $smarty->assign("regex",        $this->regex);
94     /* Do we need to flip is_account state? */
95     if (isset($_POST['modify_state'])){
96       $this->is_account= !$this->is_account;
97     }
99     /* Show tab dialog headers */
100     if ($this->is_account){
101       $display= $this->show_header(_("Remove FAI repository extension."),
102           _("This server has FAI repository features enabled. You can disable them by clicking below."));
103     } else {
104       $display= $this->show_header(_("Add FAI repository extension."),
105           _("This server has FAI repository features disabled. You can enable them by clicking below."));
106       return ($display);
107     }
109     /*
110        ADD / EDIT Repository
111        Dialog Handling
112      */
113       
114     $allow_edit = chkacl($this->acl,"FAIclass") == "";
116     $once = false;
117     if(isset($_POST['servRepository'])){
118       foreach($_POST as $name => $value){
120         if(preg_match("/AddRepository/",$name) && !$once && $allow_edit){
121           $once = true;
122           $this->dialog = new servRepositorySetup($this->config,$this->dn);
123           $this->dialog->acl = $this->acl;
124         }
126         if((preg_match("/^delete_/",$name))&&(!$once) && $allow_edit){
127           $once = true;
128           $value = preg_replace("/delete_/","",$name);
129           $value = base64_decode(preg_replace("/_.*$/","",$value));
131           $url = $this->repositories[$value]['Url'];
132           $release = $this->repositories[$value]['Release'];
134           $ldap = $this->config->get_ldap_link();
135           $ldap->cd ($this->config->current['BASE']);
137           $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
139           $found = false;
140           $found_in = " ";
141           while($attrs = $ldap->fetch()){
142             foreach($attrs['FAIclass'] as $class){
143               if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
144                 $found = true;  
145                 $found_in .= $attrs['cn'][0]." ";
146               }
147             }
148           }
150           if($found){
151             print_red(sprintf(_("You can't delete this release, it is still used by these workstations [%s]. Please solve this dependencies first, to keep data base consistency."),$found_in));
152           }else{
153             if(isset($this->repositories[$value])){
154               unset($this->repositories[$value]);
155             }
156           }
157         }
159         if((preg_match("/^edit_/",$name))&&(!$once) && $allow_edit){
160           $value = preg_replace("/edit_/","",$name);
161           $value = base64_decode(preg_replace("/_.$/","",$value));
163           if(isset($this->repositories[$value])){
165             $ldap = $this->config->get_ldap_link();
166             $ldap->cd ($this->config->current['BASE']);
168             $url = $this->repositories[$value]['Url'];
169             $release = $this->repositories[$value]['Release'];
171             $ldap->search("(&(objectClass=gotoWorkstation)(objectClass=FAIobject)(FAIdebianMirror=".$url."))",array("cn","FAIclass"));
173             $found = false;
174             $found_in = " ";
175             while($attrs = $ldap->fetch()){
176               foreach($attrs['FAIclass'] as $class){
177                 if(preg_match("/".str_replace("/","\/",$release)."$/i",$class)){
178                   $found = true;
179                   $found_in .= $attrs['cn'][0]." ";
180                 }
181               }
182             }
184             if($found){
185               print_red(sprintf(_("Be careful editing this release, it is still used by these workstations [%s]."),$found_in));
186             } 
188             if(isset($this->repositories[$value])){
189               $once = true;
190               $obj = $this->repositories[$value];
192               /* to be able to detect if this was renamed */
193               $obj['initialy_was'] = $obj['Release'];
194               $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
195               $this->dialog->acl = $this->acl;
196             }
197           }
198         }
199       }
200     }
201     if((isset($_GET['act']))&&($_GET['act']=="open")&&(isset($_GET['id'])) && $allow_edit){
202       $obj = $this->repositories[base64_decode($_GET['id'])];
203       $obj['initialy_was'] = $obj['Release'];
204       $this->dialog = new servRepositorySetup($this->config,$this->dn,$obj);
205       $this->dialog->acl = $this->acl;
206     }
208     /*
209     if(isset($_POST['AddRepository'])){
210       $this->dialog = new servRepositorySetup($this->config,$this->dn);
211       $this->dialog->acl = $this->acl;
212     }
213     */
215     if(isset($_POST['repository_setup_save'])){
216       $this->dialog->save_object();
217       if(($this->dialog->is_new_name())&&(isset($this->repositories[$this->dialog->GetName()]))){
218         print_red(_("This name is already in use."));
219       }else
221       if(count($this->dialog->check())!=0){
222         foreach($this->dialog->check() as $msg){
223           print_red($msg);
224         }
225       }else{
226         $obj = $this->dialog->save();
227         if($this->dialog->is_new_name()){
228           $oldname = $this->dialog->initialy_was;
229           $this->repositories[$obj['Release']]=$obj;        
230           unset($this->repositories[$oldname]);
231         }else{ 
232           $this->repositories[$obj['Release']]=$obj;        
233         }
234         $this->dialog = NULL;
235         $this->is_dialog= false;
236       }
237     }
239     if(isset($_POST['repository_setup_cancel'])){
240       $this->dialog=NULL;
241       $this->is_dialog = false;
242     }
243    
244     if($this->dialog != NULL){
245       $this->dialog->save_object();
246       $this->is_dialog = true;
247       return($this->dialog->execute());
248     }
250     /*
251       Repository setup dialog handling /END
252     */
254     $divlist = new divList("repositories");
255     $divlist->SetEntriesPerPage(0);
256     $divlist->setHeight(400);
258     $divlist->setHeader(array(array("string"=>_("Release"),"attach"=>"style='width:80px;'"),
259                               array("string"=>_("Sections")),
260                               array("string"=>_("Options"),"attach"=>"style='border-right:0px;width:55px;'")
261                        )     );
263     $link   = "<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
264     $edit   = "<input type='image' value='%s' name='edit_%s'   src='images/edit.png'>&nbsp;";
265     $delete = "<input type='image' value='%s' name='delete_%s' src='images/edittrash.png'>";
266   
267     foreach($this->repositories as $name => $reps){
269       $str = " ";
270       if(preg_match("/".str_replace("*",".*",$this->regex)."/",$reps['Release'])){
272       foreach($reps['Sections'] as $sec){
273         $str.=$sec." ";  
274       }    
276       if($allow_edit){
277         $link_str = sprintf($link,base64_encode($name),$name);
278         $sections = sprintf($link,base64_encode($name),_("Sections")." :".$str);
279         $options  = preg_replace("/%s/",base64_encode($name),$edit.$delete);
280       }else{
281         $link_str = $name;
282         $sections = _("Sections")." :".$str;
283         $options  = "";
284       }
285     
286  
287       $divlist->AddEntry(array(
288                               array("string"=>$link_str,"attach"=>"style='width:80px;'"),
289                               array("string"=>$sections),
290                               array("string"=>$options,"attach"=>"style='border-right:0px;width:55px;text-align:right;'")));
291       }
292     }
293  
294     $smarty -> assign("Repositories",$divlist->DrawList());
295     $smarty -> assign("FAIclassACL",chkacl($this->acl,"FAIclass"));
297     $display.= $smarty->fetch(get_template_path('servRepository.tpl', TRUE));
298     return($display);
299   }
301   function remove_from_parent()
302   {
303     plugin::remove_from_parent();    
304     $ldap= $this->config->get_ldap_link();
305     $ldap->cd ($this->config->current['BASE']);
306     
307     $ldap->cat($this->dn, array('dn'));
308     
309     if($ldap->count()){
310       $ldap->cd($this->dn);
311       $this->cleanup();
312       $ldap->modify ($this->attrs); 
314       $this->handle_post_events("modify");
315     }
316   }
319   /* Save data to object */
320   function save_object()
321   {
322     plugin::save_object();
323     if(isset($_POST['regex'])){
324       $this->regex = $_POST['regex'];
325     }
326   }
329   /* Check supplied data */
330   function check()
331   {
332     /* Call common method to give check the hook */
333     $message= plugin::check();
334         
335     return ($message);
336   }
339   /* Save to LDAP */
340   function save()
341   {
342    
343     /* Skip if not allowed */ 
344     if(preg_match("/disabled/",chkacl($this->acl,"FAIclass"))) return;
345     plugin::save();
347     $arr = array();
348     foreach($this->repositories as $servername => $conf){
349       $str = "";
350       foreach($conf['Sections'] as $sec){
351         $str.=$sec.",";
352       }
353       $str=preg_replace("/,$/","",$str);
354         
355       if($conf['ParentServer']=="none"){
356         $conf['ParentServer'] ="";
357       }    
358     
359       $arr[]=$conf['Url']."|".$conf['ParentServer']."|".$conf['Release']."|".$str;
360     }
361     $this->attrs['FAIrepository'] = $arr;
363     $ldap= $this->config->get_ldap_link();
364     $ldap->cd ($this->config->current['BASE']);
365     
366     $ldap->cat($this->dn, array('dn'));
367     
368     if($ldap->count()){
369       $ldap->cd($this->dn);
370       $this->cleanup();
371       $ldap->modify ($this->attrs);       
373       $this->handle_post_events("modify");
374     }else{
375       $ldap->cd ($this->config->current['BASE']);
376       $ldap->create_missing_trees($this->dn);
377       $ldap->cd($this->dn);
378       $ldap->add($this->attrs);
379       $this->handle_post_events("add");
380     }
381   }
385 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
386 ?>