FusionDirectory
class_SnapshotRestoreDialog.inc
1 <?php
2 /*
3  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
4 
5  Copyright (C) 2003-2010 Cajus Pollmeier
6  Copyright (C) 2011-2019 FusionDirectory
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
27 {
28  public $global;
29  public $aclCategory;
30 
31  protected $snapDn;
32  protected $snapAction;
33 
34  static function plInfo (): array
35  {
36  return [
37  'plShortName' => 'SnapshotRestoreDialog',
38  ];
39  }
40 
41  static function getAttributesInfo (): array
42  {
43  return [
44  'main' => [
45  'name' => _('Restoring snapshots'),
46  'class' => ['fullwidth'],
47  'attrs' => [
48  new DisplayAttribute(
49  _('Object'), _('DN of the object you are creating a snapshot of'),
50  'object_dn', FALSE
51  ),
53  _('Snapshots'), _('Existing snapshots for this object'),
54  'snapshots', FALSE,
55  [],
56  'SnapshotHandler'
57  ),
58  ]
59  ],
60  ];
61  }
62 
63  function __construct (string $dn, management $parent, bool $global, array $aclCategories)
64  {
65  $this->global = $global;
66  parent::__construct(NULL, NULL, $parent);
67  $this->object_dn = $dn;
68  $this->aclCategory = $aclCategories;
69  if ($this->global) {
70  $this->attributesAccess['object_dn']->setVisible(FALSE);
71  }
72  $this->updateList();
73  }
74 
75  function getAclBase (bool $callParent = TRUE): string
76  {
77  return $this->object_dn;
78  }
79 
80  function updateList ()
81  {
82  if ($this->global) {
83  $this->snapshots = $this->parent->getAllDeletedSnapshots();
84  } else {
85  $this->snapshots = $this->parent->getAvailableSnapsShots($this->object_dn);
86  }
87  }
88 
89  function render (): string
90  {
91  global $ui;
92  if ($this->dialog == 'delete') {
93  $objects = [
94  [
95  'name' => 'snapshot',
96  'dn' => $this->snapDn,
97  'icon' => 'geticon.php?context=actions&icon=document-restore&size=16',
98  'type' => 'Snapshot'
99  ]
100  ];
101 
102  // Display confirmation dialog.
103  $smarty = get_smarty();
104  $smarty->assign('objects', $objects);
105  $str = $smarty->fetch(get_template_path('simple-remove.tpl'));
106  } elseif ($this->dialog == 'restore') {
107  $objects = [
108  [
109  'name' => 'snapshot',
110  'dn' => $this->snapDn,
111  'icon' => 'geticon.php?context=actions&icon=document-restore&size=16',
112  'type' => 'Snapshot'
113  ]
114  ];
115 
116  // Display confirmation dialog.
117  $smarty = get_smarty();
118  $smarty->assign('objects', $objects);
119  $str = $smarty->fetch(get_template_path('restore-confirm.tpl'));
120  } else {
121  $smarty = get_smarty();
122  $permissions = $ui->get_snapshot_permissions($this->getAclBase(), $this->aclCategory);
123  $acl = '';
124  if (in_array('r', $permissions)) {
125  $acl .= 'r';
126  }
127  if (in_array(($this->global ? 'restore_deleted' : 'restore_over'), $permissions)) {
128  $acl .= 'w';
129  }
130  if (in_array('d', $permissions)) {
131  $acl .= 'd';
132  }
133  $smarty->assign('SnapshotHandlerACL', $acl);
134  $str = parent::render();
135  $str .= '<p class="plugbottom">'.
136  ' <input type="submit" formnovalidate="formnovalidate" name="'.$this->post_cancel.'" value="'.msgPool::backButton().'"/>'.
137  '</p>';
138  }
139 
140  return $str;
141  }
142 
143  function attrIsWriteable ($attr): bool
144  {
145  global $ui;
146 
147  if (!is_object($attr)) {
148  $attr = $this->attributesAccess[$attr];
149  }
150  if ($attr->getLdapName() == 'snapshots') {
151  return in_array(($this->global ? 'restore_deleted' : 'restore_over'), $ui->get_snapshot_permissions($this->getAclBase(), $this->aclCategory));
152  } else {
153  return parent::attrIsWriteable($attr);
154  }
155  }
156 
157  function triggerRestore ($dn)
158  {
159  $this->dialog = 'restore';
160  $this->snapDn = $dn;
161  }
162 
163  function triggerDelete ($dn)
164  {
165  $this->dialog = 'delete';
166  $this->snapDn = $dn;
167  }
168 
169  function readPost ()
170  {
171  if ($this->dialog == 'delete') {
172  if (isset($_POST['delete_confirmed'])) {
173  $this->parent->removeSnapshot($this->snapDn);
174  $this->closeDialog();
175  } elseif (isset($_POST['delete_cancel'])) {
176  $this->closeDialog();
177  }
178  } elseif ($this->dialog == 'restore') {
179  if (isset($_POST['restore_confirmed'])) {
180  $this->parent->restoreSnapshot($this->snapDn);
181  $this->closeDialog();
182  } elseif (isset($_POST['restore_cancel'])) {
183  $this->closeDialog();
184  }
185  } else {
186  parent::readPost();
187  }
188  }
189 
190  function closeDialog ()
191  {
192  parent::closeDialog();
193  $this->snapDn = NULL;
194  $this->updateList();
195  }
196 
197  function save (): array
198  {
199  return [];
200  }
201 }
$parent
Reference to parent object.
readPost()
Interpret POST content.
get_template_path($filename='', $plugin=FALSE, $path='')
Return themed path for specified base file.
Definition: functions.inc:174
$dn
dn of the opened object
render()
Render the dialog and returns the HTML code.
save()
Save data to the LDAP and return errors.
Attribute which list the snapshots.
Snapshot restoration dialog.
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
attrIsWriteable($attr)
Check if logged in user have enough right to write this attribute value.
This class allow to display a text in front of an attribute.
static backButton($escape=TRUE)
Text for a back button.
Management base class.
Management dialog.