Code

Added acls for printer glpi
[gosa.git] / plugins / admin / ogroups / class_termgroup.inc
1 <?php
3 class termgroup extends plugin
4 {
5   /* attribute list for save action */
6   var $attributes= array("gotoMode","gotoSyslogServer", "gotoNtpServer");
7   var $objectclasses= array("gotoWorkstationTemplate");
9   var $members= array();
11   var $gotoMode           = "locked";
12   var $gotoSyslogServer   = "";
13   var $gotoNtpServer      = array();
14   var $gotoNtpServers     = array();
15   var $modes              = array();
16   var $inheritTimeServer  = true;
17   var $is_account                     = true; 
18   var $orig_dn            = "";
19   var $didAction          = FALSE;
20   var $mapActions   = array("reboot"          => "localboot",
21                             "instant_update"  => "softupdate",
22                             "update"          => "sceduledupdate",
23                             "reinstall"       => "install",
24                             "rescan"          => "",
25                             "memcheck"        => "memcheck",
26                             "sysinfo"         => "sysinfo");
28   function termgroup ($config, $dn= NULL, $parent= NULL)
29   {
30     /***************
31       Some  initialisations
32      ***************/
34     plugin::plugin($config, $dn, $parent);
36     $ldap= $config->get_ldap_link();
38     $this->is_account = true;
39     $this->modes["active"]= _("Activated");
40     $this->modes["locked"]= _("Locked");
41 //    $this->modes["memcheck"]= _("Memory test");
42 //    $this->modes["sysinfo"]= _("System analysis");
44     $this->orig_dn =    $this->dn;
46     /*************** 
47       Get mac addresses from member objects  
48      ***************/
50     /* We're only interested in the terminal members here, evaluate
51        these... */
52     if(isset($this->attrs['member'])){
53       for ($i= 0; $i<$this->attrs['member']['count']; $i++){
54         $member= $this->attrs['member'][$i];
55         $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
56         if (preg_match("/success/i", $ldap->error)){
57           $attrs = $ldap->fetch();
58           if (in_array("gotoTerminal", $attrs['objectClass']) ||
59               in_array("gotoWorkstation", $attrs['objectClass'])){
60             if (isset($attrs['macAddress'])){
61               $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
62             } else {
63               $this->members[$attrs['cn'][0]]= "";
64             }
65           }
66         }
67       }
68     }
70     /*************** 
71       Perpare NTP settings 
72      ***************/
74     /* Create used ntp server array */
75     $this->gotoNtpServer= array();
76     if(isset($this->attrs['gotoNtpServer'])){
77       $this->inheritTimeServer = false;
78       unset($this->attrs['gotoNtpServer']['count']);
79       foreach($this->attrs['gotoNtpServer'] as $server){
80         $this->gotoNtpServer[$server] = $server;
81       }
82     }
84     /* Set inherit checkbox state */
85     if(in_array("default",$this->gotoNtpServer)){
86       $this->inheritTimeServer = true;
87       $this->gotoNtpServer=array();
88     }
90     /* Create available ntp options */
91     $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
92     foreach($this->gotoNtpServers as $key => $server){
93       if($server == "default"){
94         unset($this->gotoNtpServers[$key]);
95       }
96     }
99   }
101   function check()
102   {
103      /* Call common method to give check the hook */
104     $message= plugin::check();
106     if ($this->acl_is_createable() && $this->dn == "new"){
107       $message[]= _("You have no permissions to create a workstation on this 'Base'.");
108     }
110     /* Check for valid ntpServer selection */
111     if((!$this->inheritTimeServer) && (!count($this->gotoNtpServer))){
112       $message[]= _("There must be at least one NTP server selected.");
113     }
114     return($message);
115   }
117   function remove_from_parent()
118   {
119     /* Workstation startup is using gotoWorkstationTemplate too,
120         if we remove this oc all other not manged attributes will cause errors */
121     if(isset($this->attrs['gotoKernelParameters'])){
122       $this->objectclasses = array();
123     }
125     /* Remove acc */
126     plugin::remove_from_parent();
127     $ldap = $this->config->get_ldap_link();
128     $ldap->cd($this->orig_dn);
129     $ldap->modify($this->attrs);
130     $this->handle_post_events("remove");
131   }
134   function update_term_member_FAIstate($act)
135   {
136     /* Get required informations */
137     $og     = $this->parent->by_object['ogroup'];
138     $allobs = $og->allobjects;
140     /* Get correct value for FAIstate */
141     $action = $this->mapActions[$act];
143     /* Get ldap connection */
144     $ldap = $this->config->get_ldap_link();
145     $ldap->cd ($this->config->current['BASE']);
147     /* Foreach member of mthis ogroup  ... */
148     foreach($og->mem  as $key ){
149   
150       /* check objectClasses and create attributes array */
151       $attrs = array("FAIstate"=>$action);  
152       for($i = 0; $i < $allobs[$key]['objectClass']['count'] ; $i ++){
153         $attrs['objectClass'][] = $allobs[$key]['objectClass'][$i];
154       }
155       if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
156         $attrs['objectClass'][] = "FAIobject";
157       }
158       if($attrs['FAIstate'] == ""){
159         $attrs['FAIstate'] = array();
160       }
162       /* If this objects is workstation,terminal or server upodate FAIstate */ 
163       if(preg_match("/(w|t|s)/i",$allobs[$key]['type'])){
164         $ldap->cd ($key);
165         $ldap->modify($attrs);
166         show_ldap_error($ldap->get_error(),sprintf(_("Setting action state (FAIstate) failed for object '%s', value was '%s'."),$key,$action));
167         
168         if(!preg_match("/success/i",$ldap->get_error())) {
169           gosa_log("FAILED !! Updating FAIstate to '".$action."' : ".$key);
170         }else{
171           gosa_log("OK.  Updating FAIstate to '".$action."' : ".$key);
172         }
173       }
174     }
175   }
177   function execute()
178   {
179     /* Call parent execute */
180     plugin::execute();
183     /*************** 
184       Handle requested action
185      ***************/
187     /* Watch for events */
188     if (isset($_POST['action'])){
189       $macaddresses="";
190       $names="";
191       foreach ($this->members as $cn => $macAddress){
192         $macaddresses.= "$macAddress ";
193         $names.= "$cn ";
194       }
196       if (isset($_POST['action'])){
197   
198         /* Update members fai state */
199         $this->update_term_member_FAIstate(trim($_POST['saction']));
201         $cmd= search_config($this->config->data['TABS'], "workgeneric", "ACTIONCMD");
203         if ($cmd == ""){
204           print_red(_("No ACTIONCMD definition found in your gosa.conf"));
205         } else {
206           exec ($cmd." ".$macaddresses." ".escapeshellarg($_POST['saction']), $dummy, $retval);
207           if ($retval != 0){
208             print_red(sprintf(_("Execution of '%s' failed!"), $cmd));
209           } else {
210             $this->didAction= TRUE;
212             /* Get dns from member objects. Create ldap object  */
213             $member = $this->parent->by_object['ogroup']->member;
214             $ldap = $this->config->get_ldap_link();
215             $ldap->cd($this->config->current['BASE']);
217             /* walk trough members and add FAIstate */ 
218             foreach($member as $dn =>  $object){
220               /* Get object */
221               $ldap->cat($dn,array("objectClass"));
222               $res                = $ldap->fetch();
223               $attrs              = array();
225               /* Add FAI state */
226               $attrs['FAIstate']  = "";
227               if(isset($this->mapActions[$_POST['saction']])){
228                 $attrs['FAIstate'] = $this->mapActions[$_POST ['saction']];
229               }
231               /* Fix objectClass index */
232               for($i = 0; $i < $res['objectClass']['count'] ; $i ++){
233                 $attrs['objectClass'][] = $res['objectClass'][$i];
234               }
236               /* Check if we must add the objectClass */
237               if(($attrs['FAIstate'] != "") && (!in_array("FAIobject",$attrs['objectClass']))){
238                 $attrs['objectClass'][] = "FAIobject";
239               }
241               if($attrs['FAIstate'] == ""){
242                 $attrs['FAIstate'] = array();
243               }
245               $ldap->cd($dn);
246               $ldap->modify($attrs);
247               show_ldap_error($ldap->get_error(), sprintf(_("Saving of system workstation/generic (FAIstate) with dn '%s' failed."),$dn));
248             }
249             $this->didAction= TRUE;
250           }
251         }
252       }
253     }
256     /*************** 
257       Add remove NTP server
258      ***************/
260     /* Add new ntp Server to our list */
261     if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
262       $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
263     }
265     /* Delete selected NtpServer for list of used servers  */
266     if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
267       foreach($_POST['gotoNtpServerSelected'] as $name){
268         unset($this->gotoNtpServer[$name]);
269       }
270     }
273     /*************** 
274       Prepare smarty 
275      ***************/
277     /* Set government mode */
278     $smarty= get_smarty();
280     $tmp = $this->plInfo();
281     foreach($tmp['plProvidedAcls'] as $name => $translated) {
282       $smarty->assign($name."ACL",$this->getacl($name));
283     }
285     foreach($this->attributes as $attr){
286       $smarty->assign($attr,      $this->$attr);
287     }
289     /* Variables */
290     foreach(array("gotoMode","gotoNtpServer") as $val){
291       $smarty->assign($val."_select", $this->$val);
292     }
294     $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
295           "instant_update" => _("Instant update"),
296           "update" => _("Scheduled update"),
297           "reinstall" => _("Reinstall"),
298           "rescan" => _("Rescan hardware"),
299           "memcheck" => _("Memory test"),
300           "sysinfo"  => _("System analysis")));
302     $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
303     $smarty->assign("modes", $this->modes);
305     $tmp = array();
306     foreach($this->gotoNtpServers as $server){
307       if(!in_array($server,$this->gotoNtpServer)){
308         $tmp[$server] = $server;
309       }
310     }
312     $smarty->assign("gotoNtpServers",$tmp); 
313     $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']); 
314     $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer); 
316     /* Show main page */
317     return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE)));
318   }
320   function save_object()
321   {
322     plugin::save_object();  
323     /* Set inherit mode */
324     if(isset($_POST['workgeneric_posted'])){
325       if(isset($_POST["inheritTimeServer"])){
326         $this->inheritTimeServer = true;
327       }else{
328         $this->inheritTimeServer = false;
329       }
330     }
331   }
333   /* Save to LDAP */
334   function save()
335   {
336     plugin::save();
338     /***************
339       Prepare special vars 
340      ***************/
342     /* Unset some special vars ... */
343     foreach (array("gotoSyslogServer") as $val){
344       if ($this->attrs[$val] == "default"){
345         $this->attrs[$val]= array();
346       }
347     }
349     /* Update ntp server settings */
350     if($this->inheritTimeServer){
351       $this->attrs['gotoNtpServer'] = "default";
352     }else{
353       /* Set ntpServers */
354       $this->attrs['gotoNtpServer'] = array();
355       foreach($this->gotoNtpServer as $server){
356         $this->attrs['gotoNtpServer'][] = $server;
357       }
358     }
361     /***************
362       Write to ldap 
363      ***************/
365     /* Write back to ldap */
366     $ldap= $this->config->get_ldap_link();
367     $ldap->cd($this->dn);
368     $this->cleanup();
369     $ldap->modify ($this->attrs);
371     if(!$this->didAction){
372       $this->handle_post_events("modify");
373     }
374     show_ldap_error($ldap->get_error(), _("Saving workstation failed"));
376   }
377   
378   function plInfo()
379   {
380     return (array(
381           "plShortName"   => _("System"),
382           "plDescription" => _("System group"),
383           "plSelfModify"  => FALSE,
384           "plDepends"     => array(),
385           "plPriority"    => 0,
386           "plSection"     => array("administration"),
387           "plCategory"    => array("ogroups"),
388           "plProvidedAcls"=> array(
389             "gotoMode"          => _("Mode"),     
390             "gotoSyslogServer"  => _("Syslog server"), 
391             "FAIstate"          => _("Action flag"), 
392             "gotoNtpServer"     => _("Ntp server"))
393           ));
394   }
400 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
401 ?>