class_termgroup.inc 13.7 KB
Newer Older
1
2
3
4
5
<?php

/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2003-2010  Cajus Pollmeier
6
  Copyright (C) 2011-2013  FusionDirectory
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/

class termgroup extends plugin
{
  /* attribute list for save action */
  var $members= array();

  /* Stores a list of the member dn's */
  var $member_dn = array();

  var $gotoMode     = "locked";
  var $gotoSyslogServer   = "";
  var $gotoNtpServer    = array();
  var $gotoTerminalPath   = array();
  var $gotoSwapServer   = array();
  var $modes      = array();
  var $inheritTimeServer  = true;
38
  var $is_account   = true;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
  var $orig_dn      = "";
  var $didAction    = FALSE;
  var $mapActions   = array("halt"            => "halt",
                                "reboot"          => "reboot",
                                "update"          => "update",
                                "reinstall"       => "reinstall",
                                "wake"            => "wakeup",
         # These are currently not supported by the tftp daemon
                           # "memcheck"        => "memcheck",
                           # "sysinfo"         => "sysinfo"
         );

  var $attributes     = array("gotoMode","gotoSyslogServer", "gotoNtpServer", "gotoTerminalPath", "gotoSwapServer");
  var $objectclasses  = array("gotoWorkstationTemplate");
  var $CopyPasteVars  = array("gotoNtpServers","modes","inheritTimeServer","members");
  var $view_logged    = FALSE;
  var $nfsservers     = array();
  var $swapservers    = array();
  var $member_of_ogroup= false;
  var $members_inherit_from_group = false;

  function termgroup (&$config, $dn= NULL, $parent= NULL)
  {
    /***************
      Some  initialisations
     ***************/

66
    parent::__construct($config, $dn, $parent);
67
68
69
70
71
72
73
74
75
76
    $ldap= $config->get_ldap_link();

    $this->is_account = true;
    $this->modes["active"]= _("Activated");
    $this->modes["locked"]= _("Locked");
//    $this->modes["memcheck"]= _("Memory test");
//    $this->modes["sysinfo"]= _("System analysis");

    $this->orig_dn =  $this->dn;

77
78
    /***************
      Get mac addresses from member objects
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
     ***************/

    /* We're only interested in the terminal members here, evaluate
       these... */
    if(isset($this->attrs['member'])){
      for ($i= 0; $i<$this->attrs['member']['count']; $i++){
        $member= $this->attrs['member'][$i];
        array_push($this->member_dn, $member);
        $ldap->cat($member, array('objectClass', 'macAddress', 'cn'));
        if ($ldap->success()){
          $attrs = $ldap->fetch();
          if (in_array("gotoTerminal", $attrs['objectClass']) ||
              in_array("gotoWorkstation", $attrs['objectClass'])){
            if (isset($attrs['macAddress'])){
              $this->members[$attrs['cn'][0]]= $attrs['macAddress'][0];
            } else {
              $this->members[$attrs['cn'][0]]= "";
            }
          }
        }
      }
    }

102
103
    /***************
      Perpare NTP settings
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
     ***************/

    /* Create used ntp server array */
    $this->gotoNtpServer= array();
    if(isset($this->attrs['gotoNtpServer'])){
      $this->inheritTimeServer = false;
      unset($this->attrs['gotoNtpServer']['count']);
      foreach($this->attrs['gotoNtpServer'] as $server){
        $this->gotoNtpServer[$server] = $server;
      }
    }

    /* Get Share servers */
    $tmp2 = array();
    $tmp2['!']= _("Local swap");
    foreach($this->config->data['SERVERS']['NBD'] as $server){
      if($server != "default"){
        $tmp2[$server]= $server;
      }else{
        if($this->member_of_ogroup){
          $tmp2[$server]="["._("inherited")."]";
        }
      }
    }
    $this->swapservers= $tmp2;

    $tmp2 = array();
    foreach($this->config->data['SERVERS']['NFS'] as $server){
      if($server != "default"){
        $tmp2[$server]= $server;
      }else{
        if($this->member_of_ogroup){
          $tmp2[$server]="["._("inherited")."]";
        }
      }
    }
    $this->nfsservers= $tmp2;

    /* Set inherit checkbox state */
    if(in_array("default",$this->gotoNtpServer)){
      $this->inheritTimeServer = true;
      $this->gotoNtpServer=array();
    }

    /* Create available ntp options */
    $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
    foreach($this->gotoNtpServers as $key => $server){
      if($server == "default"){
        unset($this->gotoNtpServers[$key]);
      }
    }
  }

  function check()
  {
     /* Call common method to give check the hook */
    $message= plugin::check();

    if (!$this->acl_is_createable() && $this->dn == "new"){
      $message[]= msgPool::permCreate();
    }
  }

  function remove_from_parent()
  {
    /* Workstation startup is using gotoWorkstationTemplate too,
        if we remove this oc all other not manged attributes will cause errors */
    if(isset($this->attrs['gotoKernelParameters'])){
      $this->objectclasses = array();
    }

    /* Remove acc */
176
    parent::remove_from_parent();
177
178
179
180
    $ldap = $this->config->get_ldap_link();
    $ldap->cd($this->orig_dn);
    $ldap->modify($this->attrs);
    $this->handle_post_events("remove");
181
    new log("remove","ogroup/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
182
183
184
185
186
187
188
  }



  function execute()
  {
    /* Call parent execute */
189
    parent::execute();
190
191
192

    if(!$this->view_logged){
      $this->view_logged = TRUE;
193
      new log("view","ogroup/".get_class($this),$this->dn);
194
195
196
    }


197
    /***************
198
199
200
201
      Handle requested action
     ***************/

    /* Watch for events */
202
    if ((isset($_POST['action'])) && ($this->acl_is_writeable("FAIstate")) && isset($this->mapActions[$_POST['saction']])) {
203
204
205

      /* Check if we have an DaemonEvent for this action */
      $action = $this->mapActions[$_POST['saction']];
206
      if (class_available("DaemonEvent")) {
207
        $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
208
        $macaddresses = array();
209
210
211
212
        foreach ($this->members as $cn => $macAddress){
          $macaddresses[]= $macAddress;
        }

213
214
        if (isset($events[$action])) {
          $tmp = new DaemonEvent($this->config, $action);
215
216
217
          $tmp->add_targets($macaddresses);
          $tmp->set_type(TRIGGERED_EVENT);
          $o_queue = new gosaSupportDaemon();
218
          $o_queue->append($tmp);
219
          if ($o_queue->is_error()) {
220
            msg_dialog::display(_("Infrastructure service"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
221
222
223
224
225
226
227
228
229
          }
        }
      } else {
        msg_dialog::display(_("Event error"),
            sprintf(_("Event '%s' is not available!"),$action),ERROR_DIALOG);
      }
    }


230
    /***************
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
      Add remove NTP server
     ***************/

    /* Add new ntp Server to our list */
    if((isset($_POST['addNtpServer'])) && (isset($_POST['gotoNtpServers']))){
      $this->gotoNtpServer[$_POST['gotoNtpServers']] = $_POST['gotoNtpServers'];
    }

    /* Delete selected NtpServer for list of used servers  */
    if((isset($_POST['delNtpServer'])) && (isset($_POST['gotoNtpServerSelected']))){
      foreach($_POST['gotoNtpServerSelected'] as $name){
        unset($this->gotoNtpServer[$name]);
      }
    }


247
248
    /***************
      Prepare smarty
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
     ***************/

    /* Set government mode */
    $smarty= get_smarty();

    if (isset($this->parent->by_name['termstartup'])){
      $smarty->assign("is_termgroup", "1");
    } else {
      $smarty->assign("is_termgroup", "0");
    }

    $tmp = $this->plInfo();
    foreach($tmp['plProvidedAcls'] as $name => $translated) {
      $smarty->assign($name."ACL",$this->getacl($name));
    }

    foreach($this->attributes as $attr){
      $smarty->assign($attr,      $this->$attr);
    }

    /* Variables */
    foreach(array("gotoMode","gotoNtpServer") as $val){
      $smarty->assign($val."_select", $this->$val);
    }

    $smarty->assign("actions", array("halt" => _("Switch off"), "reboot" => _("Reboot"),
          "instant_update" => _("Instant update"),
          "update" => _("Scheduled update"),
          "wake" => _("Wake"),
          "reinstall" => _("Reinstall"),
          "memcheck" => _("Memory test"),
          "sysinfo"  => _("System analysis")));

    $smarty->assign("inheritTimeServer",$this->inheritTimeServer);
    $smarty->assign("modes", $this->modes);

    $tmp = array();
    foreach($this->gotoNtpServers as $server){
      if(!in_array($server,$this->gotoNtpServer)){
        $tmp[$server] = $server;
      }
    }

292
    $smarty->assign("gotoNtpServers",$tmp);
293
294
295
296

    $smarty->assign("nfsservers",     $this->nfsservers);
    $smarty->assign("swapservers",    $this->swapservers);

297
298
    $smarty->assign("syslogservers", $this->config->data['SERVERS']['SYSLOG']);
    $smarty->assign("gotoSyslogServer_select", $this->gotoSyslogServer);
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313

    /* Variables */
    foreach(array("gotoTerminalPath", "gotoSwapServer") as $val){
      $smarty->assign($val."_select", $this->$val);
    }


    /* Show main page */
    $smarty->assign("members_inherit_from_group", $this->members_inherit_from_group);
    return ($smarty->fetch (get_template_path('termgroup.tpl', TRUE, dirname(__FILE__))));
  }


  function save_object()
  {
314
    parent::save_object();
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
    /* Set inherit mode */
    if(isset($_POST['workgeneric_posted'])){

      /* Handle the inherit to members button */
      $this->members_inherit_from_group = isset($_POST['members_inherit_from_group']);

      if(isset($_POST["inheritTimeServer"])){
        $this->inheritTimeServer = true;
      }else{
        $this->inheritTimeServer = false;
      }
    }
  }

  /* Save to LDAP */
  function save()
  {
    if (isset($this->parent->by_name['termstartup'])){
      $this->objectclasses= array("gotoTerminalTemplate");
    } else {
      $this->objectclasses= array("gotoWorkstationTemplate");
    }

338
    parent::save();
339
340

    /* Tell members to inherit all attributes if the flag is set  */
341
    //~ $this->inherit_attributes_to_members();
342
343

    /***************
344
      Prepare special vars
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
     ***************/

    /* Unset some special vars ... */
    foreach (array("gotoSyslogServer") as $val){
      if ($this->attrs[$val] == "default"){
        $this->attrs[$val]= array();
      }
    }

    /* Update ntp server settings */
    if($this->inheritTimeServer){
      $this->attrs['gotoNtpServer'] = "default";
    }else{
      /* Set ntpServers */
      $this->attrs['gotoNtpServer'] = array();
      foreach($this->gotoNtpServer as $server){
        $this->attrs['gotoNtpServer'][] = $server;
      }
    }


    /***************
367
      Write to ldap
368
369
370
371
372
373
374
375
376
     ***************/

    /* Write back to ldap */
    $ldap= $this->config->get_ldap_link();
    $ldap->cd($this->dn);
    $this->cleanup();
    $ldap->modify ($this->attrs);

    if($this->initially_was_account){
377
      new log("modify","ogroup/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
378
    }else{
379
      new log("create","ogroup/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
380
381
382
383
384
385
    }

    if(!$this->didAction){
      $this->handle_post_events("modify");
    }
    if (!$ldap->success()){
386
      msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()), LDAP_ERROR);
387
388
389
    }
  }

390

391
392
  static function plInfo()
  {
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
    return array(
      'plShortName'   => _('System'),
      'plDescription' => _('System group'),
      'plSelfModify'  => FALSE,
      'plPriority'    => 5,
      'plObjectType'  => array('ogroup'),

      'plProvidedAcls'  => array(
        'gotoMode'          => _('Mode'),
        'gotoSyslogServer'  => _('Syslog server'),
        'FAIstate'          => _('Action flag'),
        'gotoNtpServer'     => _('Ntp server'),
        'gotoTerminalPath'  => _('Root server'),
        'gotoSwapServer'    => _('Swap server')
      )
    );
409
410
  }

411
412
413
414
415
416
417
418
419
420
  //~ function inherit_attributes_to_members()
  //~ {
    //~ if ($this->members_inherit_from_group) {
      //~ foreach ($this->member_dn as $dn) {
        //~ $member_obj = new worktabs($this->config, $this->config->data['TABS']['WORKSTATIONTABS'], $dn, "workstation");
        //~ $member_obj->getBaseObject()->set_everything_to_inherited();
        //~ $member_obj->save();
      //~ }
    //~ }
  //~ }
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457

  function PrepareForCopyPaste($source)
  {
    /* Create used ntp server array */
    $this->gotoNtpServer= array();

    $source_o = new termgroup ($this->config, $source['dn']);
    foreach(array("gotoTerminalPath","gotoSwapServer","gotoSyslogServer","gotoMode") as $attr){
      $this->$attr = $source_o->$attr;
    }

    if(isset($source['gotoNtpServer'])){
      $this->inheritTimeServer = false;
      unset($source['gotoNtpServer']['count']);
      foreach($source['gotoNtpServer'] as $server){
        $this->gotoNtpServer[$server] = $server;
      }
    }

    /* Set inherit checkbox state */
    if(in_array("default",$this->gotoNtpServer)){
      $this->inheritTimeServer = true;
      $this->gotoNtpServer=array();
    }

    /* Create available ntp options */
    $this->gotoNtpServers = $this->config->data['SERVERS']['NTP'];
    foreach($this->gotoNtpServers as $key => $server){
      if($server == "default"){
        unset($this->gotoNtpServers[$key]);
      }
    }
  }
}

// vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
?>