From 804a4738ea74f1907336e0a86c295d1ee4d7fb4f Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 26 Feb 2025 18:59:08 +0000 Subject: [PATCH] share the workflow config --- src/Api/Program.cs | 6 +- src/Yavsc.Server/Helpers/WorkflowHelpers.cs | 56 ++++++++++++++++++- .../Services/FileSystemAuthManager.cs | 47 +++++++--------- .../Services/IFileSystemAuthManager.cs | 6 +- src/Yavsc/Extensions/HostingExtensions.cs | 48 +--------------- 5 files changed, 83 insertions(+), 80 deletions(-) diff --git a/src/Api/Program.cs b/src/Api/Program.cs index 5ef8afde..ef4a6667 100644 --- a/src/Api/Program.cs +++ b/src/Api/Program.cs @@ -15,6 +15,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Yavsc.Helpers; using Yavsc.Interface; using Yavsc.Models; using Yavsc.Services; @@ -71,13 +72,14 @@ internal class Program services.AddTransient() .AddTransient() .AddTransient(); + services.AddTransient(); /* services.AddSingleton(); services.AddSingleton(); - services.AddTransient(); services.AddIdentityApiEndpoints(); services.AddSession(); - */ + */ + WorkflowHelpers.ConfigureBillingService(); using (var app = builder.Build()) { if (app.Environment.IsDevelopment()) diff --git a/src/Yavsc.Server/Helpers/WorkflowHelpers.cs b/src/Yavsc.Server/Helpers/WorkflowHelpers.cs index 4f382f7a..c45c7f0c 100644 --- a/src/Yavsc.Server/Helpers/WorkflowHelpers.cs +++ b/src/Yavsc.Server/Helpers/WorkflowHelpers.cs @@ -5,13 +5,19 @@ namespace Yavsc.Helpers using System.Collections.Generic; using System.Linq; using Microsoft.EntityFrameworkCore; + using Yavsc.Abstract.Workflow; + using Yavsc.Billing; using Yavsc.Models; + using Yavsc.Models.Billing; + using Yavsc.Models.Haircut; + using Yavsc.Models.Workflow; using Yavsc.Services; using Yavsc.ViewModels.FrontOffice; public static class WorkflowHelpers { - public static async Task> ListPerformersAsync(this ApplicationDbContext context, + public static async Task> + ListPerformersAsync(this ApplicationDbContext context, IBillingService billing, string actCode) { @@ -32,5 +38,53 @@ namespace Yavsc.Helpers return result; } + public static void RegisterBilling(string code, Func getter) where T : IBillable + { + BillingService.Billing.Add(code, getter); + BillingService.GlobalBillingMap.Add(typeof(T).Name, code); + } + + public static void ConfigureBillingService() + { + foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (var c in a.GetTypes()) + { + if (c.IsClass && !c.IsAbstract && + c.GetInterface("ISpecializationSettings") != null) + { + Config.ProfileTypes.Add(c); + } + } + } + + foreach (var propertyInfo in typeof(ApplicationDbContext).GetProperties()) + { + foreach (var attr in propertyInfo.CustomAttributes) + { + // something like a DbSet? + if (typeof(Yavsc.Attributes.ActivitySettingsAttribute).IsAssignableFrom(attr.AttributeType)) + { + BillingService.UserSettings.Add(propertyInfo); + } + } + } + + RegisterBilling(BillingCodes.Brush, new Func + ((db, id) => + { + var query = db.HairCutQueries.Include(q => q.Prestation).Include(q => q.Regularisation).Single(q => q.Id == id); + query.SelectedProfile = db.BrusherProfile.Single(b => b.UserId == query.PerformerId); + return query; + })); + + RegisterBilling(BillingCodes.MBrush, new Func + ((db, id) => db.HairMultiCutQueries.Include(q => q.Regularisation).Single(q => q.Id == id))); + + RegisterBilling(BillingCodes.Rdv, new Func + ((db, id) => db.RdvQueries.Include(q => q.Regularisation).Single(q => q.Id == id))); + } + } } diff --git a/src/Yavsc.Server/Services/FileSystemAuthManager.cs b/src/Yavsc.Server/Services/FileSystemAuthManager.cs index fe17f2fc..25ca399f 100644 --- a/src/Yavsc.Server/Services/FileSystemAuthManager.cs +++ b/src/Yavsc.Server/Services/FileSystemAuthManager.cs @@ -32,40 +32,29 @@ namespace Yavsc.Services private readonly SiteSettings SiteSettings; - private readonly string aclfileName; - readonly RuleSetParser ruleSetParser; public FileSystemAuthManager(ApplicationDbContext dbContext, IOptions sitesOptions) { _dbContext = dbContext; SiteSettings = sitesOptions.Value; - aclfileName = SiteSettings.AccessListFileName; ruleSetParser = new RuleSetParser(false); } - public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, IFileInfo file) + public FileAccessRight GetFilePathAccess(ClaimsPrincipal user, string fileRelativePath) { - var parts = file.PhysicalPath.Split(Path.DirectorySeparatorChar); - var cwd = Environment.CurrentDirectory.Split(Path.DirectorySeparatorChar).Length; - - - // below 3 parts behind cwd, no file name. - if (parts.Length < cwd + 3) return FileAccessRight.None; - - var fileDir = string.Join("/", parts.Take(parts.Length - 1)); - var fileName = parts[parts.Length - 1]; - + var cusername = user.GetUserName(); - - var funame = parts[cwd+1]; - if (funame == cusername) + FileInfo fi = new FileInfo( + Path.Combine(Config.UserFilesDirName, fileRelativePath)); + + if (fileRelativePath.StartsWith(cusername+'/')) { return FileAccessRight.Read | FileAccessRight.Write; } + var funame = fileRelativePath.Split('/')[0]; - if (aclfileName == fileName) - return FileAccessRight.None; + // TODO Assert valid user name ruleSetParser.Reset(); var cuserid = user.GetUserId(); @@ -81,15 +70,19 @@ namespace Yavsc.Services ruleSetParser.Definitions.Add(circle.Name, In); else ruleSetParser.Definitions.Add(circle.Name, Out); } - - for (int dirlevel = parts.Length - 1; dirlevel > cwd + 1; dirlevel--) + var userFilesDir = new DirectoryInfo( + Path.Combine(Config.UserFilesDirName, funame)); + var currentACLDir = fi.Directory; + do { - 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); - } + 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); + if (ruleSetParser.Rules.Allow(cusername)) { diff --git a/src/Yavsc.Server/Services/IFileSystemAuthManager.cs b/src/Yavsc.Server/Services/IFileSystemAuthManager.cs index c28cd0c3..94b431e9 100644 --- a/src/Yavsc.Server/Services/IFileSystemAuthManager.cs +++ b/src/Yavsc.Server/Services/IFileSystemAuthManager.cs @@ -16,14 +16,12 @@ namespace Yavsc.Services string NormalizePath (string path); /// - /// A full path starts with a slash, - /// continues with a user name, - /// and returns true by the helper fonction : + /// /// /// /// /// - FileAccessRight GetFilePathAccess(ClaimsPrincipal user, IFileInfo file); + FileAccessRight GetFilePathAccess(ClaimsPrincipal user, string fileRelativePath); void SetAccess (long circleId, string normalizedFullPath, FileAccessRight access); diff --git a/src/Yavsc/Extensions/HostingExtensions.cs b/src/Yavsc/Extensions/HostingExtensions.cs index 49553679..a03a05f7 100644 --- a/src/Yavsc/Extensions/HostingExtensions.cs +++ b/src/Yavsc/Extensions/HostingExtensions.cs @@ -90,51 +90,6 @@ public static class HostingExtensions #endregion - public static void ConfigureWorkflow() - { - foreach (var a in System.AppDomain.CurrentDomain.GetAssemblies()) - { - foreach (var c in a.GetTypes()) - { - if (c.IsClass && !c.IsAbstract && - c.GetInterface("ISpecializationSettings") != null) - { - Config.ProfileTypes.Add(c); - } - } - } - - foreach (var propertyInfo in typeof(ApplicationDbContext).GetProperties()) - { - foreach (var attr in propertyInfo.CustomAttributes) - { - // something like a DbSet? - if (typeof(Yavsc.Attributes.ActivitySettingsAttribute).IsAssignableFrom(attr.AttributeType)) - { - BillingService.UserSettings.Add(propertyInfo); - } - } - } - - RegisterBilling(BillingCodes.Brush, new Func - ((db, id) => - { - var query = db.HairCutQueries.Include(q => q.Prestation).Include(q => q.Regularisation).Single(q => q.Id == id); - query.SelectedProfile = db.BrusherProfile.Single(b => b.UserId == query.PerformerId); - return query; - })); - - RegisterBilling(BillingCodes.MBrush, new Func - ((db, id) => db.HairMultiCutQueries.Include(q => q.Regularisation).Single(q => q.Id == id))); - - RegisterBilling(BillingCodes.Rdv, new Func - ((db, id) => db.RdvQueries.Include(q => q.Regularisation).Single(q => q.Id == id))); - } - public static void RegisterBilling(string code, Func getter) where T : IBillable - { - BillingService.Billing.Add(code, getter); - BillingService.GlobalBillingMap.Add(typeof(T).Name, code); - } internal static WebApplication ConfigureWebAppServices(this WebApplicationBuilder builder) { @@ -410,7 +365,8 @@ public static class HostingExtensions //pp.MapRazorPages(); app.MapHub("/chatHub"); - ConfigureWorkflow(); + WorkflowHelpers.ConfigureBillingService(); + var services = app.Services; ILoggerFactory loggerFactory = services.GetRequiredService(); var siteSettings = services.GetRequiredService>();