Code

Some acl fixes
[gosa.git] / plugins / admin / ogroups / class_termgroup.inc
1 <?php
3 class termgroup extends plugin
4 {
5   /* attribute list for save action */
6   var $members= array();
8   var $gotoMode           = "locked";
9   var $gotoSyslogServer   = "";
10   var $gotoNtpServer      = array();
11   var $gotoNtpServers     = array();
12   var $modes              = array();
13   var $inheritTimeServer  = true;
14   var $is_account                     = true; 
15   var $orig_dn            = "";
16   var $didAction          = FALSE;
17   var $mapActions   = array("reboot"          => "localboot",
18                             "instant_update"  => "softupdate",
19                             "update"          => "sceduledupdate",
20                             "reinstall"       => "install",
21                             "rescan"          => "",
22                             "memcheck"        => "memcheck",
23                             "sysinfo"         => "sysinfo");
25   var $attributes     = array("gotoMode","gotoSyslogServer", "gotoNtpServer");
26   var $objectclasses  = array("gotoWorkstationTemplate");
27   var $CopyPasteVars  = array("gotoNtpServers","modes","inheritTimeServer","members");
29   function termgroup ($config, $dn= NULL, $parent= NULL)
30   {
31     /***************
32       Some  initialisations
33      ***************/
35     plugin::plugin($config, $dn, $parent);
37     $ldap= $config->get_ldap_link();
39     $this->is_account = true;
40     $this->modes["active"]= _("Activated");
41     $this->modes["locked"]= _("Locked");
42 //    $this->modes["memcheck"]= _("Memory test");
43 //    $this->modes["sysinfo"]= _("System analysis");
45     $this->orig_dn =    $this->dn;
47     /*************** 
48       Get mac addresses from member objects  
49      ***************/
51     /* We're only interested in the terminal members here, evaluate
52        these... */
53     if(isset($this->attrs['member'])){
54       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
55         $member= $this->attrs['member'][$i];
56         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
57         if (preg_match("/success/i", $ldap->error)){
58           $attrs = $ldap->fetch();
59           if (in_array("gotoTerminal", $attrs['objectClass']) ||
60               in_array("gotoWorkstation", $attrs['objectClass'])){
61             if (isset($attrs['macAddress'])){
62               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
63             } else {
64               $this->members[$attrs['cn'][0]]= "";
65             }
66           }
67         }
68       }
69     }
71     /*************** 
72       Perpare NTP settings 
73      ***************/
75     /* Create used ntp server array */
76     $this->gotoNtpServer= array();
77     if(isset($this->attrs['gotoNtpServer'])){
78       $this->inheritTimeServer = false;
79       unset($this->attrs['gotoNtpServer']['count']);
80       foreach($this->attrs['gotoNtpServer'] as $server){
81         $this->gotoNtpServer[$server] = $server;
82       }
83     }
85     /* Set inherit checkbox state */
86     if(in_array("default",$this->gotoNtpServer)){
87       $this->inheritTimeServer = true;
88       $this->gotoNtpServer=array();
89     }
91     /* Create available ntp options */
92     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
93     foreach($this->gotoNtpServers as $key => $server){
94       if($server == "default"){
95         unset($this->gotoNtpServers[$key]);
96       }
97     }
100   }
102   function check()
103   {
104      /* Call common method to give check the hook */
105     $message= plugin::check();
107     if (!$this->acl_is_createable() && $this->dn == "new"){
108       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
109     }
111     /* Check for valid ntpServer selection */
112     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
113       $message[]= _("There must be at least one NTP server selected.");
114     }
115     return($message);
116   }
118   function remove_from_parent()
119   {
120     /* Workstation startup is using gotoWorkstationTemplate too,
121         if we remove this oc all other not manged attributes will cause errors */
122     if(isset($this->attrs['gotoKernelParameters'])){
123       $this->objectclasses = array();
124     }
126     /* Remove acc */
127     plugin::remove_from_parent();
128     $ldap = $this->config->get_ldap_link();
129     $ldap->cd($this->orig_dn);
130     $ldap->modify($this->attrs);
131     $this->handle_post_events("remove");
132   }
135   function update_term_member_FAIstate($act)
136   {
137     /* Get required informations */
138     $og     = $this->parent->by_object['ogroup'];
139     $allobs = $og->allobjects;
141     /* Get correct value for FAIstate */
142     $action = $this->mapActions[$act];
144     /* Get ldap connection */
145     $ldap = $this->config->get_ldap_link();
146     $ldap->cd ($this->config->current['BASE']);
148     /* Foreach member of mthis ogroup  ... */
149     foreach($og->mem  as $key ){
150   
151       /* check objectClasses and create attributes array */
152       $attrs = array("FAIstate"=>$action);  
153       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
154         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
155       }
156       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
157         $attrs['objectClass'][] = "FAIobject";
158       }
159       if($attrs['FAIstate'] == ""){
160         $attrs['FAIstate'] = array();
161       }
163       /* If this objects is workstation,terminal or server upodate FAIstate */ 
164       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
165         $ldap->cd ($key);
166         $ldap->modify($attrs);
167         show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
168         
169         if(!preg_match("/success/i",$ldap->get_error())) {
170           gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
171         }else{
172           gosa_log("OK.  Updating FAIstate to '".$action."' : ".$key);
173         }
174       }
175     }
176   }
178   function execute()
179   {
180     /* Call parent execute */
181     plugin::execute();
184     /*************** 
185       Handle requested action
186      ***************/
188     /* Watch for events */
189     if (isset($_POST['action'])){
190       $macaddresses="";
191       $names="";
192       foreach ($this->members as $cn => $macAddress){
193         $macaddresses.= "$macAddress ";
194         $names.= "$cn ";
195       }
197       if (isset($_POST['action'])){
198   
199         /* Update members fai state */
200         $this->update_term_member_FAIstate(trim($_POST['saction']));
202         $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
204         if ($cmd == ""){
205           print_red(_("No ACTIONCMD definition found in your gosa.conf"));
206         } else {
207           exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
208           if ($retval != 0){
209             print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
210           } else {
211             $this->didAction= TRUE;
213             /* Get dns from member objects. Create ldap object  */
214             $member = $this->parent->by_object['ogroup']->member;
215             $ldap = $this->config->get_ldap_link();
216             $ldap->cd($this->config->current['BASE']);
218             /* walk trough members and add FAIstate */ 
219             foreach($member as $dn =>  $object){
221               /* Get object */
222               $ldap->cat($dn,array("objectClass"));
223               $res                = $ldap->fetch();
224               $attrs              = array();
226               /* Add FAI state */
227               $attrs['FAIstate']  = "";
228               if(isset($this->mapActions[$_POST['saction']])){
229                 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
230               }
232               /* Fix objectClass index */
233               for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
234                 $attrs['objectClass'][] = $res['objectClass'][$i];
235               }
237               /* Check if we must add the objectClass */
238               if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
239                 $attrs['objectClass'][] = "FAIobject";
240               }
242               if($attrs['FAIstate'] == ""){
243                 $attrs['FAIstate'] = array();
244               }
246               $ldap->cd($dn);
247               $ldap->modify($attrs);
248               show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
249             }
250             $this->didAction= TRUE;
251           }
252         }
253       }
254     }
257     /*************** 
258       Add remove NTP server
259      ***************/
261     /* Add new ntp Server to our list */
262     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
263       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
264     }
266     /* Delete selected NtpServer for list of used servers  */
267     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
268       foreach($_POST['gotoNtpServerSelected'] as $name){
269         unset($this->gotoNtpServer[$name]);
270       }
271     }
274     /*************** 
275       Prepare smarty 
276      ***************/
278     /* Set government mode */
279     $smarty= get_smarty();
281     $tmp = $this->plInfo();
282     foreach($tmp['plProvidedAcls'] as $name => $translated) {
283       $smarty->assign($name."ACL",$this->getacl($name));
284     }
286     foreach($this->attributes as $attr){
287       $smarty->assign($attr,      $this->$attr);
288     }
290     /* Variables */
291     foreach(array("gotoMode","gotoNtpServer") as $val){
292       $smarty->assign($val."_select", $this->$val);
293     }
295     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
296           "instant_update" => _("Instant update"),
297           "update" => _("Scheduled update"),
298           "reinstall" => _("Reinstall"),
299           "rescan" => _("Rescan hardware"),
300           "memcheck" => _("Memory test"),
301           "sysinfo"  => _("System analysis")));
303     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
304     $smarty->assign("modes", $this->modes);
306     $tmp = array();
307     foreach($this->gotoNtpServers as $server){
308       if(!in_array($server,$this->gotoNtpServer)){
309         $tmp[$server] = $server;
310       }
311     }
313     $smarty->assign("gotoNtpServers",$tmp); 
314     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
315     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
317     /* Show main page */
318     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
319   }
321   function save_object()
322   {
323     plugin::save_object();  
324     /* Set inherit mode */
325     if(isset($_POST['workgeneric_posted'])){
326       if(isset($_POST["inheritTimeServer"])){
327         $this->inheritTimeServer = true;
328       }else{
329         $this->inheritTimeServer = false;
330       }
331     }
332   }
334   /* Save to LDAP */
335   function save()
336   {
337     plugin::save();
339     /***************
340       Prepare special vars 
341      ***************/
343     /* Unset some special vars ... */
344     foreach (array("gotoSyslogServer") as $val){
345       if ($this->attrs[$val] == "default"){
346         $this->attrs[$val]= array();
347       }
348     }
350     /* Update ntp server settings */
351     if($this->inheritTimeServer){
352       $this->attrs['gotoNtpServer'] = "default";
353     }else{
354       /* Set ntpServers */
355       $this->attrs['gotoNtpServer'] = array();
356       foreach($this->gotoNtpServer as $server){
357         $this->attrs['gotoNtpServer'][] = $server;
358       }
359     }
362     /***************
363       Write to ldap 
364      ***************/
366     /* Write back to ldap */
367     $ldap= $this->config->get_ldap_link();
368     $ldap->cd($this->dn);
369     $this->cleanup();
370     $ldap->modify ($this->attrs);
372     if(!$this->didAction){
373       $this->handle_post_events("modify");
374     }
375     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
377   }
378   
379   function plInfo()
380   {
381     return (array(
382           "plShortName"   => _("System"),
383           "plDescription" => _("System group"),
384           "plSelfModify"  => FALSE,
385           "plDepends"     => array(),
386           "plPriority"    => 5,
387           "plSection"     => array("administration"),
388           "plCategory"    => array("ogroups"),
389           "plProvidedAcls"=> array(
390             "gotoMode"          => _("Mode"),     
391             "gotoSyslogServer"  => _("Syslog server"), 
392             "FAIstate"          => _("Action flag"), 
393             "gotoNtpServer"     => _("Ntp server"))
394           ));
395   }
401 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
402 ?>