diff --git a/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs b/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs index e4162daf7e..c62f4a45be 100644 --- a/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs +++ b/products/ASC.Files/Core/Core/Dao/Interfaces/IProviderDao.cs @@ -37,5 +37,6 @@ public interface IProviderDao Task UpdateProviderInfoAsync(int linkId, FolderType rootFolderType); Task UpdateProviderInfoAsync(int linkId, string folderId, FolderType folderType); Task UpdateProviderInfoAsync(int linkId, string customerTitle, AuthData authData, FolderType folderType, Guid? userId = null); + Task UpdateBackupProviderInfoAsync(string providerKey, string customerTitle, AuthData authData); Task RemoveProviderInfoAsync(int linkId); } diff --git a/products/ASC.Files/Core/Core/FileStorageService.cs b/products/ASC.Files/Core/Core/FileStorageService.cs index 2f4fb9f749..1529153575 100644 --- a/products/ASC.Files/Core/Core/FileStorageService.cs +++ b/products/ASC.Files/Core/Core/FileStorageService.cs @@ -1768,8 +1768,7 @@ public class FileStorageService //: IFileStorageService } else { - curProviderId = Convert.ToInt32(thirdparty.ProviderId); - curProviderId = await providerDao.UpdateProviderInfoAsync(curProviderId, thirdPartyParams.CustomerTitle, thirdPartyParams.AuthData, folderType); + curProviderId = await providerDao.UpdateBackupProviderInfoAsync(thirdPartyParams.ProviderKey, thirdPartyParams.CustomerTitle, thirdPartyParams.AuthData); messageAction = MessageAction.ThirdPartyUpdated; } diff --git a/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs b/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs index a99c478b4c..98a1579e30 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/IThirdPartyProviderDao.cs @@ -422,17 +422,29 @@ internal abstract class ThirdPartyProviderDao : ThirdPartyProviderDao, IDispo { if (filterTypes.Any() && !filterTypes.Contains(FilterType.None)) { - var filter = filterTypes.Select(f => f switch + var filter = new HashSet(); + foreach(var f in filterTypes) { - FilterType.FillingFormsRooms => FolderType.FillingFormsRoom, - FilterType.EditingRooms => FolderType.EditingRoom, - FilterType.ReviewRooms => FolderType.ReviewRoom, - FilterType.ReadOnlyRooms => FolderType.ReadOnlyRoom, - FilterType.CustomRooms => FolderType.CustomRoom, - _ => FolderType.CustomRoom - }).ToHashSet(); - - return folders.Where(f => filter.Contains(f.FolderType)); + switch (f) + { + case FilterType.FillingFormsRooms: + filter.Add(FolderType.FillingFormsRoom); + break; + case FilterType.EditingRooms: + filter.Add(FolderType.EditingRoom); + break; + case FilterType.ReviewRooms: + filter.Add(FolderType.ReviewRoom); + break; + case FilterType.ReadOnlyRooms: + filter.Add(FolderType.ReadOnlyRoom); + break; + case FilterType.CustomRooms: + filter.Add(FolderType.CustomRoom); + break; + } + } + return filter.Count == 0 ? folders : folders.Where(f => filter.Contains(f.FolderType)); } return folders; diff --git a/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs b/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs index 0cb2af3459..5bfe475005 100644 --- a/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs +++ b/products/ASC.Files/Core/Core/Thirdparty/ProviderAccountDao.cs @@ -347,6 +347,64 @@ internal class ProviderAccountDao : IProviderDao return toUpdate.Count == 1 ? linkId : default; } + + public virtual async Task UpdateBackupProviderInfoAsync(string providerKey, string customerTitle, AuthData newAuthData) + { + var querySelect = + FilesDbContext.ThirdpartyAccount + .AsQueryable() + .Where(r => r.TenantId == TenantID) + .Where(r => r.FolderType == FolderType.ThirdpartyBackup); + + DbFilesThirdpartyAccount thirdparty; + try + { + thirdparty = await querySelect.SingleAsync().ConfigureAwait(false); + } + catch (Exception e) + { + _logger.ErrorUpdateBackupProviderInfo(_securityContext.CurrentAccount.ID, e); + throw; + } + + if (!ProviderTypesExtensions.TryParse(providerKey, true, out var key)) + { + throw new ArgumentException("Unrecognize ProviderType"); + } + + if (newAuthData != null && !newAuthData.IsEmpty()) + { + if (!string.IsNullOrEmpty(newAuthData.Token)) + { + newAuthData = GetEncodedAccesToken(newAuthData, key); + } + + if (!await CheckProviderInfoAsync(ToProviderInfo(0, key, customerTitle, newAuthData, _securityContext.CurrentAccount.ID, FolderType.ThirdpartyBackup, _tenantUtil.DateTimeToUtc(_tenantUtil.DateTimeNow()))).ConfigureAwait(false)) + { + throw new UnauthorizedAccessException(string.Format(FilesCommonResource.ErrorMassage_SecurityException_Auth, key)); + } + } + + if (!string.IsNullOrEmpty(customerTitle)) + { + thirdparty.Title = customerTitle; + } + + thirdparty.UserId = _securityContext.CurrentAccount.ID; + thirdparty.Provider = providerKey; + + if (!newAuthData.IsEmpty()) + { + thirdparty.UserName = newAuthData.Login ?? ""; + thirdparty.Password = EncryptPassword(newAuthData.Password); + thirdparty.Token = EncryptPassword(newAuthData.Token ?? ""); + thirdparty.Url = newAuthData.Url ?? ""; + } + + await FilesDbContext.SaveChangesAsync().ConfigureAwait(false); + + return thirdparty.Id; + } public virtual async Task RemoveProviderInfoAsync(int linkId) { diff --git a/products/ASC.Files/Core/Log/ProviderAccountDaoLogger.cs b/products/ASC.Files/Core/Log/ProviderAccountDaoLogger.cs index 9930c24914..7d1c5b053f 100644 --- a/products/ASC.Files/Core/Log/ProviderAccountDaoLogger.cs +++ b/products/ASC.Files/Core/Log/ProviderAccountDaoLogger.cs @@ -36,6 +36,9 @@ internal static partial class ProviderAccountDaoLogger [LoggerMessage(Level = LogLevel.Error, Message = "UpdateProviderInfo: linkId = {linkId} , user = {userId}")] public static partial void ErrorUpdateProviderInfo(this ILogger logger, int linkId, Guid userId, Exception exception); + [LoggerMessage(Level = LogLevel.Error, Message = "UpdateProviderInfo: user = {userId}")] + public static partial void ErrorUpdateBackupProviderInfo(this ILogger logger, Guid userId, Exception exception); + [LoggerMessage(Level = LogLevel.Error, Message = "DecryptPassword error: linkId = {linkId} , user = {userId}")] public static partial void ErrorDecryptPassword(this ILogger logger, int linkId, Guid userId, Exception exception); }