yavsc/src/Yavsc/Services/FileSystemAuthManager.cs

117 lines
3.7 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;
2020-10-09 19:35:39 +01:00
using Microsoft.Extensions.OptionsModel;
using System.IO;
using rules;
2020-10-17 11:35:15 +01:00
using Microsoft.Data.Entity;
using Microsoft.AspNet.FileProviders;
2019-08-04 11:45:00 +02:00
namespace Yavsc.Services
{
public class FileSystemAuthManager : IFileSystemAuthManager
{
2020-10-17 11:35:15 +01:00
class BelongsToCircle : UserMatch
{
public override bool Match(string userId)
{
return true;
}
}
class OutOfCircle : UserMatch
{
public override bool Match(string userId)
{
return false;
}
}
2021-03-10 01:41:31 +00:00
private readonly UserMatch Out = new OutOfCircle();
private readonly UserMatch In = new BelongsToCircle();
2019-08-04 11:45:00 +02:00
2021-03-10 01:41:31 +00:00
private readonly ApplicationDbContext _dbContext;
2020-10-09 19:35:39 +01:00
2021-03-10 01:41:31 +00:00
private readonly SiteSettings SiteSettings;
private readonly string aclfileName;
2020-10-09 19:35:39 +01:00
readonly RuleSetParser ruleSetParser;
2021-03-10 01:41:31 +00:00
public FileSystemAuthManager(ApplicationDbContext dbContext, IOptions<SiteSettings> sitesOptions)
2019-08-04 11:45:00 +02:00
{
_dbContext = dbContext;
2020-10-09 19:35:39 +01:00
SiteSettings = sitesOptions.Value;
aclfileName = SiteSettings.AccessListFileName;
ruleSetParser = new RuleSetParser(false);
2019-08-04 11:45:00 +02:00
}
public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, IFileInfo file)
2019-08-04 11:45:00 +02:00
{
var parts = file.PhysicalPath.Split(Path.DirectorySeparatorChar);
var cwd = Environment.CurrentDirectory.Split(Path.DirectorySeparatorChar).Length;
2019-08-14 14:11:27 +01:00
2020-10-18 10:15:08 +01:00
// below 3 parts behind cwd, no file name.
if (parts.Length < cwd + 3) return FileAccessRight.None;
2020-10-09 19:35:39 +01:00
var fileDir = string.Join("/", parts.Take(parts.Length - 1));
2020-10-17 11:35:15 +01:00
var fileName = parts[parts.Length - 1];
var cusername = user.GetUserName();
2019-08-14 14:11:27 +01:00
var funame = parts[cwd+1];
2020-10-17 11:35:15 +01:00
if (funame == cusername)
2020-10-09 19:35:39 +01:00
{
return FileAccessRight.Read | FileAccessRight.Write;
}
if (aclfileName == fileName)
2020-10-17 11:35:15 +01:00
return FileAccessRight.None;
2020-10-09 19:35:39 +01:00
ruleSetParser.Reset();
2020-10-17 11:35:15 +01:00
var cuserid = user.GetUserId();
var fuserid = _dbContext.Users.SingleOrDefault(u => u.UserName == funame).Id;
2020-10-18 10:15:08 +01:00
if (string.IsNullOrEmpty(fuserid)) return FileAccessRight.None;
2020-10-18 10:15:08 +01:00
2020-10-17 11:35:15 +01:00
var circles = _dbContext.Circle.Include(mb => mb.Members).Where(c => c.OwnerId == fuserid).ToArray();
foreach (var circle in circles)
{
if (circle.Members.Any(m => m.MemberId == cuserid))
ruleSetParser.Definitions.Add(circle.Name, In);
else ruleSetParser.Definitions.Add(circle.Name, Out);
}
for (int dirlevel = parts.Length - 1; dirlevel > cwd + 1; dirlevel--)
{
fileDir = string.Join(Path.DirectorySeparatorChar.ToString(), parts.Take(dirlevel));
var aclfin = Path.Combine(fileDir, aclfileName);
var aclfi = new FileInfo(aclfin);
if (!aclfi.Exists) continue;
ruleSetParser.ParseFile(aclfi.FullName);
}
2020-10-09 19:35:39 +01:00
if (ruleSetParser.Rules.Allow(cusername))
2020-10-09 19:35:39 +01:00
{
return FileAccessRight.Read;
2019-08-14 14:11:27 +01:00
}
return FileAccessRight.None;
2020-10-18 10:15:08 +01:00
// TODO default user scoped file access policy
2020-10-09 19:35:39 +01:00
2019-08-04 11:45:00 +02:00
}
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
}