Commit 38402320 authored by Côme Chilliet's avatar Côme Chilliet
Browse files

feat(management) Added back snapshot handling

parent 915913c6
......@@ -277,6 +277,7 @@ class SnapshotRestoreDialog extends simplePlugin
$this->global = $global;
parent::__construct();
$this->object_dn = $dn;
$this->acl_base = $dn;
$this->parent = $parent;
$this->aclCategory = $aclCategory;
if ($this->global) {
......
......@@ -30,8 +30,6 @@
*/
class SnapshotHandler
{
var $snapshotBases = array();
protected $enabled;
static function plInfo()
......@@ -78,26 +76,6 @@ class SnapshotHandler
return $this->enabled;
}
/*!
* \brief Set a new snapshot bases
*
* \param array $bases
*/
function setSnapshotBases($bases)
{
$this->snapshotBases = $bases;
}
/*!
* \brief Get the current snapshot bases
*
* \return array $bases
*/
function getSnapshotBases()
{
return $this->snapshotBases;
}
/* \brief Get the snapshot dn of an object dn
*/
protected function snapshot_dn($dn)
......@@ -110,9 +88,9 @@ class SnapshotHandler
/*!
* \brief Check if there are deleted snapshots
*/
function hasDeletedSnapshots()
function hasDeletedSnapshots($bases)
{
foreach ($this->getSnapshotBases() as $base) {
foreach ($bases as $base) {
if (count($this->getAllDeletedSnapshots($base)) > 0) {
return TRUE;
}
......
......@@ -1724,7 +1724,7 @@ class listing
if ($ui->allow_snapshot_restore($this->base, $this->categories, TRUE)) {
// Draw icons according to the restore flag
if ($this->snapshotHandler->hasDeletedSnapshots()) {
if ($this->snapshotHandler->hasDeletedSnapshots($this->parent->getSnapshotBases())) {
$result .= $this->renderActionMenuActionLink($separator, 'restore', _('Restore snapshots'), 'geticon.php?context=actions&icon=document-restore&size=16');
} else {
$result .= "<li$separator><a href='#'><img src='geticon.php?context=actions&amp;icon=document-restore&amp;size=16&amp;disabled=1' alt='restore' class='center'>&nbsp;"._("Restore snapshots")."</a></li>";
......
......@@ -88,4 +88,20 @@ class ListingEntry implements ArrayAccess
return TRUE;
}
public function snapshotCreationAllowed()
{
global $ui;
$infos = objects::infos($this->getTemplatedType());
return $ui->allow_snapshot_create($this->dn, $infos['aclCategory']);
}
public function snapshotRestoreAllowed()
{
global $ui;
$infos = objects::infos($this->getTemplatedType());
return $ui->allow_snapshot_restore($this->dn, $infos['aclCategory'], FALSE);
}
}
......@@ -32,14 +32,15 @@ class management
/* managementFilter instance which manages the filters */
public $filter;
// Copy&Paste handler
protected $cpHandler = NULL;
// Indicates that we want to paste objects right now.
/* Copy&Paste */
protected $cpHandler = NULL;
protected $cpPastingStarted = FALSE;
protected $skipCpHandler = FALSE;
/* Snapshots */
protected $snapHandler = NULL;
public static $skipSnapshots = FALSE;
// The currently used object(s) (e.g. in edit, removal)
protected $dn = '';
protected $dns = array();
......@@ -110,7 +111,10 @@ class management
// Add copy&paste and snapshot handler.
if (!$this->skipCpHandler) {
$this->cpHandler = new CopyPasteHandler();
//~ $this->headpage->setCopyPasteHandler($this->cpHandler);
}
if (!static::$skipSnapshots && ($config->get_cfg_value('enableSnapshots') == 'TRUE')) {
$this->snapHandler = new SnapshotHandler();
//~ $this->headpage->setSnapshotHandler($this->snapHandler);
}
$this->configureActions();
......@@ -123,6 +127,8 @@ class management
protected function configureActions()
{
global $config;
// Register default actions
$createMenu = array();
......@@ -220,6 +226,22 @@ class management
);
}
if (!self::$skipSnapshots && ($config->get_cfg_value('enableSnapshots') == 'TRUE')) {
$this->registerAction(
new Action(
'restore', _('Restore snapshot'), 'geticon.php?context=actions&icon=document-restore&size=16',
'*', 'restoreSnapshotDialog',
array('w')
)
);
$this->registerAction(
new Action(
'snapshot', _('Create snapshot'), 'geticon.php?context=actions&icon=snapshot&size=16',
'1', 'createSnapshotDialog'
)
);
}
/* Actions from footer are not in any menus and do not need a label */
$this->registerAction(new HiddenAction('apply', 'applyChanges'));
$this->registerAction(new HiddenAction('save', 'saveChanges'));
......@@ -518,6 +540,16 @@ class management
set_object_info();
}
protected function listAclCategories()
{
$cat = array();
foreach ($this->objectTypes as $type) {
$infos = objects::infos($type);
$cat[] = $infos['aclCategory'];
}
return array_unique($cat);
}
/*!
* \brief Generates the footer which is used whenever a tab object is displayed.
*/
......@@ -925,9 +957,156 @@ class management
return '';
}
/*!
* \brief Opens the snapshot creation dialog for the given target.
*/
function createSnapshotDialog(array $action)
{
global $config, $ui;
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $action['targets'], 'Snaptshot creation initiated!');
$this->dn = array_pop($action['targets']);
if (empty($this->dn)) {
return;
}
$entry = $this->listing->getEntry($this->dn);
if ($entry->snapshotCreationAllowed()) {
$this->dialogObject = new SnapshotCreateDialog($this->dn, $this, '');
$this->dialogObject->set_acl_base($this->dn);
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to create a snapshot for %s.'), $this->dn),
ERROR_DIALOG);
}
}
/*!
* \brief Displays the "Restore snapshot dialog" for a given target.
* If no target is specified, open the restore removed object dialog.
*/
function restoreSnapshotDialog(array $action)
{
global $config, $ui;
if (!count($action['targets'])) {
// No target, open the restore removed object dialog.
$this->dn = $this->listing->getBase();
$aclCategory = $this->listAclCategories();
} else {
// Display the restore points for a given object.
$this->dn = $action['targets'][0];
if (empty($this->dn)) {
return;
}
$aclCategory = objects::infos($this->getEntry($this->dn)->getTemplatedType())['aclCategory'];
}
if ($ui->allow_snapshot_restore($this->dn, $aclCategory, !count($action['targets']))) {
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->dn, 'Snaptshot restoring initiated!');
$this->dialogObject = new SnapshotRestoreDialog($this->dn, $this, !count($action['targets']), $aclCategory);
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $this->dn),
ERROR_DIALOG);
}
}
/* End of action handlers */
/* Methods related to Snapshots */
function getSnapshotBases()
{
$bases = array();
foreach ($this->objectTypes as $type) {
$infos = objects::infos($type);
$bases[] = $infos['ou'].$this->listing->getBase();
}
// No bases specified? Try base
if (!count($bases)) {
$bases[] = $this->listing->getBase();
}
return array_unique($bases);
}
/*!
* \brief Get all deleted snapshots
*/
function getAllDeletedSnapshots()
{
$bases = $this->getSnapshotBases();
$tmp = array();
foreach ($bases as $base) {
$tmp = array_merge($tmp, $this->snapHandler->getAllDeletedSnapshots($base));
}
return $tmp;
}
/*
* \brief Return available snapshots for the given base
*
* \param string $dn The DN
*/
function getAvailableSnapsShots($dn)
{
return $this->snapHandler->getAvailableSnapsShots($dn);
}
/*!
* \brief Creates a new snapshot entry
*/
function createSnapshot($dn, $description)
{
global $ui;
if ($this->dn !== $dn) {
trigger_error('There was a problem with the snapshot workflow');
return;
}
if (!empty($dn) && $ui->allow_snapshot_create($dn, $this->dialogObject->aclCategory)) {
$this->snapHandler->createSnapshot($dn, $description);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot created!');
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
/*!
* \brief Restores a snapshot object.
*
* \param String $dn The DN of the snapshot
*/
function restoreSnapshot($dn)
{
global $ui;
if (!empty($dn) && $ui->allow_snapshot_restore($dn, $this->dialogObject->aclCategory, $this->dialogObject->global)) {
$this->snapHandler->restoreSnapshot($dn);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot restored');
$this->closeDialogs();
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
/*!
* \brief Delete a snapshot
*
* \param string $dn DN of the snapshot
*/
function removeSnapshot($dn)
{
global $ui;
if (!empty($dn) && $ui->allow_snapshot_delete($dn, $this->dialogObject->aclCategory)) {
$this->snapHandler->removeSnapshot($dn);
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $dn, 'Snaptshot deleted');
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to delete a snapshot for %s.'), $dn),
ERROR_DIALOG);
}
}
static function mainInc ($classname, $objectTypes = FALSE)
{
global $remove_lock, $cleanup, $display;
......
......@@ -588,21 +588,6 @@ class simpleManagement
return $this->getHeader().$display;
}
// Set current restore base for snapshot handling.
if (is_object($this->snapHandler)) {
$bases = array();
foreach ($this->storagePoints as $sp) {
$bases[] = $sp.$this->headpage->getBase();
}
// No bases specified? Try base
if (!count($bases)) {
$bases[] = $this->headpage->getBase();
}
$this->snapHandler->setSnapshotBases($bases);
}
// Display list
return $this->renderList();
}
......@@ -1178,9 +1163,7 @@ class simpleManagement
if ($ui->allow_snapshot_restore($this->dn, $aclCategory, !count($target))) {
@DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $this->dn, 'Snaptshot restoring initiated!');
$this->snapHandler->setSnapshotBases($bases);
$this->dialogObject = new SnapshotRestoreDialog($this->dn, $this, !count($target), $aclCategory);
$this->dialogObject->set_acl_base($this->dn);
} else {
msg_dialog::display(_('Permission'), sprintf(_('You are not allowed to restore a snapshot for %s.'), $this->dn),
ERROR_DIALOG);
......@@ -1285,19 +1268,30 @@ class simpleManagement
}
}
function getSnapshotBases()
{
$bases = array();
foreach ($this->storagePoints as $sp) {
$bases[] = $sp.$this->headpage->getBase();
}
// No bases specified? Try base
if (!count($bases)) {
$bases[] = $this->headpage->getBase();
}
return $bases;
}
/*!
* \brief Get all deleted snapshots
*/
function getAllDeletedSnapshots()
{
$bases = $this->getSnapshotBases();
$tmp = array();
$snapshotBases = $this->snapHandler->getSnapshotBases();
if (is_array($snapshotBases)) {
foreach ($snapshotBases as $base) {
$tmp = array_merge($tmp, $this->snapHandler->getAllDeletedSnapshots($base));
}
} else {
$tmp = $this->snapHandler->getAllDeletedSnapshots($snapshotBases);
foreach ($bases as $base) {
$tmp = array_merge($tmp, $this->snapHandler->getAllDeletedSnapshots($base));
}
return $tmp;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment