diff --git a/BusinessLogic/BusinessLogic/UserLogic.cs b/BusinessLogic/BusinessLogic/UserLogic.cs index c2f21ba..63e8130 100644 --- a/BusinessLogic/BusinessLogic/UserLogic.cs +++ b/BusinessLogic/BusinessLogic/UserLogic.cs @@ -11,8 +11,12 @@ using Contracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; +using System.Net.Mail; +using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace BusinessLogic.BusinessLogic @@ -30,11 +34,15 @@ namespace BusinessLogic.BusinessLogic public UserViewModel Create(UserBindingModel model) { - ArgumentNullException.ThrowIfNull(model); - // Проверяем пароль - _validatePassword(model.Password); + // Проверяем модель + _validate(model); + var usr = _userStorage.GetElement(new() { Email = model.Email }); + if (usr is not null) + { + throw new AccountException("An account with that email already exists."); + } // Хешируем пароль - model.PasswordHash = PasswordHasher.Hash(model.Password); + model.PasswordHash = PasswordHasher.Hash(model.Password!); var user = _userStorage.Insert(model); if (user is null) { @@ -92,13 +100,9 @@ namespace BusinessLogic.BusinessLogic public UserViewModel Update(UserBindingModel model) { - ArgumentNullException.ThrowIfNull(model); + _validate(model); - if (model.Password is not null) - { - _validatePassword(model.Password); - model.PasswordHash = PasswordHasher.Hash(model.Password); - } + model.PasswordHash = PasswordHasher.Hash(model.Password!); var user = _userStorage.Update(model); if (user is null) { @@ -112,10 +116,7 @@ namespace BusinessLogic.BusinessLogic public string Login(string email, string password) { - if (email is null) - { - throw new AccountException("Email is null"); - } + _isValidEmail(email); var user = _userStorage.GetElement(new() { Email = email }); if (user is null) @@ -123,7 +124,7 @@ namespace BusinessLogic.BusinessLogic throw new ElementNotFoundException(); } // Проверяем пароль - _validatePassword(password); + _isValidPassword(password); if (!PasswordHasher.Verify(password, user.PasswordHash)) { throw new AccountException("The passwords don't match."); @@ -131,12 +132,36 @@ namespace BusinessLogic.BusinessLogic return JwtProvider.Generate(user); } - public void _validatePassword(string? password) + private void _validate(UserBindingModel model) + { + ArgumentNullException.ThrowIfNull(model); + _isValidPassword(model.Password); + _isValidEmail(model.Email); + } + + private void _isValidPassword(string? password) { if (string.IsNullOrWhiteSpace(password)) { throw new AccountException("The password is null."); } + var hasMin8Max15Chars = new Regex(@".{8,15}"); + if (!hasMin8Max15Chars.IsMatch(password)) + { + throw new AccountException("The password must not be less than 8 or more than 15 characters long."); + } + } + + private void _isValidEmail(string? email) + { + if (string.IsNullOrWhiteSpace(email)) + { + throw new AccountException("The email is null."); + } + if (!MailAddress.TryCreate(email, out _)) + { + throw new AccountException("The email is not valid."); + } } } } \ No newline at end of file