Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HEEDLS-537 Common code for 532 and 416 #457

Merged
merged 2 commits into from
Jun 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using System.Transactions;
using DigitalLearningSolutions.Data.DataServices;
using DigitalLearningSolutions.Data.Exceptions;
using DigitalLearningSolutions.Data.Mappers;
using DigitalLearningSolutions.Data.Tests.TestHelpers;
using FluentAssertions;
Expand Down Expand Up @@ -336,6 +335,19 @@ public void GetAdminUsersByCentreId_gets_all_admins_at_centre()
Assert.That(returnedIds.SequenceEqual(expectedAdminIds));
}

[Test]
public void GetAdminUsersByCentreId_populates_correct_properties_on_admin()
{
// Given
var expectedAdminUser = UserTestHelper.GetDefaultAdminUser();

// When
var admin = userDataService.GetAdminUsersByCentreId(2).Single(a => a.Id == 7);

// Then
admin.Should().BeEquivalentTo(expectedAdminUser);
}

[Test]
public void GetNumberOfActiveAdminsAtCentre_returns_expected_count()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static AdminUser GetDefaultAdminUser(
bool isSupervisor = true,
bool isTrainer = true,
bool isFrameworkDeveloper = true,
bool importOnly = false
bool importOnly = true
)
{
return new AdminUser
Expand Down
101 changes: 69 additions & 32 deletions DigitalLearningSolutions.Data/DataServices/UserDataService.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
namespace DigitalLearningSolutions.Data.DataServices
{
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Transactions;
using Dapper;
using DigitalLearningSolutions.Data.Exceptions;
using DigitalLearningSolutions.Data.Models.User;

public interface IUserDataService
Expand Down Expand Up @@ -90,7 +88,9 @@ public UserDataService(IDbConnection connection)
au.IsFrameworkContributor,
au.IsWorkforceManager,
au.IsWorkforceContributor,
au.IsLocalWorkforceManager
au.IsLocalWorkforceManager,
au.ImportOnly,
au.FailedLoginCount
FROM AdminUsers AS au
INNER JOIN Centres AS ct ON ct.CentreID = au.CentreID
LEFT JOIN CourseCategories AS cc ON cc.CourseCategoryID = au.CategoryID
Expand Down Expand Up @@ -138,25 +138,37 @@ public List<AdminUser> GetAdminUsersByCentreId(int centreId)
{
var users = connection.Query<AdminUser>(
@"SELECT
AdminID AS Id,
CentreID,
Email AS EmailAddress,
Forename AS FirstName,
Surname AS LastName,
Password,
CentreAdmin AS IsCentreAdmin,
IsCentreManager,
ContentCreator AS IsContentCreator,
ContentManager AS IsContentManager,
PublishToAll,
SummaryReports,
UserAdmin AS IsUserAdmin,
CategoryID,
Supervisor AS IsSupervisor,
Trainer AS IsTrainer,
ImportOnly
FROM AdminUsers
WHERE Active = 1 AND Approved = 1 AND CentreId = @centreId",
au.AdminID AS Id,
au.CentreID,
ct.CentreName,
ct.Active AS CentreActive,
au.Email AS EmailAddress,
au.Forename AS FirstName,
au.Surname AS LastName,
au.Password,
au.CentreAdmin AS IsCentreAdmin,
au.IsCentreManager,
au.ContentCreator AS IsContentCreator,
au.ContentManager AS IsContentManager,
au.PublishToAll,
au.SummaryReports,
au.UserAdmin AS IsUserAdmin,
au.CategoryID,
cc.CategoryName,
au.Supervisor AS IsSupervisor,
au.Trainer AS IsTrainer,
au.IsFrameworkDeveloper,
au.ProfileImage,
au.IsFrameworkContributor,
au.IsWorkforceManager,
au.IsWorkforceContributor,
au.IsLocalWorkforceManager,
au.ImportOnly,
au.FailedLoginCount
FROM AdminUsers AS au
INNER JOIN Centres AS ct ON ct.CentreID = au.CentreID
LEFT JOIN CourseCategories AS cc ON cc.CourseCategoryID = au.CategoryID
stellake marked this conversation as resolved.
Show resolved Hide resolved
WHERE au.Active = 1 AND au.Approved = 1 AND au.CentreId = @centreId",
new { centreId }
).ToList();

Expand Down Expand Up @@ -191,7 +203,9 @@ FROM AdminUsers
au.IsFrameworkContributor,
au.IsWorkforceManager,
au.IsWorkforceContributor,
au.IsLocalWorkforceManager
au.IsLocalWorkforceManager,
au.ImportOnly,
au.FailedLoginCount
FROM AdminUsers AS au
INNER JOIN Centres AS ct ON ct.CentreID = au.CentreID
LEFT JOIN CourseCategories AS cc ON cc.CourseCategoryID = au.CategoryID
Expand Down Expand Up @@ -230,14 +244,37 @@ FROM Candidates AS cd
{
return connection.Query<AdminUser>(
@"SELECT
AdminID AS Id,
Forename AS FirstName,
Surname AS LastName,
Email AS EmailAddress,
Password,
ResetPasswordID
FROM AdminUsers
WHERE (Email = @emailAddress)",
au.AdminID AS Id,
au.CentreID,
ct.CentreName,
ct.Active AS CentreActive,
au.Email AS EmailAddress,
au.Forename AS FirstName,
au.Surname AS LastName,
au.Password,
au.CentreAdmin AS IsCentreAdmin,
au.IsCentreManager,
au.ContentCreator AS IsContentCreator,
au.ContentManager AS IsContentManager,
au.PublishToAll,
au.SummaryReports,
au.UserAdmin AS IsUserAdmin,
au.CategoryID,
cc.CategoryName,
au.Supervisor AS IsSupervisor,
au.Trainer AS IsTrainer,
au.IsFrameworkDeveloper,
au.ProfileImage,
au.IsFrameworkContributor,
au.IsWorkforceManager,
au.IsWorkforceContributor,
au.IsLocalWorkforceManager,
au.ImportOnly,
au.FailedLoginCount
FROM AdminUsers AS au
INNER JOIN Centres AS ct ON ct.CentreID = au.CentreID
LEFT JOIN CourseCategories AS cc ON cc.CourseCategoryID = au.CategoryID
WHERE (au.Email = @emailAddress)",
new { emailAddress }
).SingleOrDefault();
}
Expand Down Expand Up @@ -399,7 +436,7 @@ public int GetDelegateCountWithAnswerForPrompt(int centreId, int promptNumber)
FROM Candidates
WHERE CentreID = @centreId AND Answer{promptNumber} IS NOT NULL",
new { centreId }
).Count(x => !string.IsNullOrWhiteSpace(x)); ;
).Count(x => !string.IsNullOrWhiteSpace(x));
}

public void DeleteAllAnswersForPrompt(int centreId, int promptNumber)
Expand Down
7 changes: 7 additions & 0 deletions DigitalLearningSolutions.Data/Models/User/AdminUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

public class AdminUser : User
{
private const int FailedLoginThreshold = 5;

public bool IsCentreAdmin { get; set; }

public bool IsCentreManager { get; set; }
Expand Down Expand Up @@ -33,6 +35,11 @@ public class AdminUser : User
public bool IsLocalWorkforceManager { get; set; }
public bool ImportOnly { get; set; }

public int FailedLoginCount { get; set; }

public bool IsLocked => FailedLoginCount >= FailedLoginThreshold;
public bool IsCmsAdministrator => ImportOnly && IsContentManager;
stellake marked this conversation as resolved.
Show resolved Hide resolved

public override UserReference ToUserReference()
{
return new UserReference(Id, UserType.AdminUser);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ public SearchableAdminViewModel(AdminUser adminUser)
{
Id = adminUser.Id;
Name = adminUser.SearchableName;
CategoryName = adminUser.CategoryName;
}

public int Id { get; set; }

public string Name { get; set; }

public string? CategoryName { get; set; }
}
}