Added session first implementation:

+ Added session in the database and added session controller
+ Added session in authorize attribute/Login
+ Added Logout function
* Refactor database models
This commit is contained in:
Lucio Maranta
2018-01-25 17:26:44 +01:00
parent af6adf1558
commit 44dfe1753b
23 changed files with 255 additions and 82 deletions
@@ -0,0 +1,73 @@
using Step.Model.DatabaseModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Step.Database.Controllers
{
public class SessionsController : IDisposable
{
private DatabaseContext dbCtx;
public SessionsController()
{
// Initialize database context
dbCtx = new DatabaseContext();
}
public void Dispose()
{
// Clear database context
dbCtx.Dispose();
}
public SessionModel FindSessionByToken(string token)
{
return dbCtx
.Sessions
.Where(x => x.Token == token) // Find session by token
.SingleOrDefault();
}
public void DeleteUserSessions(int machineUserId)
{
dbCtx
.Sessions
.RemoveRange( // Delete rows
dbCtx
.Sessions
.Where(x => x.MachineUserId == machineUserId) // Find all the session with matching machineUserId
);
// Commit changes
dbCtx.SaveChanges();
}
public void DeleteSessionsByUserAndMachineId(int machineId, int userId)
{
MachineUserModel machineUser = null;
using (MachinesUsersController machinesUsersController = new MachinesUsersController())
{
// Find machine_user id
machineUser = machinesUsersController.FindByUserId(machineId, userId);
}
DeleteUserSessions(machineUser.MachineUserId);
}
public void Create(int machineUserId, string token)
{
// Create session model
SessionModel session = new SessionModel()
{
MachineUserId = machineUserId,
Token = token
};
// Add to database
dbCtx.Sessions.Add(session);
// Commit changes
dbCtx.SaveChanges();
}
}
}