yavsc/src/Yavsc/Services/FileSystemAuthManager.cs

94 lines
3.1 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;
5 years ago
using Microsoft.Extensions.OptionsModel;
using System.IO;
using rules;
6 years ago
namespace Yavsc.Services
{
public class FileSystemAuthManager : IFileSystemAuthManager
{
readonly ApplicationDbContext _dbContext;
readonly ILogger _logger;
6 years ago
5 years ago
readonly SiteSettings SiteSettings;
readonly string aclfileName;
readonly RuleSetParser ruleSetParser;
public FileSystemAuthManager(ApplicationDbContext dbContext, ILoggerFactory loggerFactory,
IOptions<SiteSettings> sitesOptions)
6 years ago
{
_dbContext = dbContext;
_logger = loggerFactory.CreateLogger<FileSystemAuthManager>();
5 years ago
SiteSettings = sitesOptions.Value;
aclfileName = SiteSettings.AccessListFileName;
ruleSetParser = new RuleSetParser(true);
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.
5 years ago
if (parts.Length < 4) return FileAccessRight.None;
var fileDir = string.Join("/", parts.Take(parts.Length - 1));
var firstFileNamePart = parts[3];
5 years ago
if (firstFileNamePart == "pub")
{
_logger.LogInformation("Serving public file.");
return FileAccessRight.Read;
}
var funame = parts[2];
5 years ago
_logger.LogInformation($"Accessing {normalizedFullPath} from {funame}");
5 years ago
if (funame == user?.GetUserName())
{
_logger.LogInformation("Serving file to owner.");
return FileAccessRight.Read | FileAccessRight.Write;
}
var aclfi = new FileInfo(Path.Combine(Environment.CurrentDirectory, fileDir, aclfileName));
// TODO default user scoped file access policy
if (!aclfi.Exists) return FileAccessRight.Read;
ruleSetParser.Reset();
ruleSetParser.ParseFile(aclfi.FullName);
if (ruleSetParser.Rules.Allow(user.GetUserName()))
return FileAccessRight.Read;
6 years ago
var ucl = user.Claims.Where(c => c.Type == YavscClaimTypes.CircleMembership).Select(c => long.Parse(c.Value)).Distinct().ToArray();
5 years ago
var uclString = string.Join(",", ucl);
_logger.LogInformation($"{uclString} ");
foreach (
var cid in ucl
5 years ago
)
{
var ok = _dbContext.CircleAuthorizationToFile.Any(a => a.CircleId == cid && a.FullPath == fileDir);
if (ok) return FileAccessRight.Read;
}
5 years ago
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();
}
}
}