This commit is contained in:
vfedosevich 2015-01-16 00:38:07 -08:00
parent f2c54df2b0
commit 41a540bd1e
13 changed files with 165 additions and 114 deletions

View file

@ -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
{

View file

@ -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; }
}
}
}

View file

@ -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; }

View file

@ -5,13 +5,11 @@ namespace WebsitePanel.WebDav.Core.Interfaces.Managers
{
public interface IWebDavManager
{
string RootPath { get; }
void OpenFolder(string pathPart);
IEnumerable<IHierarchyItem> GetChildren();
bool IsFile(string fileName);
byte[] GetFileBytes(string fileName);
IResource GetResource( string fileName);
string GetFileUrl(string fileName);
IEnumerable<IHierarchyItem> 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);

View file

@ -21,16 +21,8 @@ namespace WebsitePanel.WebDav.Core.Managers
private readonly ILog Log;
private IFolder _currentFolder;
private bool _isRoot = true;
private Lazy<IList<SystemFile>> _rootFolders;
private Lazy<string> _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<IList<SystemFile>>(ConnectToWebDavServer);
_webDavRootPath = new Lazy<string>(() =>
{
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<IHierarchyItem> GetChildren()
public IEnumerable<IHierarchyItem> 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<IHierarchyItem> 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
}
}

View file

@ -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);
}

View file

@ -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);

View file

@ -45,6 +45,7 @@
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.DirectoryServices.AccountManagement" />
@ -83,9 +84,8 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="WebsitePanel.EnterpriseServer.Base, Version=2.1.0.1, Culture=neutral, PublicKeyToken=da8782a6fc4d0081, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\Scheduler Domains\WebsitePanel\Bin\WebsitePanel.EnterpriseServer.Base.dll</HintPath>
<Reference Include="WebsitePanel.EnterpriseServer.Base">
<HintPath>..\WebsitePanel.WebPortal\Bin\WebsitePanel.EnterpriseServer.Base.dll</HintPath>
</Reference>
<Reference Include="WebsitePanel.EnterpriseServer.Client">
<HintPath>..\WebsitePanel.WebPortal\Bin\WebsitePanel.EnterpriseServer.Client.dll</HintPath>
@ -114,6 +114,7 @@
<Compile Include="Config\WebConfigSections\SessionKeysElementCollection.cs" />
<Compile Include="Config\WebConfigSections\UserDomainElement.cs" />
<Compile Include="Config\WebConfigSections\WebDavExplorerConfigurationSettingsSection.cs" />
<Compile Include="Config\WebConfigSections\WebdavRootElement.cs" />
<Compile Include="Config\WebConfigSections\WebsitePanelConstantUserElement.cs" />
<Compile Include="Config\WebDavAppConfigManager.cs" />
<Compile Include="Entities\Owa\CheckFileInfo.cs" />
@ -155,6 +156,7 @@
<Folder Include="Interfaces\ActiveDirectory\" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>

View file

@ -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);

View file

@ -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<IHierarchyItem> children = _webdavManager.GetChildren().Where(x => !WebDavAppConfigManager.Instance.ElementsRendering.ElementsToIgnore.Contains(x.DisplayName.Trim('/')));
IEnumerable<IHierarchyItem> 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<IHierarchyItem> children = _webdavManager.GetChildren();
IEnumerable<IHierarchyItem> children = _webdavManager.OpenFolder(path);
var result = children.Skip(renderedElementsCount).Take(WebDavAppConfigManager.Instance.ElementsRendering.AddElementsCount);

View file

@ -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<HttpSessionState>().ToProvider<HttpSessionStateProvider>();
kernel.Bind<ICryptography>().To<CryptoUtils>();
kernel.Bind<IAuthenticationService>().To<FormsAuthenticationService>();
kernel.Bind<IWebDavManager>().ToProvider<WebDavManagerProvider>();
kernel.Bind<IWebDavManager>().To<WebDavManager>();
kernel.Bind<IWopiServer>().To<WopiServer>();
}
}

View file

@ -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);

View file

@ -38,6 +38,7 @@
</appSettings>
<webDavExplorerConfigurationSettings>
<userDomain value="websitepanel.net" />
<webdavRoot value="https://webdav.websitepanel.net/" />
<applicationName value="WebDAV Explorer" />
<authTimeoutCookieName value=".auth-logout-timeout" />
<elementsRendering defaultCount="20" addElementsCount="20" elementsToIgnoreKey="web.config" />