// (c) Copyright Ascensio System SIA 2010-2022 // // This program is a free software product. // You can redistribute it and/or modify it under the terms // of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software // Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended // to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of // any third-party rights. // // This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see // the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html // // You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021. // // The interactive user interfaces in modified source and object code versions of the Program must // display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3. // // Pursuant to Section 7(b) of the License you must retain the original Product logo when // distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under // trademark law for use of our trademarks. // // All the Product's GUI elements, including illustrations and icon sets, as well as technical writing // 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 namespace ASC.Files.Api; public class ThirdpartyController : ApiControllerBase { private readonly CoreBaseSettings _coreBaseSettings; private readonly EntryManager _entryManager; private readonly FilesSettingsHelper _filesSettingsHelper; private readonly FileStorageService _fileStorageService; private readonly FileStorageService _fileStorageServiceThirdparty; private readonly GlobalFolderHelper _globalFolderHelper; private readonly SecurityContext _securityContext; private readonly ThirdpartyConfiguration _thirdpartyConfiguration; private readonly UserManager _userManager; private readonly WordpressHelper _wordpressHelper; private readonly WordpressToken _wordpressToken; private readonly RequestHelper _requestHelper; private readonly FileSecurityCommon _fileSecurityCommon; public ThirdpartyController( CoreBaseSettings coreBaseSettings, EntryManager entryManager, FilesSettingsHelper filesSettingsHelper, FileStorageService fileStorageService, FileStorageService fileStorageServiceThirdparty, GlobalFolderHelper globalFolderHelper, SecurityContext securityContext, ThirdpartyConfiguration thirdpartyConfiguration, UserManager userManager, WordpressHelper wordpressHelper, WordpressToken wordpressToken, RequestHelper requestHelper, FolderDtoHelper folderDtoHelper, FileDtoHelper fileDtoHelper, FileSecurityCommon fileSecurityCommon) : base(folderDtoHelper, fileDtoHelper) { _coreBaseSettings = coreBaseSettings; _entryManager = entryManager; _filesSettingsHelper = filesSettingsHelper; _fileStorageService = fileStorageService; _fileStorageServiceThirdparty = fileStorageServiceThirdparty; _globalFolderHelper = globalFolderHelper; _securityContext = securityContext; _thirdpartyConfiguration = thirdpartyConfiguration; _userManager = userManager; _wordpressHelper = wordpressHelper; _wordpressToken = wordpressToken; _requestHelper = requestHelper; _fileSecurityCommon = fileSecurityCommon; } /// /// Get a list of available providers /// /// Third-Party Integration /// List of provider key /// List of provider key: DropboxV2, Box, WebDav, Yandex, OneDrive, SharePoint, GoogleDrive /// [HttpGet("thirdparty/capabilities")] public List> Capabilities() { var result = new List>(); if (_userManager.IsVisitor(_securityContext.CurrentAccount.ID) || (!_filesSettingsHelper.EnableThirdParty && !_coreBaseSettings.Personal)) { return result; } return _thirdpartyConfiguration.GetProviders(); } /// false [HttpPost("wordpress")] public bool CreateWordpressPost(CreateWordpressPostRequestDto inDto) { try { var token = _wordpressToken.GetToken(); var meInfo = _wordpressHelper.GetWordpressMeInfo(token.AccessToken); var parser = JObject.Parse(meInfo); if (parser == null) { return false; } var blogId = parser.Value("token_site_id"); if (blogId != null) { var createPost = _wordpressHelper.CreateWordpressPost(inDto.Title, inDto.Content, inDto.Status, blogId, token); return createPost; } return false; } catch (Exception) { return false; } } /// /// Removes the third party file storage service account with the ID specified in the request /// /// Provider ID. Provider id is part of folder id. /// Example, folder id is "sbox-123", then provider id is "123" /// /// Remove third party account /// Third-Party Integration /// Folder id /// [HttpDelete("thirdparty/{providerId:int}")] public Task DeleteThirdPartyAsync(int providerId) { return _fileStorageServiceThirdparty.DeleteThirdPartyAsync(providerId.ToString(CultureInfo.InvariantCulture)); } /// false [HttpGet("wordpress-delete")] public object DeleteWordpressInfo() { var token = _wordpressToken.GetToken(); if (token != null) { _wordpressToken.DeleteToken(token); return new { success = true }; } return new { success = false }; } /// /// Returns the list of third party services connected in the 'Common Documents' section /// /// Third-Party Integration /// Get third party folder /// Connected providers folder [HttpGet("thirdparty/common")] public async IAsyncEnumerable> GetCommonThirdPartyFoldersAsync() { var parent = await _fileStorageService.GetFolderAsync(await _globalFolderHelper.FolderCommonAsync); var thirdpartyFolders = _entryManager.GetThirpartyFoldersAsync(parent); await foreach (var r in thirdpartyFolders) { yield return await _folderDtoHelper.GetAsync(r); } } /// /// Returns the list of all connected third party services /// /// Third-Party Integration /// Get third party list /// Connected providers [HttpGet("thirdparty")] public IAsyncEnumerable GetThirdPartyAccountsAsync() { return _fileStorageServiceThirdparty.GetThirdPartyAsync(); } /// /// Return connected third party backup services /// /// Third-Party Integration /// Get third party list /// Connected providers [HttpGet("thirdparty/backup")] public async Task> GetBackupThirdPartyAccountAsync() { var folder = await _fileStorageServiceThirdparty.GetBackupThirdPartyAsync(); if (folder != null) { return await _folderDtoHelper.GetAsync(folder); } else { return null; } } /// false [HttpGet("wordpress-info")] public object GetWordpressInfo() { var token = _wordpressToken.GetToken(); if (token != null) { var meInfo = _wordpressHelper.GetWordpressMeInfo(token.AccessToken); var blogId = JObject.Parse(meInfo).Value("token_site_id"); var wordpressUserName = JObject.Parse(meInfo).Value("username"); var blogInfo = _requestHelper.PerformRequest(WordpressLoginProvider.WordpressSites + blogId, "", "GET", ""); var jsonBlogInfo = JObject.Parse(blogInfo); jsonBlogInfo.Add("username", wordpressUserName); blogInfo = jsonBlogInfo.ToString(); return new { success = true, data = blogInfo }; } return new { success = false }; } /// /// Saves the third party file storage service account /// /// Save third party account /// Connection url for SharePoint /// Login /// Password /// Authentication token /// /// Title /// Provider Key /// Provider ID /// Third-Party Integration /// Folder contents /// List of provider key: DropboxV2, Box, WebDav, Yandex, OneDrive, SharePoint, GoogleDrive /// [HttpPost("thirdparty")] public async Task> SaveThirdPartyAsync(ThirdPartyRequestDto inDto) { var thirdPartyParams = new ThirdPartyParams { AuthData = new AuthData(inDto.Url, inDto.Login, inDto.Password, inDto.Token), Corporate = inDto.IsRoomsStorage ? false : inDto.IsCorporate, RoomsStorage = inDto.IsCorporate ? false : inDto.IsRoomsStorage, CustomerTitle = inDto.CustomerTitle, ProviderId = inDto.ProviderId, ProviderKey = inDto.ProviderKey, }; var folder = await _fileStorageServiceThirdparty.SaveThirdPartyAsync(thirdPartyParams); return await _folderDtoHelper.GetAsync(folder); } /// /// Saves the third party backup file storage service account /// /// Save third party account /// Connection url for SharePoint /// Login /// Password /// Authentication token /// Title /// Provider Key /// Provider ID /// Third-Party Integration /// Folder contents /// List of provider key: DropboxV2, Box, WebDav, Yandex, OneDrive, SharePoint, GoogleDrive /// [HttpPost("thirdparty/backup")] public async Task> SaveThirdPartyBackupAsync(ThirdPartyBackupRequestDto inDto) { if (!_fileSecurityCommon.IsAdministrator(_securityContext.CurrentAccount.ID)) { throw new InvalidOperationException(FilesCommonResource.ErrorMassage_SecurityException_Create); } var thirdPartyParams = new ThirdPartyParams { AuthData = new AuthData(inDto.Url, inDto.Login, inDto.Password, inDto.Token), CustomerTitle = inDto.CustomerTitle, ProviderKey = inDto.ProviderKey, }; var folder = await _fileStorageServiceThirdparty.SaveThirdPartyBackupAsync(thirdPartyParams); return await _folderDtoHelper.GetAsync(folder); } /// false [HttpPost("wordpress-save")] public object WordpressSave(WordpressSaveRequestDto inDto) { if (inDto.Code.Length == 0) { return new { success = false }; } try { var token = _wordpressToken.SaveTokenFromCode(inDto.Code); var meInfo = _wordpressHelper.GetWordpressMeInfo(token.AccessToken); var blogId = JObject.Parse(meInfo).Value("token_site_id"); var wordpressUserName = JObject.Parse(meInfo).Value("username"); var blogInfo = _requestHelper.PerformRequest(WordpressLoginProvider.WordpressSites + blogId, "", "GET", ""); var jsonBlogInfo = JObject.Parse(blogInfo); jsonBlogInfo.Add("username", wordpressUserName); blogInfo = jsonBlogInfo.ToString(); return new { success = true, data = blogInfo }; } catch (Exception) { return new { success = false }; } } }