Skip to content

Commit

Permalink
feat(User) : add regex class and exception
Browse files Browse the repository at this point in the history
* feat(User) : add regex class and exception

* feat(User) : add regex method in regeister api

* feat(User) : add reset Password api

* fix(User) : add reset Password api
  • Loading branch information
mahdijafariii authored Aug 16, 2024
1 parent 97b3e8d commit 1299b55
Show file tree
Hide file tree
Showing 19 changed files with 249 additions and 44 deletions.
37 changes: 30 additions & 7 deletions AnalysisData/AnalysisData/Controllers/UserController.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,71 @@
using System.Security.Claims;
using AnalysisData.Exception;
using AnalysisData.Services;
using AnalysisData.UserManage.LoginModel;
using AnalysisData.UserManage.RegisterModel;
using AnalysisData.UserManage.ResetPasswordModel;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AnalysisData.Controllers;

[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
private readonly IUserService _userService;
private readonly IPermissionService _permissionService;

public UserController(IUserService userService,IPermissionService permissionService)
public UserController(IUserService userService, IPermissionService permissionService)
{
_userService = userService;
_permissionService = permissionService;
}

[HttpPost("login")]
public IActionResult Login([FromBody] UserLoginModel userLoginModel)
{
var user = _userService.Login(userLoginModel);
return Ok(new { user.Result.FirstName , user.Result.LastName , user.Result.ImageURL });
return Ok(new { user.Result.FirstName, user.Result.LastName, user.Result.ImageURL });
}

[Authorize(Roles = "admin")]
[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] UserRegisterModel userRegisterModel)
{
var check = await _userService.Register(userRegisterModel);
var check = await _userService.Register(userRegisterModel);
if (check)
{
return Ok("success");
}

return NotFound("not success");
return BadRequest("not success");
}


[HttpGet("permissions")]
public IActionResult GetPermissions()
{
var userClaims = User;
var permission = _permissionService.GetPermission(userClaims);
return Ok(new { permission });
var username = userClaims.FindFirstValue("username");
var firstName = userClaims.FindFirstValue("firstname");
var lastName = userClaims.FindFirstValue("lastname");
return Ok(new { username, firstName, lastName, permission });
}


[HttpPost("reset-password")]
public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordModel resetPasswordModel)
{
var userClaim = User;
var check = await _userService.ResetPassword(userClaim, resetPasswordModel.NewPassword,
resetPasswordModel.ConfirmPassword);
if (check)
{
return Ok("success");
}

return BadRequest("not success");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.Exception;

public class InvalidEmailFormatException : System.Exception
{
public InvalidEmailFormatException() : base(Resources.InvalidEmailFormatException)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.Exception;

public class InvalidPasswordFormatException : System.Exception
{
public InvalidPasswordFormatException() : base(Resources.InvalidPasswordFormatException)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.Exception;

public class InvalidPhoneNumberFormatException : System.Exception
{
public InvalidPhoneNumberFormatException() : base(Resources.InvalidPhoneNumberFormatException)
{
}
}
9 changes: 0 additions & 9 deletions AnalysisData/AnalysisData/Exception/NotFoundUserException.cs

This file was deleted.

9 changes: 9 additions & 0 deletions AnalysisData/AnalysisData/Exception/UserNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace AnalysisData.Exception;
using System;

public class UserNotFoundException : Exception
{
public UserNotFoundException() : base(Resources.UserNotFoundException)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task InvokeAsync(HttpContext httpContext)
{
await _next(httpContext);
}
catch (NotFoundUserException ex)
catch (UserNotFoundException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status404NotFound);
}
Expand Down Expand Up @@ -58,19 +58,28 @@ public async Task InvokeAsync(HttpContext httpContext)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}

{}

}
catch (InvalidEmailFormatException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (InvalidPasswordFormatException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
catch (InvalidPhoneNumberFormatException ex)
{
await HandleExceptionAsync(httpContext, ex, StatusCodes.Status401Unauthorized);
}
}

private Task HandleExceptionAsync(HttpContext context, System.Exception exception, int statusCode)
private Task HandleExceptionAsync(HttpContext context, System.Exception exception, int _statusCode)
{
context.Response.ContentType = "application/json";
context.Response.StatusCode = statusCode;
context.Response.StatusCode = _statusCode;

var response = new
{
statusCode = statusCode,
statusCode = _statusCode,
message = exception.Message,
};

Expand Down
1 change: 1 addition & 0 deletions AnalysisData/AnalysisData/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ICookieService, CookieService>();
builder.Services.AddScoped<IPermissionService, PermissionService>();
builder.Services.AddScoped<IRegexService, RegexService>();
builder.Services.AddHttpContextAccessor();

builder.Services.AddDbContext<ApplicationDbContext>(options =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public interface IUserRepository
{
Task<User> GetUser(string userName);
Task<IReadOnlyList<User>> GetAllUser();
bool DeleteUser(string userName);
Task<bool> DeleteUser(string userName);
void AddUser(User user);
Task UpdateUser(User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,25 @@ public async Task<IReadOnlyList<User>> GetAllUser()
return await _context.Users.ToListAsync();
}

public bool DeleteUser(string userName)
public async Task<bool> DeleteUser(string userName)
{
var user = _context.Users.FirstOrDefault(x => x.Username == userName);
var user = await _context.Users.FirstOrDefaultAsync(x => x.Username == userName);
if (user == null) return false;
_context.Users.Remove(user);
_context.SaveChanges();
await _context.SaveChangesAsync();
return true;
}

public void AddUser(User user)
public async void AddUser(User user)
{
_context.Users.Add(user);
_context.SaveChanges();
await _context.SaveChangesAsync();
}

public async Task UpdateUser(User user)
{
_context.Users.Update(user);
await _context.SaveChangesAsync();
}
}
}
33 changes: 30 additions & 3 deletions AnalysisData/AnalysisData/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion AnalysisData/AnalysisData/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="NotFoundUserException" xml:space="preserve">
<data name="UserNotFoundException" xml:space="preserve">
<value>user not found ! </value>
</data>
<data name="InvalidPasswordException" xml:space="preserve">
Expand All @@ -33,4 +33,13 @@
<data name="RoleNotFoundException" xml:space="preserve">
<value>We do not have this role !</value>
</data>
<data name="InvalidEmailFormatException" xml:space="preserve">
<value>Invalid Email format !</value>
</data>
<data name="InvalidPhoneNumberFormatException" xml:space="preserve">
<value>Invalid Phone number format !</value>
</data>
<data name="InvalidPasswordFormatException" xml:space="preserve">
<value>password format is invlaid ! </value>
</data>
</root>
8 changes: 8 additions & 0 deletions AnalysisData/AnalysisData/Services/IRegexService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AnalysisData.Services;

public interface IRegexService
{
public void EmailCheck(string email);
public void PasswordCheck(string password);
public void PhoneNumberCheck(string phoneNumber);
}
2 changes: 2 additions & 0 deletions AnalysisData/AnalysisData/Services/IUserService.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Security.Claims;
using AnalysisData.UserManage.LoginModel;
using AnalysisData.UserManage.RegisterModel;

Expand All @@ -7,4 +8,5 @@ public interface IUserService
{
Task<User> Login(UserLoginModel userLoginModel);
Task<bool> Register(UserRegisterModel userRegisterModel);
Task<bool> ResetPassword(ClaimsPrincipal userClaim, string password, string confirmPassword);
}
43 changes: 43 additions & 0 deletions AnalysisData/AnalysisData/Services/RegexService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Text.RegularExpressions;
using AnalysisData.Exception;
using AnalysisData.UserManage.Model;

namespace AnalysisData.Services;

public class RegexService : IRegexService
{
public void EmailCheck(string email)
{
string pattern = RegexPatterns.EmailRegex;

Regex regex = new Regex(pattern);
bool isMatch = regex.IsMatch(email);
if (!isMatch)
{
throw new InvalidEmailFormatException();
}
}

public void PhoneNumberCheck(string phoneNumber)
{
string pattern = RegexPatterns.PhoneNumberRegex;
Regex regex = new Regex(pattern);
bool isMatch = regex.IsMatch(phoneNumber);
if (!isMatch)
{
throw new InvalidPhoneNumberFormatException();
}
}

public void PasswordCheck(string password)
{
string pattern = RegexPatterns.PasswordRegex;
Regex regex = new Regex(pattern);
bool isMatch = regex.IsMatch(password);
if (!isMatch)
{
throw new InvalidPasswordFormatException();
}

}
}
Loading

0 comments on commit 1299b55

Please sign in to comment.