2019-08-04 11:45:00 +02:00
|
|
|
using System.Security.Claims;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.FileProviders;
|
|
|
|
|
using Microsoft.Extensions.Options;
|
2020-10-09 19:35:39 +01:00
|
|
|
using rules;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Yavsc.Helpers;
|
|
|
|
|
using Yavsc.Models;
|
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;
|
|
|
|
|
|
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;
|
2020-10-17 23:40:37 +01:00
|
|
|
ruleSetParser = new RuleSetParser(false);
|
2019-08-04 11:45:00 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-26 18:59:08 +00:00
|
|
|
public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, string fileRelativePath)
|
2019-08-04 11:45:00 +02:00
|
|
|
{
|
2025-02-26 18:59:08 +00:00
|
|
|
|
2020-10-17 23:40:37 +01:00
|
|
|
var cusername = user.GetUserName();
|
2025-02-26 18:59:08 +00:00
|
|
|
FileInfo fi = new FileInfo(
|
|
|
|
|
Path.Combine(Config.UserFilesDirName, fileRelativePath));
|
|
|
|
|
|
|
|
|
|
if (fileRelativePath.StartsWith(cusername+'/'))
|
2020-10-09 19:35:39 +01:00
|
|
|
{
|
|
|
|
|
return FileAccessRight.Read | FileAccessRight.Write;
|
|
|
|
|
}
|
2025-02-26 18:59:08 +00:00
|
|
|
var funame = fileRelativePath.Split('/')[0];
|
2020-10-17 23:40:37 +01:00
|
|
|
|
2025-02-26 18:59:08 +00:00
|
|
|
// TODO Assert valid user name
|
2020-10-17 11:35:15 +01:00
|
|
|
|
2020-10-09 19:35:39 +01:00
|
|
|
ruleSetParser.Reset();
|
2020-10-17 11:35:15 +01:00
|
|
|
var cuserid = user.GetUserId();
|
2020-10-17 23:40:37 +01:00
|
|
|
|
|
|
|
|
var fuserid = _dbContext.Users.SingleOrDefault(u => u.UserName == funame).Id;
|
2020-10-18 10:15:08 +01:00
|
|
|
|
2020-10-17 23:40:37 +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);
|
|
|
|
|
}
|
2025-02-26 18:59:08 +00:00
|
|
|
var userFilesDir = new DirectoryInfo(
|
|
|
|
|
Path.Combine(Config.UserFilesDirName, funame));
|
|
|
|
|
var currentACLDir = fi.Directory;
|
|
|
|
|
do
|
2020-10-11 01:45:45 +01:00
|
|
|
{
|
2025-02-26 18:59:08 +00:00
|
|
|
var aclfileName = Path.Combine(currentACLDir.FullName,
|
|
|
|
|
SiteSettings.AccessListFileName);
|
|
|
|
|
FileInfo accessFileInfo = new FileInfo(aclfileName);
|
|
|
|
|
if (accessFileInfo.Exists)
|
|
|
|
|
ruleSetParser.ParseFile(accessFileInfo.FullName);
|
|
|
|
|
currentACLDir = currentACLDir.Parent;
|
|
|
|
|
} while (currentACLDir != userFilesDir);
|
|
|
|
|
|
2020-10-09 19:35:39 +01:00
|
|
|
|
2020-10-17 23:40:37 +01:00
|
|
|
if (ruleSetParser.Rules.Allow(cusername))
|
2020-10-09 19:35:39 +01:00
|
|
|
{
|
2020-10-17 23:40:37 +01:00
|
|
|
return FileAccessRight.Read;
|
2019-08-14 14:11:27 +01:00
|
|
|
}
|
2020-10-17 23:40:37 +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
|
|
|
}
|