2024-04-17 15:49:34 +03:30
|
|
|
|
using Back.Services;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using Shared.DTOs;
|
|
|
|
|
|
|
|
|
|
namespace Back.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Route("api/[controller]")]
|
2024-04-18 00:33:46 +03:30
|
|
|
|
[Authorize]
|
2024-04-17 15:49:34 +03:30
|
|
|
|
[ApiController]
|
|
|
|
|
public class UserController : ControllerBase
|
|
|
|
|
{
|
|
|
|
|
private readonly servUser _servUser;
|
|
|
|
|
public UserController(servUser servUser)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
_servUser = servUser;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
[HttpPost("authenticate")]
|
|
|
|
|
[AllowAnonymous]
|
2024-04-18 00:33:46 +03:30
|
|
|
|
public async Task<ActionResult<UserAuthenticationDTO>> Login([FromBody]Authentication model)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
|
|
|
|
var result = await _servUser.UserAuthentication(model.Username, model.Password);
|
|
|
|
|
if (result != null) return Ok(result);
|
|
|
|
|
else return NotFound("کاربری با این مشخصات یافت نشد");
|
2024-04-18 00:33:46 +03:30
|
|
|
|
}
|
2024-04-18 18:26:12 +03:30
|
|
|
|
[HttpGet("CheckAuthenticate")]
|
|
|
|
|
public async Task<ActionResult<UserAuthenticationDTO>> CheckAuthenticate()
|
2024-04-18 00:33:46 +03:30
|
|
|
|
{
|
2024-04-18 18:26:12 +03:30
|
|
|
|
// var accessToken = Request.Headers["Authorization"].ToString().Split(' ')[1];
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
2024-04-18 18:26:12 +03:30
|
|
|
|
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
|
|
|
|
|
var UserID = claim.Value;
|
|
|
|
|
var result = await _servUser.UserAuthentication(UserID,newtoken:false);
|
|
|
|
|
return Ok(result);
|
2024-04-17 15:49:34 +03:30
|
|
|
|
|
|
|
|
|
}
|
2024-04-29 07:58:41 +03:30
|
|
|
|
[HttpPost("ChangePassword")]
|
|
|
|
|
public async Task<ActionResult<bool>> ChangePassword(ChangePasswordDto item)
|
|
|
|
|
{
|
|
|
|
|
if (item.newPass.Trim() != item.renewPass.Trim())
|
|
|
|
|
return BadRequest(new List<string> { "تکرار کلمه عبور با کلمه عبور مطابقت ندارد" });
|
|
|
|
|
if (item.newPass.Trim().Length <= 3)
|
|
|
|
|
return BadRequest(new List<string> { "کلمه عبور جدید باید بیشتر از 3کاراکتر باشد" });
|
|
|
|
|
var UserID = HttpContext.User.Claims.First(c => c.Type == "UserID").Value;
|
|
|
|
|
if (!await _servUser.PermissionChangePassword(item.oldPass.Trim(), Convert.ToInt32(UserID)))
|
|
|
|
|
return BadRequest(new List<string> { "کلمه عبور قبلی صحیح نمی باشد" });
|
|
|
|
|
return Ok(await _servUser.ChangePassword(item.newPass.Trim(), Convert.ToInt32(UserID)));
|
|
|
|
|
|
|
|
|
|
}
|
2024-04-18 18:26:12 +03:30
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
|
|
|
|
}
|