file dl auth
parent
e71f598bd3
commit
8eddb95fa5
@ -1,18 +1,30 @@
|
|||||||
using Microsoft.AspNet.Authorization;
|
using Microsoft.AspNet.Authorization;
|
||||||
|
using Yavsc.Services;
|
||||||
using Yavsc.ViewModels.Auth;
|
using Yavsc.ViewModels.Auth;
|
||||||
|
|
||||||
namespace Yavsc.AuthorizationHandlers
|
namespace Yavsc.AuthorizationHandlers {
|
||||||
{
|
|
||||||
public class ViewFileHandler : AuthorizationHandler<ViewRequirement, ViewFileContext>
|
public class ViewFileHandler : AuthorizationHandler<ViewRequirement, ViewFileContext> {
|
||||||
{
|
|
||||||
protected override void Handle(AuthorizationContext context, ViewRequirement requirement, ViewFileContext fileContext)
|
IFileSystemAuthManager _authManager;
|
||||||
{
|
|
||||||
|
public ViewFileHandler (IFileSystemAuthManager authManager) {
|
||||||
|
_authManager = authManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Handle (AuthorizationContext context, ViewRequirement requirement, ViewFileContext fileContext) {
|
||||||
// TODO file access rules
|
// TODO file access rules
|
||||||
if (fileContext.Path.StartsWith("/pub/"))
|
if (fileContext.Path.StartsWith ("/pub/"))
|
||||||
context.Succeed(requirement);
|
context.Succeed (requirement);
|
||||||
else {
|
else {
|
||||||
// TODO use "/blog/{num}/" path to link to blog access list
|
if (!fileContext.Path.StartsWith ("/"))
|
||||||
context.Succeed(requirement);
|
context.Fail ();
|
||||||
|
else {
|
||||||
|
var rights = _authManager.GetFilePathAccess (context.User, fileContext.Path.Substring (1));
|
||||||
|
if ((rights & FileAccessRight.Read) > 0)
|
||||||
|
context.Succeed (requirement);
|
||||||
|
else context.Fail ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Security.Principal;
|
||||||
|
using System.Security.Claims;
|
||||||
|
using Yavsc.Models;
|
||||||
|
|
||||||
|
namespace Yavsc.Services
|
||||||
|
{
|
||||||
|
public class FileSystemAuthManager : IFileSystemAuthManager
|
||||||
|
{
|
||||||
|
ApplicationDbContext _dbContext;
|
||||||
|
|
||||||
|
public FileSystemAuthManager(ApplicationDbContext dbContext)
|
||||||
|
{
|
||||||
|
_dbContext = dbContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, string normalizedFullPath)
|
||||||
|
{
|
||||||
|
// Assert (normalizedFullPath!=null)
|
||||||
|
var parts = normalizedFullPath.Split('/');
|
||||||
|
if (parts.Length<2) return FileAccessRight.None;
|
||||||
|
var funame = parts[0];
|
||||||
|
if (funame == user.GetUserName()) return FileAccessRight.Read | FileAccessRight.Write;
|
||||||
|
|
||||||
|
var ucl = user.Claims.Where(c => c.Type == YavscClaimTypes.CircleMembership).Select(c => long.Parse(c.Value)).ToArray();
|
||||||
|
|
||||||
|
if (_dbContext.CircleAuthorizationToFile.Any(
|
||||||
|
r => r.FullPath == normalizedFullPath && ucl.Contains(r.CircleId)
|
||||||
|
)) return FileAccessRight.Read;
|
||||||
|
return FileAccessRight.None;
|
||||||
|
}
|
||||||
|
|
||||||
|
public string NormalizePath(string path)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetAccess(long circleId, string normalizedFullPath, FileAccessRight access)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue