Merge remote-tracking branch 'origin/main'

# Conflicts:
#	csharp/App/Backend/DataTypes/Methods/User.cs
#	csharp/App/Backend/Database/Db.cs
This commit is contained in:
Kim 2023-03-16 10:33:23 +01:00
commit e4dd24c1b0
11 changed files with 64 additions and 65 deletions

View File

@ -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;

View File

@ -33,7 +33,9 @@ public static class FolderMethods
public static IEnumerable<Folder> Ancestors(this Folder folder)
{
return folder.Unfold(Parent);
return folder
.Unfold(Parent)
.Skip(1); // skip self
}
public static Folder? Parent(this Folder folder)

View File

@ -33,9 +33,12 @@ public static class InstallationMethods
{
var parentFolder = Parent(installation);
return parentFolder is null
? Enumerable.Empty<Folder>()
: parentFolder.Ancestors();
if (parentFolder is null)
return Enumerable.Empty<Folder>();
return parentFolder
.Ancestors()
.Prepend(parentFolder);
}
public static Folder? Parent(this Installation installation)

View File

@ -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

View File

@ -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<User> 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

View File

@ -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;
}
}

View File

@ -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<Folder> Folders => Connection.Table<Folder>();
public static TableQuery<Installation> Installations => Connection.Table<Installation>();
public static TableQuery<User> Users => Connection.Table<User>();
public static TableQuery<User2Folder> User2Folder => Connection.Table<User2Folder>();
public static TableQuery<User2Installation> User2Installation => Connection.Table<User2Installation>();
public static TableQuery<FolderAccess> User2Folder => Connection.Table<FolderAccess>();
public static TableQuery<InstallationAccess> User2Installation => Connection.Table<InstallationAccess>();
static Db()
@ -33,8 +32,8 @@ public static partial class Db
Connection.CreateTable<User>();
Connection.CreateTable<Installation>();
Connection.CreateTable<Folder>();
Connection.CreateTable<User2Folder>();
Connection.CreateTable<User2Installation>();
Connection.CreateTable<FolderAccess>();
Connection.CreateTable<InstallationAccess>();
Connection.CreateTable<Session>();
});
@ -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<Boolean> Cleanup(Int64 _)
private static void Cleanup(Int64 _)
{
await UpdateS3Urls();
DeleteStaleSessions();

View File

@ -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

View File

@ -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);

View File

@ -2,7 +2,7 @@ using SQLite;
namespace InnovEnergy.App.Backend.Relations;
public class User2Folder : Relation<Int64, Int64>
public class FolderAccess : Relation<Int64, Int64>
{
[Indexed] public Int64 UserId { get => Left ; init => Left = value;}
[Indexed] public Int64 FolderId { get => Right; init => Right = value;}

View File

@ -2,7 +2,7 @@ using SQLite;
namespace InnovEnergy.App.Backend.Relations;
public class User2Installation : Relation<Int64, Int64>
public class InstallationAccess : Relation<Int64, Int64>
{
[Indexed] public Int64 UserId { get => Left ; init => Left = value;}
[Indexed] public Int64 InstallationId { get => Right; init => Right = value;}