Code

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