add validation #9

Merged
mfnefd merged 2 commits from registration into main 2024-06-15 16:08:24 +04:00
Showing only changes of commit 5115152aff - Show all commits

View File

@ -11,8 +11,12 @@ using Contracts.ViewModels;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Net.Mail;
using System.Reflection;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace BusinessLogic.BusinessLogic namespace BusinessLogic.BusinessLogic
@ -30,11 +34,15 @@ namespace BusinessLogic.BusinessLogic
public UserViewModel Create(UserBindingModel model) public UserViewModel Create(UserBindingModel model)
{ {
ArgumentNullException.ThrowIfNull(model); // Проверяем модель
// Проверяем пароль _validate(model);
_validatePassword(model.Password); 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); var user = _userStorage.Insert(model);
if (user is null) if (user is null)
{ {
@ -92,13 +100,9 @@ namespace BusinessLogic.BusinessLogic
public UserViewModel Update(UserBindingModel model) public UserViewModel Update(UserBindingModel model)
{ {
ArgumentNullException.ThrowIfNull(model); _validate(model);
if (model.Password is not null) model.PasswordHash = PasswordHasher.Hash(model.Password!);
{
_validatePassword(model.Password);
model.PasswordHash = PasswordHasher.Hash(model.Password);
}
var user = _userStorage.Update(model); var user = _userStorage.Update(model);
if (user is null) if (user is null)
{ {
@ -112,10 +116,7 @@ namespace BusinessLogic.BusinessLogic
public string Login(string email, string password) public string Login(string email, string password)
{ {
if (email is null) _isValidEmail(email);
{
throw new AccountException("Email is null");
}
var user = _userStorage.GetElement(new() { Email = email }); var user = _userStorage.GetElement(new() { Email = email });
if (user is null) if (user is null)
@ -123,7 +124,7 @@ namespace BusinessLogic.BusinessLogic
throw new ElementNotFoundException(); throw new ElementNotFoundException();
} }
// Проверяем пароль // Проверяем пароль
_validatePassword(password); _isValidPassword(password);
if (!PasswordHasher.Verify(password, user.PasswordHash)) if (!PasswordHasher.Verify(password, user.PasswordHash))
{ {
throw new AccountException("The passwords don't match."); throw new AccountException("The passwords don't match.");
@ -131,12 +132,36 @@ namespace BusinessLogic.BusinessLogic
return JwtProvider.Generate(user); 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)) if (string.IsNullOrWhiteSpace(password))
{ {
throw new AccountException("The password is null."); 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.");
}
} }
} }
} }