This commit is contained in:
ig 2023-03-21 11:45:50 +01:00
parent 1b5baf90ae
commit 8d7f0cd8bf
7 changed files with 18 additions and 88 deletions

View File

@ -16,7 +16,7 @@ public class Controller : ControllerBase
[HttpPost(nameof(Login))]
public ActionResult<Session> Login(String username, String password)
{
var user = Db.GetUserByEmail(username);
var user = Db.GetUserByName(username);
if (user is null || !user.VerifyPassword(password))
return Unauthorized();

View File

@ -6,7 +6,7 @@ public abstract partial class TreeNode
{
[PrimaryKey, AutoIncrement]
public Int64 Id { get; set; }
public String Name { get; set; } = "";
public virtual String Name { get; set; } = ""; // overridden by User (unique)
public String Information { get; set; } = ""; // unstructured random info
[Indexed] // parent/child relation

View File

@ -4,11 +4,13 @@ namespace InnovEnergy.App.Backend.DataTypes;
public class User : TreeNode
{
[Indexed]
public String Email { get; set; } = null!;
public Boolean HasWriteAccess { get; set; } = false;
public String Language { get; set; } = null!;
public String Password { get; set; } = null!;
[Unique]
public override String Name { get; set; } = null!;
// TODO: must reset pwd
}

View File

@ -1,5 +1,4 @@
using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations;
@ -21,11 +20,6 @@ public static partial class Db
public static Boolean Create(User user)
{
if (GetUserByEmail(user.Email) is not null)
return false;
user.Password = user.SaltAndHashPassword(user.Password);
return Connection.Insert(user) > 0;
}

View File

@ -7,13 +7,13 @@ namespace InnovEnergy.App.Backend.Database;
public static partial class Db
{
public static Folder? GetFolderById(Int64 id)
public static Folder? GetFolderById(Int64? id)
{
return Folders
.FirstOrDefault(f => f.Id == id);
}
public static Installation? GetInstallationById(Int64 id)
public static Installation? GetInstallationById(Int64? id)
{
return Installations
.FirstOrDefault(i => i.Id == id);
@ -25,22 +25,10 @@ public static partial class Db
.FirstOrDefault(u => u.Id == id);
}
// private!!
private static Session? GetSessionById(Int64 id)
{
#pragma warning disable CS0618
return Sessions
.FirstOrDefault(u => u.Id == id);
#pragma warning restore CS0618
}
public static User? GetUserByEmail(String email)
public static User? GetUserByName(String userName)
{
return Users
.FirstOrDefault(u => u.Email == email);
.FirstOrDefault(u => u.Name == userName);
}
public static Session? GetSession(String token)
@ -62,25 +50,4 @@ public static partial class Db
return session;
}
public static User? GetUserBySessionToken(String token)
{
var session = Sessions
.FirstOrDefault(s => s.Token == token);
// cannot user session.Expired in the DB query above.
// It does not exist in the db (IgnoreAttribute)
if (session is null)
return null;
if (!session.Valid)
{
Delete(session);
return null;
}
return GetUserById(session.UserId);
}
}

View File

@ -1,7 +1,4 @@
using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations;
namespace InnovEnergy.App.Backend.Database;
@ -10,54 +7,24 @@ public static partial class Db
{
public static Boolean Update(Folder folder)
{
if (folder.IsRelativeRoot()) // TODO: triple check
{
var original = GetFolderById(folder.Id);
if (original is null)
return false;
folder.ParentId = original.ParentId;
}
return Connection.InsertOrReplace(folder) > 0;
return Connection.Update(folder) > 0;
}
public static Boolean Update(Installation installation)
{
if (installation.IsRelativeRoot()) // TODO: triple check
{
var original = GetInstallationById(installation.Id);
if (original is null)
return false;
installation.ParentId = original.ParentId;
return Connection.Update(installation) > 0;
}
return Connection.InsertOrReplace(installation) > 0;
}
public static Boolean Update(User user)
{
var originalUser = GetUserById(user.Id);
return originalUser is not null
&& user.Id == originalUser.Id // these columns must not be modified!
&& user.ParentId == originalUser.ParentId
&& user.Email == originalUser.Email
&& Connection.InsertOrReplace(user) > 0;
&& user.ParentId == originalUser.ParentId // these columns must not be modified!
&& user.Name == originalUser.Name
&& Connection.Update(user) > 0;
}
public static Boolean Update(this Session session)
{
#pragma warning disable CS0618
var originalSession = GetSessionById(session.Id);
#pragma warning restore CS0618
return originalSession is not null
&& session.Token == originalSession.Token // these columns must not be modified!
&& session.UserId == originalSession.UserId
&& Connection.InsertOrReplace(session) > 0;
}
}

Binary file not shown.