2022-03-29 20:29:03 +00:00
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
2022-03-15 18:00:53 +00:00
|
|
|
//
|
|
|
|
// This program is a free software product.
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
// any third-party rights.
|
|
|
|
//
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
//
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
//
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
//
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
//
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2022-02-14 11:18:58 +00:00
|
|
|
|
|
|
|
namespace ASC.Data.Backup.Controllers;
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
[Scope]
|
|
|
|
[DefaultRoute]
|
|
|
|
[ApiController]
|
2022-04-13 16:28:58 +00:00
|
|
|
public class BackupController : ControllerBase
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
private readonly BackupAjaxHandler _backupHandler;
|
|
|
|
private readonly CoreBaseSettings _coreBaseSettings;
|
|
|
|
private readonly TenantExtra _tenantExtra;
|
2022-02-14 11:18:58 +00:00
|
|
|
private readonly IEventBus _eventBus;
|
|
|
|
private readonly Guid _currentUserId;
|
2022-04-14 19:23:57 +00:00
|
|
|
private readonly int _tenantId;
|
2022-01-19 16:25:10 +00:00
|
|
|
|
|
|
|
public BackupController(
|
|
|
|
BackupAjaxHandler backupAjaxHandler,
|
|
|
|
CoreBaseSettings coreBaseSettings,
|
2022-02-14 11:18:58 +00:00
|
|
|
TenantManager tenantManager,
|
|
|
|
SecurityContext securityContext,
|
|
|
|
TenantExtra tenantExtra,
|
|
|
|
IEventBus eventBus)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-04-13 16:28:58 +00:00
|
|
|
_currentUserId = securityContext.CurrentAccount.ID;
|
2022-03-01 12:57:49 +00:00
|
|
|
_tenantId = tenantManager.GetCurrentTenant().Id;
|
2022-01-19 16:25:10 +00:00
|
|
|
_backupHandler = backupAjaxHandler;
|
|
|
|
_coreBaseSettings = coreBaseSettings;
|
|
|
|
_tenantExtra = tenantExtra;
|
2022-02-14 11:18:58 +00:00
|
|
|
_eventBus = eventBus;
|
2022-01-19 16:25:10 +00:00
|
|
|
}
|
|
|
|
/// <summary>
|
|
|
|
/// Returns the backup schedule of the current portal
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Backup Schedule</returns>
|
|
|
|
[Read("getbackupschedule")]
|
|
|
|
public BackupAjaxHandler.Schedule GetBackupSchedule()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return _backupHandler.GetSchedule();
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Create the backup schedule of the current portal
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="storageType">Storage type</param>
|
|
|
|
/// <param name="storageParams">Storage parameters</param>
|
|
|
|
/// <param name="backupsStored">Max of the backup's stored copies</param>
|
|
|
|
/// <param name="cronParams">Cron parameters</param>
|
|
|
|
/// <param name="backupMail">Include mail in the backup</param>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
[Create("createbackupschedule")]
|
2022-05-17 09:29:24 +00:00
|
|
|
public bool CreateBackupSchedule(BackupScheduleDto backupSchedule)
|
2022-01-19 16:25:10 +00:00
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-01-19 16:25:10 +00:00
|
|
|
var storageType = backupSchedule.StorageType == null ? BackupStorageType.Documents : (BackupStorageType)Int32.Parse(backupSchedule.StorageType);
|
|
|
|
var storageParams = backupSchedule.StorageParams == null ? new Dictionary<string, string>() : backupSchedule.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString());
|
|
|
|
var backupStored = backupSchedule.BackupsStored == null ? 0 : Int32.Parse(backupSchedule.BackupsStored);
|
|
|
|
var cron = new CronParams()
|
|
|
|
{
|
|
|
|
Period = backupSchedule.CronParams.Period == null ? BackupPeriod.EveryDay : (BackupPeriod)Int32.Parse(backupSchedule.CronParams.Period),
|
|
|
|
Hour = backupSchedule.CronParams.Hour == null ? 0 : Int32.Parse(backupSchedule.CronParams.Hour),
|
|
|
|
Day = backupSchedule.CronParams.Day == null ? 0 : Int32.Parse(backupSchedule.CronParams.Day),
|
|
|
|
};
|
|
|
|
_backupHandler.CreateSchedule(storageType, storageParams, backupStored, cron, backupSchedule.BackupMail);
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Delete the backup schedule of the current portal
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
[Delete("deletebackupschedule")]
|
|
|
|
public bool DeleteBackupSchedule()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
_backupHandler.DeleteSchedule();
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Start a backup of the current portal
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="storageType">Storage Type</param>
|
|
|
|
/// <param name="storageParams">Storage Params</param>
|
|
|
|
/// <param name="backupMail">Include mail in the backup</param>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Backup Progress</returns>
|
|
|
|
[Create("startbackup")]
|
2022-05-17 09:29:24 +00:00
|
|
|
public BackupProgress StartBackup(BackupDto backup)
|
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-01-19 16:25:10 +00:00
|
|
|
var storageType = backup.StorageType == null ? BackupStorageType.Documents : (BackupStorageType)Int32.Parse(backup.StorageType);
|
|
|
|
var storageParams = backup.StorageParams == null ? new Dictionary<string, string>() : backup.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString());
|
2022-02-14 11:18:58 +00:00
|
|
|
|
2022-04-13 16:28:58 +00:00
|
|
|
_eventBus.Publish(new BackupRequestIntegrationEvent(
|
|
|
|
tenantId: _tenantId,
|
2022-02-14 11:18:58 +00:00
|
|
|
storageParams: storageParams,
|
2022-04-13 16:28:58 +00:00
|
|
|
storageType: storageType,
|
|
|
|
backupMail: backup.BackupMail,
|
2022-02-14 11:18:58 +00:00
|
|
|
createBy: _currentUserId
|
2022-04-13 16:28:58 +00:00
|
|
|
));
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return _backupHandler.GetBackupProgress();
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the progress of the started backup
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Backup Progress</returns>
|
|
|
|
[Read("getbackupprogress")]
|
|
|
|
public BackupProgress GetBackupProgress()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return _backupHandler.GetBackupProgress();
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the backup history of the started backup
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Backup History</returns>
|
|
|
|
[Read("getbackuphistory")]
|
|
|
|
public List<BackupHistoryRecord> GetBackupHistory()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
|
|
|
{
|
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return _backupHandler.GetBackupHistory();
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Delete the backup with the specified id
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
[Delete("deletebackup/{id}")]
|
|
|
|
public bool DeleteBackup(Guid id)
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
|
|
|
{
|
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
_backupHandler.DeleteBackup(id);
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Delete all backups of the current portal
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Backup History</returns>
|
|
|
|
[Delete("deletebackuphistory")]
|
|
|
|
public bool DeleteBackupHistory()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
|
|
|
{
|
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
_backupHandler.DeleteAllBackups();
|
|
|
|
return true;
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Start a data restore of the current portal
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="backupId">Backup Id</param>
|
|
|
|
/// <param name="storageType">Storage Type</param>
|
|
|
|
/// <param name="storageParams">Storage Params</param>
|
|
|
|
/// <param name="notify">Notify about backup to users</param>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Restore Progress</returns>
|
|
|
|
[Create("startrestore")]
|
2022-05-17 09:29:24 +00:00
|
|
|
public BackupProgress StartBackupRestore(BackupRestoreDto backupRestore)
|
2022-01-19 16:25:10 +00:00
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-01-19 16:25:10 +00:00
|
|
|
var storageParams = backupRestore.StorageParams == null ? new Dictionary<string, string>() : backupRestore.StorageParams.ToDictionary(r => r.Key.ToString(), r => r.Value.ToString());
|
2022-03-02 15:25:28 +00:00
|
|
|
|
|
|
|
_eventBus.Publish(new BackupRestoreRequestIntegrationEvent(
|
|
|
|
tenantId: _tenantId,
|
|
|
|
createBy: _currentUserId,
|
|
|
|
storageParams: storageParams,
|
|
|
|
storageType: (BackupStorageType)Int32.Parse(backupRestore.StorageType.ToString()),
|
|
|
|
notify: backupRestore.Notify,
|
|
|
|
backupId: backupRestore.BackupId
|
2022-04-13 16:28:58 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return _backupHandler.GetBackupProgress();
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Returns the progress of the started restore
|
|
|
|
/// </summary>
|
|
|
|
/// <category>Backup</category>
|
|
|
|
/// <returns>Restore Progress</returns>
|
|
|
|
[Read("getrestoreprogress", true)] //NOTE: this method doesn't check payment!!!
|
|
|
|
public BackupProgress GetRestoreProgress()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
return _backupHandler.GetRestoreProgress();
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
|
2022-01-19 16:25:10 +00:00
|
|
|
///<visible>false</visible>
|
|
|
|
[Read("backuptmp")]
|
|
|
|
public object GetTempPath()
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
2021-08-31 09:40:28 +00:00
|
|
|
{
|
2022-01-19 16:25:10 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-01-19 16:25:10 +00:00
|
|
|
|
|
|
|
return _backupHandler.GetTmpFolder();
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|
2022-02-07 10:57:00 +00:00
|
|
|
|
|
|
|
///<visible>false</visible>
|
|
|
|
[Read("enablerestore")]
|
|
|
|
public bool EnableRestore()
|
|
|
|
{
|
|
|
|
try
|
2022-02-07 09:40:50 +00:00
|
|
|
{
|
2022-02-07 10:57:00 +00:00
|
|
|
if (_coreBaseSettings.Standalone)
|
2022-02-07 09:40:50 +00:00
|
|
|
{
|
2022-02-07 10:57:00 +00:00
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
2022-02-07 09:40:50 +00:00
|
|
|
}
|
2022-02-07 10:57:00 +00:00
|
|
|
_backupHandler.DemandPermissionsRestore();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return false;
|
2022-02-07 09:40:50 +00:00
|
|
|
}
|
2022-02-07 10:57:00 +00:00
|
|
|
}
|
2022-02-09 14:39:02 +00:00
|
|
|
|
|
|
|
///<visible>false</visible>
|
|
|
|
[Read("enableAutoBackup")]
|
|
|
|
public bool EnableAutoBackup()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (_coreBaseSettings.Standalone)
|
|
|
|
{
|
|
|
|
_tenantExtra.DemandControlPanelPermission();
|
|
|
|
}
|
|
|
|
_backupHandler.DemandPermissionsAutoBackup();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2021-08-31 09:40:28 +00:00
|
|
|
}
|