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))] [HttpPost(nameof(Login))]
public ActionResult<Session> Login(String username, String password) 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)) if (user is null || !user.VerifyPassword(password))
return Unauthorized(); return Unauthorized();

View File

@ -6,7 +6,7 @@ public abstract partial class TreeNode
{ {
[PrimaryKey, AutoIncrement] [PrimaryKey, AutoIncrement]
public Int64 Id { get; set; } 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 public String Information { get; set; } = ""; // unstructured random info
[Indexed] // parent/child relation [Indexed] // parent/child relation

View File

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

View File

@ -1,5 +1,4 @@
using InnovEnergy.App.Backend.DataTypes; using InnovEnergy.App.Backend.DataTypes;
using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations; using InnovEnergy.App.Backend.Relations;
@ -21,11 +20,6 @@ public static partial class Db
public static Boolean Create(User user) 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; return Connection.Insert(user) > 0;
} }

View File

@ -7,13 +7,13 @@ namespace InnovEnergy.App.Backend.Database;
public static partial class Db public static partial class Db
{ {
public static Folder? GetFolderById(Int64 id) public static Folder? GetFolderById(Int64? id)
{ {
return Folders return Folders
.FirstOrDefault(f => f.Id == id); .FirstOrDefault(f => f.Id == id);
} }
public static Installation? GetInstallationById(Int64 id) public static Installation? GetInstallationById(Int64? id)
{ {
return Installations return Installations
.FirstOrDefault(i => i.Id == id); .FirstOrDefault(i => i.Id == id);
@ -25,22 +25,10 @@ public static partial class Db
.FirstOrDefault(u => u.Id == id); .FirstOrDefault(u => u.Id == id);
} }
// private!! public static User? GetUserByName(String userName)
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)
{ {
return Users return Users
.FirstOrDefault(u => u.Email == email); .FirstOrDefault(u => u.Name == userName);
} }
public static Session? GetSession(String token) public static Session? GetSession(String token)
@ -62,25 +50,4 @@ public static partial class Db
return session; 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;
using InnovEnergy.App.Backend.DataTypes.Methods;
using InnovEnergy.App.Backend.Relations;
namespace InnovEnergy.App.Backend.Database; namespace InnovEnergy.App.Backend.Database;
@ -10,54 +7,24 @@ public static partial class Db
{ {
public static Boolean Update(Folder folder) public static Boolean Update(Folder folder)
{ {
if (folder.IsRelativeRoot()) // TODO: triple check return Connection.Update(folder) > 0;
{
var original = GetFolderById(folder.Id);
if (original is null)
return false;
folder.ParentId = original.ParentId;
}
return Connection.InsertOrReplace(folder) > 0;
} }
public static Boolean Update(Installation installation) public static Boolean Update(Installation installation)
{ {
if (installation.IsRelativeRoot()) // TODO: triple check return Connection.Update(installation) > 0;
{
var original = GetInstallationById(installation.Id);
if (original is null)
return false;
installation.ParentId = original.ParentId;
} }
return Connection.InsertOrReplace(installation) > 0;
}
public static Boolean Update(User user) public static Boolean Update(User user)
{ {
var originalUser = GetUserById(user.Id); var originalUser = GetUserById(user.Id);
return originalUser is not null return originalUser is not null
&& user.Id == originalUser.Id // these columns must not be modified! && user.ParentId == originalUser.ParentId // these columns must not be modified!
&& user.ParentId == originalUser.ParentId && user.Name == originalUser.Name
&& user.Email == originalUser.Email && Connection.Update(user) > 0;
&& Connection.InsertOrReplace(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.