This commit is contained in:
mmrbnjd
2024-04-29 07:58:41 +03:30
parent fd13de3e1d
commit 7b8127dc72
23 changed files with 526 additions and 39 deletions

View File

@@ -26,7 +26,6 @@
<ItemGroup>
<Folder Include="Common\DTOs\" />
<Folder Include="Features\" />
</ItemGroup>
<ItemGroup>

View File

@@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
namespace Back.Controllers
{
@@ -59,7 +60,7 @@ namespace Back.Controllers
[HttpPost("CreateCsrAndPrivateKey")]
public async Task<ActionResult<TaxToolsDTO>> CreateCsrAndPrivateKey(CsrPrivateKeyDto model)
{
var resultValidationmodel = await _mobilevalidation.ValidateAsync(model.Mobile);
var resultValidationmodel = await _mobilevalidation.ValidateAsync(Tuple.Create(model.Mobile,false));
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
return Ok(await _sBase.CreateCsrAndPrivateKey(model));
@@ -167,6 +168,23 @@ namespace Back.Controllers
}
}
[HttpPost("ForgetPassWord")]
[AllowAnonymous]
public async Task<ActionResult<string>> ForgetPassWord(ForgetPasswordItem Item)
{
var resultValidationmodel = await _mobilevalidation.ValidateAsync(Tuple.Create(Item.Username, true));
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
var ID = await _servValidatinMsg.GenerateCode(new VerificationCode
{
prm = Item.Username,
val = Item.PassWord,
Type = "ForgetPassword"
});
_servSendMsg.Authentication(Item.Username, ID.ToString());
return Ok(ID);
}
}
}

View File

@@ -0,0 +1,29 @@
using Back.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs;
namespace Back.Controllers
{
[Route("api/[controller]")]
[Authorize]
[ApiController]
public class CompanyController : ControllerBase
{
private readonly servCompany _servCompany;
public CompanyController(servCompany servCompany)
{
_servCompany = servCompany;
}
[HttpPost("ChangeLogo")]
public async Task<ActionResult<bool>> ChangeLogo(byte[] logo)
{
//var result = await _sBase.ReadPublicKeyFromCER(modelfromBase64);
//if (result.type == "error")
// return BadRequest();
return Ok();
}
}
}

View File

@@ -2,6 +2,7 @@
using Back.Data.Models;
using Back.Services;
using Back.Validations;
using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@@ -33,7 +34,7 @@ namespace Back.Controllers
[AllowAnonymous]
public async Task<ActionResult<int>> NewTicketNoAuthentication(CTicketNoAuthenticationDto item)
{
var resultValidationmodel = await _mobilevalidation.ValidateAsync(item.Mobile);
var resultValidationmodel = await _mobilevalidation.ValidateAsync(Tuple.Create(item.Mobile,false));
if (!resultValidationmodel.IsValid)
return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());

View File

@@ -37,6 +37,19 @@ namespace Back.Controllers
return Ok(result);
}
[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)));
}

View File

@@ -17,13 +17,15 @@ namespace Back.Controllers
private readonly GetVerificationValidation _getVerificationValidation;
private readonly servSendMsg _servSendMsg;
private readonly servCompany _servCompany;
private readonly servUser _servUser;
public VerificationController(ServValidatinMsg servValidatinMsg, GetVerificationValidation getVerificationValidation
, servCompany servCompany, servSendMsg servSendMsg)
, servCompany servCompany, servSendMsg servSendMsg, servUser servUser)
{
_servValidatinMsg = servValidatinMsg;
_getVerificationValidation = getVerificationValidation;
_servCompany = servCompany;
_servSendMsg = servSendMsg;
_servUser = servUser;
}
[HttpGet("GetVerification/{ID}")]
[AllowAnonymous]
@@ -61,6 +63,12 @@ namespace Back.Controllers
_servSendMsg.Authentication(company.Mobile, ID.ToString());
break;
case "ForgetPassword":
var user = await _servUser.ChangePasswordByMobile(_getVerificationValidation.verificationCode.prm, _getVerificationValidation.verificationCode.val);
_servSendMsg.Authentication(_getVerificationValidation.verificationCode.prm, ID.ToString());
break;
default:
return BadRequest("این نوع احراز تعریف نشده");
}

View File

@@ -0,0 +1,50 @@
using Back.Services;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Threading.Tasks;
namespace Back.Features
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class CheckOnlineUser
{
private readonly RequestDelegate _next;
public CheckOnlineUser(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
int UserID = Convert.ToInt32(httpContext.User.Claims.Where(w => w.Type == "UserID").Select(s => s.Value).FirstOrDefault());
var accessToken = httpContext.GetTokenAsync("access_token").Result;
if (UserID==null || UserID==0 || string.IsNullOrEmpty(accessToken))
await _next(httpContext);
else
{
servUser _servUser = (servUser)httpContext.RequestServices.GetService(typeof(servUser));
var user = _servUser.GetUserByUserID(UserID).Result;
if (user.Token==accessToken)
await _next(httpContext);
else
{
httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
}
}
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CheckOnlineUserExtensions
{
public static IApplicationBuilder UseCheckOnlineUser(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CheckOnlineUser>();
}
}
}

View File

@@ -1,6 +1,7 @@
using Back;
using Back.Data.Contracts;
using Back.Data.Infrastructure.Repository;
using Back.Features;
using Back.Services;
using Back.Validations;
using Microsoft.EntityFrameworkCore;
@@ -80,7 +81,7 @@ app.UseHttpsRedirection();
app.UseCors(origins);
app.UseAuthentication();
app.UseAuthorization();
app.UseCheckOnlineUser();
app.MapControllers();
app.Run();

View File

@@ -103,9 +103,9 @@ namespace Back.Services
public async Task<bool> ExistMobileAndCompanyIsActive(string mobile)
{
return await _repoCompany.GetAll().AnyAsync(w => w.Mobile == mobile && w.IsActive);
}
}
}

View File

@@ -5,6 +5,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Shared.DTOs;
using System.IdentityModel.Tokens.Jwt;
using System.Reflection;
using System.Security.Claims;
using System.Text;
@@ -56,7 +57,7 @@ namespace Back.Services
return null;
string Jwt_Lifetime_Minutes = await GetJwt_Lifetime_Minutes();
ret.UserName = user.Username;
ret.Token =newtoken ? await CerateToken(user.ID, user.Username, Jwt_Lifetime_Minutes) : user.Token;
ret.FullName = user.Fullname;
ret.Photo = user.Photo==null ? null : Convert.ToBase64String(user.Photo);
@@ -175,7 +176,14 @@ namespace Back.Services
await _RepoUser.UpdateAsync(user);
}
}
public async Task<bool> ChangePasswordByMobile(string mobile, string newpassword)
{
var user =await GetUserByUsername(mobile);
if (user == null)
return false;
user.Password = newpassword.encrypted();
return await _RepoUser.UpdateAsync(user);
}
public async Task<DashBoardDTO> GetDashBoard(int CompanyID,int UserID)
{
DashBoardDTO request=new DashBoardDTO();
@@ -237,6 +245,19 @@ namespace Back.Services
//});
return await _RepoUser.UpdateByObjAsync(user);
}
public async Task<bool> ChangePassword(string newPass, int UserID)
{
var user = await GetUserByUserID(UserID);
if (user == null)
return false;
user.Password = newPass.encrypted();
return await _RepoUser.UpdateAsync(user);
}
public async Task<bool> PermissionChangePassword(string oldPass,int UserID)
{
return await _RepoUser.GetAll().AnyAsync(w => w.ID == UserID && w.Password==oldPass.encrypted() && w.IsActive);
}
//--------internal
private async Task<string> GetJwt_Lifetime_Minutes()
{

View File

@@ -1,19 +1,33 @@
using FluentValidation;
using Back.Services;
using FluentValidation;
using Shared.DTOs;
using System;
namespace Back.Validations
{
public class MobileValidation : AbstractValidator<string>
public class MobileValidation : AbstractValidator<Tuple<string,bool>>
{
public MobileValidation()
public MobileValidation(servCompany servCompany)
{
CascadeMode = CascadeMode.Stop;
RuleFor(m => m)
RuleFor(m => m.Item1)
.NotEmpty().WithMessage("موبایل نمی تواند باشد")
.NotNull().WithMessage("موبایل نمی تواند باشد")
.Length(11).WithMessage("فرمت موبایل صحیح نمی باشد")
.Must(m => m.StartsWith("09")).WithMessage("فرمت موبایل صحیح نمی باشد");
RuleFor(m => m)
.Custom((model, context) => {
if (model.Item2)
{
if (!servCompany.ExistMobileAndCompanyIsActive(model.Item1).Result)
{
context.AddFailure("این موبایل یافت نشد");
}
}
});
}
}
}

View File

@@ -10,6 +10,6 @@
},
"Fixedvalues": {
"Jwt_Lifetime_Minutes": "60"
"Jwt_Lifetime_Minutes": "144000"
}
}