Skip to content

Commit

Permalink
Merge branch 'main' into JavaScript-Animations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmad-Alqadiri committed Dec 13, 2024
2 parents 5778952 + cf2240e commit 5a9c0d8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
6 changes: 4 additions & 2 deletions GameLibrary.Tests/Pages/Account/RegisterModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ namespace GameLibrary.Tests.Pages.Account;
public class RegisterModelTests
{
private readonly Mock<UserManager<User>> _mockUserManager;
private readonly Mock<RoleManager<Role>> _mockRoleManager;
private readonly Mock<SignInManager<User>> _mockSignInManager;
private readonly Mock<IUserEmailStore<User>> _mockEmailStore;
private readonly Mock<ILogger<RegisterModel>> _mockLogger;
Expand All @@ -38,15 +39,16 @@ public RegisterModelTests()
_mockEmailStore = new Mock<IUserEmailStore<User>>();
_mockUserManager = new Mock<UserManager<User>>(
_mockEmailStore.Object, null!, null!, null!, null!, null!, null!, null!, null!);

_mockRoleManager = new Mock<RoleManager<Role>>(
Mock.Of<IRoleStore<Role>>(), null!, null!, null!, null!);
_mockUserManager.Setup(u => u.SupportsUserEmail).Returns(true);

_mockSignInManager = MockSignInManager();
_mockLogger = new Mock<ILogger<RegisterModel>>();
_mockEmailSender = new Mock<IEmailSender>();

_registerModel = new RegisterModel(
_mockUserManager.Object,
_mockRoleManager.Object,
_mockEmailStore.Object,
_mockSignInManager.Object,
_mockLogger.Object,
Expand Down
33 changes: 33 additions & 0 deletions GameLibrary/Pages/Account/Register.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.WebUtilities;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Text;
using System.Text.Encodings.Web;

Expand All @@ -31,19 +32,22 @@ public class RegisterModel : PageModel
{
private readonly SignInManager<User> _signInManager;
private readonly UserManager<User> _userManager;
private readonly RoleManager<Role> _roleManager;
private readonly IUserStore<User> _userStore;
private readonly IUserEmailStore<User> _emailStore;
private readonly ILogger<RegisterModel> _logger;
private readonly IEmailSender _emailSender;

public RegisterModel(
UserManager<User> userManager,
RoleManager<Role> roleManager,
IUserStore<User> userStore,
SignInManager<User> signInManager,
ILogger<RegisterModel> logger,
IEmailSender emailSender)
{
_userManager = userManager;
_roleManager = roleManager;
_userStore = userStore;
_emailStore = GetEmailStore();
_signInManager = signInManager;
Expand Down Expand Up @@ -128,6 +132,8 @@ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
_logger.LogInformation("User created a new account with password.");

await AssignRoleToUser(user, "User");

var userId = await _userManager.GetUserIdAsync(user);
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
Expand Down Expand Up @@ -182,4 +188,31 @@ private IUserEmailStore<User> GetEmailStore()
}
return (IUserEmailStore<User>) _userStore;
}

private async Task AssignRoleToUser(User user, string roleName)
{
// Check if the role exists
if (!await _roleManager.RoleExistsAsync(roleName))
{
// Create the role if it doesn't exist
var roleResult = await _roleManager.CreateAsync(new Role { Name = roleName });
if (!roleResult.Succeeded)
{
var errorMessages = string.Join(", ", roleResult.Errors.Select(e => e.Description));
_logger.LogError("Failed to create role '{RoleName}': {Errors}", roleName, errorMessages);
throw new InvalidOperationException($"Failed to create role '{roleName}': {errorMessages}");
}
}

// Assign the role to the user
var addToRoleResult = await _userManager.AddToRoleAsync(user, roleName);
if (!addToRoleResult.Succeeded)
{
var errorMessages = string.Join(", ", addToRoleResult.Errors.Select(e => e.Description));
_logger.LogError("Failed to assign role '{RoleName}' to user '{UserId}': {Errors}", roleName, user.Id, errorMessages);
throw new InvalidOperationException($"Failed to assign role '{roleName}' to user '{user.Id}': {errorMessages}");
}

_logger.LogInformation("Successfully assigned role '{RoleName}' to user '{UserId}'", roleName, user.Id);
}
}

0 comments on commit 5a9c0d8

Please sign in to comment.