Compare commits

...

4 Commits

8 changed files with 47 additions and 44 deletions

View File

@ -6,7 +6,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Using Include="BCrypt.Net.BCrypt" Static="True"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
</ItemGroup>

View File

@ -102,19 +102,21 @@ namespace BusinessLogic.BusinessLogic
return UserConverter.ToView(user);
}
public UserViewModel Login(UserBindingModel model)
public UserViewModel Login(string email, string password)
{
ArgumentNullException.ThrowIfNull(model);
var user = _userStorage.GetElement(new() { Email = model.Email });
if (email is null)
{
throw new AccountException("Email is null");
}
var user = _userStorage.GetElement(new() { Email = email });
if (user is null)
{
throw new ElementNotFoundException();
}
// Проверяем пароль
_validatePassword(model.Password);
if (PasswordHasher.Verify(model.Password, user.PasswordHash))
_validatePassword(password);
if (!PasswordHasher.Verify(password, user.PasswordHash))
{
throw new AccountException("The passwords don't match.");
}

View File

@ -16,11 +16,7 @@ namespace BusinessLogic.Tools
/// <returns>Хеш пароля</returns>
public static string Hash(string password)
{
using (SHA256 sha256 = SHA256.Create())
{
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(password));
return Convert.ToBase64String(bytes);
}
return BCrypt.Net.BCrypt.HashPassword(password);
}
/// <summary>
@ -31,8 +27,7 @@ namespace BusinessLogic.Tools
/// <returns></returns>
public static bool Verify(string password, string passHash)
{
var hash = Hash(password);
return hash == passHash;
return BCrypt.Net.BCrypt.Verify(password, passHash);
}
}
}

View File

@ -3,6 +3,7 @@ using Contracts.SearchModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -11,7 +12,7 @@ namespace Contracts.BusinessLogicContracts
{
public interface IUserLogic
{
UserViewModel Login(UserBindingModel model);
UserViewModel Login(string email, string password);
UserViewModel Create(UserBindingModel model);

View File

@ -12,7 +12,6 @@ namespace Contracts.ViewModels
public string FirstName { get; set; } = string.Empty;
public string SecondName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string PasswordHash { get; set; } = string.Empty;
public DateTime Birthday { get; set; }
public RoleViewModel Role { get; set; } = null!;
}

View File

@ -3,6 +3,7 @@ using Contracts.SearchModels;
using Contracts.StorageContracts;
using DatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;
@ -21,7 +22,9 @@ namespace DatabaseImplement.Implements
}
var context = new Database();
var user = context.Users.FirstOrDefault(u => u.Equals(model));
var user = context.Users.FirstOrDefault(u =>
(model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)));
if (user is null)
{
@ -42,7 +45,9 @@ namespace DatabaseImplement.Implements
var context = new Database();
return context.Users
.Include(u => u.Role)
.FirstOrDefault(u => u.Equals(model))
.FirstOrDefault(u =>
(model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
?.GetBindingModel();
}
@ -60,7 +65,9 @@ namespace DatabaseImplement.Implements
return [];
}
return context.Users
.Where(u => u.Equals(model))
.Where(u =>
(model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
.Include(u => u.Role)
.Select(r => r.GetBindingModel());
}
@ -68,7 +75,12 @@ namespace DatabaseImplement.Implements
public UserBindingModel? Insert(UserBindingModel model)
{
var context = new Database();
var newUser = Models.User.ToUserFromBinding(model);
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
if (role is null)
{
return null;
}
var newUser = Models.User.ToUserFromBinding(model, role);
context.Users.Add(newUser);
context.SaveChanges();
@ -80,15 +92,15 @@ namespace DatabaseImplement.Implements
{
var context = new Database();
var user = context.Users
.Include(u => u.Role)
.FirstOrDefault(u => u.Id == model.Id);
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
if (user is null)
if (user is null || role is null)
{
return null;
}
user.Update(model);
user.Update(model, role);
context.SaveChanges();
return user.GetBindingModel();

View File

@ -43,17 +43,17 @@ namespace DatabaseImplement.Models
Role = Role?.GetBindingModel() ?? new()
};
public static User ToUserFromView(UserViewModel model) => new()
public static User ToUserFromView(UserViewModel model, Role role) => new()
{
Id = model.Id,
FirstName = model.FirstName,
SecondName = model.SecondName,
Email = model.Email,
Birthday = model.Birthday,
Role = Models.Role.ToRoleFromView(model.Role)
Role = role
};
public static User ToUserFromBinding(UserBindingModel model) => new()
public static User ToUserFromBinding(UserBindingModel model, Role role) => new()
{
Id = model.Id,
FirstName = model.FirstName,
@ -61,10 +61,10 @@ namespace DatabaseImplement.Models
Email = model.Email,
PasswordHash = model.PasswordHash,
Birthday = model.Birthday,
Role = Models.Role.ToRoleFromBinding(model.Role)
Role = role
};
public void Update(UserBindingModel model)
public void Update(UserBindingModel model, Role role)
{
if (model is null)
{
@ -76,20 +76,7 @@ namespace DatabaseImplement.Models
SecondName = model.SecondName;
PasswordHash = model.PasswordHash;
Birthday = model.Birthday;
Role = Models.Role.ToRoleFromBinding(model.Role);
}
public bool Equals(UserSearchModel model)
{
if (model.Id is null)
{
return Email.Contains(model.Email!);
}
if (string.IsNullOrWhiteSpace(model.Email))
{
return Id == model.Id;
}
return false;
Role = role;
}
}
}

View File

@ -21,11 +21,11 @@ namespace RestAPI.Controllers
}
[HttpPost]
public IResult Login([FromBody] UserBindingModel model)
public IResult Login(string email, string password)
{
try
{
var res = _userLogic.Login(model);
var res = _userLogic.Login(email, password);
return Results.Ok(res);
}
catch (ElementNotFoundException ex)
@ -50,17 +50,19 @@ namespace RestAPI.Controllers
{
try
{
var res = _userLogic.Login(model);
var res = _userLogic.Create(model);
return Results.Ok(res);
}
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong registration data");
throw;
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error create user");
throw;
return Results.Problem(ex.Message);
}
}