From 405308d7d663cbe2f66d50d45f0c5a62a956d654 Mon Sep 17 00:00:00 2001 From: ig Date: Thu, 16 Mar 2023 09:18:39 +0100 Subject: [PATCH] make Ancestors exclude self --- csharp/App/Backend/DataTypes/Methods/Folder.cs | 4 +++- .../App/Backend/DataTypes/Methods/Installation.cs | 9 ++++++--- csharp/App/Backend/DataTypes/Methods/User.cs | 13 ++++++------- 3 files changed, 15 insertions(+), 11 deletions(-) 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 795e8a1dc..dafcf2bd0 100644 --- a/csharp/App/Backend/DataTypes/Methods/Installation.cs +++ b/csharp/App/Backend/DataTypes/Methods/Installation.cs @@ -9,9 +9,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/User.cs b/csharp/App/Backend/DataTypes/Methods/User.cs index 99988f7e8..7f4796db7 100644 --- a/csharp/App/Backend/DataTypes/Methods/User.cs +++ b/csharp/App/Backend/DataTypes/Methods/User.cs @@ -1,8 +1,5 @@ -using System.Net.Http.Headers; using System.Net.Mail; using System.Security.Cryptography; -using System.Text.Json.Nodes; -using System.Text.RegularExpressions; using InnovEnergy.App.Backend.Database; using InnovEnergy.Lib.Utils; using Convert = System.Convert; @@ -88,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) @@ -135,7 +134,8 @@ public static class UserMethods if (folder is null) return false; - return folder + return user.HasDirectAccessTo(folder) + || folder .Ancestors() .Any(user.HasDirectAccessTo); } @@ -144,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) @@ -163,7 +163,6 @@ public static class UserMethods return other .Ancestors() - .Skip(1) // Important! skip self, user cannot delete or edit himself .Contains(user); }