Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions DigitalLearningSolutions.Data/DataServices/LoginDataService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace DigitalLearningSolutions.Data.DataServices
{
using Dapper;
using System.Data;

public interface ILoginDataService
{
void UpdateLastAccessedForUsersTable(int Id);
}

public class LoginDataService : ILoginDataService
{
private readonly IDbConnection connection;

public LoginDataService(IDbConnection connection)
{
this.connection = connection;
}

public void UpdateLastAccessedForUsersTable(int Id)
{
connection.Execute(
@"UPDATE Users SET
LastAccessed = GETDATE()
WHERE ID = @Id",
new
{
Id
}
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Enums;
using DigitalLearningSolutions.Data.Helpers;
using DigitalLearningSolutions.Data.Models;
Expand Down Expand Up @@ -32,14 +33,15 @@ private static readonly (string?, List<(int centreId, string centreName, string
private LoginService loginService = null!;
private IUserService userService = null!;
private IUserVerificationService userVerificationService = null!;
private ILoginDataService loginDataService = null!;

[SetUp]
public void Setup()
{
userVerificationService = A.Fake<IUserVerificationService>(x => x.Strict());
userService = A.Fake<IUserService>(x => x.Strict());

loginService = new LoginService(userService, userVerificationService);
loginService = new LoginService(userService, userVerificationService, loginDataService);
}

[Test]
Expand Down
7 changes: 7 additions & 0 deletions DigitalLearningSolutions.Web/Controllers/LoginController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ public async Task<IActionResult> Index(LoginViewModel model, string timeZone = "

var loginResult = loginService.AttemptLogin(model.Username!.Trim(), model.Password!);

if (loginResult.LoginAttemptResult == LoginAttemptResult.LogIntoSingleCentre ||
loginResult.LoginAttemptResult == LoginAttemptResult.ChooseACentre)
{
loginService.UpdateLastAccessedForUsersTable(loginResult.UserEntity.UserAccount.Id);
}


switch (loginResult.LoginAttemptResult)
{
case LoginAttemptResult.InvalidCredentials:
Expand Down
13 changes: 12 additions & 1 deletion DigitalLearningSolutions.Web/Services/LoginService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Enums;
using DigitalLearningSolutions.Data.Helpers;
using DigitalLearningSolutions.Data.Models;
using DigitalLearningSolutions.Data.Models.User;
using DigitalLearningSolutions.Data.Utilities;
using DigitalLearningSolutions.Data.ViewModels;
using DigitalLearningSolutions.Web.Helpers;
using Microsoft.AspNetCore.Authentication;
Expand All @@ -28,6 +30,8 @@ List<int> idsOfCentresWithUnverifiedEmails

bool CentreEmailIsVerified(int userId, int centreIdIfLoggingIntoSingleCentre);

void UpdateLastAccessedForUsersTable(int Id);

Task<string> HandleLoginResult(
LoginResult loginResult,
TicketReceivedContext context,
Expand All @@ -41,11 +45,18 @@ public class LoginService : ILoginService
{
private readonly IUserService userService;
private readonly IUserVerificationService userVerificationService;
private readonly ILoginDataService loginDataService;

public LoginService(IUserService userService, IUserVerificationService userVerificationService)
public LoginService(IUserService userService, IUserVerificationService userVerificationService, ILoginDataService loginDataService)
{
this.userService = userService;
this.userVerificationService = userVerificationService;
this.loginDataService = loginDataService;
}

public void UpdateLastAccessedForUsersTable(int Id)
{
loginDataService.UpdateLastAccessedForUsersTable(Id);
}

public LoginResult AttemptLogin(string username, string password)
Expand Down
1 change: 1 addition & 0 deletions DigitalLearningSolutions.Web/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ private static void RegisterDataServices(IServiceCollection services)
services.AddScoped<IRequestSupportTicketDataService, RequestSupportTicketDataService>();
services.AddScoped<ICentreApplicationsDataService, CentreApplicationsDataService>();
services.AddScoped<ICentreSelfAssessmentsDataService, CentreSelfAssessmentsDataService>();
services.AddScoped<ILoginDataService, LoginDataService>();
}

private static void RegisterHelpers(IServiceCollection services)
Expand Down
Loading