2024-04-29 18:15:46 +03:30
|
|
|
|
using Back.Data.Models;
|
|
|
|
|
using Back.Services;
|
|
|
|
|
using Back.Validations;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
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;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
private readonly servCompany _servCompany;
|
|
|
|
|
private readonly MobileValidation _mobilevalidation;
|
|
|
|
|
private readonly ServValidatinMsg _servValidatinMsg;
|
|
|
|
|
private readonly servSendMsg _servSendMsg;
|
|
|
|
|
public UserController(servUser servUser, MobileValidation mobilevalidation, servCompany servCompany
|
|
|
|
|
, ServValidatinMsg servValidatinMsg, servSendMsg servSendMsg)
|
2024-04-17 15:49:34 +03:30
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
_servUser = servUser;
|
2024-04-29 18:15:46 +03:30
|
|
|
|
_mobilevalidation = mobilevalidation;
|
|
|
|
|
_servCompany = servCompany;
|
|
|
|
|
_servValidatinMsg = servValidatinMsg;
|
|
|
|
|
_servSendMsg = servSendMsg;
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
|
|
|
|
[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-29 18:15:46 +03:30
|
|
|
|
[HttpGet("ChangeUserName/{newUsername}")]
|
|
|
|
|
public async Task<ActionResult<bool>> ChangeUserName(string newUsername)
|
|
|
|
|
{
|
|
|
|
|
var resultValidationmodel = await _mobilevalidation.ValidateAsync(Tuple.Create(newUsername, ActionMobileValidation.nonExistMobile));
|
|
|
|
|
if (!resultValidationmodel.IsValid)
|
|
|
|
|
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
|
|
|
|
|
|
|
|
|
|
var UserID = HttpContext.User.Claims.First(c => c.Type == "UserID").Value;
|
2024-04-18 18:26:12 +03:30
|
|
|
|
|
2024-04-29 18:15:46 +03:30
|
|
|
|
var ID = await _servValidatinMsg.GenerateCode(new VerificationCode
|
|
|
|
|
{
|
|
|
|
|
prm = UserID,
|
|
|
|
|
val = newUsername,
|
|
|
|
|
Type = "ChangeUserName"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_servSendMsg.Authentication(newUsername, ID.ToString());
|
|
|
|
|
return Ok(ID);
|
|
|
|
|
|
|
|
|
|
// return Ok(await _servUser.ChangeUserName(newUsername, Convert.ToInt32(UserID)));
|
|
|
|
|
}
|
2024-04-18 18:26:12 +03:30
|
|
|
|
|
2024-06-10 17:26:31 +03:30
|
|
|
|
[HttpGet("GetDashBoard")]
|
|
|
|
|
public async Task<ActionResult<DashBoardDTO>> GetDashBoard()
|
|
|
|
|
{
|
|
|
|
|
var claim = HttpContext.User.Claims.First(c => c.Type == "UserID");
|
|
|
|
|
var UserID = claim.Value;
|
|
|
|
|
|
2024-04-18 18:26:12 +03:30
|
|
|
|
|
2024-06-10 17:26:31 +03:30
|
|
|
|
return Ok(await _servUser.GetDashBoard(Convert.ToInt32(UserID)));
|
|
|
|
|
|
|
|
|
|
}
|
2024-04-18 18:26:12 +03:30
|
|
|
|
|
2024-04-17 15:49:34 +03:30
|
|
|
|
}
|
|
|
|
|
}
|