diff --git a/csharp/App/Backend/Controllers/Controller.cs b/csharp/App/Backend/Controllers/Controller.cs index 322242871..2cb2ba46f 100644 --- a/csharp/App/Backend/Controllers/Controller.cs +++ b/csharp/App/Backend/Controllers/Controller.cs @@ -4,7 +4,6 @@ using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.Relations; using Microsoft.AspNetCore.Mvc; using static System.Net.HttpStatusCode; -using static System.String; using Folder = InnovEnergy.App.Backend.DataTypes.Folder; using Installation = InnovEnergy.App.Backend.DataTypes.Installation; using Object = System.Object; diff --git a/csharp/App/Backend/DataTypes/Methods/Folder.cs b/csharp/App/Backend/DataTypes/Methods/Folder.cs index 274574a4f..321aca9eb 100644 --- a/csharp/App/Backend/DataTypes/Methods/Folder.cs +++ b/csharp/App/Backend/DataTypes/Methods/Folder.cs @@ -33,7 +33,9 @@ public static class FolderMethods public static IEnumerable Ancestors(this Folder folder) { - return folder.Unfold(Parent); + return folder + .Unfold(Parent) + .Skip(1); // skip self } public static Folder? Parent(this Folder folder) diff --git a/csharp/App/Backend/DataTypes/Methods/Installation.cs b/csharp/App/Backend/DataTypes/Methods/Installation.cs index 15fda5ff1..6d50415fc 100644 --- a/csharp/App/Backend/DataTypes/Methods/Installation.cs +++ b/csharp/App/Backend/DataTypes/Methods/Installation.cs @@ -33,9 +33,12 @@ public static class InstallationMethods { var parentFolder = Parent(installation); - return parentFolder is null - ? Enumerable.Empty() - : parentFolder.Ancestors(); + if (parentFolder is null) + return Enumerable.Empty(); + + return parentFolder + .Ancestors() + .Prepend(parentFolder); } public static Folder? Parent(this Installation installation) diff --git a/csharp/App/Backend/DataTypes/Methods/Session.cs b/csharp/App/Backend/DataTypes/Methods/Session.cs index fc66f90d1..86960dab2 100644 --- a/csharp/App/Backend/DataTypes/Methods/Session.cs +++ b/csharp/App/Backend/DataTypes/Methods/Session.cs @@ -110,6 +110,34 @@ public static class SessionMethods && Db.Delete(userToDelete); } + + public static Boolean GrantUserAccessTo(this Session? session, User? user, Installation? installation) + { + var sessionUser = session?.User; + + return sessionUser is not null + && user is not null + && installation is not null + && user.IsDescendantOf(sessionUser) + && sessionUser.HasAccessTo(installation) + && !user.HasAccessTo(installation) + && Db.Create(new InstallationAccess { UserId = user.Id, InstallationId = installation.Id }); + + } + + public static Boolean GrantUserAccessTo(this Session? session, User? user, Folder? folder) + { + var sessionUser = session?.User; + + return sessionUser is not null + && user is not null + && folder is not null + && user.IsDescendantOf(sessionUser) + && sessionUser.HasAccessTo(folder) + && !user.HasAccessTo(folder) + && Db.Create(new FolderAccess { UserId = user.Id, FolderId = folder.Id }); + } + public static Boolean Logout(this Session? session) { return session is not null diff --git a/csharp/App/Backend/DataTypes/Methods/User.cs b/csharp/App/Backend/DataTypes/Methods/User.cs index b517bbf8c..e0b47f0a6 100644 --- a/csharp/App/Backend/DataTypes/Methods/User.cs +++ b/csharp/App/Backend/DataTypes/Methods/User.cs @@ -1,11 +1,5 @@ -using System.Net.Http.Headers; using System.Net.Mail; using System.Security.Cryptography; -using System.Text; -using System.Text.Json.Nodes; -using System.Text.RegularExpressions; -using CliWrap; -using CliWrap.Buffered; using InnovEnergy.App.Backend.Database; using InnovEnergy.Lib.Utils; using Convert = System.Convert; @@ -91,7 +85,9 @@ public static class UserMethods private static IEnumerable Ancestors(this User user) { - return user.Unfold(Parent); + return user + .Unfold(Parent) + .Skip(1); // skip self } public static Boolean VerifyPassword(this User user, String password) @@ -138,7 +134,8 @@ public static class UserMethods if (folder is null) return false; - return folder + return user.HasDirectAccessTo(folder) + || folder .Ancestors() .Any(user.HasDirectAccessTo); } @@ -147,7 +144,7 @@ public static class UserMethods { return Db .User2Installation - .Any(r => r.InstallationId == installation.Id && r.UserId == user.Id); + .Any(r => r.UserId == user.Id && r.InstallationId == installation.Id); } public static Boolean HasAccessTo(this User user, Installation? installation) @@ -166,7 +163,6 @@ public static class UserMethods return other .Ancestors() - .Skip(1) // Important! skip self, user cannot delete or edit himself .Contains(user); } @@ -177,8 +173,8 @@ public static class UserMethods return $"{user.Id}InnovEnergy"; } + - // TODO diff --git a/csharp/App/Backend/Database/Create.cs b/csharp/App/Backend/Database/Create.cs index 875b5ddef..537192476 100644 --- a/csharp/App/Backend/Database/Create.cs +++ b/csharp/App/Backend/Database/Create.cs @@ -29,10 +29,18 @@ public static partial class Db return Connection.Insert(user) > 0; } - public static Boolean Create(Session session) { return Connection.Insert(session) > 0; } + public static Boolean Create(InstallationAccess installationAccess) + { + return Connection.Insert(installationAccess) > 0; + } + + public static Boolean Create(FolderAccess folderAccess) + { + return Connection.Insert(folderAccess) > 0; + } } \ No newline at end of file diff --git a/csharp/App/Backend/Database/Db.cs b/csharp/App/Backend/Database/Db.cs index 05b3c6a7c..959322d8f 100644 --- a/csharp/App/Backend/Database/Db.cs +++ b/csharp/App/Backend/Database/Db.cs @@ -1,6 +1,5 @@ using System.Reactive.Linq; using InnovEnergy.App.Backend.DataTypes; -using InnovEnergy.App.Backend.DataTypes.Methods; using InnovEnergy.App.Backend.Relations; using InnovEnergy.Lib.Utils; using SQLite; @@ -20,8 +19,8 @@ public static partial class Db public static TableQuery Folders => Connection.Table(); public static TableQuery Installations => Connection.Table(); public static TableQuery Users => Connection.Table(); - public static TableQuery User2Folder => Connection.Table(); - public static TableQuery User2Installation => Connection.Table(); + public static TableQuery User2Folder => Connection.Table(); + public static TableQuery User2Installation => Connection.Table(); static Db() @@ -33,8 +32,8 @@ public static partial class Db Connection.CreateTable(); Connection.CreateTable(); Connection.CreateTable(); - Connection.CreateTable(); - Connection.CreateTable(); + Connection.CreateTable(); + Connection.CreateTable(); Connection.CreateTable(); }); @@ -71,46 +70,10 @@ public static partial class Db - public static Boolean AddToAccessibleInstallations(Int64 userId, Int64 updatedInstallationId) - { - var con = new User2Installation - { - UserId = userId, - InstallationId = updatedInstallationId - }; - - try - { - Connection.Insert(con); - return true; - } - catch (Exception e) - { - return false; - } - } - - public static Boolean AddToAccessibleFolders(Int64 userId, Int64 updatedFolderId) - { - var con = new User2Folder - { - UserId = userId, - FolderId = updatedFolderId - }; - - try - { - Connection.Insert(con); - return true; - } - catch (Exception e) - { - return false; - } - } + - private static async Task Cleanup(Int64 _) + private static void Cleanup(Int64 _) { await UpdateS3Urls(); DeleteStaleSessions(); diff --git a/csharp/App/Backend/Database/Fake.cs b/csharp/App/Backend/Database/Fake.cs index ab144c11d..ce00e87a4 100644 --- a/csharp/App/Backend/Database/Fake.cs +++ b/csharp/App/Backend/Database/Fake.cs @@ -70,7 +70,7 @@ public static partial class Db foreach (var user in Users) while (Random.Shared.Next((Int32)(nUsers - user.Id + 1)) != 0) { - var relation = new User2Folder + var relation = new FolderAccess { UserId = user.Id, FolderId = Random.Shared.Next(nFolders) + 1 @@ -89,7 +89,7 @@ public static partial class Db foreach (var user in Users) while (Random.Shared.Next(5) != 0) { - var relation = new User2Installation + var relation = new InstallationAccess { UserId = user.Id, InstallationId = Random.Shared.Next(nbInstallations) + 1 diff --git a/csharp/App/Backend/Program.cs b/csharp/App/Backend/Program.cs index d49fa856c..d32326b7a 100644 --- a/csharp/App/Backend/Program.cs +++ b/csharp/App/Backend/Program.cs @@ -57,7 +57,7 @@ public static class Program var session = Db.GetSession(token); if (session is not null) - ctx.Items["User"] = session; + ctx.Items["Session"] = session; } await next(ctx); diff --git a/csharp/App/Backend/Relations/User2Folder.cs b/csharp/App/Backend/Relations/FolderAccess.cs similarity index 80% rename from csharp/App/Backend/Relations/User2Folder.cs rename to csharp/App/Backend/Relations/FolderAccess.cs index 0cd1fa191..545647a46 100644 --- a/csharp/App/Backend/Relations/User2Folder.cs +++ b/csharp/App/Backend/Relations/FolderAccess.cs @@ -2,7 +2,7 @@ using SQLite; namespace InnovEnergy.App.Backend.Relations; -public class User2Folder : Relation +public class FolderAccess : Relation { [Indexed] public Int64 UserId { get => Left ; init => Left = value;} [Indexed] public Int64 FolderId { get => Right; init => Right = value;} diff --git a/csharp/App/Backend/Relations/User2Installation.cs b/csharp/App/Backend/Relations/InstallationAccess.cs similarity index 79% rename from csharp/App/Backend/Relations/User2Installation.cs rename to csharp/App/Backend/Relations/InstallationAccess.cs index f898253db..c233ca953 100644 --- a/csharp/App/Backend/Relations/User2Installation.cs +++ b/csharp/App/Backend/Relations/InstallationAccess.cs @@ -2,7 +2,7 @@ using SQLite; namespace InnovEnergy.App.Backend.Relations; -public class User2Installation : Relation +public class InstallationAccess : Relation { [Indexed] public Int64 UserId { get => Left ; init => Left = value;} [Indexed] public Int64 InstallationId { get => Right; init => Right = value;}