2023-03-15 13:38:06 +00:00
|
|
|
using InnovEnergy.App.Backend.Database;
|
|
|
|
using InnovEnergy.App.Backend.DataTypes;
|
2024-08-12 07:48:16 +00:00
|
|
|
using InnovEnergy.App.Backend.DataTypes.Methods;
|
2023-03-15 13:38:06 +00:00
|
|
|
using SQLite;
|
|
|
|
|
|
|
|
namespace InnovEnergy.App.Backend.Relations;
|
|
|
|
|
|
|
|
public class Session : Relation<String, Int64>
|
|
|
|
{
|
|
|
|
public static TimeSpan MaxAge { get; } = TimeSpan.FromDays(7);
|
|
|
|
|
|
|
|
[Unique ] public String Token { get => Left ; init => Left = value;}
|
|
|
|
[Indexed] public Int64 UserId { get => Right; init => Right = value;}
|
|
|
|
[Indexed] public DateTime LastSeen { get; set; }
|
2024-08-12 07:48:16 +00:00
|
|
|
public Boolean AccessToSalimax { get; set; } = false;
|
|
|
|
public Boolean AccessToSalidomo { get; set; } = false;
|
2023-03-21 10:49:17 +00:00
|
|
|
[Ignore] public Boolean Valid => DateTime.Now - LastSeen < MaxAge
|
|
|
|
&& (User) is not null;
|
2023-03-15 13:38:06 +00:00
|
|
|
|
|
|
|
[Ignore] public User User => _User ??= Db.GetUserById(UserId)!;
|
|
|
|
|
|
|
|
|
|
|
|
private User? _User;
|
|
|
|
|
|
|
|
[Obsolete("To be used only by deserializer")]
|
|
|
|
public Session()
|
|
|
|
{}
|
|
|
|
|
|
|
|
public Session(User user)
|
|
|
|
{
|
|
|
|
_User = user;
|
|
|
|
Token = CreateToken();
|
|
|
|
UserId = user.Id;
|
|
|
|
LastSeen = DateTime.Now;
|
2024-08-12 07:48:16 +00:00
|
|
|
AccessToSalimax = user.AccessibleInstallations(product: 0).ToList().Count > 0;
|
|
|
|
AccessToSalidomo = user.AccessibleInstallations(product: 1).ToList().Count > 0;
|
|
|
|
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String CreateToken()
|
|
|
|
{
|
2023-11-14 11:30:57 +00:00
|
|
|
//var token = new Byte[24];
|
|
|
|
//Random.Shared.NextBytes(token);
|
|
|
|
//return Convert.ToBase64String(token).Replace("/","");
|
|
|
|
return Guid.NewGuid().ToString("N");
|
2023-03-15 13:38:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|