yavsc/src/Yavsc/Services/FileSystemAuthManager.cs

73 lines
2.4 KiB
C#
Raw Normal View History

2019-08-04 11:45:00 +02:00
using System;
using System.Linq;
using System.Security.Principal;
using System.Security.Claims;
using Yavsc.Models;
2019-08-14 14:11:27 +01:00
using Microsoft.Extensions.Logging;
2019-08-04 11:45:00 +02:00
namespace Yavsc.Services
{
public class FileSystemAuthManager : IFileSystemAuthManager
{
2020-09-12 01:11:30 +01:00
readonly ApplicationDbContext _dbContext;
readonly ILogger _logger;
2019-08-04 11:45:00 +02:00
2019-08-14 14:11:27 +01:00
public FileSystemAuthManager(ApplicationDbContext dbContext, ILoggerFactory loggerFactory)
2019-08-04 11:45:00 +02:00
{
_dbContext = dbContext;
2019-08-14 14:11:27 +01:00
_logger = loggerFactory.CreateLogger<FileSystemAuthManager>();
2019-08-04 11:45:00 +02:00
}
public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, string normalizedFullPath)
{
2019-08-14 14:11:27 +01:00
2019-08-04 11:45:00 +02:00
// Assert (normalizedFullPath!=null)
var parts = normalizedFullPath.Split('/');
// below 4 parts, no file name.
2019-08-14 14:11:27 +01:00
if (parts.Length<4) return FileAccessRight.None;
2019-08-14 14:11:27 +01:00
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];
2019-08-14 14:11:27 +01:00
_logger.LogInformation($"{normalizedFullPath} from {funame}");
if (funame == user?.GetUserName())
{
_logger.LogInformation("Serving file to owner.");
return FileAccessRight.Read | FileAccessRight.Write;
}
2019-08-14 14:11:27 +01:00
2019-08-04 11:45:00 +02:00
2019-08-14 14:11:27 +01:00
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;
}
2019-08-04 11:45:00 +02:00
return FileAccessRight.None;
}
public string NormalizePath(string path)
{
throw new NotImplementedException();
}
public void SetAccess(long circleId, string normalizedFullPath, FileAccessRight access)
{
throw new NotImplementedException();
}
}
2020-09-12 01:11:30 +01:00
}