Code

b8c1d68dd71aaafd4d2fe7d6c361bbb8944dd13e
[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");
28   var $view_logged    = FALSE;
31   function termgroup ($config, $dn= NULL, $parent= NULL)
32   {
33     /***************
34       Some  initialisations
35      ***************/
37     plugin::plugin($config, $dn, $parent);
39     $ldap= $config->get_ldap_link();
41     $this->is_account = true;
42     $this->modes["active"]= _("Activated");
43     $this->modes["locked"]= _("Locked");
44 //    $this->modes["memcheck"]= _("Memory test");
45 //    $this->modes["sysinfo"]= _("System analysis");
47     $this->orig_dn =    $this->dn;
49     /*************** 
50       Get mac addresses from member objects  
51      ***************/
53     /* We're only interested in the terminal members here, evaluate
54        these... */
55     if(isset($this->attrs['member'])){
56       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
57         $member= $this->attrs['member'][$i];
58         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
59         if (preg_match("/success/i", $ldap->error)){
60           $attrs = $ldap->fetch();
61           if (in_array("gotoTerminal", $attrs['objectClass']) ||
62               in_array("gotoWorkstation", $attrs['objectClass'])){
63             if (isset($attrs['macAddress'])){
64               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
65             } else {
66               $this->members[$attrs['cn'][0]]= "";
67             }
68           }
69         }
70       }
71     }
73     /*************** 
74       Perpare NTP settings 
75      ***************/
77     /* Create used ntp server array */
78     $this->gotoNtpServer= array();
79     if(isset($this->attrs['gotoNtpServer'])){
80       $this->inheritTimeServer = false;
81       unset($this->attrs['gotoNtpServer']['count']);
82       foreach($this->attrs['gotoNtpServer'] as $server){
83         $this->gotoNtpServer[$server] = $server;
84       }
85     }
87     /* Set inherit checkbox state */
88     if(in_array("default",$this->gotoNtpServer)){
89       $this->inheritTimeServer = true;
90       $this->gotoNtpServer=array();
91     }
93     /* Create available ntp options */
94     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
95     foreach($this->gotoNtpServers as $key => $server){
96       if($server == "default"){
97         unset($this->gotoNtpServers[$key]);
98       }
99     }
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     @log::log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
133   }
136   function update_term_member_FAIstate($act)
137   {
138     /* Get required informations */
139     $og     = $this->parent->by_object['ogroup'];
140     $allobs = $og->objcache;
142     /* Get correct value for FAIstate */
143     $action = $this->mapActions[$act];
145     /* Get ldap connection */
146     $ldap = $this->config->get_ldap_link();
147     $ldap->cd ($this->config->current['BASE']);
149     /* Foreach member of mthis ogroup  ... */
150     foreach($og->member  as $key ){
151   
152       /* check objectClasses and create attributes array */
153       $attrs = array("FAIstate"=>$action);  
154       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
155         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
156       }
157       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
158         $attrs['objectClass'][] = "FAIobject";
159       }
160       if($attrs['FAIstate'] == ""){
161         $attrs['FAIstate'] = array();
162       }
164       /* If this objects is workstation,terminal or server upodate FAIstate */ 
165       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
166         $ldap->cd ($key);
167         $ldap->modify($attrs);
168         show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
169         
170         if(!preg_match("/success/i",$ldap->get_error())) {
171           gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
172         }else{
173           gosa_log("OK.  Updating FAIstate to '".$action."' : ".$key);
174         }
175       }
176     }
177   }
179   function execute()
180   {
181     /* Call parent execute */
182     plugin::execute();
184     if(!$this->view_logged){
185       $this->view_logged = TRUE;
186       @log::log("view","ogroups/".get_class($this),$this->dn);
187     }
189     /*************** 
190       Handle requested action
191      ***************/
193     /* Watch for events */
194     if (isset($_POST['action'])){
195       $macaddresses="";
196       $names="";
197       foreach ($this->members as $cn => $macAddress){
198         $macaddresses.= "$macAddress ";
199         $names.= "$cn ";
200       }
202       if (isset($_POST['action'])){
203   
204         /* Update members fai state */
205         $this->update_term_member_FAIstate(trim($_POST['saction']));
207         $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
209         if ($cmd == ""){
210           print_red(_("No ACTIONCMD definition found in your gosa.conf"));
211         } else {
212           exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
213           if ($retval != 0){
214             print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
215           } else {
216             $this->didAction= TRUE;
218             /* Get dns from member objects. Create ldap object  */
219             $member = $this->parent->by_object['ogroup']->member;
220             $ldap = $this->config->get_ldap_link();
221             $ldap->cd($this->config->current['BASE']);
223             /* walk trough members and add FAIstate */ 
224             foreach($member as $dn =>  $object){
226               /* Get object */
227               $ldap->cat($dn,array("objectClass"));
228               $res                = $ldap->fetch();
229               $attrs              = array();
231               /* Add FAI state */
232               $attrs['FAIstate']  = "";
233               if(isset($this->mapActions[$_POST['saction']])){
234                 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
235               }
237               /* Fix objectClass index */
238               for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
239                 $attrs['objectClass'][] = $res['objectClass'][$i];
240               }
242               /* Check if we must add the objectClass */
243               if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
244                 $attrs['objectClass'][] = "FAIobject";
245               }
247               if($attrs['FAIstate'] == ""){
248                 $attrs['FAIstate'] = array();
249               }
251               $ldap->cd($dn);
252               $ldap->modify($attrs);
253               show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
254             }
255             $this->didAction= TRUE;
256           }
257         }
258       }
259     }
262     /*************** 
263       Add remove NTP server
264      ***************/
266     /* Add new ntp Server to our list */
267     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
268       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
269     }
271     /* Delete selected NtpServer for list of used servers  */
272     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
273       foreach($_POST['gotoNtpServerSelected'] as $name){
274         unset($this->gotoNtpServer[$name]);
275       }
276     }
279     /*************** 
280       Prepare smarty 
281      ***************/
283     /* Set government mode */
284     $smarty= get_smarty();
286     $tmp = $this->plInfo();
287     foreach($tmp['plProvidedAcls'] as $name => $translated) {
288       $smarty->assign($name."ACL",$this->getacl($name));
289     }
291     foreach($this->attributes as $attr){
292       $smarty->assign($attr,      $this->$attr);
293     }
295     /* Variables */
296     foreach(array("gotoMode","gotoNtpServer") as $val){
297       $smarty->assign($val."_select", $this->$val);
298     }
300     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
301           "instant_update" => _("Instant update"),
302           "update" => _("Scheduled update"),
303           "reinstall" => _("Reinstall"),
304           "rescan" => _("Rescan hardware"),
305           "memcheck" => _("Memory test"),
306           "sysinfo"  => _("System analysis")));
308     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
309     $smarty->assign("modes", $this->modes);
311     $tmp = array();
312     foreach($this->gotoNtpServers as $server){
313       if(!in_array($server,$this->gotoNtpServer)){
314         $tmp[$server] = $server;
315       }
316     }
318     $smarty->assign("gotoNtpServers",$tmp); 
319     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
320     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
322     /* Show main page */
323     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
324   }
326   function save_object()
327   {
328     plugin::save_object();  
329     /* Set inherit mode */
330     if(isset($_POST['workgeneric_posted'])){
331       if(isset($_POST["inheritTimeServer"])){
332         $this->inheritTimeServer = true;
333       }else{
334         $this->inheritTimeServer = false;
335       }
336     }
337   }
339   /* Save to LDAP */
340   function save()
341   {
342     plugin::save();
344     /***************
345       Prepare special vars 
346      ***************/
348     /* Unset some special vars ... */
349     foreach (array("gotoSyslogServer") as $val){
350       if ($this->attrs[$val] == "default"){
351         $this->attrs[$val]= array();
352       }
353     }
355     /* Update ntp server settings */
356     if($this->inheritTimeServer){
357       $this->attrs['gotoNtpServer'] = "default";
358     }else{
359       /* Set ntpServers */
360       $this->attrs['gotoNtpServer'] = array();
361       foreach($this->gotoNtpServer as $server){
362         $this->attrs['gotoNtpServer'][] = $server;
363       }
364     }
367     /***************
368       Write to ldap 
369      ***************/
371     /* Write back to ldap */
372     $ldap= $this->config->get_ldap_link();
373     $ldap->cd($this->dn);
374     $this->cleanup();
375     $ldap->modify ($this->attrs);
377     if($this->initially_was_account){
378       @log::log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
379     }else{
380       @log::log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
381     }
383     if(!$this->didAction){
384       $this->handle_post_events("modify");
385     }
386     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
388   }
389   
390   function plInfo()
391   {
392     return (array(
393           "plShortName"   => _("System"),
394           "plDescription" => _("System group"),
395           "plSelfModify"  => FALSE,
396           "plDepends"     => array(),
397           "plPriority"    => 5,
398           "plSection"     => array("administration"),
399           "plCategory"    => array("ogroups"),
400           "plProvidedAcls"=> array(
401             "gotoMode"          => _("Mode"),     
402             "gotoSyslogServer"  => _("Syslog server"), 
403             "FAIstate"          => _("Action flag"), 
404             "gotoNtpServer"     => _("Ntp server"))
405           ));
406   }
412 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
413 ?>