Innovenergy_trunk/csharp/App/Backend/Relations/Session.cs

48 lines
1.5 KiB
C#
Raw Normal View History

2023-03-15 13:38:06 +00:00
using InnovEnergy.App.Backend.Database;
using InnovEnergy.App.Backend.DataTypes;
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; }
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;
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()
{
//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
}
}