diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebDavExplorerConfigurationSettingsSection.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebDavExplorerConfigurationSettingsSection.cs index af4e472e..1849b692 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebDavExplorerConfigurationSettingsSection.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebDavExplorerConfigurationSettingsSection.cs @@ -6,6 +6,7 @@ namespace WebsitePanel.WebDavPortal.WebConfigSections public class WebDavExplorerConfigurationSettingsSection : ConfigurationSection { private const string UserDomainKey = "userDomain"; + private const string WebdavRootKey = "webdavRoot"; private const string AuthTimeoutCookieNameKey = "authTimeoutCookieName"; private const string AppName = "applicationName"; private const string WebsitePanelConstantUserKey = "websitePanelConstantUser"; @@ -25,6 +26,13 @@ namespace WebsitePanel.WebDavPortal.WebConfigSections set { this[AuthTimeoutCookieNameKey] = value; } } + [ConfigurationProperty(WebdavRootKey, IsRequired = true)] + public WebdavRootElement WebdavRoot + { + get { return (WebdavRootElement)this[WebdavRootKey]; } + set { this[WebdavRootKey] = value; } + } + [ConfigurationProperty(UserDomainKey, IsRequired = true)] public UserDomainElement UserDomain { diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebdavRootElement.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebdavRootElement.cs new file mode 100644 index 00000000..2e6d1222 --- /dev/null +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebConfigSections/WebdavRootElement.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace WebsitePanel.WebDav.Core.Config.WebConfigSections +{ + public class WebdavRootElement : ConfigurationElement + { + private const string ValueKey = "value"; + + [ConfigurationProperty(ValueKey, IsKey = true, IsRequired = true)] + public string Value + { + get { return (string)this[ValueKey]; } + set { this[ValueKey] = value; } + } + } +} diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebDavAppConfigManager.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebDavAppConfigManager.cs index cd28a090..01eebf42 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebDavAppConfigManager.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Config/WebDavAppConfigManager.cs @@ -30,6 +30,11 @@ namespace WebsitePanel.WebDav.Core.Config get { return _configSection.UserDomain.Value; } } + public string WebdavRoot + { + get { return _configSection.WebdavRoot.Value; } + } + public string ApplicationName { get { return _configSection.ApplicationName.Value; } diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Interfaces/Managers/IWebDavManager.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Interfaces/Managers/IWebDavManager.cs index f3900c88..abbbaad0 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Interfaces/Managers/IWebDavManager.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Interfaces/Managers/IWebDavManager.cs @@ -5,13 +5,11 @@ namespace WebsitePanel.WebDav.Core.Interfaces.Managers { public interface IWebDavManager { - string RootPath { get; } - void OpenFolder(string pathPart); - IEnumerable GetChildren(); - bool IsFile(string fileName); - byte[] GetFileBytes(string fileName); - IResource GetResource( string fileName); - string GetFileUrl(string fileName); + IEnumerable OpenFolder(string path); + bool IsFile(string path); + byte[] GetFileBytes(string path); + IResource GetResource(string path); + string GetFileUrl(string path); string CreateFileId(string path); string FilePathFromId(string id); diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Managers/WebDavManager.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Managers/WebDavManager.cs index 05a7d69f..f6e43bd7 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Managers/WebDavManager.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Managers/WebDavManager.cs @@ -21,16 +21,8 @@ namespace WebsitePanel.WebDav.Core.Managers private readonly ILog Log; - private IFolder _currentFolder; private bool _isRoot = true; - - private Lazy> _rootFolders; - private Lazy _webDavRootPath; - - public string RootPath - { - get { return _webDavRootPath.Value; } - } + private IFolder _currentFolder; public WebDavManager(ICryptography cryptography) { @@ -38,49 +30,29 @@ namespace WebsitePanel.WebDav.Core.Managers Log = LogManager.GetLogger(this.GetType()); _webDavSession = new WebDavSession(); - - _rootFolders = new Lazy>(ConnectToWebDavServer); - _webDavRootPath = new Lazy(() => - { - if (_rootFolders.Value.Any()) - { - var folder = _rootFolders.Value.First(); - var uri = new Uri(folder.Url); - return uri.Scheme + "://" + uri.Host + uri.Segments[0] + uri.Segments[1]; - } - - return string.Empty; - }); - } - public void OpenFolder(string pathPart) - { - if (string.IsNullOrWhiteSpace(pathPart)) - { - _isRoot = true; - return; - } - - _isRoot = false; - - _webDavSession.Credentials = new NetworkCredential(WspContext.User.Login, _cryptography.Decrypt(WspContext.User.EncryptedPassword), WebDavAppConfigManager.Instance.UserDomain); - - _currentFolder = _webDavSession.OpenFolder(_webDavRootPath.Value + pathPart); - } - - public IEnumerable GetChildren() + public IEnumerable OpenFolder(string pathPart) { IHierarchyItem[] children; - if (_isRoot) + if (string.IsNullOrWhiteSpace(pathPart)) { - children = _rootFolders.Value.Select(x => new WebDavHierarchyItem {Href = new Uri(x.Url), ItemType = ItemType.Folder}).ToArray(); + children = ConnectToWebDavServer().Select(x => new WebDavHierarchyItem { Href = new Uri(x.Url), ItemType = ItemType.Folder }).ToArray(); } else - { + { + if (_currentFolder == null || _currentFolder.Path.ToString() != pathPart) + { + _webDavSession.Credentials = new NetworkCredential(WspContext.User.Login, + _cryptography.Decrypt(WspContext.User.EncryptedPassword), + WebDavAppConfigManager.Instance.UserDomain); + + _currentFolder = _webDavSession.OpenFolder(string.Format("{0}{1}/{2}",WebDavAppConfigManager.Instance.WebdavRoot, WspContext.User.OrganizationId , pathPart)); + } + children = _currentFolder.GetChildren(); - } + } List sortedChildren = children.Where(x => x.ItemType == ItemType.Folder).OrderBy(x => x.DisplayName).ToList(); sortedChildren.AddRange(children.Where(x => x.ItemType != ItemType.Folder).OrderBy(x => x.DisplayName)); @@ -88,30 +60,40 @@ namespace WebsitePanel.WebDav.Core.Managers return sortedChildren; } - public bool IsFile(string fileName) + public bool IsFile(string path) { - if (string.IsNullOrWhiteSpace(fileName) | _currentFolder == null) - return false; + string folder = GetFileFolder(path); - try + if (string.IsNullOrWhiteSpace(folder)) { - IResource resource = _currentFolder.GetResource(fileName); - //Stream stream = resource.GetReadStream(); - return true; + return false; } - catch (InvalidOperationException) - { - } - return false; + + var resourceName = GetResourceName(path); + + OpenFolder(folder); + + IResource resource = _currentFolder.GetResource(resourceName); + + return resource.ItemType == ItemType.Resource; } - public byte[] GetFileBytes(string fileName) + + public byte[] GetFileBytes(string path) { try { - IResource resource = _currentFolder.GetResource(fileName); + string folder = GetFileFolder(path); + + var resourceName = GetResourceName(path); + + OpenFolder(folder); + + IResource resource = _currentFolder.GetResource(resourceName); + Stream stream = resource.GetReadStream(); byte[] fileBytes = ReadFully(stream); + return fileBytes; } catch (InvalidOperationException exception) @@ -120,12 +102,17 @@ namespace WebsitePanel.WebDav.Core.Managers } } - public IResource GetResource(string fileName) + public IResource GetResource(string path) { try { - IResource resource = _currentFolder.GetResource(fileName); - return resource; + string folder = GetFileFolder(path); + + var resourceName = GetResourceName(path); + + OpenFolder(folder); + + return _currentFolder.GetResource(resourceName); } catch (InvalidOperationException exception) { @@ -133,11 +120,17 @@ namespace WebsitePanel.WebDav.Core.Managers } } - public string GetFileUrl(string fileName) + public string GetFileUrl(string path) { try { - IResource resource = _currentFolder.GetResource(fileName); + string folder = GetFileFolder(path); + + var resourceName = GetResourceName(path); + + OpenFolder(folder); + + IResource resource = _currentFolder.GetResource(resourceName); return resource.Href.ToString(); } catch (InvalidOperationException exception) @@ -171,19 +164,6 @@ namespace WebsitePanel.WebDav.Core.Managers return rootFolders; } - private byte[] ReadFully(Stream input) - { - var buffer = new byte[16*1024]; - using (var ms = new MemoryStream()) - { - int read; - while ((read = input.Read(buffer, 0, buffer.Length)) > 0) - ms.Write(buffer, 0, read); - return ms.ToArray(); - } - } - - public string CreateFileId(string path) { return _cryptography.Encrypt(path).Replace("/", "AAAAA"); @@ -193,5 +173,45 @@ namespace WebsitePanel.WebDav.Core.Managers { return _cryptography.Decrypt(id.Replace("AAAAA", "/")); } + + #region Helpers + + private byte[] ReadFully(Stream input) + { + var buffer = new byte[16 * 1024]; + using (var ms = new MemoryStream()) + { + int read; + while ((read = input.Read(buffer, 0, buffer.Length)) > 0) + ms.Write(buffer, 0, read); + return ms.ToArray(); + } + } + + private string GetFileFolder(string path) + { + if (string.IsNullOrEmpty(path) || !path.Contains('/')) + { + return string.Empty; + } + + string fileName = path.Split('/').Last(); + int index = path.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase); + string folder = path.Remove(index - 1, fileName.Length + 1); + + return folder; + } + + private string GetResourceName(string path) + { + if (string.IsNullOrEmpty(path) || !path.Contains('/')) + { + return string.Empty; + } + + return path.Split('/').Last(); ; + } + + #endregion } } \ No newline at end of file diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Owa/WopiServer.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Owa/WopiServer.cs index 7f646f02..c55f3e53 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Owa/WopiServer.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Owa/WopiServer.cs @@ -23,13 +23,7 @@ namespace WebsitePanel.WebDav.Core.Owa public CheckFileInfo GetCheckFileInfo(string path) { - string fileName = path.Split('/').Last(); - int index = path.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase); - string folder = path.Remove(index - 1, fileName.Length + 1); - - _webDavManager.OpenFolder(folder); - - var resource = _webDavManager.GetResource(fileName); + var resource = _webDavManager.GetResource(path); var cFileInfo = new CheckFileInfo { @@ -44,13 +38,7 @@ namespace WebsitePanel.WebDav.Core.Owa public FileResult GetFile(string path) { - string fileName = path.Split('/').Last(); - int index = path.LastIndexOf(fileName, StringComparison.InvariantCultureIgnoreCase); - string folder = path.Remove(index - 1, fileName.Length + 1); - - _webDavManager.OpenFolder(folder); - - var fileBytes = _webDavManager.GetFileBytes(fileName); + var fileBytes = _webDavManager.GetFileBytes(path); return new FileContentResult(fileBytes, MediaTypeNames.Application.Octet); } diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Security/Authentication/FormsAuthenticationService.cs b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Security/Authentication/FormsAuthenticationService.cs index bf1cdec0..05c523fa 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Security/Authentication/FormsAuthenticationService.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/Security/Authentication/FormsAuthenticationService.cs @@ -25,12 +25,17 @@ namespace WebsitePanel.WebDav.Core.Security.Authentication public WspPrincipal LogIn(string login, string password) { - if (_principalContext.ValidateCredentials(login, password) == false) + if (string.IsNullOrEmpty(login) || string.IsNullOrEmpty(password)) { return null; } - //var user = UserPrincipal.FindByIdentity(_principalContext, IdentityType.UserPrincipalName, login); + var user = UserPrincipal.FindByIdentity(_principalContext, IdentityType.UserPrincipalName, login); + + if (_principalContext.ValidateCredentials(login, password) == false && user != null) + { + return null; + } var principal = new WspPrincipal(login); diff --git a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/WebsitePanel.WebDav.Core.csproj b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/WebsitePanel.WebDav.Core.csproj index 9823dce5..8f0667be 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDav.Core/WebsitePanel.WebDav.Core.csproj +++ b/WebsitePanel/Sources/WebsitePanel.WebDav.Core/WebsitePanel.WebDav.Core.csproj @@ -45,6 +45,7 @@ True + @@ -83,9 +84,8 @@ - - False - ..\..\..\..\Scheduler Domains\WebsitePanel\Bin\WebsitePanel.EnterpriseServer.Base.dll + + ..\WebsitePanel.WebPortal\Bin\WebsitePanel.EnterpriseServer.Base.dll ..\WebsitePanel.WebPortal\Bin\WebsitePanel.EnterpriseServer.Client.dll @@ -114,6 +114,7 @@ + @@ -155,6 +156,7 @@ + diff --git a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/AccountController.cs b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/AccountController.cs index b63ff4dd..d26d43ee 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/AccountController.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/AccountController.cs @@ -40,9 +40,9 @@ namespace WebsitePanel.WebDavPortal.Controllers { var user = _authenticationService.LogIn(model.Login, model.Password); - ViewBag.LdapIsAuthentication = user.Identity.IsAuthenticated; + ViewBag.LdapIsAuthentication = user != null; - if (user.Identity.IsAuthenticated) + if (user != null && user.Identity.IsAuthenticated) { _authenticationService.CreateAuthenticationTicket(user); diff --git a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/FileSystemController.cs b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/FileSystemController.cs index f2411e82..a18452a9 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/FileSystemController.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Controllers/FileSystemController.cs @@ -41,19 +41,21 @@ namespace WebsitePanel.WebDavPortal.Controllers public ActionResult ShowContent(string org, string pathPart = "") { if (org != WspContext.User.OrganizationId) - return new HttpStatusCodeResult(HttpStatusCode.NoContent); - - string fileName = pathPart.Split('/').Last(); - if (_webdavManager.IsFile(fileName)) { - var fileBytes = _webdavManager.GetFileBytes(fileName); + return new HttpStatusCodeResult(HttpStatusCode.NoContent); + } + + string fileName = pathPart.Split('/').Last(); + + if (_webdavManager.IsFile(pathPart)) + { + var fileBytes = _webdavManager.GetFileBytes(pathPart); return File(fileBytes, MediaTypeNames.Application.Octet, fileName); } try { - _webdavManager.OpenFolder(pathPart); - IEnumerable children = _webdavManager.GetChildren().Where(x => !WebDavAppConfigManager.Instance.ElementsRendering.ElementsToIgnore.Contains(x.DisplayName.Trim('/'))); + IEnumerable children = _webdavManager.OpenFolder(pathPart).Where(x => !WebDavAppConfigManager.Instance.ElementsRendering.ElementsToIgnore.Contains(x.DisplayName.Trim('/'))); var model = new ModelForWebDav { Items = children.Take(WebDavAppConfigManager.Instance.ElementsRendering.DefaultCount), UrlSuffix = pathPart }; Session[WebDavAppConfigManager.Instance.SessionKeys.ResourseRenderCount] = WebDavAppConfigManager.Instance.ElementsRendering.DefaultCount; @@ -70,7 +72,7 @@ namespace WebsitePanel.WebDavPortal.Controllers { var owaOpener = WebDavAppConfigManager.Instance.OfficeOnline.Single(x => x.Extension == Path.GetExtension(pathPart)); - string fileUrl = _webdavManager.RootPath.TrimEnd('/') + "/" + pathPart.TrimStart('/'); + string fileUrl = WebDavAppConfigManager.Instance.WebdavRoot+ org + "/" + pathPart.TrimStart('/'); string accessToken = _authenticationService.CreateAccessToken(WspContext.User); string wopiSrc = Server.UrlDecode(Url.RouteUrl(OwaRouteNames.CheckFileInfo, new { encodedPath = _webdavManager.CreateFileId(pathPart) }, Request.Url.Scheme)); @@ -81,13 +83,13 @@ namespace WebsitePanel.WebDavPortal.Controllers } [HttpPost] - public ActionResult ShowAdditionalContent() + public ActionResult ShowAdditionalContent(string path) { if (Session[WebDavAppConfigManager.Instance.SessionKeys.ResourseRenderCount] != null) { var renderedElementsCount = (int)Session[WebDavAppConfigManager.Instance.SessionKeys.ResourseRenderCount]; - IEnumerable children = _webdavManager.GetChildren(); + IEnumerable children = _webdavManager.OpenFolder(path); var result = children.Skip(renderedElementsCount).Take(WebDavAppConfigManager.Instance.ElementsRendering.AddElementsCount); diff --git a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/DependencyInjection/PortalDependencies.cs b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/DependencyInjection/PortalDependencies.cs index c6031ca0..12c1cc62 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/DependencyInjection/PortalDependencies.cs +++ b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/DependencyInjection/PortalDependencies.cs @@ -7,6 +7,7 @@ using System.Web.SessionState; using WebsitePanel.WebDav.Core.Interfaces.Managers; using WebsitePanel.WebDav.Core.Interfaces.Owa; using WebsitePanel.WebDav.Core.Interfaces.Security; +using WebsitePanel.WebDav.Core.Managers; using WebsitePanel.WebDav.Core.Owa; using WebsitePanel.WebDav.Core.Security; using WebsitePanel.WebDav.Core.Security.Authentication; @@ -23,7 +24,7 @@ namespace WebsitePanel.WebDavPortal.DependencyInjection kernel.Bind().ToProvider(); kernel.Bind().To(); kernel.Bind().To(); - kernel.Bind().ToProvider(); + kernel.Bind().To(); kernel.Bind().To(); } } diff --git a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Scripts/appScripts/uploadingData2.js b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Scripts/appScripts/uploadingData2.js index 7988b483..cd9dd1d2 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Scripts/appScripts/uploadingData2.js +++ b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Scripts/appScripts/uploadingData2.js @@ -15,7 +15,7 @@ function GetResources() { $.ajax({ type: 'POST', url: '/FileSystem/ShowAdditionalContent', - data: '', + data: { path: window.location.pathname }, dataType: "html", success: function (result) { var domElement = $(result); diff --git a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Web.config b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Web.config index ba449d53..aaabd2ca 100644 --- a/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Web.config +++ b/WebsitePanel/Sources/WebsitePanel.WebDavPortal/Web.config @@ -38,6 +38,7 @@ +