2021-04-02 15:53:15 +04:00
|
|
|
|
using ModuleTools.BusinessLogics;
|
|
|
|
|
using ModuleTools.Enums;
|
2021-04-02 20:04:46 +04:00
|
|
|
|
using ModuleTools.Extensions;
|
2021-04-02 15:53:15 +04:00
|
|
|
|
using ModuleTools.Interfaces;
|
|
|
|
|
using ModuleTools.Models;
|
|
|
|
|
using SecurityBusinessLogic.BindingModels;
|
|
|
|
|
using SecurityBusinessLogic.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-04-02 20:04:46 +04:00
|
|
|
|
using System.IO;
|
2021-04-02 15:53:15 +04:00
|
|
|
|
|
|
|
|
|
namespace SecurityBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логика работы с бекапом
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class BackupBusinessLogic
|
|
|
|
|
{
|
2021-04-02 20:04:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Серивс для работы с бекапом
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IBackupService _service;
|
2021-04-02 15:53:15 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Менеджер безопасности
|
|
|
|
|
/// </summary>
|
2021-04-02 20:04:46 +04:00
|
|
|
|
private readonly ISecurityManager _security;
|
2021-04-02 15:53:15 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Перечень ошибок при выполнении операции
|
|
|
|
|
/// </summary>
|
|
|
|
|
public List<(string Title, string Message)> Errors { get; protected set; }
|
|
|
|
|
|
2021-04-02 20:04:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логика работы с бекапом
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="service"></param>
|
2021-04-02 15:53:15 +04:00
|
|
|
|
public BackupBusinessLogic(IBackupService service)
|
|
|
|
|
{
|
|
|
|
|
_service = service;
|
|
|
|
|
_security = DependencyManager.Instance.Resolve<ISecurityManager>();
|
|
|
|
|
Errors = new();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-02 20:04:46 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание бекапа с данными
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
2021-04-02 15:53:15 +04:00
|
|
|
|
public bool CreateBackUp(BackupBindingModel model)
|
|
|
|
|
{
|
2021-04-02 20:04:46 +04:00
|
|
|
|
if (model.FolderName.IsEmpty())
|
2021-04-02 15:53:15 +04:00
|
|
|
|
{
|
2021-04-02 20:04:46 +04:00
|
|
|
|
Errors.Add(("Ошибка", "Путь до папки не указан"));
|
2021-04-02 15:53:15 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-04-02 20:04:46 +04:00
|
|
|
|
try
|
2021-04-02 15:53:15 +04:00
|
|
|
|
{
|
2021-04-02 20:04:46 +04:00
|
|
|
|
if (NoAccess())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
var result = _service.CreateBackUp(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(result.Errors);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка", ex.Message));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Восстанволение данных через бекап
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool RestoreBackUp(BackupBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model.FolderName.IsEmpty() && model.ArchiveFileName.IsEmpty())
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка", "Путь до папки/архива не указан"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (model.ArchiveFileName.IsNotEmpty() && !File.Exists(model.ArchiveFileName))
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка", "Файл с архивом не найден"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (model.ArchiveFileName.IsEmpty() && !Directory.Exists(model.FolderName))
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка", "Папка с даными не найдена"));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (NoAccess())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
var result = _service.RestoreBackUp(model);
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(result.Errors);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка", ex.Message));
|
2021-04-02 15:53:15 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проверка доступности операции для пользователя
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
private bool NoAccess()
|
|
|
|
|
{
|
2021-04-02 20:04:46 +04:00
|
|
|
|
if (_security.CheckAccess(new SecurityManagerCheckAccessModel(null, AccessOperation.РаботасБекапом, AccessType.Delete, "бекап")))
|
2021-04-02 15:53:15 +04:00
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
Errors.Add(("Ошибка безопасности", _security.ErrorMessage));
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|