Code

Removed some duplicated acl assign
[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");
63   function workgeneric ($config, $dn= NULL, $parent= NULL)
64   {
65     plugin::plugin ($config, $dn, $parent);
66     $this->netConfigDNS = new termDNS($this->config,$this->dn,$this->objectclasses);
68     /* Read arrays */
69     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
70       if (!isset($this->attrs[$val])){
71         continue;
72       }
73       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
74         array_push($this->$val, $this->attrs[$val][$i]);
75       }
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)) || (count($this->gotoNtpServer) == 0)){
90       $this->inheritTimeServer = true;
91       $this->gotoNtpServer=array();
92     }
94     /* Create available ntp options */
95     $tmp = $this->config->data['SERVERS']['NTP'];
96     $this->gotoNtpServers = array();
97     foreach($tmp as $key => $server){
98       if($server == "default") continue;
99       $this->gotoNtpServers[$server] = $server;
100     }
102     $this->modes["active"]= _("Activated");
103     $this->modes["locked"]= _("Locked");
105     /* Set base */
106     if ($this->dn == "new"){
107       $ui= get_userinfo();
108       $this->base= dn2base($ui->dn);
109     } else {
110       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
111     }
113     /* Create an array of all Syslog servers */
114     $tmp = $this->config->data['SERVERS']['SYSLOG'];
115     foreach($tmp as $server){
116       $visible = $server;
117       if($server == "default") {
118         $visible = "["._("inherited")."]";
119       }
120       $this->gotoSyslogServers[$server] = $visible;
121     }
123     /* Save 'dn' for later referal */
124     $this->orig_dn= $this->dn;
125   }
128   function set_acl_base($base)
129   {
130     plugin::set_acl_base($base);
131     $this->netConfigDNS->set_acl_base($base);
132   }
134   function set_acl_category($cat)
135   {
136     plugin::set_acl_category($cat);
137     $this->netConfigDNS->set_acl_category($cat);
138   }
140   function execute()
141   {
142     /* Call parent execute */
143     plugin::execute();
145     /* Do we need to flip is_account state? */
146     if(isset($_POST['modify_state'])){
147       if($this->is_account && $this->acl_is_removeable()){
148         $this->is_account= FALSE;
149       }elseif(!$this->is_account && $this->acl_is_createable()){
150         $this->is_account= TRUE;
151       }
152     }
154     if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate"))){
155       $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
156       if ($cmd == ""){
157         print_red(_("No ACTIONCMD definition found in your gosa.conf"));
158       } else {
159         exec ($cmd." ".$this->netConfigDNS->macAddress." ".escapeshellarg($_POST['saction']), $dummy, $retval);
160         if ($retval != 0){
161           print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
162         } elseif ($_POST['saction'] != "wake") {
164           /* Set FAIstate */
165           $ldap = $this->config->get_ldap_link();
166           $ldap->cd($this->config->current['BASE']);
167           $ldap->cat($this->dn,array("objectClass"));
168           $res = $ldap->fetch();
170           $attrs = array();
171           $attrs['FAIstate'] = "";
172           if(isset($this->mapActions[$_POST['saction']])){
173             $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
174           }
176           for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
177             $attrs['objectClass'][] = $res['objectClass'][$i];
178           }
180           if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
181             $attrs['objectClass'][] = "FAIobject";
182           }
184           if($attrs['FAIstate'] == ""){
185             #FIXME we should check if FAIobject is used anymore
186             $attrs['FAIstate'] = array();
187           }
189           $ldap->cd($this->dn);
190           $ldap->modify($attrs);
191           show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$this->dn));
192  
193           $this->didAction= TRUE;
194         }
195       }
196     }
198     /* Do we represent a valid terminal? */
199     if (!$this->is_account && $this->parent == NULL){
200       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
201         _("This 'dn' has no workstation features.")."</b>";
202       return($display);
203     }
205     /* Base select dialog */
206     $once = true;
207     foreach($_POST as $name => $value){
208       if(preg_match("/^chooseBase/",$name) && $once && $this->acl_is_writeable("base")){
209         $once = false;
210         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
211         $this->dialog->setCurrentBase($this->base);
212       }
213     }
215     /* Dialog handling */
216     if(is_object($this->dialog)){
217       /* Must be called before save_object */
218       $this->dialog->save_object();
220       if($this->dialog->isClosed()){
221         $this->dialog = false;
222       }elseif($this->dialog->isSelected()){
223         $this->base = $this->dialog->isSelected();
225         /* A new base was selected, check if it is a valid one */
226         $tmp = $this->get_allowed_bases();
227         if(isset($tmp[$this->dialog->isSelected()])){
228           $this->base = $this->dialog->isSelected();
229         }
231         $this->dialog= false;
232       }else{
233         return($this->dialog->execute());
234       }
235     }
237     /* Add new ntp Server to our list */ 
238     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers'])) && $this->acl_is_writeable("gotoNtpServer")){
239       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
240     }
242     /* Delete selected NtpServer for list of used servers  */
243     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected'])) && $this->acl_is_writeable("gotoNtpServer")){
244       foreach($_POST['gotoNtpServerSelected'] as $name){
245         unset($this->gotoNtpServer[$name]);
246       }
247     }
249     /* Fill templating stuff */
250     $smarty= get_smarty();
252     /* Set acls */
253     $tmp = $this->plInfo();
254     foreach($tmp['plProvidedAcls'] as $name => $translation){
255       $smarty->assign($name."ACL",$this->getacl($name));
256     }
258     $smarty->assign("cn", $this->cn);
259     $smarty->assign("l", $this->l);
260     $smarty->assign("bases", $this->get_allowed_bases());
261     $smarty->assign("staticAddress", "");
263     $tmp = array();
264     foreach($this->gotoNtpServers as $server){
265       if(!in_array($server,$this->gotoNtpServer)){
266         $tmp[$server] = $server;
267       }
268     }
269     $smarty->assign("gotoNtpServers",$tmp);
270         
271     /* Check if workstation is online */
272     $query= "fping -q -r 1 -t 500 ".$this->cn;
273     exec ($query, $dummy, $retval);
275     /* Offline */
276     if ($retval == 0){
277       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
278                                        "instant_update" => _("Instant update"),
279                                        "update" => _("Scheduled update"),
280                                        "reinstall" => _("Reinstall"),
281                                        "rescan" => _("Rescan hardware"),
282                                        "memcheck" => _("Memory test"),
283                                        "sysinfo"  => _("System analysis")));
284     } else {
285       $smarty->assign("actions", array("wake" => _("Wake up"),
286                                        "reinstall" => _("Reinstall"),
287                                        "update" => _("Scheduled update"),
288                                        "memcheck" => _("Memory test"),
289                                        "sysinfo"  => _("System analysis")));
290     }
291     /* Arrays */
292     $smarty->assign("modes", $this->modes);
293     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
294     $smarty->assign("syslogservers", $this->gotoSyslogServers);
296     $ntpser = array();
297     foreach($this->gotoNtpServers as $server){
298       if(!in_array($server,$this->gotoNtpServer)){
299         $ntpser[$server] = $server;
300       }
301     }
302     $smarty->assign("gotoNtpServers", $ntpser);
304     /* Variables */
305     foreach(array("base", "gotoMode", "gotoSyslogServer", "gotoNtpServer") as $val){
306       $smarty->assign($val."_select", $this->$val);
307     }
309     /* tell smarty the inherit checkbox state */
310     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
312     /* Show main page */
313     $smarty->assign("netconfig", $this->netConfigDNS->execute());
314     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
315   }
317   function remove_from_parent()
318   {
319     if($this->acl_is_removeable()){
321       $this->netConfigDNS->remove_from_parent();
322       $ldap= $this->config->get_ldap_link();
323       $ldap->rmdir($this->dn);
324       show_ldap_error($ldap->get_error(), sprintf(_("Removing of system workstation/generic with dn '%s' failed."),$this->dn));
326       /* Optionally execute a command after we're done */
327       $this->handle_post_events("remove", array("macAddress" => $this->netConfigDNS->macAddress));
329       /* Delete references to object groups */
330       $ldap->cd ($this->config->current['BASE']);
331       $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
332       while ($ldap->fetch()){
333         $og= new ogroup($this->config, $ldap->getDN());
334         unset($og->member[$this->dn]);
335         $og->save ();
336       }
337     }
338   }
341   /* Save data to object */
342   function save_object()
343   {
345     /* Create a base backup and reset the
346        base directly after calling plugin::save_object();
347        Base will be set seperatly a few lines below */
348     $base_tmp = $this->base;
349     plugin::save_object();
350     $this->base = $base_tmp;
352     /* Save base, since this is no LDAP attribute */
353     $tmp = $this->get_allowed_bases();
354     if(isset($_POST['base'])){
355       if(isset($tmp[$_POST['base']])){
356         $this->base= $_POST['base'];
357       }
358     }
360     $this->netConfigDNS->save_object();
362     /* Set inherit mode */
363     if((isset($_POST['workgeneric_posted'])) && ($this->acl_is_writeable("gotoNtpServer"))){
364       if(isset($_POST["inheritTimeServer"])){
365         $this->inheritTimeServer = true;
366       }else{
367         $this->inheritTimeServer = false;
368       }
369     }
371   }
374   /* Check supplied data */
375   function check()
376   {
377     /* Call common method to give check the hook */
378     $message= plugin::check();
379   
380     /* Skip IP & Mac checks if this is a template */
381     if($this->cn != "wdefault"){
382       $message= array_merge($message, $this->netConfigDNS->check());
383     }
385     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
387     /* Set new acl base */
388     if($this->dn == "new") {
389       $this->set_acl_base($this->base);
390     }
392     if ($this->orig_dn == "new" && !$this->acl_is_createable()){
393       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
394     }
397     if ($this->cn == ""){
398       $message[]= _("The required field 'Workstation name' is not set.");
399     }
401     if ($this->orig_dn != $this->dn){
402       $ldap= $this->config->get_ldap_link();
403       $ldap->cd ($this->base);
405       if($this->cn == "wdefault"){
406         $ldap->cat($this->dn);
407       }else{
408         $ldap->search ("(&(cn=".$this->cn.")(objectClass=gotoWorkstation))", array("cn"));
409       }
410       if ($ldap->count() != 0){
411         while ($attrs= $ldap->fetch()){
412           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
413             continue;
414           } else {
415             if ($attrs['dn'] != $this->orig_dn){
416               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
417               break;
418             }
419           }
420         }
421       }
422     }
424     /* Check for valid ntpServer selection */
425     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
426       $message[]= _("There must be at least one NTP server selected, or the inherit mode activated.");
427     }
429     return ($message);
430   }
433   /* Save to LDAP */
434   function save()
435   {
436     plugin::save();
438     /* Strip out 'default' values */
439     foreach (array("gotoSyslogServer") as $val){
441       if ($this->attrs[$val] == "default"){
442         $this->attrs[$val]= array();
443       }
444     }
446     /* Add missing arrays */
447     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
448       if (isset ($this->$val) && count ($this->$val) != 0){
449         $this->attrs["$val"]= $this->$val;
450       }
451     }
453     /* Remove all empty values */
454     if ($this->orig_dn == 'new'){
455       $attrs= array();
456       foreach ($this->attrs as $key => $val){
457         if (is_array($val) && count($val) == 0){
458           continue;
459         }
460         $attrs[$key]= $val;
461       }
462       $this->attrs= $attrs;
463     }
465     /* Update ntp server settings */
466     if($this->inheritTimeServer){
467       if($this->is_new){
468         if(isset($this->attrs['gotoNtpServer'])){
469           unset($this->attrs['gotoNtpServer']);
470         }
471       }else{
472         $this->attrs['gotoNtpServer'] = array();
473       }
474     }else{
475       /* Set ntpServers */
476       $this->attrs['gotoNtpServer'] = array();
477       foreach($this->gotoNtpServer as $server){
478         $this->attrs['gotoNtpServer'][] = $server;
479       }
480     }
482     if(($this->gosaUnitTag) && (!in_array_ics("gosaAdministrativeUnitTag",$this->attrs['objectClass']))){
483       $this->attrs['objectClass'][] = "gosaAdministrativeUnitTag";
484     }
486     /* Write back to ldap */
487     $ldap= $this->config->get_ldap_link();
488     if ($this->orig_dn == 'new'){
489       $ldap->cd($this->config->current['BASE']);
490       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
491       $ldap->cd($this->dn);
492       $ldap->add($this->attrs);
493       show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
494       if(!$this->didAction){
495         $this->handle_post_events("add");
496       }
497     } else {
498       if ($this->orig_dn != $this->dn){
499         $this->move($this->orig_dn, $this->dn);
500       }
501       $ldap->cd($this->dn);
502       $this->cleanup();
503       $ldap->modify ($this->attrs); 
505       if(!$this->didAction){
506         $this->handle_post_events("modify");
507       }
508     }
510     /* cn=default and macAddress=- indicates that this is a template */
511     if($this->cn == "wdefault"){
512       $this->netConfigDNS->macAddress = "-";
513     }
515     $this->netConfigDNS->cn = $this->cn;
516     $this->netConfigDNS->save($this->dn);
517     show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic with dn '%s' failed."),$this->dn));
518   }
521   /* Return plugin informations for acl handling 
522       #FIXME FAIscript seams to ununsed within this class... */ 
523   function plInfo()
524   {
525     return (array(  
526           "plShortName"   => _("Generic"),
527           "plDescription" => _("Workstation generic"),
528           "plSelfModify"  => FALSE,
529           "plDepends"     => array(),
530           "plPriority"    => 0,
531           "plSection"     => array("administration"),
532           "plCategory"    => array("workstation" => array("description"  => _("Workstation"),
533                                                           "objectClass"  => "gotoWorkstation")),
534           "plProvidedAcls"=> array(
535             "cn"                  => _("Workstation name"),
536             "l"                   => _("Location") ,
537             "base"                => _("Base") ,
538             "gotoMode"            => _("Goto mode"), 
539             "gotoSyslogServer"    => _("Syslog server"), 
540             "gotoNtpServer"       => _("Ntp server"), 
541             "gotoRootPasswd"      => _("Root password"),
542             "FAIstate"            => _("Action flag"))
543           ));
544   }
548 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
549 ?>