yavsc/src/Yavsc/Services/FileSystemAuthManager.cs

73 lines
2.3 KiB
C#

6 years ago
using System;
using System.Linq;
using System.Security.Principal;
using System.Security.Claims;
using Yavsc.Models;
using Microsoft.Extensions.Logging;
6 years ago
namespace Yavsc.Services
{
public class FileSystemAuthManager : IFileSystemAuthManager
{
ApplicationDbContext _dbContext;
ILogger _logger;
6 years ago
public FileSystemAuthManager(ApplicationDbContext dbContext, ILoggerFactory loggerFactory)
6 years ago
{
_dbContext = dbContext;
_logger = loggerFactory.CreateLogger<FileSystemAuthManager>();
6 years ago
}
public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, string normalizedFullPath)
{
6 years ago
// Assert (normalizedFullPath!=null)
var parts = normalizedFullPath.Split('/');
// below 4 parts, no file name.
if (parts.Length<4) return FileAccessRight.None;
var filePath = string.Join("/",parts.Skip(3));
var firstFileNamePart = parts[3];
if (firstFileNamePart == "pub")
{
_logger.LogInformation("Serving public file.");
return FileAccessRight.Read;
}
var funame = parts[2];
_logger.LogInformation($"{normalizedFullPath} from {funame}");
if (funame == user?.GetUserName())
{
_logger.LogInformation("Serving file to owner.");
return FileAccessRight.Read | FileAccessRight.Write;
}
6 years ago
var ucl = user.Claims.Where(c => c.Type == YavscClaimTypes.CircleMembership).Select(c => long.Parse(c.Value)).Distinct().ToArray();
var uclString = string.Join(",", ucl);
_logger.LogInformation($"{uclString} ");
foreach (
var cid in ucl
) {
var ok = _dbContext.CircleAuthorizationToFile.Any(a => a.CircleId == cid && a.FullPath == filePath);
if (ok) return FileAccessRight.Read;
}
6 years ago
return FileAccessRight.None;
}
public string NormalizePath(string path)
{
throw new NotImplementedException();
}
public void SetAccess(long circleId, string normalizedFullPath, FileAccessRight access)
{
throw new NotImplementedException();
}
}
}