Code

2d6396060dd72f424ddeb7d445185477986410a4
[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   /* Stores a list of the member dn's */
9   var $member_dn = array();
11   var $gotoMode           = "locked";
12   var $gotoSyslogServer   = "";
13   var $gotoNtpServer      = array();
14   var $gotoTerminalPath   = array();
15   var $gotoSwapServer     = array();
16   var $modes              = array();
17   var $inheritTimeServer  = true;
18   var $is_account                     = true; 
19   var $orig_dn            = "";
20   var $didAction          = FALSE;
21   var $mapActions   = array("halt"            => "halt",
22                             "reboot"          => "reboot",
23                             "update"          => "update",
24                             "reinstall"       => "reinstall",
25                             "rescan"          => "rescan",
26                             "wake"            => "wakeup",
27                            # These are currently not supported by the tftp daemon
28                            # "memcheck"        => "memcheck",
29                            # "sysinfo"         => "sysinfo"
30                            );
32   var $attributes     = array("gotoMode","gotoSyslogServer", "gotoNtpServer", "gotoTerminalPath", "gotoSwapServer");
33   var $objectclasses  = array("gotoWorkstationTemplate");
34   var $CopyPasteVars  = array("gotoNtpServers","modes","inheritTimeServer","members");
35   var $view_logged    = FALSE;
36   var $nfsservers     = array();
37   var $swapservers    = array();
38   var $member_of_ogroup= false;
39   var $members_inherit_from_group = false;
41   function termgroup (&$config, $dn= NULL, $parent= NULL)
42   {
43     /***************
44       Some  initialisations
45      ***************/
47     plugin::plugin($config, $dn, $parent);
48     $ldap= $config->get_ldap_link();
50     $this->is_account = true;
51     $this->modes["active"]= _("Activated");
52     $this->modes["locked"]= _("Locked");
53 //    $this->modes["memcheck"]= _("Memory test");
54 //    $this->modes["sysinfo"]= _("System analysis");
56     $this->orig_dn =    $this->dn;
58     /*************** 
59       Get mac addresses from member objects  
60      ***************/
62     /* We're only interested in the terminal members here, evaluate
63        these... */
64     if(isset($this->attrs['member'])){
65       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
66         $member= $this->attrs['member'][$i];
67         array_push($this->member_dn, $member);
68         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
69         if ($ldap->success()){
70           $attrs = $ldap->fetch();
71           if (in_array("gotoTerminal", $attrs['objectClass']) ||
72               in_array("gotoWorkstation", $attrs['objectClass'])){
73             if (isset($attrs['macAddress'])){
74               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
75             } else {
76               $this->members[$attrs['cn'][0]]= "";
77             }
78           }
79         }
80       }
81     }
83     /*************** 
84       Perpare NTP settings 
85      ***************/
87     /* Create used ntp server array */
88     $this->gotoNtpServer= array();
89     if(isset($this->attrs['gotoNtpServer'])){
90       $this->inheritTimeServer = false;
91       unset($this->attrs['gotoNtpServer']['count']);
92       foreach($this->attrs['gotoNtpServer'] as $server){
93         $this->gotoNtpServer[$server] = $server;
94       }
95     }
97     /* Get Share servers */
98     $tmp2 = array();
99     $tmp2['!']= _("Local swap");
100     foreach($this->config->data['SERVERS']['NBD'] as $server){
101       if($server != "default"){
102         $tmp2[$server]= $server;
103       }else{
104         if($this->member_of_ogroup){
105           $tmp2[$server]="["._("inherited")."]";
106         }
107       }
108     }
109     $this->swapservers= $tmp2;
111     $tmp2 = array();
112     foreach($this->config->data['SERVERS']['NFS'] as $server){
113       if($server != "default"){
114         $tmp2[$server]= $server;
115       }else{
116         if($this->member_of_ogroup){
117           $tmp2[$server]="["._("inherited")."]";
118         }
119       }
120     }
121     $this->nfsservers= $tmp2;
123     /* Set inherit checkbox state */
124     if(in_array("default",$this->gotoNtpServer)){
125       $this->inheritTimeServer = true;
126       $this->gotoNtpServer=array();
127     }
129     /* Create available ntp options */
130     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
131     foreach($this->gotoNtpServers as $key => $server){
132       if($server == "default"){
133         unset($this->gotoNtpServers[$key]);
134       }
135     }
136   }
138   function check()
139   {
140      /* Call common method to give check the hook */
141     $message= plugin::check();
143     if (!$this->acl_is_createable() && $this->dn == "new"){
144       $message[]= msgPool::permCreate();
145     }
147     /* Check for valid ntpServer selection */
148     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
149       $message[]= msgPool::required(_("NTP server"));
150     }
151     return($message);
152   }
154   function remove_from_parent()
155   {
156     /* Workstation startup is using gotoWorkstationTemplate too,
157         if we remove this oc all other not manged attributes will cause errors */
158     if(isset($this->attrs['gotoKernelParameters'])){
159       $this->objectclasses = array();
160     }
162     /* Remove acc */
163     plugin::remove_from_parent();
164     $ldap = $this->config->get_ldap_link();
165     $ldap->cd($this->orig_dn);
166     $ldap->modify($this->attrs);
167     $this->handle_post_events("remove");
168     new log("remove","ogroups/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
169   }
173   function execute()
174   {
175     /* Call parent execute */
176     plugin::execute();
178     if(!$this->view_logged){
179       $this->view_logged = TRUE;
180       new log("view","ogroups/".get_class($this),$this->dn);
181     }
184     /*************** 
185       Handle requested action
186      ***************/
188     /* Watch for events */
189     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']]) ){
191       /* Check if we have an DaemonEvent for this action */
192       $action = $this->mapActions[$_POST['saction']];
193       if(class_available("DaemonEvent")){
194         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
195         $macaddresses= array();
196         foreach ($this->members as $cn => $macAddress){
197           $macaddresses[]= $macAddress;
198         }
200         if(isset($events['TRIGGERED']["DaemonEvent_".$action])){
201           $evt = $events['TRIGGERED']["DaemonEvent_".$action];
202           $tmp = new $evt['CLASS_NAME']($this->config);
203           $tmp->add_targets($macaddresses);
204           $tmp->set_type(TRIGGERED_EVENT);
205           $o_queue = new gosaSupportDaemon();
206           if(!$o_queue->append($tmp)){
207             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
208           }
209         }
210       } else {
211         msg_dialog::display(_("Event error"),
212             sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
213       }
214     }
217     /*************** 
218       Add remove NTP server
219      ***************/
221     /* Add new ntp Server to our list */
222     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
223       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
224     }
226     /* Delete selected NtpServer for list of used servers  */
227     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
228       foreach($_POST['gotoNtpServerSelected'] as $name){
229         unset($this->gotoNtpServer[$name]);
230       }
231     }
234     /*************** 
235       Prepare smarty 
236      ***************/
238     /* Set government mode */
239     $smarty= get_smarty();
241     if (isset($this->parent->by_name['termstartup'])){
242         $smarty->assign("is_termgroup", "1");
243     } else {
244         $smarty->assign("is_termgroup", "0");
245     }
247     $tmp = $this->plInfo();
248     foreach($tmp['plProvidedAcls'] as $name => $translated) {
249       $smarty->assign($name."ACL",$this->getacl($name));
250     }
252     foreach($this->attributes as $attr){
253       $smarty->assign($attr,      $this->$attr);
254     }
256     /* Variables */
257     foreach(array("gotoMode","gotoNtpServer") as $val){
258       $smarty->assign($val."_select", $this->$val);
259     }
261     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
262           "instant_update" => _("Instant update"),
263           "update" => _("Scheduled update"),
264           "wake" => _("Wake"),
265           "reinstall" => _("Reinstall"),
266           "rescan" => _("Rescan hardware"),
267           "memcheck" => _("Memory test"),
268           "sysinfo"  => _("System analysis")));
270     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
271     $smarty->assign("modes", $this->modes);
273     $tmp = array();
274     foreach($this->gotoNtpServers as $server){
275       if(!in_array($server,$this->gotoNtpServer)){
276         $tmp[$server] = $server;
277       }
278     }
280     $smarty->assign("gotoNtpServers",$tmp); 
282     $smarty->assign("nfsservers",     $this->nfsservers);
283     $smarty->assign("swapservers",    $this->swapservers);
285     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
286     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
288     /* Variables */
289     foreach(array("gotoTerminalPath", "gotoSwapServer") as $val){
290             $smarty->assign($val."_select", $this->$val);
291     }
294     /* Show main page */
295     $smarty->assign("members_inherit_from_group", $this->members_inherit_from_group);
296     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
297   }
300   function save_object()
301   {
302     plugin::save_object();  
303     /* Set inherit mode */
304     if(isset($_POST['workgeneric_posted'])){
306       /* Handle the inherit to members button */
307       $this->members_inherit_from_group = isset($_POST['members_inherit_from_group']);
309       if(isset($_POST["inheritTimeServer"])){
310         $this->inheritTimeServer = true;
311       }else{
312         $this->inheritTimeServer = false;
313       }
314     }
315   }
317   /* Save to LDAP */
318   function save()
319   {
320     if (isset($this->parent->by_name['termstartup'])){
321         $this->objectclasses= array("gotoTerminalTemplate");
322     } else {
323         $this->objectclasses= array("gotoWorkstationTemplate");
324     }
326     plugin::save();
328     /* Tell members to inherit all attributes if the flag is set  */
329     $this->inherit_attributes_to_members();
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     }
376   }
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             "gotoTerminalPath"    => _("Root server"),
395             "gotoSwapServer"      => _("Swap server"))
396           ));
397   }
399   function inherit_attributes_to_members()
400   {
401     if ($this->members_inherit_from_group) {
402       foreach ($this->member_dn as $dn) {
403         $member_obj = new worktabs($this->config, $this->config->data['TABS']['WORKTABS'], $dn, "workstation");
404         $member_obj->by_object['workgeneric']->set_everything_to_inherited();
405         $member_obj->save();
406       }
407     }
408   }
410   function PrepareForCopyPaste($source)
411   {
412     /* Create used ntp server array */
413     $this->gotoNtpServer= array();
415     $source_o = new termgroup ($this->config, $source['dn']);
416     foreach(array("gotoTerminalPath","gotoSwapServer") as $attr){
417       $this->$attr = $source_o->$attr;
418     }
420     if(isset($source['gotoNtpServer'])){
421       $this->inheritTimeServer = false;
422       unset($source['gotoNtpServer']['count']);
423       foreach($source['gotoNtpServer'] as $server){
424         $this->gotoNtpServer[$server] = $server;
425       }
426     }
428     /* Set inherit checkbox state */
429     if(in_array("default",$this->gotoNtpServer)){
430       $this->inheritTimeServer = true;
431       $this->gotoNtpServer=array();
432     }
434     /* Create available ntp options */
435     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
436     foreach($this->gotoNtpServers as $key => $server){
437       if($server == "default"){
438         unset($this->gotoNtpServers[$key]);
439       }
440     }
441   }
444 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
445 ?>