Code

Updated workgeneric acls
[gosa.git] / plugins / admin / systems / class_workstationGeneric.inc
1 <?php
3 class workgeneric extends plugin
4 {
5   /* CLI vars */
6   var $cli_summary= "Manage workstation base objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* Generic terminal attributes */
11   var $gotoMode= "locked";
12   var $gotoSyslogServer= "";
13   var $gotoSyslogServers= array();
14   var $gotoNtpServer= array();
15   var $gotoNtpServers= array();
16   var $gotoSndModule= "";
17   var $gotoFloppyEnable= "";
18   var $gotoCdromEnable= "";
19   var $ghCpuType= "-";
20   var $ghMemSize= "-";
21   var $ghUsbSupport= "-";
22   var $ghNetNic= array();
23   var $ghIdeDev= array();
24   var $ghScsiDev= array();
25   var $ghGfxAdapter= "-";
26   var $ghSoundAdapter= "-";
27   var $gotoLastUser= "-";
28   var $FAIscript= "";
29   var $didAction= FALSE;
30   var $FAIstate= "";
32   /* Needed values and lists */
33   var $base= "";
34   var $cn= "";
35   var $l= "";
36   var $orig_dn= "";
38   /* Plugin side filled */
39   var $modes= array();
41   var $netConfigDNS;
43   var $inheritTimeServer = true;
45   /* attribute list for save action */
46   var $ignore_account= TRUE;
47   var $attributes= array("gotoMode", "gotoSyslogServer", "gotoNtpServer",
48       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
49       "ghCpuType", "ghMemSize", "ghUsbSupport",
50       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser", "l","FAIscript");
51   var $objectclasses= array("top", "gotoWorkstation", "GOhard","FAIobject");
53   var $mapActions   = array("reboot"          => "localboot",
54                             "instant_update"  => "softupdate",
55                             "update"          => "sceduledupdate",
56                             "reinstall"       => "install",
57                             "rescan"          => "",
58                             "memcheck"        => "memcheck",
59                             "sysinfo"         => "sysinfo");
62   var $fai_activated = FALSE;
64   function workgeneric ($config, $dn= NULL, $parent= NULL)
65   {
66     /* Check if FAI is activated */
67     $tmp = search_config($config->data,"faiManagement","CLASS");
68     if(!empty($tmp)){
69       $this->fai_activated = TRUE;
70     }
72     plugin::plugin ($config, $dn, $parent);
73     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
74     $this->netConfigDNS->acl = $this->acl;
76     /* Read arrays */
77     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
78       if (!isset($this->attrs[$val])){
79         continue;
80       }
81       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
82         array_push($this->$val, $this->attrs[$val][$i]);
83       }
84     }
86     /* Create used ntp server array */
87     $this->gotoNtpServer= array();
88     if(isset($this->attrs['gotoNtpServer'])){
89       $this->inheritTimeServer = false;
90       unset($this->attrs['gotoNtpServer']['count']);
91       foreach($this->attrs['gotoNtpServer'] as $server){
92         $this->gotoNtpServer[$server] = $server;
93       }
94     }
96     /* Set inherit checkbox state */
97     if((in_array("default",$this->gotoNtpServer)) || (count($this->gotoNtpServer) == 0)){
98       $this->inheritTimeServer = true;
99       $this->gotoNtpServer=array();
100     }
102     /* Create available ntp options */
103     $tmp = $this->config->data['SERVERS']['NTP'];
104     $this->gotoNtpServers = array();
105     foreach($tmp as $key => $server){
106       if($server == "default") continue;
107       $this->gotoNtpServers[$server] = $server;
108     }
110     $this->modes["active"]= _("Activated");
111     $this->modes["locked"]= _("Locked");
113     /* Set base */
114     if ($this->dn == "new"){
115       $ui= get_userinfo();
116       $this->base= dn2base($ui->dn);
117     } else {
118       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
119     }
121     /* Create an array of all Syslog servers */
122     $tmp = $this->config->data['SERVERS']['SYSLOG'];
123     foreach($tmp as $server){
124       $visible = $server;
125       if($server == "default") {
126         $visible = "["._("inherited")."]";
127       }
128       $this->gotoSyslogServers[$server] = $visible;
129     }
131     /* Save 'dn' for later referal */
132     $this->orig_dn= $this->dn;
133   }
135   function execute()
136   {
137     /* Call parent execute */
138     plugin::execute();
139     $this->netConfigDNS->acl = $this->acl;
141     /* Do we need to flip is_account state? */
142     if (isset($_POST['modify_state'])){
143       $this->is_account= !$this->is_account;
144     }
146     if (isset($_POST['action']) && chkacl($this->acl,"FAIstate") == ""){
147       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
148       if ($cmd == ""){
149         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
150       } else {
151         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
152         if ($retval != 0){
153           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
154         } elseif ($_POST['saction'] != "wake") {
156           if($this->fai_activated && $this->dn != "new"){
157             /* Set FAIstate */
158             $ldap = $this->config->get_ldap_link();
159             $ldap->cd($this->config->current['BASE']);
160             $ldap->cat($this->dn,array("objectClass"));
161             $res = $ldap->fetch();
163             $attrs = array();
164             $attrs['FAIstate'] = "";
165             if(isset($this->mapActions[$_POST['saction']])){
166               $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
167             }
169             for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
170               $attrs['objectClass'][] = $res['objectClass'][$i];
171             }
173             if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
174               $attrs['objectClass'][] = "FAIobject";
175             }
177             if($attrs['FAIstate'] == ""){
178 #FIXME we should check if FAIobject is used anymore
179               $attrs['FAIstate'] = array();
180             }
182             $ldap->cd($this->dn);
183             $ldap->modify($attrs);
184             show_ldap_error($ldap->get_error());
186             $this->didAction= TRUE; 
187           }
188         }
189       }
190     }
192     /* Do we represent a valid terminal? */
193     if (!$this->is_account && $this->parent == NULL){
194       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
195         _("This 'dn' has no workstation features.")."</b>";
196       return($display);
197     }
199     /* Base select dialog */
200     $once = true;
201     foreach($_POST as $name => $value){
202       if(preg_match("/^chooseBase/",$name) && $once){
203         $once = false;
204         $this->dialog = new baseSelectDialog($this->config);
205         $this->dialog->setCurrentBase($this->base);
206       }
207     }
209     /* Dialog handling */
210     if(is_object($this->dialog)){
211       /* Must be called before save_object */
212       $this->dialog->save_object();
214       if($this->dialog->isClosed()){
215         $this->dialog = false;
216       }elseif($this->dialog->isSelected()){
217         $this->base = $this->dialog->isSelected();
218         $this->dialog= false;
219       }else{
220         return($this->dialog->execute());
221       }
222     }
224     /* Add new ntp Server to our list */ 
225     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && chkacl($this->acl,"gotoNtpServer") == ""){
226       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
227     }
229     /* Delete selected NtpServer for list of used servers  */
230     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && chkacl($this->acl,"gotoNtpServer") == ""){
231       foreach($_POST['gotoNtpServerSelected'] as $name){
232         unset($this->gotoNtpServer[$name]);
233       }
234     }
236     /* Fill templating stuff */
237     $smarty= get_smarty();
238     $smarty->assign("cn", $this->cn);
239     $smarty->assign("l", $this->l);
240     $smarty->assign("bases", $this->config->idepartments);
241     $smarty->assign("staticAddress", "");
242     $smarty->assign("gotoNtpServers",$this->gotoNtpServers);
243         
244     /* Check if workstation is online */
245     $query= "fping -q -r 1 -t 500 ".$this->cn;
246     exec ($query, $dummy, $retval);
248     /* Offline */
249     if ($retval == 0){
250       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
251                                        "instant_update" => _("Instant update"),
252                                        "update" => _("Scheduled update"),
253                                        "reinstall" => _("Reinstall"),
254                                        "rescan" => _("Rescan hardware"),
255                                        "memcheck" => _("Memory test"),
256                                        "sysinfo"  => _("System analysis")));
257     } else {
258       $smarty->assign("actions", array("wake" => _("Wake up"),
259                                        "reinstall" => _("Reinstall"),
260                                        "update" => _("Scheduled update"),
261                                        "memcheck" => _("Memory test"),
262                                        "sysinfo"  => _("System analysis")));
263     }
264     /* Arrays */
265     $smarty->assign("modes", $this->modes);
266     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
267     $smarty->assign("syslogservers", $this->gotoSyslogServers);
268     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
270     /* Variables */
271     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
272       $smarty->assign($val."_select", $this->$val);
273       $smarty->assign($val."ACL", chkacl($this->acl, $val));
274     }
275     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
277     /* tell smarty the inherit checkbox state */
278     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
280     /* Show main page */
281     $this->netConfigDNS->cn= $this->cn;
282     $smarty->assign("netconfig", $this->netConfigDNS->execute());
283     $smarty->assign("fai_activated",$this->fai_activated);
284     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
285   }
287   function remove_from_parent()
288   {
289     $this->netConfigDNS->acl = $this->acl;
290     $this->netConfigDNS->remove_from_parent();
291     $ldap= $this->config->get_ldap_link();
292     $ldap->rmdir($this->dn);
293     show_ldap_error($ldap->get_error(), _("Removing workstation failed"));
295     /* Optionally execute a command after we're done */
296     $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
298     /* Delete references to object groups */
299     $ldap->cd ($this->config->current['BASE']);
300     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
301     while ($ldap->fetch()){
302       $og= new ogroup($this->config, $ldap->getDN());
303       unset($og->member[$this->dn]);
304       $og->save ();
305     }
306   }
309   /* Save data to object */
310   function save_object()
311   {
312     plugin::save_object();
314     /* Save base, since this is no LDAP attribute */
315     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
316       $this->base= $_POST['base'];
317     }
318     $this->netConfigDNS->save_object();
320     /* Set inherit mode */
321     if(isset($_POST['workgeneric_posted'])){
322       if(isset($_POST["inheritTimeServer"])){
323         $this->inheritTimeServer = true;
324       }else{
325         $this->inheritTimeServer = false;
326       }
327     }
329   }
332   /* Check supplied data */
333   function check()
334   {
335     /* Call common method to give check the hook */
336     $message= plugin::check();
338     if($this->cn != "wdefault"){
339       $message= array_merge($message, $this->netConfigDNS->check());
340     }
342     $ui= get_userinfo();
343     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
344     $acl= get_permissions ($this->dn, $ui->subtreeACL);
345     $acl= get_module_permission($acl, "group", $this->dn);
346     if (chkacl($acl, "create") != ""){
347       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
348     }
350     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
351       $message[]= _("The required field 'Workstation name' is not set.");
352     }
354     if ($this->orig_dn != $this->dn){
355       $ldap= $this->config->get_ldap_link();
356       $ldap->cd ($this->base);
357   
358       if($this->cn == "wdefault"){
359         $ldap->cat($this->dn);
360       }else{
361         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
362       }
363       if ($ldap->count() != 0){
364         while ($attrs= $ldap->fetch()){
365           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
366             continue;
367           } else {
368             if ($attrs['dn'] != $this->orig_dn){
369               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
370               break;
371             }
372           }
373         }
374       }
375     }
378     /* Check for valid ntpServer selection */
379     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
380       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
381     }
383     return ($message);
384   }
387   /* Save to LDAP */
388   function save()
389   {
390     $this->netConfigDNS->acl = $this->acl;
391     plugin::save();
393     /* Strip out 'default' values */
394     foreach (array("gotoSyslogServer") as $val){
395       if (!isset($this->attrs[$val]) || $this->attrs[$val] == "default"){
396         $this->attrs[$val]= array();
397       }
398     }
400     /* Add missing arrays */
401     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
402       if (isset ($this->$val) && count ($this->$val) != 0){
403         $this->attrs["$val"]= $this->$val;
404       }
405     }
407     /* Remove all empty values */
408     if ($this->orig_dn == 'new'){
409       $attrs= array();
410       foreach ($this->attrs as $key => $val){
411         if (is_array($val) && count($val) == 0){
412           continue;
413         }
414         $attrs[$key]= $val;
415       }
416       $this->attrs= $attrs;
417     }
419     /* Update ntp server settings */
420     if($this->inheritTimeServer){
421       if($this->new){
422         if(isset($this->attrs['gotoNtpServer'])){
423           unset($this->attrs['gotoNtpServer']);
424         }
425       }else{
426         $this->attrs['gotoNtpServer'] = array();
427       }
428     }else{
429       /* Set ntpServers */
430       $this->attrs['gotoNtpServer'] = array();
431       foreach($this->gotoNtpServer as $server){
432         $this->attrs['gotoNtpServer'][] = $server;
433       }
434     }
436     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
437       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
438     }
440     /* Write back to ldap */
441     $ldap= $this->config->get_ldap_link();
442     if ($this->orig_dn == 'new'){
443       $ldap->cd($this->config->current['BASE']);
444       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
445       $ldap->cd($this->dn);
446       $ldap->add($this->attrs);
447       show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
448       if(!$this->didAction){
449         $this->handle_post_events("add");
450       }
451     } else {
452       if ($this->orig_dn != $this->dn){
453         $this->move($this->orig_dn, $this->dn);
454       }
455       $ldap->cd($this->dn);
456       $this->cleanup();
457       $ldap->modify ($this->attrs); 
459       if(!$this->didAction){
460         $this->handle_post_events("modify");
461       }
462     }
464     /* Set macAddress to '-' this indicates that this is a Workstation Template  */
465     if($this->cn == "wdefault"){
466       $this->netConfigDNS->macAddress = "-";
467     }
469     $this->netConfigDNS->cn = $this->cn;
470     $this->netConfigDNS->save($this->dn);
471     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
472   }
476 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
477 ?>