Code

Updated functionality for postmodify and passwords
[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= "disabled";
12   var $gotoTerminalPath= "";
13   var $gotoSwapServer= "";
14   var $gotoSyslogServer= "";
15   var $gotoNtpServer= "";
16   var $gotoSndModule= "";
17   var $gotoFloppyEnable= "";
18   var $gotoCdromEnable= "";
19   var $ghCpuType= "-";
20   var $ghMemSize= "-";
21   var $macAddress= "";
22   var $ipHostNumber= "";
23   var $ghUsbSupport= "-";
24   var $ghNetNic= array();
25   var $ghIdeDev= array();
26   var $ghScsiDev= array();
27   var $ghGfxAdapter= "-";
28   var $ghSoundAdapter= "-";
29   var $gotoLastUser= "-";
31   /* Needed values and lists */
32   var $base= "";
33   var $cn= "";
34   var $orig_dn= "";
36   /* Plugin side filled */
37   var $modes= array();
39   /* attribute list for save action */
40   var $ignore_account= TRUE;
41   var $attributes= array("gotoMode", "gotoTerminalPath", "macAddress",
42       "gotoSwapServer", "gotoSyslogServer", "gotoNtpServer",
43       "gotoFloppyEnable", "gotoCdromEnable", "cn", "gotoSndModule",
44       "ghCpuType", "ghMemSize", "ghUsbSupport", "ipHostNumber",
45       "ghGfxAdapter", "ghSoundAdapter", "gotoLastUser");
46   var $objectclasses= array("top", "gotoWorkstation", "GOhard");
48   function workgeneric ($config, $dn= NULL)
49   {
50     plugin::plugin ($config, $dn);
52     /* Load available modes */
53     $ldap= $this->config->get_ldap_link();
54     $ldap->cd ($this->config->current['BASE']);
55     $ldap->search ("(objectClass=gotoInstallProfile)");
56     while ($attrs= $ldap->fetch()){
57       $this->modes[]= $attrs["cn"][0];
58     }
60     /* Read arrays */
61     foreach (array("ghNetNic", "ghIdeDev", "ghScsiDev") as $val){
62       if (!isset($this->attrs[$val])){
63         continue;
64       }
65       for ($i= 0; $i<$this->attrs[$val]['count']; $i++){
66         array_push($this->$val, $this->attrs[$val][$i]);
67       }
68     }
70     $this->modes["disabled"]= _("disabled");
71     $this->modes["text"]= _("text");
72     $this->modes["graphic"]= _("graphic");
74     /* Set base */
75     if ($this->dn == "new"){
76       $ui= get_userinfo();
77       $this->base= dn2base($ui->dn);
78     } else {
79       $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
80     }
82     /* Save 'dn' for later referal */
83     $this->orig_dn= $this->dn;
84   }
86   function execute()
87   {
88     /* Do we need to flip is_account state? */
89     if (isset($_POST['modify_state'])){
90       $this->is_account= !$this->is_account;
91     }
93     if (isset($_POST['action'])){
94       switch($_POST['saction']){
95         case 'wake':
96           $cmd= search_config($this->config->data['TABS'], "terminfo", "WAKECMD");
97           if ($cmd == ""){
98             print_red(_("No WAKECMD definition found in your gosa.conf"));
99           } else {
100             exec ($cmd." ".$this->macAddress, $dummy, $retval);
101             if ($retval != 0){
102               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
103             }
104           }
105           break;
107         case 'reboot':
108           $cmd= search_config($this->config->data['TABS'], "terminfo", "REBOOTCMD");
109           if ($cmd == ""){
110             print_red(_("No REBOOTCMD definition found in your gosa.conf"));
111           } else {
112             exec ($cmd." ".$this->cn, $dummy, $retval);
113             if ($retval != 0){
114               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
115             }
116           }
117           break;
119         case 'halt':
120           $cmd= search_config($this->config->data['TABS'], "terminfo", "HALTCMD");
121           if ($cmd == ""){
122             print_red(_("No HALTCMD definition found in your gosa.conf"));
123           } else {
124             exec ($cmd." ".$this->cn, $dummy, $retval);
125             if ($retval != 0){
126               print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
127             }
128           }
129           break;
130       }
131     }
133     /* Do we represent a valid terminal? */
134     if (!$this->is_account && $this->parent == NULL){
135       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
136         _("This 'dn' has no workstation features.")."</b>";
137       return($display);
138     }
140     /* Fill templating stuff */
141     $smarty= get_smarty();
142     $smarty->assign("cn", $this->cn);
143     $smarty->assign("bases", $this->config->idepartments);
144     $smarty->assign("staticAddress", "");
145         
146     /* Check if workstation is online */
147     $query= "fping -q -r 1 -t 500 ".$this->cn;
148     exec ($query, $dummy, $retval);
150     /* Offline */
151     if ($retval == 0){
152       $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot")));
153     } else {
154       $smarty->assign("actions", array("wake" => _("Wake up")));
155     }
156     /* Arrays */
157     $smarty->assign("modes", $this->modes);
158     $smarty->assign("nfsservers", $this->config->data['SERVERS']['NFS']);
159     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
160     $smarty->assign("ntpservers", $this->config->data['SERVERS']['NTP']);
162     /* Variables */
163     foreach(array("base", "gotoMode", "gotoTerminalPath", "gotoSwapServer",
164           "gotoSyslogServer", "gotoNtpServer") as $val){
166       $smarty->assign($val."_select", $this->$val);
167       $smarty->assign($val."ACL", chkacl($this->acl, $val));
168     }
169     $smarty->assign("ipHostNumber", $this->ipHostNumber);
170     $smarty->assign("macAddress", $this->macAddress);
171     $smarty->assign("actionACL", chkacl($this->acl, 'action'));
173     /* Show main page */
174     $smarty->assign("netconfig", dirname(__FILE__)."/network.tpl");
175     return($smarty->fetch (get_template_path('workstation.tpl', TRUE)));
176   }
178   function remove_from_parent()
179   {
180     $ldap= $this->config->get_ldap_link();
181     $ldap->rmdir($this->dn);
182     show_ldap_error($ldap->get_error());
184     /* Optionally execute a command after we're done */
185     $this->handle_post_events("remove");
187     /* Delete references to object groups */
188     $ldap->cd ($this->config->current['BASE']);
189     $ldap->search ("(&(objectClass=gosaGroupOfNames)(member=".$this->dn."))", array("cn"));
190     while ($ldap->fetch()){
191       $og= new ogroup($this->config, $ldap->getDN());
192       unset($og->member[$this->dn]);
193       $og->save ();
194     }
196   }
199   /* Save data to object */
200   function save_object()
201   {
202     plugin::save_object();
204     /* Save base, since this is no LDAP attribute */
205     if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
206       $this->base= $_POST['base'];
207     }
209     /* Save terminal path to parent since it is used by termstartup, too */
210     $this->parent->by_object['termstartup']->gotoTerminalPath=
211       $this->gotoTerminalPath;
212   }
215   /* Check supplied data */
216   function check()
217   {
218     $message= array();
220     $ui= get_userinfo();
221     $this->dn= "cn=".$this->cn.",ou=workstations,ou=systems,".$this->base;
222     $acl= get_permissions ($this->dn, $ui->subtreeACL);
223     $acl= get_module_permission($acl, "group", $this->dn);
224     if (chkacl($acl, "create") != ""){
225       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
226     }
228     if ($this->cn == "" && chkacl ($this->acl, "cn") == ""){
229       $message[]= _("The required field 'Workstation name' is not set.");
230     }
232     if ($this->orig_dn != $this->dn){
233       $ldap= $this->config->get_ldap_link();
234       $ldap->cd ($this->base);
235       $ldap->search ("(cn=".$this->cn.")", array("cn"));
236       if ($ldap->count() != 0){
237         while ($attrs= $ldap->fetch()){
238           if (preg_match ("/,ou=incoming,/", $ldap->getDN())){
239             continue;
240           } else {
241             if ($attrs['dn'] != $this->orig_dn){
242               $message[]= sprintf (_("There is already an entry '%s' in the base choosen by you"), $this->cn);
243               break;
244             }
245           }
246         }
247       }
248     }
250     return ($message);
251   }
254   /* Save to LDAP */
255   function save()
256   {
257     plugin::save();
259     /* Strip out 'default' values */
260     foreach (array("gotoTerminalPath", "gotoSwapServer", "gotoSyslogServer") as $val){
262       if ($this->attrs[$val] == "default"){
263         $this->attrs[$val]= array();
264       }
265     }
267     /* Add missing arrays */
268     foreach (array("ghScsiDev", "ghIdeDev", "ghNetNic") as $val){
269       if (isset ($this->$val) && count ($this->$val) != 0){
270         $this->attrs["$val"]= $this->$val;
271       }
272     }
274     /* Remove all empty values */
275     if ($this->orig_dn == 'new'){
276       $attrs= array();
277       foreach ($this->attrs as $key => $val){
278         if (is_array($val) && count($val) == 0){
279           continue;
280         }
281         $attrs[$key]= $val;
282       }
283       $this->attrs= $attrs;
284     }
286     /* Write back to ldap */
287     $ldap= $this->config->get_ldap_link();
288     if ($this->orig_dn == 'new'){
289       $ldap->cd($this->config->current['BASE']);
290       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
291       $ldap->cd($this->dn);
292       $ldap->add($this->attrs);
293       $this->handle_post_events("add");
294     } else {
295       if ($this->orig_dn != $this->dn){
296         $this->move($this->orig_dn, $this->dn);
297       }
298       $ldap->cd($this->dn);
299       $ldap->modify($this->attrs);
300       $this->handle_post_events("modify");
301     }
302     show_ldap_error($ldap->get_error());
304     /* Optionally execute a command after we're done */
305     $this->postcreate();
306   }
310 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
311 ?>