Skip to content

Commit

Permalink
Merge develop into ja-117
Browse files Browse the repository at this point in the history
and resolve conflicts
  • Loading branch information
skrawus committed Jun 26, 2024
2 parents c51b510 + 3c2139d commit 3ae5360
Show file tree
Hide file tree
Showing 22 changed files with 1,343 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public UserJsonRepository(IOptions<DataJsonFilePaths> options) : base(options.Va

}

public User CreateUser(string name, UserType type, string email, string passwordHash)
public User CreateUser(string name, UserType type, string email, string passwordHash, string googleid)
{
User newUser = new(GetNewId(), name, type, email, passwordHash);
User newUser = new(GetNewId(), name, type, email, passwordHash, googleid);
Data.Add(newUser);
SaveToJson();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace TutorLizard.BusinessLogic.Interfaces.Data.Repositories;
public interface IUserRepository
{
User CreateUser(string name, UserType type, string email, string passwordHash);
User CreateUser(string name, UserType type, string email, string passwordHash, string googleId);
void DeleteUserById(int id);
List<User> GetAllUsers();
User? GetUserById(int id);
Expand Down
3 changes: 3 additions & 0 deletions TutorLizard.BusinessLogic/Interfaces/Services/IUserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ public interface IUserService
public Task<LogInResult> LogIn(string username, string password);
public Task<bool> RegisterUser(string userName, UserType type, string email, string password, string activationCode);
Task<ActivationResult> ActivateUserAsync(string activationCode);
public Task<bool> RegisterUserWithGoogle(string username, string email, string googleId);
public Task<bool> IsTheGoogleUserRegistered(string googleId);
public Task<UserDto?> LogInWithGoogle(string username, string googleId);
}
2 changes: 2 additions & 0 deletions TutorLizard.BusinessLogic/Models/DTOs/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class UserDto

[Display(Name = "Data rejestracji")]
public DateTime DateCreated { get; set; } = DateTime.Now;
public string? GoogleId { get; set; }

public UserDto(User user)
{
Expand All @@ -31,5 +32,6 @@ public UserDto(User user)
UserType = user.UserType;
Email = user.Email;
DateCreated = user.DateCreated;
GoogleId = user.GoogleId;
}
}
10 changes: 6 additions & 4 deletions TutorLizard.BusinessLogic/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace TutorLizard.BusinessLogic.Models;

public class User
public class User
{
public int Id { get; set; }
public bool IsActive { get; set; }
Expand All @@ -20,21 +20,23 @@ public class User
[MaxLength(100)]
public string Email { get; set; }

[Required]
[DataType(DataType.Password)]
[MinLength(8)]
[MaxLength(100)]
public string PasswordHash { get; set; }
public string? PasswordHash { get; set; }

public DateTime DateCreated { get; set; } = DateTime.Now;

public User(int id, string name, UserType userType, string email, string passwordHash)
public string? GoogleId { get; set; }

public User(int id, string name, UserType userType, string email, string passwordHash, string? googleId)
{
Id = id;
Name = name;
UserType = userType;
Email = email;
PasswordHash = passwordHash;
GoogleId = googleId;
}
public User()

Check warning on line 41 in TutorLizard.BusinessLogic/Models/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 41 in TutorLizard.BusinessLogic/Models/User.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Email' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
{
Expand Down
44 changes: 44 additions & 0 deletions TutorLizard.BusinessLogic/Services/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public UserService(IDbRepository<User> userRepository)
{
_userRepository = userRepository;
}

public async Task<LogInResult> LogIn(string username, string password)
{
var user = await _userRepository.GetAll()
Expand Down Expand Up @@ -54,8 +55,25 @@ public async Task<LogInResult> LogIn(string username, string password)
{
ResultCode = LogInResultCode.InvalidPassword
};
}

public async Task<UserDto?> LogInWithGoogle(string username, string googleId)
{
var user = await _userRepository.GetAll()
.FirstOrDefaultAsync(user => user.GoogleId == googleId);

if (user == null)
{
return null;
}

if (user.GoogleId == googleId)
return user.ToDto();

return null;
}


public async Task<bool> RegisterUser(string userName, UserType type, string email, string password, string activationCode)
{
if (await _userRepository.GetAll().AnyAsync(user => user.Name == userName))
Expand All @@ -78,6 +96,32 @@ public async Task<bool> RegisterUser(string userName, UserType type, string emai
return true;
}

public async Task<bool> RegisterUserWithGoogle(string username, string email, string googleId)
{
if (await _userRepository.GetAll().AnyAsync(user => user.Name == username))
return false;

User user = new()
{
Name = username,
UserType = UserType.Regular,
Email = email,
GoogleId = googleId
};

await _userRepository.Create(user);

return true;
}

public async Task<bool> IsTheGoogleUserRegistered(string googleId)
{
if (await _userRepository.GetAll().AnyAsync(user => user.GoogleId == googleId))
return true;

return false;
}

public async Task<ActivationResult> ActivateUserAsync(string activationCode)
{

Expand Down
72 changes: 70 additions & 2 deletions TutorLizard.Web/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Security.Authentication;
using System.Security.Claims;
using System.ComponentModel;
using System.Net;
using System.Net.Mail;
Expand All @@ -11,6 +16,7 @@


namespace TutorLizard.Web.Controllers;

public class AccountController : Controller
{
private readonly IUserAuthenticationService _userAuthenticationService;
Expand All @@ -31,15 +37,76 @@ public IActionResult Index()
return View();
}

public IActionResult Login([FromQuery] string? returnUrl)
public async Task<IActionResult> Login([FromQuery] string? returnUrl)
{
if (returnUrl is not null)
{
TempData["returnUrl"] = returnUrl;
}

return View();
}

public async Task GoogleLogin()
{
await HttpContext.ChallengeAsync(GoogleDefaults.AuthenticationScheme,
new AuthenticationProperties
{
RedirectUri = Url.Action("GoogleResponse"),
Items =
{
{ "prompt", "select_account" }
}
});
}

public async Task<IActionResult> GoogleResponse()
{
try
{
var result = await HttpContext.AuthenticateAsync(GoogleDefaults.AuthenticationScheme);

if (result?.Succeeded != true)
{
return RedirectToAction("Login");
}

var claims = result.Principal.Identities.FirstOrDefault()?.Claims.ToList();

var claimNameIdentifier = claims?.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value;
var claimName = claims?.FirstOrDefault(x => x.Type == ClaimTypes.Name)?.Value;
var claimEmail = claims?.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value;

if(!(await _userAuthenticationService.IsGoogleUserRegistered(claimNameIdentifier)))
{
try
{
await _userAuthenticationService.RegisterUserWithGoogle(claimName, claimEmail, claimNameIdentifier);
}
catch (Exception ex)
{
_uiMessagesService.ShowFailureMessage("Rejestracja użytkownika za pomocą konta google się nie powiodła");
return RedirectToAction("Login");
}
}

var loggedIn = await _userAuthenticationService.LogInWithGoogleAsync(claimName,claimNameIdentifier);

if (!loggedIn)
{
_uiMessagesService.ShowFailureMessage("Logowanie nieudane.");
return RedirectToAction("Login");
}

return RedirectToAction("Index", "Home");
}
catch (Exception ex)
{
_uiMessagesService.ShowFailureMessage("Logowanie nieudane.");
return RedirectToAction("Login");
}
}

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel model)
Expand Down Expand Up @@ -98,6 +165,7 @@ public async Task<IActionResult> Login(LoginModel model)
public async Task<IActionResult> Logout()
{
await _userAuthenticationService.LogOutAsync();

return RedirectToAction("Index", "Home");
}
public IActionResult Register()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public interface IUserAuthenticationService
int? GetLoggedInUserId();
Task<Models.DTOs.LogInResult> LogInAsync(string username, string password);
public Task LogOutAsync();
public Task<bool> IsGoogleUserRegistered(string googleid);
public Task<bool> RegisterUserWithGoogle(string username, string email, string googleId);
public Task<bool> LogInWithGoogleAsync(string username, string googleId);
Task<(bool, string)> RegisterUser(string username, UserType type, string email, string password);
void SendActivationEmail(string email, string activationCode);
}
Loading

0 comments on commit 3ae5360

Please sign in to comment.