FusionDirectory
class_SnapshotCreateDialog.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 $aclCategory;
29 
30  static function plInfo (): array
31  {
32  return [
33  'plShortName' => 'SnapshotCreateDialog',
34  ];
35  }
36 
37  static function getAttributesInfo (): array
38  {
39  return [
40  'main' => [
41  'name' => _('Creating an object snapshot'),
42  'attrs' => [
43  new DisplayAttribute(
44  _('Object'), _('DN of the object you are creating a snapshot of'),
45  'object_dn', FALSE
46  ),
47  new DisplayAttribute(
48  _('Timestamp'), _('Timestamp of this snapshot creation'),
49  'timestamp', FALSE
50  ),
52  _('Reason'), _('Reason for creating this snapshot'),
53  'description', TRUE,
54  '',
55  'SnapshotHandler'
56  ),
57  ]
58  ],
59  'dataSource' => [
60  'name' => _('dataSource - only available via web-service.'),
61  'attrs' => [
62  new SelectAttribute(
63  'Data source', _('Origin / Source of the data'),
64  'snapshotSource', FALSE,
65  ),
66  ]
67  ],
68  ];
69  }
70 
71  function __construct (string $dn, management $parent, string $aclCategory)
72  {
73  parent::__construct(NULL, NULL, $parent);
74  // The attribut will be passed to parent for later saving, dataSource might require same logic.
75  $this->attributesAccess['description']->setInLdap(FALSE);
76  $this->attributesAccess['snapshotSource']->setInLdap(FALSE);
77  $this->attributesAccess['snapshotSource']->setVisible(FALSE);
78 
79  $recordedDataSources = $this->getLdapRecordedDataSources();
80  if (!empty($recordedDataSources)) {
81  $this->attributesAccess['snapshotSource']->setChoices($recordedDataSources);
82  }
83 
84  $this->object_dn = $dn;
85  $this->aclCategory = $aclCategory;
86  }
87 
88  /*
89  * Retrieve the data sources from configuration.
90  */
91  public function getLdapRecordedDataSources () : array
92  {
93  global $config;
94 
95  $recordedDataSources = [];
96  if (isset($config->current['SNAPSHOTSOURCEDATA']) && !empty($config->current['SNAPSHOTSOURCEDATA'])) {
97  $recordedDataSources = $config->current['SNAPSHOTSOURCEDATA'];
98  }
99 
100  return $recordedDataSources;
101  }
102 
106  function getAclBase (bool $callParent = TRUE): string
107  {
108  return $this->object_dn;
109  }
110 
115  function attrIsWriteable ($attr): bool
116  {
117  global $ui;
118 
119  if (!is_object($attr)) {
120  $attr = $this->attributesAccess[$attr];
121  }
122  if ($attr->getLdapName() == 'description') {
123  return in_array('c', $ui->get_snapshot_permissions($this->object_dn, $this->aclCategory));
124  } else {
125  return parent::attrIsWriteable($attr);
126  }
127  }
128 
129  function renderAttributes (bool $readOnly = FALSE)
130  {
131  global $ui;
132  $smarty = get_smarty();
133 
134  $permissions = $ui->get_snapshot_permissions($this->object_dn, $this->aclCategory);
135  $acl = '';
136  if (in_array('c', $permissions)) {
137  $acl .= 'crw';
138  }
139  $smarty->assign('SnapshotHandlerACL', $acl);
140 
141  return parent::renderAttributes($readOnly);
142  }
143 
144  public function update (): bool
145  {
146  $this->timestamp = date(_('Y-m-d, H:i:s'));
147  return parent::update();
148  }
149 
150  function save (): array
151  {
152  // snapshotSource is always set but can be empty and must be defaulted.
153  if (empty($this->snapshotSource)) {
154  $this->snapshotSource = 'FD';
155  }
156  $this->parent->createSnapshot($this->object_dn, $this->description, $this->snapshotSource);
157  return [];
158  }
159 
160  function fillHookAttrs (array &$addAttrs)
161  {
162  parent::fillHookAttrs($addAttrs);
163  foreach (array_keys($this->attributesAccess) as $attr) {
164  if (!isset($addAttrs[$attr])) {
165  $addAttrs[$attr] = $this->$attr;
166  }
167  }
168  }
169 }
This class allow to handle easily a Select LDAP attribute with a set of choices.
$parent
Reference to parent object.
Snapshot creation dialog.
$dn
dn of the opened object
attrIsWriteable($attr)
Check if logged in user have enough right to write this attribute value.
update()
Update state and return FALSE if the dialog was closed.
& get_smarty()
Get global smarty object.
Definition: functions.inc:324
fillHookAttrs(array &$addAttrs)
Fill attributes which may be used in hooks.
This class allow to display a text in front of an attribute.
This class allow to handle easily a String LDAP attribute that appears as a text area.
save()
Save data to the LDAP and return errors.
Management base class.
getAclBase(bool $callParent=TRUE)
Get LDAP base to use for ACL checks.
Management dialog.