DocSpace-buildtools/web/ASC.Web.Api/Api/Settings/StorageController.cs

465 lines
15 KiB
C#
Raw Normal View History

2022-03-01 11:44:13 +00:00
namespace ASC.Web.Api.Controllers.Settings;
2022-03-01 10:58:02 +00:00
2022-03-02 08:08:59 +00:00
public class StorageController : BaseSettingsController
2022-03-01 10:58:02 +00:00
{
private Tenant Tenant { get { return _apiContext.Tenant; } }
private readonly MessageService _messageService;
private readonly StudioNotifyService _studioNotifyService;
private readonly IWebHostEnvironment _webHostEnvironment;
private readonly ConsumerFactory _consumerFactory;
private readonly TenantManager _tenantManager;
private readonly TenantExtra _tenantExtra;
private readonly PermissionContext _permissionContext;
private readonly SettingsManager _settingsManager;
private readonly CoreBaseSettings _coreBaseSettings;
private readonly CommonLinkUtility _commonLinkUtility;
private readonly StorageSettingsHelper _storageSettingsHelper;
private readonly ServiceClient _serviceClient;
private readonly EncryptionServiceClient _encryptionServiceClient;
private readonly EncryptionSettingsHelper _encryptionSettingsHelper;
private readonly BackupAjaxHandler _backupAjaxHandler;
private readonly ICacheNotify<DeleteSchedule> _cacheDeleteSchedule;
private readonly EncryptionWorker _encryptionWorker;
private readonly ILog _log;
public StorageController(
2022-03-02 08:08:59 +00:00
IOptionsMonitor<ILog> option,
ServiceClient serviceClient,
2022-03-01 10:58:02 +00:00
MessageService messageService,
StudioNotifyService studioNotifyService,
ApiContext apiContext,
TenantManager tenantManager,
TenantExtra tenantExtra,
PermissionContext permissionContext,
SettingsManager settingsManager,
WebItemManager webItemManager,
CoreBaseSettings coreBaseSettings,
CommonLinkUtility commonLinkUtility,
StorageSettingsHelper storageSettingsHelper,
IWebHostEnvironment webHostEnvironment,
ConsumerFactory consumerFactory,
IMemoryCache memoryCache,
EncryptionServiceClient encryptionServiceClient,
EncryptionSettingsHelper encryptionSettingsHelper,
BackupAjaxHandler backupAjaxHandler,
ICacheNotify<DeleteSchedule> cacheDeleteSchedule,
EncryptionWorker encryptionWorker) : base(apiContext, memoryCache, webItemManager)
{
2022-03-02 08:08:59 +00:00
_log = option.Get("ASC.Api");
_serviceClient = serviceClient;
2022-03-01 10:58:02 +00:00
_webHostEnvironment = webHostEnvironment;
_consumerFactory = consumerFactory;
_messageService = messageService;
_studioNotifyService = studioNotifyService;
_tenantManager = tenantManager;
_tenantExtra = tenantExtra;
_permissionContext = permissionContext;
_settingsManager = settingsManager;
_coreBaseSettings = coreBaseSettings;
_commonLinkUtility = commonLinkUtility;
_storageSettingsHelper = storageSettingsHelper;
_encryptionServiceClient = encryptionServiceClient;
_encryptionSettingsHelper = encryptionSettingsHelper;
_backupAjaxHandler = backupAjaxHandler;
_cacheDeleteSchedule = cacheDeleteSchedule;
_encryptionWorker = encryptionWorker;
}
[Read("storage")]
2022-03-15 10:00:41 +00:00
public List<StorageDto> GetAllStorages()
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
_tenantExtra.DemandControlPanelPermission();
var current = _settingsManager.Load<StorageSettings>();
var consumers = _consumerFactory.GetAll<DataStoreConsumer>();
2022-03-15 10:00:41 +00:00
return consumers.Select(consumer => new StorageDto(consumer, current)).ToList();
2022-03-01 10:58:02 +00:00
}
[Read("storage/progress", false)]
public double GetStorageProgress()
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (!_coreBaseSettings.Standalone) return -1;
return _serviceClient.GetProgress(Tenant.Id);
}
public readonly object Locker = new object();
[Create("encryption/start")]
2022-03-15 10:00:41 +00:00
public bool StartStorageEncryptionFromBody([FromBody] StorageEncryptionRequestsDto storageEncryption)
2022-03-01 10:58:02 +00:00
{
return StartStorageEncryption(storageEncryption);
}
[Create("encryption/start")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-15 10:00:41 +00:00
public bool StartStorageEncryptionFromForm([FromForm] StorageEncryptionRequestsDto storageEncryption)
2022-03-01 10:58:02 +00:00
{
return StartStorageEncryption(storageEncryption);
}
2022-03-15 10:00:41 +00:00
private bool StartStorageEncryption(StorageEncryptionRequestsDto storageEncryption)
2022-03-01 10:58:02 +00:00
{
if (_coreBaseSettings.CustomMode)
{
return false;
}
lock (Locker)
{
var activeTenants = _tenantManager.GetTenants();
if (activeTenants.Count > 0)
{
StartEncryption(storageEncryption.NotifyUsers);
}
}
return true;
}
private void StartEncryption(bool notifyUsers)
{
if (!SetupInfo.IsVisibleSettings<EncryptionSettings>())
{
throw new NotSupportedException();
}
if (!_coreBaseSettings.Standalone)
{
throw new NotSupportedException();
}
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
_tenantExtra.DemandControlPanelPermission();
if (!_tenantManager.GetTenantQuota(_tenantManager.GetCurrentTenant().Id).DiscEncryption)
{
throw new BillingException(Resource.ErrorNotAllowedOption, "DiscEncryption");
}
var storages = GetAllStorages();
if (storages.Any(s => s.Current))
{
throw new NotSupportedException();
}
var cdnStorages = GetAllCdnStorages();
if (cdnStorages.Any(s => s.Current))
{
throw new NotSupportedException();
}
var tenants = _tenantManager.GetTenants();
foreach (var tenant in tenants)
{
var progress = _backupAjaxHandler.GetBackupProgress(tenant.Id);
if (progress != null && !progress.IsCompleted)
{
throw new Exception();
}
}
foreach (var tenant in tenants)
{
_cacheDeleteSchedule.Publish(new DeleteSchedule() { TenantId = tenant.Id }, Common.Caching.CacheNotifyAction.Insert);
}
var settings = _encryptionSettingsHelper.Load();
settings.NotifyUsers = notifyUsers;
if (settings.Status == EncryprtionStatus.Decrypted)
{
settings.Status = EncryprtionStatus.EncryptionStarted;
settings.Password = _encryptionSettingsHelper.GeneratePassword(32, 16);
}
else if (settings.Status == EncryprtionStatus.Encrypted)
{
settings.Status = EncryprtionStatus.DecryptionStarted;
}
_messageService.Send(settings.Status == EncryprtionStatus.EncryptionStarted ? MessageAction.StartStorageEncryption : MessageAction.StartStorageDecryption);
var serverRootPath = _commonLinkUtility.GetFullAbsolutePath("~").TrimEnd('/');
foreach (var tenant in tenants)
{
_tenantManager.SetCurrentTenant(tenant);
if (notifyUsers)
{
if (settings.Status == EncryprtionStatus.EncryptionStarted)
{
_studioNotifyService.SendStorageEncryptionStart(serverRootPath);
}
else
{
_studioNotifyService.SendStorageDecryptionStart(serverRootPath);
}
}
tenant.SetStatus(TenantStatus.Encryption);
_tenantManager.SaveTenant(tenant);
}
_encryptionSettingsHelper.Save(settings);
var encryptionSettingsProto = new EncryptionSettingsProto
{
NotifyUsers = settings.NotifyUsers,
Password = settings.Password,
Status = settings.Status,
ServerRootPath = serverRootPath
};
_encryptionServiceClient.Start(encryptionSettingsProto);
}
/// <summary>
/// Get storage encryption settings
/// </summary>
/// <returns>EncryptionSettings</returns>
/// <visible>false</visible>
[Read("encryption/settings")]
public EncryptionSettings GetStorageEncryptionSettings()
{
try
{
if (_coreBaseSettings.CustomMode)
{
return null;
}
if (!SetupInfo.IsVisibleSettings<EncryptionSettings>())
{
throw new NotSupportedException();
}
if (!_coreBaseSettings.Standalone)
{
throw new NotSupportedException();
}
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
_tenantExtra.DemandControlPanelPermission();
if (!_tenantManager.GetTenantQuota(_tenantManager.GetCurrentTenant().Id).DiscEncryption)
{
throw new BillingException(Resource.ErrorNotAllowedOption, "DiscEncryption");
}
var settings = _encryptionSettingsHelper.Load();
settings.Password = string.Empty; // Don't show password
return settings;
}
catch (Exception e)
{
_log.Error("GetStorageEncryptionSettings", e);
return null;
}
}
[Read("encryption/progress")]
public double? GetStorageEncryptionProgress()
{
if (_coreBaseSettings.CustomMode)
{
return -1;
}
if (!SetupInfo.IsVisibleSettings<EncryptionSettings>())
{
throw new NotSupportedException();
}
if (!_coreBaseSettings.Standalone)
{
throw new NotSupportedException();
}
if (!_tenantManager.GetTenantQuota(_tenantManager.GetCurrentTenant().Id).DiscEncryption)
{
throw new BillingException(Resource.ErrorNotAllowedOption, "DiscEncryption");
}
return _encryptionWorker.GetEncryptionProgress();
}
[Update("storage")]
2022-03-15 10:00:41 +00:00
public StorageSettings UpdateStorageFromBody([FromBody] StorageRequestsDto model)
2022-03-01 10:58:02 +00:00
{
return UpdateStorage(model);
}
[Update("storage")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-15 10:00:41 +00:00
public StorageSettings UpdateStorageFromForm([FromForm] StorageRequestsDto model)
2022-03-01 10:58:02 +00:00
{
return UpdateStorage(model);
}
2022-03-15 10:00:41 +00:00
private StorageSettings UpdateStorage(StorageRequestsDto model)
2022-03-01 10:58:02 +00:00
{
try
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (!_coreBaseSettings.Standalone) return null;
_tenantExtra.DemandControlPanelPermission();
var consumer = _consumerFactory.GetByKey(model.Module);
if (!consumer.IsSet)
throw new ArgumentException("module");
var settings = _settingsManager.Load<StorageSettings>();
if (settings.Module == model.Module) return settings;
settings.Module = model.Module;
settings.Props = model.Props.ToDictionary(r => r.Key, b => b.Value);
StartMigrate(settings);
return settings;
}
catch (Exception e)
{
_log.Error("UpdateStorage", e);
throw;
}
}
[Delete("storage")]
public void ResetStorageToDefault()
{
try
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (!_coreBaseSettings.Standalone) return;
_tenantExtra.DemandControlPanelPermission();
var settings = _settingsManager.Load<StorageSettings>();
settings.Module = null;
settings.Props = null;
StartMigrate(settings);
}
catch (Exception e)
{
_log.Error("ResetStorageToDefault", e);
throw;
}
}
[Read("storage/cdn")]
2022-03-15 10:00:41 +00:00
public List<StorageDto> GetAllCdnStorages()
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (!_coreBaseSettings.Standalone) return null;
_tenantExtra.DemandControlPanelPermission();
var current = _settingsManager.Load<CdnStorageSettings>();
var consumers = _consumerFactory.GetAll<DataStoreConsumer>().Where(r => r.Cdn != null);
2022-03-15 10:00:41 +00:00
return consumers.Select(consumer => new StorageDto(consumer, current)).ToList();
2022-03-01 10:58:02 +00:00
}
[Update("storage/cdn")]
2022-03-15 10:00:41 +00:00
public CdnStorageSettings UpdateCdnFromBody([FromBody] StorageRequestsDto model)
2022-03-01 10:58:02 +00:00
{
return UpdateCdn(model);
}
[Update("storage/cdn")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-15 10:00:41 +00:00
public CdnStorageSettings UpdateCdnFromForm([FromForm] StorageRequestsDto model)
2022-03-01 10:58:02 +00:00
{
return UpdateCdn(model);
}
2022-03-15 10:00:41 +00:00
private CdnStorageSettings UpdateCdn(StorageRequestsDto model)
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (!_coreBaseSettings.Standalone) return null;
_tenantExtra.DemandControlPanelPermission();
var consumer = _consumerFactory.GetByKey(model.Module);
if (!consumer.IsSet)
throw new ArgumentException("module");
var settings = _settingsManager.Load<CdnStorageSettings>();
if (settings.Module == model.Module) return settings;
settings.Module = model.Module;
settings.Props = model.Props.ToDictionary(r => r.Key, b => b.Value);
try
{
_serviceClient.UploadCdn(Tenant.Id, "/", _webHostEnvironment.ContentRootPath, settings);
}
catch (Exception e)
{
_log.Error("UpdateCdn", e);
throw;
}
return settings;
}
[Delete("storage/cdn")]
public void ResetCdnToDefault()
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (!_coreBaseSettings.Standalone) return;
_tenantExtra.DemandControlPanelPermission();
_storageSettingsHelper.Clear(_settingsManager.Load<CdnStorageSettings>());
}
[Read("storage/backup")]
2022-03-15 10:00:41 +00:00
public List<StorageDto> GetAllBackupStorages()
2022-03-01 10:58:02 +00:00
{
_permissionContext.DemandPermissions(SecutiryConstants.EditPortalSettings);
if (_coreBaseSettings.Standalone)
{
_tenantExtra.DemandControlPanelPermission();
}
var schedule = _backupAjaxHandler.GetSchedule();
var current = new StorageSettings();
if (schedule != null && schedule.StorageType == BackupStorageType.ThirdPartyConsumer)
{
current = new StorageSettings
{
Module = schedule.StorageParams["module"],
Props = schedule.StorageParams.Where(r => r.Key != "module").ToDictionary(r => r.Key, r => r.Value)
};
}
var consumers = _consumerFactory.GetAll<DataStoreConsumer>();
2022-03-15 10:00:41 +00:00
return consumers.Select(consumer => new StorageDto(consumer, current)).ToList();
2022-03-01 10:58:02 +00:00
}
private void StartMigrate(StorageSettings settings)
{
_serviceClient.Migrate(Tenant.Id, settings);
Tenant.SetStatus(TenantStatus.Migrating);
_tenantManager.SaveTenant(Tenant);
}
}