Released windows auth, connect to ES.Services.
This commit is contained in:
parent
2569e55609
commit
d29c347ff4
294 changed files with 329583 additions and 2315 deletions
|
@ -0,0 +1,18 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class AccountModel
|
||||
{
|
||||
[Required]
|
||||
[Display(Name = @"Login")]
|
||||
public string Login { get; set; }
|
||||
|
||||
[Required]
|
||||
[DataType(DataType.Password)]
|
||||
[Display(Name = @"Password")]
|
||||
public string Password { get; set; }
|
||||
|
||||
public string LdapError { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
using System;
|
||||
using System.DirectoryServices;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class DirectoryIdentity : IIdentity
|
||||
{
|
||||
private readonly bool _auth;
|
||||
private readonly string _path;
|
||||
|
||||
public DirectoryIdentity(string userName, string password) : this(null, userName, password)
|
||||
{
|
||||
}
|
||||
|
||||
public DirectoryIdentity(string path, string userName, string password) : this(new DirectoryEntry(path, userName, password))
|
||||
{
|
||||
}
|
||||
|
||||
public DirectoryIdentity(DirectoryEntry directoryEntry)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userName = directoryEntry.Username;
|
||||
var ds = new DirectorySearcher(directoryEntry);
|
||||
if (userName.Contains("\\"))
|
||||
userName = userName.Substring(userName.IndexOf("\\") + 1);
|
||||
ds.Filter = "samaccountname=" + userName;
|
||||
ds.PropertiesToLoad.Add("cn");
|
||||
SearchResult sr = ds.FindOne();
|
||||
if (sr == null) throw new Exception();
|
||||
_path = sr.Path;
|
||||
_auth = true;
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
_auth = false;
|
||||
}
|
||||
}
|
||||
|
||||
public string AuthenticationType
|
||||
{
|
||||
get { return null; }
|
||||
}
|
||||
|
||||
public bool IsAuthenticated
|
||||
{
|
||||
get { return _auth; }
|
||||
}
|
||||
|
||||
public string Name
|
||||
{
|
||||
get
|
||||
{
|
||||
int i = _path.IndexOf('=') + 1, j = _path.IndexOf(',');
|
||||
return _path.Substring(i, j - i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
using System;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class ErrorModel
|
||||
{
|
||||
public int HttpStatusCode { get; set; }
|
||||
public string Message { get; set; }
|
||||
public Exception Exception { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
using System.Collections.Generic;
|
||||
using WebsitePanel.WebDav.Core.Client;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public interface IWebDavManager
|
||||
{
|
||||
void OpenFolder(string pathPart);
|
||||
IEnumerable<IHierarchyItem> GetChildren();
|
||||
bool IsFile(string fileName);
|
||||
byte[] GetFileBytes(string fileName);
|
||||
string GetFileUrl(string fileName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
using System.Collections.Generic;
|
||||
using WebsitePanel.WebDav.Core.Client;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class ModelForWebDav
|
||||
{
|
||||
public IEnumerable<IHierarchyItem> Items { get; set; }
|
||||
public string UrlSuffix { get; set; }
|
||||
public string Error { get; set; }
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class OfficeOnlineModel
|
||||
{
|
||||
public string Url { get; set; }
|
||||
public string FileName { get; set; }
|
||||
|
||||
public OfficeOnlineModel(string url, string fileName)
|
||||
{
|
||||
Url = url;
|
||||
FileName = fileName;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,120 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using WebsitePanel.WebDav.Core.Client;
|
||||
using WebsitePanel.WebDavPortal.Config;
|
||||
using WebsitePanel.WebDavPortal.Exceptions;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class WebDavManager : IWebDavManager
|
||||
{
|
||||
private readonly WebDavSession _webDavSession = new WebDavSession();
|
||||
private IFolder _currentFolder;
|
||||
private string _webDavRootPath;
|
||||
|
||||
public WebDavManager(ICredentials credentials)
|
||||
{
|
||||
_webDavSession.Credentials = credentials;
|
||||
ConnectToWebDavServer();
|
||||
}
|
||||
|
||||
public WebDavManager()
|
||||
{
|
||||
ConnectToWebDavServer();
|
||||
}
|
||||
|
||||
public void OpenFolder(string pathPart)
|
||||
{
|
||||
_currentFolder = _webDavSession.OpenFolder(_webDavRootPath + pathPart);
|
||||
}
|
||||
|
||||
public IEnumerable<IHierarchyItem> GetChildren()
|
||||
{
|
||||
IHierarchyItem[] 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));
|
||||
|
||||
return sortedChildren;
|
||||
}
|
||||
|
||||
public bool IsFile(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
Stream stream = resource.GetReadStream();
|
||||
return true;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public byte[] GetFileBytes(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
Stream stream = resource.GetReadStream();
|
||||
byte[] fileBytes = ReadFully(stream);
|
||||
return fileBytes;
|
||||
}
|
||||
catch (InvalidOperationException exception)
|
||||
{
|
||||
throw new ResourceNotFoundException("Resource not found", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public string GetFileUrl(string fileName)
|
||||
{
|
||||
try
|
||||
{
|
||||
IResource resource = _currentFolder.GetResource(fileName);
|
||||
return resource.Href.ToString();
|
||||
}
|
||||
catch (InvalidOperationException exception)
|
||||
{
|
||||
throw new ResourceNotFoundException("Resource not found", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void ConnectToWebDavServer()
|
||||
{
|
||||
string webDavServerPath = WebDavAppConfigManager.Instance.ConnectionStrings.WebDavServer;
|
||||
|
||||
if (webDavServerPath == null ||
|
||||
!Regex.IsMatch(webDavServerPath, @"^http(s)?://([\w-]+.)+[\w-]+(/[\w- ./?%&=])?$"))
|
||||
throw new ConnectToWebDavServerException();
|
||||
if (webDavServerPath.Last() != '/') webDavServerPath += "/";
|
||||
_webDavRootPath = webDavServerPath;
|
||||
|
||||
try
|
||||
{
|
||||
_currentFolder = _webDavSession.OpenFolder(_webDavRootPath);
|
||||
}
|
||||
catch (WebException exception)
|
||||
{
|
||||
throw new ConnectToWebDavServerException(
|
||||
string.Format("Unable to connect to a remote WebDav server \"{0}\"", webDavServerPath), exception);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
using System;
|
||||
using System.DirectoryServices;
|
||||
using System.Security.Principal;
|
||||
|
||||
namespace WebsitePanel.WebDavPortal.Models
|
||||
{
|
||||
public class WebDavPortalIdentity
|
||||
{
|
||||
private readonly DirectoryEntry _dictionaryEntry;
|
||||
public IIdentity Identity { get; protected set; }
|
||||
|
||||
public WebDavPortalIdentity(string userName, string password) : this(null, userName, password)
|
||||
{
|
||||
}
|
||||
|
||||
public WebDavPortalIdentity(string path, string userName, string password)
|
||||
{
|
||||
_dictionaryEntry = new DirectoryEntry(path, userName, password);
|
||||
Identity = new DirectoryIdentity(_dictionaryEntry);
|
||||
}
|
||||
|
||||
public object GetADObjectProperty(string name)
|
||||
{
|
||||
return _dictionaryEntry.Properties.Contains(name) ? _dictionaryEntry.Properties[name][0] : null;
|
||||
}
|
||||
|
||||
public string GetOrganizationId()
|
||||
{
|
||||
const string distinguishedName = "CN=user200000,OU=virt,OU=TESTOU,DC=test,DC=local";
|
||||
//string distinguishedName = GetADObjectProperty(DirectoryEntryPropertyNameConstants.DistinguishedName).ToString();
|
||||
|
||||
string[] distinguishedNameParts = distinguishedName.Split(',', '=');
|
||||
if (distinguishedNameParts[2] != "OU")
|
||||
throw new Exception(@"Problems with parsing 'distinguishedName' DirectoryEntry property");
|
||||
|
||||
return distinguishedNameParts[3];
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue