Code

2299d1adfa7ff16d61811419c7ae9c1b1d55d1a6
[gosa.git] / gosa-plugins / goto / admin / ogroups / goto / 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                             "wake"            => "",
23                             "memcheck"        => "memcheck",
24                             "sysinfo"         => "sysinfo");
26   var $attributes     = array("gotoMode","gotoSyslogServer", "gotoNtpServer");
27   var $objectclasses  = array("gotoWorkstationTemplate");
28   var $CopyPasteVars  = array("gotoNtpServers","modes","inheritTimeServer","members");
29   var $view_logged    = FALSE;
32   function termgroup (&$config, $dn= NULL, $parent= NULL)
33   {
34     /***************
35       Some  initialisations
36      ***************/
38     plugin::plugin($config, $dn, $parent);
40     $ldap= $config->get_ldap_link();
42     $this->is_account = true;
43     $this->modes["active"]= _("Activated");
44     $this->modes["locked"]= _("Locked");
45 //    $this->modes["memcheck"]= _("Memory test");
46 //    $this->modes["sysinfo"]= _("System analysis");
48     $this->orig_dn =    $this->dn;
50     /*************** 
51       Get mac addresses from member objects  
52      ***************/
54     /* We're only interested in the terminal members here, evaluate
55        these... */
56     if(isset($this->attrs['member'])){
57       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
58         $member= $this->attrs['member'][$i];
59         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
60         if (preg_match("/success/i", $ldap->error)){
61           $attrs = $ldap->fetch();
62           if (in_array("gotoTerminal", $attrs['objectClass']) ||
63               in_array("gotoWorkstation", $attrs['objectClass'])){
64             if (isset($attrs['macAddress'])){
65               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
66             } else {
67               $this->members[$attrs['cn'][0]]= "";
68             }
69           }
70         }
71       }
72     }
74     /*************** 
75       Perpare NTP settings 
76      ***************/
78     /* Create used ntp server array */
79     $this->gotoNtpServer= array();
80     if(isset($this->attrs['gotoNtpServer'])){
81       $this->inheritTimeServer = false;
82       unset($this->attrs['gotoNtpServer']['count']);
83       foreach($this->attrs['gotoNtpServer'] as $server){
84         $this->gotoNtpServer[$server] = $server;
85       }
86     }
88     /* Set inherit checkbox state */
89     if(in_array("default",$this->gotoNtpServer)){
90       $this->inheritTimeServer = true;
91       $this->gotoNtpServer=array();
92     }
94     /* Create available ntp options */
95     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
96     foreach($this->gotoNtpServers as $key => $server){
97       if($server == "default"){
98         unset($this->gotoNtpServers[$key]);
99       }
100     }
101   }
103   function check()
104   {
105      /* Call common method to give check the hook */
106     $message= plugin::check();
108     if (!$this->acl_is_createable() && $this->dn == "new"){
109       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
110     }
112     /* Check for valid ntpServer selection */
113     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
114       $message[]= _("There must be at least one NTP server selected.");
115     }
116     return($message);
117   }
119   function remove_from_parent()
120   {
121     /* Workstation startup is using gotoWorkstationTemplate too,
122         if we remove this oc all other not manged attributes will cause errors */
123     if(isset($this->attrs['gotoKernelParameters'])){
124       $this->objectclasses = array();
125     }
127     /* Remove acc */
128     plugin::remove_from_parent();
129     $ldap = $this->config->get_ldap_link();
130     $ldap->cd($this->orig_dn);
131     $ldap->modify($this->attrs);
132     $this->handle_post_events("remove");
133     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
134   }
137   function update_term_member_FAIstate($act)
138   {
139     /* Get required informations */
140     $og     = $this->parent->by_object['ogroup'];
141     $allobs = $og->objcache;
143     /* Get correct value for FAIstate */
144     $action = $this->mapActions[$act];
146     /* Get ldap connection */
147     $ldap = $this->config->get_ldap_link();
148     $ldap->cd ($this->config->current['BASE']);
150     /* Foreach member of mthis ogroup  ... */
151     foreach($og->member  as $key ){
152   
153       /* check objectClasses and create attributes array */
154       $attrs = array("FAIstate"=>$action);  
155       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
156         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
157       }
158       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
159         $attrs['objectClass'][] = "FAIobject";
160       }
161       if($attrs['FAIstate'] == ""){
162         $attrs['FAIstate'] = array();
163       }
165       /* If this objects is workstation,terminal or server upodate FAIstate */ 
166       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
167         $ldap->cd ($key);
168         $ldap->modify($attrs);
169         if (!$ldap->success()){
170           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $key->dn, LDAP_MOD, get_class()));
171         }
172       }
173     }
174   }
176   function execute()
177   {
178     /* Call parent execute */
179     plugin::execute();
181     if(!$this->view_logged){
182       $this->view_logged = TRUE;
183       new log("view","ogroups/".get_class($this),$this->dn);
184     }
186     /*************** 
187       Handle requested action
188      ***************/
190     /* Watch for events */
191     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
192       $macaddresses= array();
193       foreach ($this->members as $cn => $macAddress){
194         $macaddresses[]= $macAddress;
195       }
197       print_red("trigger action is missing");
198       #gosaSupportDaemon::send("gosa_trigger_action_".$_POST['saction'], "255.255.255.255", $macaddresses);
200       /* Update members fai state */
201       $this->update_term_member_FAIstate(trim($_POST['saction']));
203       $this->didAction= TRUE;
205       /* Get dns from member objects. Create ldap object  */
206       $member = $this->parent->by_object['ogroup']->member;
207       $ldap = $this->config->get_ldap_link();
208       $ldap->cd($this->config->current['BASE']);
210       /* walk trough members and add FAIstate */ 
211       if ($_POST['saction'] != "wake"){
212         foreach($member as $dn =>  $object){
214               /* Get object */
215               $ldap->cat($dn,array("objectClass"));
216               $res                = $ldap->fetch();
217               $attrs              = array();
219               /* Add FAI state */
220               $attrs['FAIstate']  = "";
221               if(isset($this->mapActions[$_POST['saction']])){
222                       $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
223               }
225               /* Fix objectClass index */
226               for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
227                       $attrs['objectClass'][] = $res['objectClass'][$i];
228               }
230               /* Check if we must add the objectClass */
231               if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
232                       $attrs['objectClass'][] = "FAIobject";
233               }
235               if($attrs['FAIstate'] == ""){
236                       $attrs['FAIstate'] = array();
237               }
239               $ldap->cd($dn);
240               $ldap->modify($attrs);
241         if (!$ldap->success()){
242           msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $dn, LDAP_MOD, get_class()));
243         }
244       }
245     }
248     /*************** 
249       Add remove NTP server
250      ***************/
252     /* Add new ntp Server to our list */
253     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
254       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
255     }
257     /* Delete selected NtpServer for list of used servers  */
258     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
259       foreach($_POST['gotoNtpServerSelected'] as $name){
260         unset($this->gotoNtpServer[$name]);
261       }
262     }
265     /*************** 
266       Prepare smarty 
267      ***************/
269     /* Set government mode */
270     $smarty= get_smarty();
272     $tmp = $this->plInfo();
273     foreach($tmp['plProvidedAcls'] as $name => $translated) {
274       $smarty->assign($name."ACL",$this->getacl($name));
275     }
277     foreach($this->attributes as $attr){
278       $smarty->assign($attr,      $this->$attr);
279     }
281     /* Variables */
282     foreach(array("gotoMode","gotoNtpServer") as $val){
283       $smarty->assign($val."_select", $this->$val);
284     }
286     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
287           "instant_update" => _("Instant update"),
288           "update" => _("Scheduled update"),
289           "wake" => _("Wake"),
290           "reinstall" => _("Reinstall"),
291           "rescan" => _("Rescan hardware"),
292           "memcheck" => _("Memory test"),
293           "sysinfo"  => _("System analysis")));
295     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
296     $smarty->assign("modes", $this->modes);
298     $tmp = array();
299     foreach($this->gotoNtpServers as $server){
300       if(!in_array($server,$this->gotoNtpServer)){
301         $tmp[$server] = $server;
302       }
303     }
305     $smarty->assign("gotoNtpServers",$tmp); 
306     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
307     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
309     /* Show main page */
310     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
311   }
313   function save_object()
314   {
315     plugin::save_object();  
316     /* Set inherit mode */
317     if(isset($_POST['workgeneric_posted'])){
318       if(isset($_POST["inheritTimeServer"])){
319         $this->inheritTimeServer = true;
320       }else{
321         $this->inheritTimeServer = false;
322       }
323     }
324   }
326   /* Save to LDAP */
327   function save()
328   {
329     plugin::save();
331     /***************
332       Prepare special vars 
333      ***************/
335     /* Unset some special vars ... */
336     foreach (array("gotoSyslogServer") as $val){
337       if ($this->attrs[$val] == "default"){
338         $this->attrs[$val]= array();
339       }
340     }
342     /* Update ntp server settings */
343     if($this->inheritTimeServer){
344       $this->attrs['gotoNtpServer'] = "default";
345     }else{
346       /* Set ntpServers */
347       $this->attrs['gotoNtpServer'] = array();
348       foreach($this->gotoNtpServer as $server){
349         $this->attrs['gotoNtpServer'][] = $server;
350       }
351     }
354     /***************
355       Write to ldap 
356      ***************/
358     /* Write back to ldap */
359     $ldap= $this->config->get_ldap_link();
360     $ldap->cd($this->dn);
361     $this->cleanup();
362     $ldap->modify ($this->attrs);
364     if($this->initially_was_account){
365       new log("modify","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
366     }else{
367       new log("create","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
368     }
370     if(!$this->didAction){
371       $this->handle_post_events("modify");
372     }
373     if (!$ldap->success()){
374       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
375     }
377   }
378   
379   static 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   }
397   function PrepareForCopyPaste($source)
398   {
399     /* Create used ntp server array */
400     $this->gotoNtpServer= array();
402     if(isset($source['gotoNtpServer'])){
403       $this->inheritTimeServer = false;
404       unset($source['gotoNtpServer']['count']);
405       foreach($source['gotoNtpServer'] as $server){
406         $this->gotoNtpServer[$server] = $server;
407       }
408     }
410     /* Set inherit checkbox state */
411     if(in_array("default",$this->gotoNtpServer)){
412       $this->inheritTimeServer = true;
413       $this->gotoNtpServer=array();
414     }
416     /* Create available ntp options */
417     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
418     foreach($this->gotoNtpServers as $key => $server){
419       if($server == "default"){
420         unset($this->gotoNtpServers[$key]);
421       }
422     }
423   }
426 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
427 ?>