Skip to content

Commit c3dd0b9

Browse files
authored
Merge pull request #2634 from TechnologyEnhancedLearning/Develop/Fixes/TD-3144-learnershouldnotbeabletochangetheirprimaryemailtomatchtheemailofanexistingaccountfixissue
TD-3144 A learner shouldn't be able to change their primary email to …
2 parents 036f5db + d440ed9 commit c3dd0b9

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

DigitalLearningSolutions.Web.Tests/Controllers/Register/RegisterDelegateByCentreControllerTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public void Setup()
7979
public void PersonalInformationPost_with_duplicate_email_for_centre_fails_validation()
8080
{
8181
// Given
82+
controller.TempData.Set(new DelegateRegistrationByCentreData());
8283
var duplicateUser = UserTestHelper.GetDefaultDelegateUser();
8384
var model = new RegisterDelegatePersonalInformationViewModel
8485
{
@@ -99,7 +100,7 @@ public void PersonalInformationPost_with_duplicate_email_for_centre_fails_valida
99100
var result = controller.PersonalInformation(model);
100101

101102
// Then
102-
result.Should().BeViewResult().WithDefaultViewName();
103+
result.Should().BeRedirectToActionResult().WithActionName("LearnerInformation");
103104
}
104105

105106
[Test]
@@ -137,7 +138,7 @@ public void PersonalInformationPost_updates_tempdata_correctly()
137138
const string firstName = "Test";
138139
const string lastName = "User";
139140
const string email = "[email protected]";
140-
141+
controller.TempData.Set(new DelegateRegistrationByCentreData());
141142
var model = new RegisterDelegatePersonalInformationViewModel
142143
{
143144
FirstName = firstName,

DigitalLearningSolutions.Web/Helpers/RegistrationEmailValidator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ IUserService userService
6363
{
6464
return;
6565
}
66-
67-
var emailIsHeldAtCentre = userService.EmailIsHeldAtCentre(email, centreId);
68-
66+
var emailIsHeldAtCentre = userService.CheckingIfPrimaryEmailExist(email, centreId);
6967
if (emailIsHeldAtCentre)
7068
{
7169
modelState.AddModelError(nameOfFieldToValidate, CommonValidationErrorMessages.EmailInUseAtCentre);

DigitalLearningSolutions.Web/Services/UserService.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ string code
115115
UserEntity? GetDelegateUserFromLearningHubAuthId(int learningHubAuthId);
116116

117117
int? GetUserLearningHubAuthId(int userId);
118+
bool CheckingIfPrimaryEmailExist(string email, int centreId);
118119
}
119120

120121
public class UserService : IUserService
@@ -629,7 +630,20 @@ private bool EmailIsHeldAsPrimaryEmailByUserAtCentre(string email, int centreId)
629630
.GetDelegateAccountsByUserId(primaryEmailOwner.Id).Any(da => da.CentreId == centreId);
630631
return primaryEmailOwnerIsAtCentre;
631632
}
633+
public bool CheckingIfPrimaryEmailExist(string email, int centreId)
634+
{
635+
var inUseAsCentreEmailAtCentre = userDataService.CentreSpecificEmailIsInUseAtCentre(email!, centreId);
636+
637+
var primaryEmailOwnerIsAtCentre = EmailIsHeldAsPrimaryEmailByUser(email);
632638

639+
return inUseAsCentreEmailAtCentre || primaryEmailOwnerIsAtCentre;
640+
}
641+
private bool EmailIsHeldAsPrimaryEmailByUser(string email)
642+
{
643+
var primaryEmailOwner = userDataService.GetUserAccountByPrimaryEmail(email);
644+
var primaryEmailOwnerIsAtCentre = primaryEmailOwner != null;
645+
return primaryEmailOwnerIsAtCentre;
646+
}
633647
private bool NewUserRolesExceedAvailableSpots(
634648
int adminId,
635649
AdminRoles adminRoles

0 commit comments

Comments
 (0)