From 356c8a6d021721f2bc674a59fb659e1beaf9fc64 Mon Sep 17 00:00:00 2001 From: kotcheshir73 Date: Fri, 2 Apr 2021 20:04:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B2=D0=BE=D1=81=D1=81=D1=82=D0=B0=D0=BD?= =?UTF-8?q?=D0=BE=D0=BB=D0=B2=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B8=D0=B7=20?= =?UTF-8?q?=D0=B1=D0=B5=D0=BA=D0=B0=D0=BF=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Scripts/SecureMigrationScript.cs | 4 +- .../BindingModels/BackupBindingModel.cs | 5 + .../BusinessLogics/BackupBusinessLogic.cs | 89 +++++++- .../Interfaces/IBackupService.cs | 12 ++ .../BackupJsonContractService.cs | 201 ++++++++++++++++++ .../Implementations/BackupService.cs | 54 ----- .../SecurityImplementationExtensions.cs | 2 +- .../SpecialControls/BackupControl.Designer.cs | 156 +++++++++++--- .../SpecialControls/BackupControl.cs | 111 ++++++++-- 9 files changed, 525 insertions(+), 109 deletions(-) create mode 100644 DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupJsonContractService.cs delete mode 100644 DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupService.cs diff --git a/DepartmentPortal/Common/DatabaseCore/Scripts/SecureMigrationScript.cs b/DepartmentPortal/Common/DatabaseCore/Scripts/SecureMigrationScript.cs index be16bc8..4d06d6e 100644 --- a/DepartmentPortal/Common/DatabaseCore/Scripts/SecureMigrationScript.cs +++ b/DepartmentPortal/Common/DatabaseCore/Scripts/SecureMigrationScript.cs @@ -24,8 +24,8 @@ GO"; SELECT Id, RoleName, RolePriority, DateCreate, DateDelete, IsDeleted FROM DepartmentDatabaseContext.dbo.DepartmentRoles"; private static readonly string userMigration = -@"INSERT INTO DepartmentDatabasePortal.dbo.Users(Id, UserName, PasswordHash, StudentId, LecturerId, Avatar, DateLastVisit, IsBanned, DateBanned, CountAttempt, DateCreate, DateDelete, IsDeleted) -SELECT Id, UserName, PasswordHash, StudentId, LecturerId, Avatar, DateLastVisit, IsLocked, DateBanned, CountAttempt, DateCreate, DateDelete, IsDeleted FROM DepartmentDatabaseContext.dbo.DepartmentUsers"; +@"INSERT INTO DepartmentDatabasePortal.dbo.Users(Id, UserName, PasswordHash, Avatar, DateLastVisit, IsBanned, DateBanned, CountAttempt, DateCreate, DateDelete, IsDeleted) +SELECT Id, UserName, PasswordHash, Avatar, DateLastVisit, IsLocked, DateBanned, CountAttempt, DateCreate, DateDelete, IsDeleted FROM DepartmentDatabaseContext.dbo.DepartmentUsers"; private static readonly string userroleMigration = @"INSERT INTO DepartmentDatabasePortal.dbo.UserRoles(Id, UserId, RoleId, DateCreate, DateDelete, IsDeleted) diff --git a/DepartmentPortal/Security/SecurityBusinessLogic/BindingModels/BackupBindingModel.cs b/DepartmentPortal/Security/SecurityBusinessLogic/BindingModels/BackupBindingModel.cs index 36bc449..405e1c3 100644 --- a/DepartmentPortal/Security/SecurityBusinessLogic/BindingModels/BackupBindingModel.cs +++ b/DepartmentPortal/Security/SecurityBusinessLogic/BindingModels/BackupBindingModel.cs @@ -19,5 +19,10 @@ /// Создать архив с выгрузкой /// public bool CreateArchive { get; set; } + + /// + /// Имя файла для архива + /// + public string ArchiveFileName { get; set; } } } \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityBusinessLogic/BusinessLogics/BackupBusinessLogic.cs b/DepartmentPortal/Security/SecurityBusinessLogic/BusinessLogics/BackupBusinessLogic.cs index fed97cf..916bc94 100644 --- a/DepartmentPortal/Security/SecurityBusinessLogic/BusinessLogics/BackupBusinessLogic.cs +++ b/DepartmentPortal/Security/SecurityBusinessLogic/BusinessLogics/BackupBusinessLogic.cs @@ -1,14 +1,13 @@ using ModuleTools.BusinessLogics; using ModuleTools.Enums; +using ModuleTools.Extensions; using ModuleTools.Interfaces; using ModuleTools.Models; using SecurityBusinessLogic.BindingModels; using SecurityBusinessLogic.Interfaces; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.IO; namespace SecurityBusinessLogic.BusinessLogics { @@ -17,18 +16,25 @@ namespace SecurityBusinessLogic.BusinessLogics /// public class BackupBusinessLogic { - private IBackupService _service; + /// + /// Серивс для работы с бекапом + /// + private readonly IBackupService _service; /// /// Менеджер безопасности /// - private ISecurityManager _security; + private readonly ISecurityManager _security; /// /// Перечень ошибок при выполнении операции /// public List<(string Title, string Message)> Errors { get; protected set; } + /// + /// Логика работы с бекапом + /// + /// public BackupBusinessLogic(IBackupService service) { _service = service; @@ -36,19 +42,80 @@ namespace SecurityBusinessLogic.BusinessLogics Errors = new(); } + /// + /// Создание бекапа с данными + /// + /// + /// public bool CreateBackUp(BackupBindingModel model) { - if (NoAccess()) + if (model.FolderName.IsEmpty()) { + Errors.Add(("Ошибка", "Путь до папки не указан")); return false; } - var result = _service.CreateBackUp(model); - if (!result.IsSucceeded) + try { - Errors.AddRange(result.Errors); + 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; + } + } + + /// + /// Восстанволение данных через бекап + /// + /// + /// + 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)); return false; } - return true; } /// @@ -57,7 +124,7 @@ namespace SecurityBusinessLogic.BusinessLogics /// private bool NoAccess() { - if (_security.CheckAccess(new SecurityManagerCheckAccessModel(null, AccessOperation.РаботасБекапом, AccessType.View, "бекап"))) + if (_security.CheckAccess(new SecurityManagerCheckAccessModel(null, AccessOperation.РаботасБекапом, AccessType.Delete, "бекап"))) { return false; } diff --git a/DepartmentPortal/Security/SecurityBusinessLogic/Interfaces/IBackupService.cs b/DepartmentPortal/Security/SecurityBusinessLogic/Interfaces/IBackupService.cs index 4eb339d..9b67b80 100644 --- a/DepartmentPortal/Security/SecurityBusinessLogic/Interfaces/IBackupService.cs +++ b/DepartmentPortal/Security/SecurityBusinessLogic/Interfaces/IBackupService.cs @@ -8,6 +8,18 @@ namespace SecurityBusinessLogic.Interfaces /// public interface IBackupService { + /// + /// Создание бекапа с данными + /// + /// + /// OperationResultModel CreateBackUp(BackupBindingModel model); + + /// + /// Восстанволение данных через бекап + /// + /// + /// + OperationResultModel RestoreBackUp(BackupBindingModel model); } } \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupJsonContractService.cs b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupJsonContractService.cs new file mode 100644 index 0000000..6a5276f --- /dev/null +++ b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupJsonContractService.cs @@ -0,0 +1,201 @@ +using DatabaseCore; +using ModuleTools.Attributes; +using ModuleTools.Extensions; +using ModuleTools.Interfaces; +using ModuleTools.Models; +using SecurityBusinessLogic.BindingModels; +using SecurityBusinessLogic.Interfaces; +using System; +using System.Collections.Generic; +using System.IO; +using System.IO.Compression; +using System.Linq; +using System.Reflection; +using System.Runtime.Serialization.Json; + +namespace SecurityDatabaseImplementation.Implementations +{ + /// + /// Реализация IBackupService для сохранения в JSON через JsonContract + /// + public class BackupJsonContractService : IBackupService + { + public OperationResultModel CreateBackUp(BackupBindingModel model) + { + try + { + var asm = typeof(DatabaseManager).Assembly; + MethodInfo method = GetType().GetTypeInfo().GetDeclaredMethod("SaveToFile"); + foreach (var t in asm.GetExportedTypes()) + { + if (t.IsClass && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntitySecurityExtenstion<>)) && + t.GetCustomAttribute() != null) + { + MethodInfo generic = method.MakeGenericMethod(t); + generic.Invoke(this, new object[] { model.FolderName, model.FullData, t }); + } + } + if (model.CreateArchive) + { + var fileName = $"{model.FolderName}.zip"; + if (File.Exists(fileName)) + { + File.Delete($"{model.FolderName}.zip"); + } + ZipFile.CreateFromDirectory(model.FolderName, fileName); + } + } + catch (Exception ex) + { + return OperationResultModel.Error(ex); + } + return OperationResultModel.Success(null); + } + + public OperationResultModel RestoreBackUp(BackupBindingModel model) + { + try + { + if (model.ArchiveFileName.IsNotEmpty()) + { + if(model.FolderName.IsEmpty()) + { + model.FolderName = $"{Path.GetDirectoryName(model.ArchiveFileName)}{Path.GetFileNameWithoutExtension(model.ArchiveFileName)}"; + } + ZipFile.ExtractToDirectory(model.ArchiveFileName, model.FolderName); + } + var asm = typeof(DatabaseManager).Assembly; + #region вытаскиваем все типы-сущности (они должны быть унаследованы от IEntitySecurityExtenstion и иметь атрибут EntityDescription) + List types = new(); + foreach (var t in asm.GetExportedTypes()) + { + if (t.IsClass && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntitySecurityExtenstion<>)) && + t.GetCustomAttribute() != null) + { + types.Add(t); + } + } + #endregion + + #region формиурем зависимости (в каком порядке загружать данные) + Dictionary typesWithLevel = new(); + while (types.Count > 0) + { + for (int i = 0; i < types.Count; ++i) + { + var depends = types[i].GetCustomAttributes(); + if ((depends == null || !depends.Any()) && !typesWithLevel.ContainsKey(types[i])) + { + typesWithLevel.Add(types[i], 0); + types.RemoveAt(i--); + continue; + } + + int? level = null; + foreach (var depend in depends) + { + var type = typesWithLevel.Keys.FirstOrDefault(x => x.Name == depend.ClassName); + if (type != null) + { + if (!level.HasValue) + { + level = typesWithLevel[type]; + } + else if (level < typesWithLevel[type]) + { + level = typesWithLevel[type]; + } + } + else + { + level = null; + break; + } + } + + if (level.HasValue) + { + typesWithLevel.Add(types[i], level.Value + 1); + types.RemoveAt(i--); + continue; + } + } + } + #endregion + + #region Удаляем записи сначала из тех, на которые никкто не ссылается и в последнюю оередь, те, на которые все ссылаются + var deleteOrder = typesWithLevel.OrderByDescending(x => x.Value); + MethodInfo delMethod = GetType().GetTypeInfo().GetDeclaredMethod("DeleteFromDB"); + foreach (var delElem in deleteOrder) + { + if (File.Exists(string.Format("{0}/{1}.json", model.FolderName, delElem.Key.Name))) + { + MethodInfo generic = delMethod.MakeGenericMethod(delElem.Key); + generic.Invoke(this, null); + } + } + #endregion + + #region Заполняем в порядке - сначала те, у которых нет родителей, потом их потомство + MethodInfo method = GetType().GetTypeInfo().GetDeclaredMethod("LoadFromFile"); + foreach (var delElem in typesWithLevel.OrderBy(x => x.Value)) + { + MethodInfo generic = method.MakeGenericMethod(delElem.Key); + generic.Invoke(this, new object[] { model.FolderName, delElem.Key }); + } + #endregion + } + catch (Exception ex) + { + return OperationResultModel.Error(ex); + } + return OperationResultModel.Success(null); + } + + /// + /// Сохранение списка сущности из БД в файл (вызывается через рефлексию) + /// + /// + /// + /// + /// + private void SaveToFile(string folderName, bool allowFullData, Type t) where T : class, IEntitySecurityExtenstion, new() + { + using var context = DatabaseManager.GetContext; + var records = context.Set().Select(x => x.SecurityCheck(x, allowFullData)); + DataContractJsonSerializer jsonFormatter = new(typeof(List)); + using FileStream fs = new(string.Format("{0}/{1}.json", folderName, t.Name), FileMode.OpenOrCreate); + jsonFormatter.WriteObject(fs, records); + } + + /// + /// Отчистка записей сущности в БД (вызывается через рефлексию) + /// + /// + private void DeleteFromDB() where T : class, new() + { + using var context = DatabaseManager.GetContext; + context.Set().RemoveRange(context.Set()); + context.SaveChanges(); + } + + /// + /// Загрузка списка сущности из файла в БД (вызывается через рефлексию) + /// + /// + /// + /// + private void LoadFromFile(string folderName, Type t) where T : class, new() + { + using var context = DatabaseManager.GetContext; + if (File.Exists(string.Format("{0}/{1}.json", folderName, t.Name))) + { + DataContractJsonSerializer jsonFormatter = new(typeof(List)); + using FileStream fs = new(string.Format("{0}/{1}.json", folderName, t.Name), FileMode.Open); + List records = (List)jsonFormatter.ReadObject(fs); + context.Set().AddRange(records); + context.SaveChanges(); + } + } + } +} \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupService.cs b/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupService.cs deleted file mode 100644 index a7f799b..0000000 --- a/DepartmentPortal/Security/SecurityDatabaseImplementation/Implementations/BackupService.cs +++ /dev/null @@ -1,54 +0,0 @@ -using DatabaseCore; -using ModuleTools.Interfaces; -using ModuleTools.Models; -using SecurityBusinessLogic.BindingModels; -using SecurityBusinessLogic.Interfaces; -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.Compression; -using System.Linq; -using System.Reflection; -using System.Runtime.Serialization.Json; - -namespace SecurityDatabaseImplementation.Implementations -{ - public class BackupService : IBackupService - { - public OperationResultModel CreateBackUp(BackupBindingModel model) - { - try - { - var asm = typeof(DatabaseManager).Assembly; - MethodInfo method = GetType().GetTypeInfo().GetDeclaredMethod("SaveToFile"); - foreach (var t in asm.GetExportedTypes()) - { - if (t.IsClass && t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEntitySecurityExtenstion<>))) - { - var elem = asm.CreateInstance(t.FullName); - MethodInfo generic = method.MakeGenericMethod(elem.GetType()); - generic.Invoke(this, new object[] { model.FolderName, model.FullData, t }); - } - } - if (model.CreateArchive) - { - ZipFile.CreateFromDirectory(model.FolderName, $"{model.FolderName}.zip"); - } - } - catch (Exception ex) - { - return OperationResultModel.Error(ex); - } - return OperationResultModel.Success(null); - } - - private void SaveToFile(string folderName, bool allowFullData, Type t) where T : class, IEntitySecurityExtenstion, new() - { - using var context = DatabaseManager.GetContext; - var records = context.Set().Select(x => x.SecurityCheck(x, allowFullData)); - DataContractJsonSerializer jsonFormatter = new(typeof(List)); - using FileStream fs = new(string.Format("{0}/{1}.json", folderName, t.Name), FileMode.OpenOrCreate); - jsonFormatter.WriteObject(fs, records); - } - } -} \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs b/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs index 4c12d2b..dbb1deb 100644 --- a/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs +++ b/DepartmentPortal/Security/SecurityDatabaseImplementation/SecurityImplementationExtensions.cs @@ -15,7 +15,7 @@ namespace SecurityDatabaseImplementation DependencyManager.Instance.RegisterType(); DependencyManager.Instance.RegisterType(); - DependencyManager.Instance.RegisterType(); + DependencyManager.Instance.RegisterType(); } } } \ No newline at end of file diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.Designer.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.Designer.cs index c14102e..5074b61 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.Designer.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.Designer.cs @@ -33,13 +33,22 @@ namespace SecurityWindowsDesktop.SpecialControls this.buttonCreateBackup = new System.Windows.Forms.Button(); this.checkBoxCreateArchive = new System.Windows.Forms.CheckBox(); this.checkBoxFullLoad = new System.Windows.Forms.CheckBox(); - this.buttonSelectFolder = new System.Windows.Forms.Button(); - this.textBoxFolderName = new System.Windows.Forms.TextBox(); - this.labelFolderName = new System.Windows.Forms.Label(); + this.buttonSaveSelectFolder = new System.Windows.Forms.Button(); + this.textBoxSaveFolderName = new System.Windows.Forms.TextBox(); + this.labelSaveFolderName = new System.Windows.Forms.Label(); this.toolStripHeader = new System.Windows.Forms.ToolStrip(); this.toolStripButtonClose = new System.Windows.Forms.ToolStripButton(); + this.groupBoxLoadBackup = new System.Windows.Forms.GroupBox(); + this.buttonRestoreBackup = new System.Windows.Forms.Button(); + this.buttonLoadArchiveSelect = new System.Windows.Forms.Button(); + this.textBoxLoadArchiveName = new System.Windows.Forms.TextBox(); + this.labelLoadArchiveName = new System.Windows.Forms.Label(); + this.buttonLoadSelectFolder = new System.Windows.Forms.Button(); + this.textBoxLoadFolderName = new System.Windows.Forms.TextBox(); + this.labelLoadFolderName = new System.Windows.Forms.Label(); this.groupBoxSaveBackup.SuspendLayout(); this.toolStripHeader.SuspendLayout(); + this.groupBoxLoadBackup.SuspendLayout(); this.SuspendLayout(); // // groupBoxSaveBackup @@ -47,9 +56,9 @@ namespace SecurityWindowsDesktop.SpecialControls this.groupBoxSaveBackup.Controls.Add(this.buttonCreateBackup); this.groupBoxSaveBackup.Controls.Add(this.checkBoxCreateArchive); this.groupBoxSaveBackup.Controls.Add(this.checkBoxFullLoad); - this.groupBoxSaveBackup.Controls.Add(this.buttonSelectFolder); - this.groupBoxSaveBackup.Controls.Add(this.textBoxFolderName); - this.groupBoxSaveBackup.Controls.Add(this.labelFolderName); + this.groupBoxSaveBackup.Controls.Add(this.buttonSaveSelectFolder); + this.groupBoxSaveBackup.Controls.Add(this.textBoxSaveFolderName); + this.groupBoxSaveBackup.Controls.Add(this.labelSaveFolderName); this.groupBoxSaveBackup.Location = new System.Drawing.Point(0, 28); this.groupBoxSaveBackup.Name = "groupBoxSaveBackup"; this.groupBoxSaveBackup.Size = new System.Drawing.Size(496, 100); @@ -87,31 +96,31 @@ namespace SecurityWindowsDesktop.SpecialControls this.checkBoxFullLoad.Text = "Полная выгрузка"; this.checkBoxFullLoad.UseVisualStyleBackColor = true; // - // buttonSelectFolder + // buttonSaveSelectFolder // - this.buttonSelectFolder.Location = new System.Drawing.Point(385, 26); - this.buttonSelectFolder.Name = "buttonSelectFolder"; - this.buttonSelectFolder.Size = new System.Drawing.Size(99, 23); - this.buttonSelectFolder.TabIndex = 2; - this.buttonSelectFolder.Text = "Выбрать папку"; - this.buttonSelectFolder.UseVisualStyleBackColor = true; - this.buttonSelectFolder.Click += new System.EventHandler(this.ButtonSelectFolder_Click); + this.buttonSaveSelectFolder.Location = new System.Drawing.Point(385, 26); + this.buttonSaveSelectFolder.Name = "buttonSaveSelectFolder"; + this.buttonSaveSelectFolder.Size = new System.Drawing.Size(99, 23); + this.buttonSaveSelectFolder.TabIndex = 2; + this.buttonSaveSelectFolder.Text = "Выбрать папку"; + this.buttonSaveSelectFolder.UseVisualStyleBackColor = true; + this.buttonSaveSelectFolder.Click += new System.EventHandler(this.ButtonSaveSelectFolder_Click); // - // textBoxFolderName + // textBoxSaveFolderName // - this.textBoxFolderName.Location = new System.Drawing.Point(107, 26); - this.textBoxFolderName.Name = "textBoxFolderName"; - this.textBoxFolderName.Size = new System.Drawing.Size(272, 23); - this.textBoxFolderName.TabIndex = 1; + this.textBoxSaveFolderName.Location = new System.Drawing.Point(110, 26); + this.textBoxSaveFolderName.Name = "textBoxSaveFolderName"; + this.textBoxSaveFolderName.Size = new System.Drawing.Size(269, 23); + this.textBoxSaveFolderName.TabIndex = 1; // - // labelFolderName + // labelSaveFolderName // - this.labelFolderName.AutoSize = true; - this.labelFolderName.Location = new System.Drawing.Point(16, 29); - this.labelFolderName.Name = "labelFolderName"; - this.labelFolderName.Size = new System.Drawing.Size(85, 15); - this.labelFolderName.TabIndex = 0; - this.labelFolderName.Text = "Путь до папки"; + this.labelSaveFolderName.AutoSize = true; + this.labelSaveFolderName.Location = new System.Drawing.Point(16, 29); + this.labelSaveFolderName.Name = "labelSaveFolderName"; + this.labelSaveFolderName.Size = new System.Drawing.Size(85, 15); + this.labelSaveFolderName.TabIndex = 0; + this.labelSaveFolderName.Text = "Путь до папки"; // // toolStripHeader // @@ -134,10 +143,89 @@ namespace SecurityWindowsDesktop.SpecialControls this.toolStripButtonClose.Size = new System.Drawing.Size(23, 22); this.toolStripButtonClose.Text = "Закрыть"; // + // groupBoxLoadBackup + // + this.groupBoxLoadBackup.Controls.Add(this.buttonRestoreBackup); + this.groupBoxLoadBackup.Controls.Add(this.buttonLoadArchiveSelect); + this.groupBoxLoadBackup.Controls.Add(this.textBoxLoadArchiveName); + this.groupBoxLoadBackup.Controls.Add(this.labelLoadArchiveName); + this.groupBoxLoadBackup.Controls.Add(this.buttonLoadSelectFolder); + this.groupBoxLoadBackup.Controls.Add(this.textBoxLoadFolderName); + this.groupBoxLoadBackup.Controls.Add(this.labelLoadFolderName); + this.groupBoxLoadBackup.Location = new System.Drawing.Point(0, 134); + this.groupBoxLoadBackup.Name = "groupBoxLoadBackup"; + this.groupBoxLoadBackup.Size = new System.Drawing.Size(496, 137); + this.groupBoxLoadBackup.TabIndex = 2; + this.groupBoxLoadBackup.TabStop = false; + this.groupBoxLoadBackup.Text = "Загрузка из бекапа"; + // + // buttonRestoreBackup + // + this.buttonRestoreBackup.Location = new System.Drawing.Point(383, 99); + this.buttonRestoreBackup.Name = "buttonRestoreBackup"; + this.buttonRestoreBackup.Size = new System.Drawing.Size(99, 23); + this.buttonRestoreBackup.TabIndex = 9; + this.buttonRestoreBackup.Text = "Загрузить"; + this.buttonRestoreBackup.UseVisualStyleBackColor = true; + this.buttonRestoreBackup.Click += new System.EventHandler(this.ButtonRestoreBackup_Click); + // + // buttonLoadArchiveSelect + // + this.buttonLoadArchiveSelect.Location = new System.Drawing.Point(383, 64); + this.buttonLoadArchiveSelect.Name = "buttonLoadArchiveSelect"; + this.buttonLoadArchiveSelect.Size = new System.Drawing.Size(99, 23); + this.buttonLoadArchiveSelect.TabIndex = 8; + this.buttonLoadArchiveSelect.Text = "Выбрать файл"; + this.buttonLoadArchiveSelect.UseVisualStyleBackColor = true; + this.buttonLoadArchiveSelect.Click += new System.EventHandler(this.ButtonLoadArchiveSelect_Click); + // + // textBoxLoadArchiveName + // + this.textBoxLoadArchiveName.Location = new System.Drawing.Point(110, 64); + this.textBoxLoadArchiveName.Name = "textBoxLoadArchiveName"; + this.textBoxLoadArchiveName.Size = new System.Drawing.Size(267, 23); + this.textBoxLoadArchiveName.TabIndex = 7; + // + // labelLoadArchiveName + // + this.labelLoadArchiveName.AutoSize = true; + this.labelLoadArchiveName.Location = new System.Drawing.Point(14, 67); + this.labelLoadArchiveName.Name = "labelLoadArchiveName"; + this.labelLoadArchiveName.Size = new System.Drawing.Size(90, 15); + this.labelLoadArchiveName.TabIndex = 6; + this.labelLoadArchiveName.Text = "Путь до архива"; + // + // buttonLoadSelectFolder + // + this.buttonLoadSelectFolder.Location = new System.Drawing.Point(385, 28); + this.buttonLoadSelectFolder.Name = "buttonLoadSelectFolder"; + this.buttonLoadSelectFolder.Size = new System.Drawing.Size(99, 23); + this.buttonLoadSelectFolder.TabIndex = 5; + this.buttonLoadSelectFolder.Text = "Выбрать папку"; + this.buttonLoadSelectFolder.UseVisualStyleBackColor = true; + this.buttonLoadSelectFolder.Click += new System.EventHandler(this.ButtonLoadSelectFolder_Click); + // + // textBoxLoadFolderName + // + this.textBoxLoadFolderName.Location = new System.Drawing.Point(110, 28); + this.textBoxLoadFolderName.Name = "textBoxLoadFolderName"; + this.textBoxLoadFolderName.Size = new System.Drawing.Size(269, 23); + this.textBoxLoadFolderName.TabIndex = 4; + // + // labelLoadFolderName + // + this.labelLoadFolderName.AutoSize = true; + this.labelLoadFolderName.Location = new System.Drawing.Point(16, 31); + this.labelLoadFolderName.Name = "labelLoadFolderName"; + this.labelLoadFolderName.Size = new System.Drawing.Size(85, 15); + this.labelLoadFolderName.TabIndex = 3; + this.labelLoadFolderName.Text = "Путь до папки"; + // // BackupControl // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.groupBoxLoadBackup); this.Controls.Add(this.toolStripHeader); this.Controls.Add(this.groupBoxSaveBackup); this.Name = "BackupControl"; @@ -146,6 +234,8 @@ namespace SecurityWindowsDesktop.SpecialControls this.groupBoxSaveBackup.PerformLayout(); this.toolStripHeader.ResumeLayout(false); this.toolStripHeader.PerformLayout(); + this.groupBoxLoadBackup.ResumeLayout(false); + this.groupBoxLoadBackup.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); @@ -154,13 +244,21 @@ namespace SecurityWindowsDesktop.SpecialControls #endregion private System.Windows.Forms.GroupBox groupBoxSaveBackup; - private System.Windows.Forms.Label labelFolderName; - private System.Windows.Forms.TextBox textBoxFolderName; - private System.Windows.Forms.Button buttonSelectFolder; + private System.Windows.Forms.Label labelSaveFolderName; + private System.Windows.Forms.TextBox textBoxSaveFolderName; + private System.Windows.Forms.Button buttonSaveSelectFolder; private System.Windows.Forms.CheckBox checkBoxCreateArchive; private System.Windows.Forms.Button buttonCreateBackup; private System.Windows.Forms.CheckBox checkBoxFullLoad; private System.Windows.Forms.ToolStrip toolStripHeader; private System.Windows.Forms.ToolStripButton toolStripButtonClose; + private System.Windows.Forms.GroupBox groupBoxLoadBackup; + private System.Windows.Forms.TextBox textBoxLoadFolderName; + private System.Windows.Forms.Label labelLoadFolderName; + private System.Windows.Forms.Button buttonLoadSelectFolder; + private System.Windows.Forms.Label labelLoadArchiveName; + private System.Windows.Forms.TextBox textBoxLoadArchiveName; + private System.Windows.Forms.Button buttonLoadArchiveSelect; + private System.Windows.Forms.Button buttonRestoreBackup; } } diff --git a/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.cs b/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.cs index fa577d3..7717bf1 100644 --- a/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.cs +++ b/DepartmentPortal/Security/SecurityWindowsDesktop/SpecialControls/BackupControl.cs @@ -7,11 +7,18 @@ using SecurityBusinessLogic.BindingModels; using SecurityBusinessLogic.BusinessLogics; using System; using System.Windows.Forms; +using System.Xml.Linq; namespace SecurityWindowsDesktop.SpecialControls { + /// + /// Контрол для работы с бекапом + /// public partial class BackupControl : UserControl, IControl { + /// + /// Класс с бизнес-лоникой работы с бекапом + /// private readonly BackupBusinessLogic _businessLogic; /// @@ -19,6 +26,9 @@ namespace SecurityWindowsDesktop.SpecialControls /// private event Action CloseEvent; + /// + /// Контрол для работы с бекапом + /// public BackupControl() { InitializeComponent(); @@ -28,6 +38,7 @@ namespace SecurityWindowsDesktop.SpecialControls AccessOperation = AccessOperation.РаботасБекапом; } + #region IControl public Guid ControlId { get; private set; } public string Title { get; private set; } @@ -36,10 +47,6 @@ namespace SecurityWindowsDesktop.SpecialControls public IControl GetInstance() => new BackupControl() { ControlId = Guid.NewGuid() }; - public void LoadFromXml(string xml) - { - } - public void Open(ControlOpenModel model) { if (model.CloseList != null) @@ -49,25 +56,58 @@ namespace SecurityWindowsDesktop.SpecialControls Dock = DockStyle.Fill; } - public string SaveToXml() + public string SaveToXml() => new XElement("Control", + new XAttribute("Type", GetType().FullName), + new XAttribute("ControlId", ControlId), + new XAttribute("Title", Title), + new XAttribute("AccessOperation", AccessOperation)).ToString(); + + public void LoadFromXml(string xml) { - return string.Empty; + var control = XElement.Parse(xml); + ControlId = new Guid(control.Attribute("ControlId").Value.ToString()); + Title = control.Attribute("Title").Value.ToString(); + AccessOperation = (AccessOperation)Enum.Parse(typeof(AccessOperation), control.Attribute("AccessOperation").Value.ToString()); + } + #endregion + + /// + /// Закрытие контрола + /// + /// + /// + private void ToolStripHeader_ItemClicked(object sender, ToolStripItemClickedEventArgs e) + { + CloseEvent?.Invoke(ControlId); + Dispose(); } - private void ButtonSelectFolder_Click(object sender, EventArgs e) + /// + /// Выбор пути для папки сохранения бекапа + /// + /// + /// + private void ButtonSaveSelectFolder_Click(object sender, EventArgs e) { var fbd = new FolderBrowserDialog(); if (fbd.ShowDialog() == DialogResult.OK) { - textBoxFolderName.Text = fbd.SelectedPath; + textBoxSaveFolderName.Text = fbd.SelectedPath; } } + /// + /// Создание бекапа + /// + /// + /// private void ButtonCreateBackup_Click(object sender, EventArgs e) { + var cursor = Cursor.Current; + Cursor.Current = Cursors.WaitCursor; if (_businessLogic.CreateBackUp(new BackupBindingModel { - FolderName = textBoxFolderName.Text, + FolderName = textBoxSaveFolderName.Text, FullData = checkBoxFullLoad.Checked, CreateArchive = checkBoxCreateArchive.Checked })) @@ -78,12 +118,59 @@ namespace SecurityWindowsDesktop.SpecialControls { DialogHelper.MessageException(_businessLogic.Errors, "Ошибки при сохранении"); } + Cursor.Current = cursor; } - private void ToolStripHeader_ItemClicked(object sender, ToolStripItemClickedEventArgs e) + /// + /// Выбор пути для папки восстановления бекапа + /// + /// + /// + private void ButtonLoadSelectFolder_Click(object sender, EventArgs e) { - CloseEvent?.Invoke(ControlId); - Dispose(); + var fbd = new FolderBrowserDialog(); + if (fbd.ShowDialog() == DialogResult.OK) + { + textBoxLoadFolderName.Text = fbd.SelectedPath; + } + } + + /// + /// Выбор пути для файла-архива с бекапом + /// + /// + /// + private void ButtonLoadArchiveSelect_Click(object sender, EventArgs e) + { + var ofd = new OpenFileDialog(); + if (ofd.ShowDialog() == DialogResult.OK) + { + textBoxLoadArchiveName.Text = ofd.FileName; + } + } + + /// + /// Восстановление + /// + /// + /// + private void ButtonRestoreBackup_Click(object sender, EventArgs e) + { + var cursor = Cursor.Current; + Cursor.Current = Cursors.WaitCursor; + if (_businessLogic.RestoreBackUp(new BackupBindingModel + { + FolderName = textBoxLoadFolderName.Text, + ArchiveFileName = textBoxLoadArchiveName.Text + })) + { + DialogHelper.MessageInformation("Сохранение прошло успешно", "Результат"); + } + else + { + DialogHelper.MessageException(_businessLogic.Errors, "Ошибки при сохранении"); + } + Cursor.Current = cursor; } } } \ No newline at end of file