From 08832f005ce76cdd777994f7ed91b633253f7350 Mon Sep 17 00:00:00 2001 From: MaksimChegulov Date: Mon, 29 Aug 2022 22:10:27 +0300 Subject: [PATCH] Files: refactor --- .../ASC.Files/Core/Core/FileStorageService.cs | 56 +++++++++++++------ products/ASC.Files/Core/GlobalUsings.cs | 3 +- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/products/ASC.Files/Core/Core/FileStorageService.cs b/products/ASC.Files/Core/Core/FileStorageService.cs index edc6157073..9ed3bd4968 100644 --- a/products/ASC.Files/Core/Core/FileStorageService.cs +++ b/products/ASC.Files/Core/Core/FileStorageService.cs @@ -24,7 +24,6 @@ // 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 -using ASC.Files.Core.ApiModels; using UrlShortener = ASC.Web.Core.Utility.UrlShortener; @@ -462,20 +461,11 @@ public class FileStorageService //: IFileStorageService throw new ArgumentNullException(nameof(share)); } + var aces = GetFullAceWrappers(share); + if (@private) { - var users = share.Select(s => s.ShareTo).ToList(); - users.Add(_authContext.CurrentAccount.ID); - var keys = _encryptionLoginProvider.GetKeys(users); - - foreach (var user in users) - { - if (!keys.ContainsKey(user)) - { - var userInfo = _userManager.GetUsers(user); - throw new InvalidOperationException($"The user {userInfo.DisplayUserName(_displayUserSettingsHelper)} does not have an encryption key"); - } - } + CheckEncryptionKeys(aces); } var parentId = await _globalFolderHelper.GetFolderVirtualRooms(); @@ -492,7 +482,7 @@ public class FileStorageService //: IFileStorageService if (@private) { - await SetAcesForPrivateRoomAsync(room, share, notify, sharingMessage); + await SetAcesForPrivateRoomAsync(room, aces, notify, sharingMessage); } return room; @@ -503,6 +493,11 @@ public class FileStorageService //: IFileStorageService ArgumentNullException.ThrowIfNull(title, nameof(title)); ArgumentNullException.ThrowIfNull(parentId, nameof(parentId)); + if (@private && (share == null || !share.Any())) + { + throw new ArgumentNullException(nameof(share)); + } + var folderDao = GetFolderDao(); var providerDao = GetProviderDao(); @@ -519,6 +514,13 @@ public class FileStorageService //: IFileStorageService throw new InvalidOperationException("This provider already corresponds to the virtual room"); } + var aces = GetFullAceWrappers(share); + + if (@private) + { + CheckEncryptionKeys(aces); + } + var result = roomType switch { RoomType.CustomRoom => (await CreateCustomRoomAsync(title, parentId, @private), FolderType.CustomRoom), @@ -531,7 +533,7 @@ public class FileStorageService //: IFileStorageService if (@private) { - await SetAcesForPrivateRoomAsync(result.Item1, share, notify, sharingMessage); + await SetAcesForPrivateRoomAsync(result.Item1, aces, notify, sharingMessage); } await providerDao.UpdateProviderInfoAsync(providerInfo.ID, result.Item1.Id.ToString(), result.Item2, @private); @@ -3140,7 +3142,7 @@ public class FileStorageService //: IFileStorageService } } - private async Task SetAcesForPrivateRoomAsync(Folder room, IEnumerable share, bool notify, string sharingMessage) + private List GetFullAceWrappers(IEnumerable share) { var dict = new List(share.Select(_fileShareParamsHelper.ToAceObject)).ToDictionary(k => k.SubjectId, v => v); @@ -3158,6 +3160,26 @@ public class FileStorageService //: IFileStorageService }; } + return dict.Values.ToList(); + } + + private void CheckEncryptionKeys(IEnumerable aceWrappers) + { + var users = aceWrappers.Select(s => s.SubjectId).ToList(); + var keys = _encryptionLoginProvider.GetKeys(users); + + foreach (var user in users) + { + if (!keys.ContainsKey(user)) + { + var userInfo = _userManager.GetUsers(user); + throw new InvalidOperationException($"The user {userInfo.DisplayUserName(_displayUserSettingsHelper)} does not have an encryption key"); + } + } + } + + private async Task SetAcesForPrivateRoomAsync(Folder room, List aces, bool notify, string sharingMessage) + { var advancedSettings = new AceAdvancedSettingsWrapper { AllowSharingPrivateRoom = true @@ -3167,7 +3189,7 @@ public class FileStorageService //: IFileStorageService { Folders = new[] { room.Id }, Files = Array.Empty(), - Aces = dict.Values.ToList(), + Aces = aces, Message = sharingMessage, AdvancedSettings = advancedSettings }; diff --git a/products/ASC.Files/Core/GlobalUsings.cs b/products/ASC.Files/Core/GlobalUsings.cs index ee0434e58b..94ca6e6635 100644 --- a/products/ASC.Files/Core/GlobalUsings.cs +++ b/products/ASC.Files/Core/GlobalUsings.cs @@ -87,7 +87,8 @@ global using ASC.FederatedLogin; global using ASC.FederatedLogin.Helpers; global using ASC.FederatedLogin.LoginProviders; global using ASC.FederatedLogin.Profile; -global using ASC.Files.Core; +global using ASC.Files.Core; +global using ASC.Files.Core.ApiModels; global using ASC.Files.Core.ApiModels.RequestDto; global using ASC.Files.Core.ApiModels.ResponseDto; global using ASC.Files.Core.Core.Entries;