1067 lines
44 KiB
C#
1067 lines
44 KiB
C#
using _0_Framework.Application;
|
|
using _0_Framework.Application.Sms;
|
|
using _0_Framework.Exceptions;
|
|
using AccountManagement.Application.Contracts.Account;
|
|
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
|
|
using AccountManagement.Domain.AccountAgg;
|
|
using AccountManagement.Domain.AccountLeftWorkAgg;
|
|
using AccountManagement.Domain.CameraAccountAgg;
|
|
using AccountManagement.Domain.InternalApiCaller;
|
|
using AccountManagement.Domain.PmDomains.PmRoleUserAgg;
|
|
using AccountManagement.Domain.PmDomains.PmUserAgg;
|
|
using AccountManagement.Domain.PositionAgg;
|
|
using AccountManagement.Domain.RoleAgg;
|
|
using AccountManagement.Domain.SubAccountAgg;
|
|
using AccountManagement.Domain.SubAccountPermissionSubtitle1Agg;
|
|
using AccountManagement.Domain.SubAccountRoleAgg;
|
|
using Company.Domain._common;
|
|
using Company.Domain.WorkshopAgg;
|
|
using Company.Domain.WorkshopSubAccountAgg;
|
|
using CompanyManagment.App.Contracts.Workshop;
|
|
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.JsonPatch.Operations;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using AccountManagement.Application.Contracts.ProgramManager;
|
|
using Shared.Contracts.PmUser;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
//using AccountManagement.Domain.RoleAgg;
|
|
|
|
namespace AccountManagement.Application;
|
|
|
|
public class AccountApplication : IAccountApplication
|
|
{
|
|
private readonly IFileUploader _fileUploader;
|
|
private readonly IPasswordHasher _passwordHasher;
|
|
private readonly IAccountRepository _accountRepository;
|
|
private readonly IAuthHelper _authHelper;
|
|
private readonly IRoleRepository _roleRepository;
|
|
private readonly ISmsService _smsService;
|
|
private readonly ICameraAccountRepository _cameraAccountRepository;
|
|
private readonly IPositionRepository _positionRepository;
|
|
private readonly IAccountLeftworkRepository _accountLeftworkRepository;
|
|
private readonly IWorkshopRepository _workshopRepository;
|
|
private readonly ISubAccountRepository _subAccountRepository;
|
|
private readonly ISubAccountRoleRepository _subAccountRoleRepository;
|
|
private readonly IWorkshopSubAccountRepository _workshopSubAccountRepository;
|
|
private readonly ISubAccountPermissionSubtitle1Repository _accountPermissionSubtitle1Repository;
|
|
private readonly IPmUserRepository _pmUserRepository;
|
|
private readonly IUnitOfWork _unitOfWork;
|
|
private readonly IPmUserQueryService _pmUserQueryService;
|
|
|
|
public AccountApplication(IAccountRepository accountRepository, IPasswordHasher passwordHasher,
|
|
IFileUploader fileUploader, IAuthHelper authHelper, IRoleRepository roleRepository, IWorker worker, ISmsService smsService, ICameraAccountRepository cameraAccountRepository, IPositionRepository positionRepository, IAccountLeftworkRepository accountLeftworkRepository, IWorkshopRepository workshopRepository, ISubAccountRepository subAccountRepository, ISubAccountRoleRepository subAccountRoleRepository, IWorkshopSubAccountRepository workshopSubAccountRepository, ISubAccountPermissionSubtitle1Repository accountPermissionSubtitle1Repository, IUnitOfWork unitOfWork, IPmUserRepository pmUserRepository, IPmUserQueryService pmUserQueryService)
|
|
{
|
|
_authHelper = authHelper;
|
|
_roleRepository = roleRepository;
|
|
_smsService = smsService;
|
|
_cameraAccountRepository = cameraAccountRepository;
|
|
_positionRepository = positionRepository;
|
|
_accountLeftworkRepository = accountLeftworkRepository;
|
|
_workshopRepository = workshopRepository;
|
|
_subAccountRepository = subAccountRepository;
|
|
_subAccountRoleRepository = subAccountRoleRepository;
|
|
_workshopSubAccountRepository = workshopSubAccountRepository;
|
|
_accountPermissionSubtitle1Repository = accountPermissionSubtitle1Repository;
|
|
_unitOfWork = unitOfWork;
|
|
_pmUserRepository = pmUserRepository;
|
|
_pmUserQueryService = pmUserQueryService;
|
|
_fileUploader = fileUploader;
|
|
_passwordHasher = passwordHasher;
|
|
_accountRepository = accountRepository;
|
|
|
|
}
|
|
|
|
public OperationResult EditClient(EditClientAccount command)
|
|
{
|
|
var opreation = new OperationResult();
|
|
var editAccount = _accountRepository.Get(command.Id);
|
|
if (editAccount == null)
|
|
return opreation.Failed(ApplicationMessages.RecordNotFound);
|
|
if (string.IsNullOrWhiteSpace(command.Fullname) || string.IsNullOrWhiteSpace(command.Username)
|
|
|| string.IsNullOrWhiteSpace(command.Mobile) ||
|
|
string.IsNullOrWhiteSpace(command.NationalCode))
|
|
return opreation.Failed("پر کردنموارد ستاره دار الزامی است");
|
|
|
|
if (_accountRepository.Exists(x =>
|
|
(x.Username == command.Username && x.id != command.Id)))
|
|
return opreation.Failed("نام کاربری تکراری است");
|
|
if (_accountRepository.Exists(x =>
|
|
(x.Mobile == command.Mobile && x.id != command.Id)))
|
|
return opreation.Failed("شماره موبایل تکراری است");
|
|
if (_accountRepository.Exists(x =>
|
|
(x.NationalCode == command.NationalCode && !string.IsNullOrWhiteSpace(x.NationalCode) && x.id != command.Id)))
|
|
return opreation.Failed("کد ملی تکراری است");
|
|
if (_accountRepository.Exists(x =>
|
|
(x.Email == command.Email && !string.IsNullOrWhiteSpace(x.Email) && x.id != command.Id)))
|
|
return opreation.Failed("ایمیل تکراری است");
|
|
|
|
var path = $"profilePhotos";
|
|
var picturePath = _fileUploader.Upload(command.ProfilePhoto, path);
|
|
editAccount.EditClient(command.Fullname, command.Username, command.Mobile, picturePath, command.Email, command.NationalCode);
|
|
_accountRepository.SaveChanges();
|
|
return opreation.Succcedded();
|
|
}
|
|
|
|
public OperationResult ChangePassword(ChangePassword command)
|
|
{
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.Get(command.Id);
|
|
if (account == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (command.Password != command.RePassword)
|
|
return operation.Failed(ApplicationMessages.PasswordsNotMatch);
|
|
|
|
var password = _passwordHasher.Hash(command.Password);
|
|
account.ChangePassword(password);
|
|
_accountRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public AccountViewModel GetAccountBy(long id)
|
|
{
|
|
var account = _accountRepository.Get(id);
|
|
return new AccountViewModel()
|
|
{
|
|
Fullname = account.Fullname,
|
|
Mobile = account.Mobile
|
|
};
|
|
}
|
|
|
|
public async Task<OperationResult> Create(CreateAccount command)
|
|
{
|
|
var operation = new OperationResult();
|
|
|
|
if (_accountRepository.Exists(x => x.Username == command.Username || x.Mobile == command.Mobile))
|
|
return operation.Failed(ApplicationMessages.DuplicatedRecord);
|
|
|
|
var password = _passwordHasher.Hash(command.Password);
|
|
var roleName = _roleRepository.GetDetails(command.RoleId);
|
|
var path = $"profilePhotos";
|
|
var picturePath = "";
|
|
if (_fileUploader != null)
|
|
{
|
|
picturePath = _fileUploader.Upload(command.ProfilePhoto, path);
|
|
|
|
}
|
|
var account = new Account(command.Fullname, command.Username, password, command.Mobile, command.RoleId,
|
|
picturePath, roleName.Name, "true", "false");
|
|
|
|
_unitOfWork.BeginAccountContext();
|
|
|
|
|
|
_accountRepository.Create(account);
|
|
_accountRepository.SaveChanges();
|
|
|
|
if (command.IsProgramManagerUser)
|
|
{
|
|
|
|
try
|
|
{
|
|
if (_pmUserRepository.Exists(x => x.FullName == command.Fullname))
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("نام و خانوادگی تکراری است");
|
|
}
|
|
|
|
if (_pmUserRepository.Exists(x => x.UserName == command.Username))
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("نام کاربری تکراری است");
|
|
}
|
|
|
|
if (_pmUserRepository.Exists(x => !string.IsNullOrWhiteSpace(x.Mobile) && x.Mobile == command.Mobile))
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("این شماره همراه قبلا به فرد دیگری اختصاص داده شده است");
|
|
}
|
|
|
|
|
|
|
|
var userRoles = command.UserRoles.Where(x => x > 0).Select(x => new PmRoleUser(x)).ToList();
|
|
var create = new PmUser(command.Fullname, command.Username, command.Password, command.Mobile,
|
|
null, account.id, userRoles);
|
|
await _pmUserRepository.CreateAsync(create);
|
|
await _pmUserRepository.SaveChangesAsync();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("خطا در ایجاد کاربر پروگرام منیجر");
|
|
}
|
|
|
|
//var url = "api/user/create";
|
|
//var key = SecretKeys.ProgramManagerInternalApi;
|
|
|
|
//var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
|
|
// url,
|
|
// key,
|
|
// parameters
|
|
//);
|
|
|
|
//if (!response.Success)
|
|
//{
|
|
// _unitOfWork.RollbackAccountContext();
|
|
// return operation.Failed(response.Error);
|
|
//}
|
|
|
|
//if (!response.Result.isSuccess)
|
|
//{
|
|
// _unitOfWork.RollbackAccountContext();
|
|
// return operation.Failed(response.Result.errorMessage);
|
|
//}
|
|
}
|
|
|
|
_unitOfWork.CommitAccountContext();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public OperationResult RegisterClient(RegisterAccount command)
|
|
{
|
|
var opreation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.Fullname) || string.IsNullOrWhiteSpace(command.Username)
|
|
|| string.IsNullOrWhiteSpace(command.Mobile) ||
|
|
string.IsNullOrWhiteSpace(command.NationalCode)
|
|
|| string.IsNullOrWhiteSpace(command.Password))
|
|
return opreation.Failed("پر کردن تمامی فیلدها الزامی است");
|
|
if (_accountRepository.Exists(x => x.Username == command.Username))
|
|
return opreation.Failed("نام کاربری تکراری است");
|
|
if (_accountRepository.Exists(x => x.Mobile == command.Mobile && x.IsActiveString == "true"))
|
|
|
|
return opreation.Failed("مقادیر وارد شده تکراری است");
|
|
|
|
//var nationalCodeValidation = command.NationalCode.NationalCodeValid();
|
|
//switch (nationalCodeValidation)
|
|
//{
|
|
// case "incorrect":
|
|
// return opreation.Failed("اعداد وارد شده برای کد ملی صحیح نیست");
|
|
// break;
|
|
// case "invalid":
|
|
// return opreation.Failed("کد ملی وارد شده معتبر نیست");
|
|
// break;
|
|
// case "lessThan10":
|
|
// return opreation.Failed("کد ملی وارد شده کمتر از 10 رقم است");
|
|
// break;
|
|
//}
|
|
var password = _passwordHasher.Hash(command.Password);
|
|
var register = new Account(command.Fullname, command.Username, password, command.Mobile, command.NationalCode);
|
|
_accountRepository.Create(register);
|
|
_accountRepository.SaveChanges();
|
|
|
|
return opreation.Succcedded(register.id, message: "ثبت نام شما با موفقیت انجام شد");
|
|
}
|
|
|
|
public async Task<OperationResult> Edit(EditAccount command)
|
|
{
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.Get(command.Id);
|
|
if (account == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (_accountRepository.Exists(x =>
|
|
(x.Username == command.Username || x.Mobile == command.Mobile) && x.id != command.Id))
|
|
return operation.Failed(ApplicationMessages.DuplicatedRecord);
|
|
|
|
var roleName = _roleRepository.GetDetails(command.RoleId);
|
|
var path = $"profilePhotos";
|
|
var picturePath = _fileUploader.Upload(command.ProfilePhoto, path);
|
|
_unitOfWork.BeginAccountContext();
|
|
account.Edit(command.Fullname, command.Username, command.Mobile, command.RoleId, picturePath, roleName.Name);
|
|
_accountRepository.SaveChanges();
|
|
var key = SecretKeys.ProgramManagerInternalApi;
|
|
|
|
//var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
|
|
// $"api/user/{account.id}",
|
|
// key
|
|
//);
|
|
var userResult = _pmUserRepository.GetByPmUsertoEditbyAccountId(account.id).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
//اگر کاربر در پروگرام منیجر قبلا ایجاد شده
|
|
if (userResult != null)
|
|
{
|
|
if (!command.UserRoles.Any())
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("حداقل یک نقش باید انتخاب شود");
|
|
}
|
|
|
|
try
|
|
{
|
|
|
|
var userRoles = command.UserRoles.Where(x => x > 0).Select(x => new PmRoleUser(x)).ToList();
|
|
userResult.Edit(command.Fullname, command.Username, command.Mobile, userRoles, command.IsProgramManagerUser);
|
|
await _pmUserRepository.SaveChangesAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر");
|
|
}
|
|
//var parameters = new EditUserCommand(
|
|
// command.Fullname,
|
|
// command.Username,
|
|
// command.Mobile,
|
|
// account.id,
|
|
// command.UserRoles,
|
|
// command.IsProgramManagerUser
|
|
//);
|
|
//var url = "api/user/edit";
|
|
//var response = InternalApiCaller.PostAsync<EditUserCommand, ApiResponse>(
|
|
// url,
|
|
// key,
|
|
// parameters
|
|
//);
|
|
|
|
//if (!response.Success)
|
|
//{
|
|
// _unitOfWork.RollbackAccountContext();
|
|
// return operation.Failed(response.Error);
|
|
|
|
//}
|
|
|
|
//if (!response.Result.isSuccess)
|
|
//{
|
|
// _unitOfWork.RollbackAccountContext();
|
|
// return operation.Failed(response.Error);
|
|
//}
|
|
|
|
}
|
|
else //اگر کاربر قبلا ایجاد نشده
|
|
{
|
|
//اگر تیک فعالیت در پروگرام منیجر روشن بود
|
|
if (command.IsProgramManagerUser)
|
|
{
|
|
if (!command.UserRoles.Any())
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("حداقل یک نقش باید انتخاب شود");
|
|
}
|
|
|
|
if (_pmUserRepository.Exists(x => x.FullName == command.Fullname))
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("نام و خانوادگی تکراری است");
|
|
}
|
|
|
|
if (_pmUserRepository.Exists(x => x.UserName == command.Username))
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("نام کاربری تکراری است");
|
|
}
|
|
|
|
if (_pmUserRepository.Exists(x => !string.IsNullOrWhiteSpace(x.Mobile) && x.Mobile == command.Mobile))
|
|
{
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("این شماره همراه قبلا به فرد دیگری اختصاص داده شده است");
|
|
}
|
|
|
|
|
|
try
|
|
{
|
|
var userRoles = command.UserRoles.Where(x => x > 0).Select(x => new PmRoleUser(x)).ToList();
|
|
var create = new PmUser(command.Fullname, command.Username, account.Password, command.Mobile,
|
|
null, account.id, userRoles);
|
|
await _pmUserRepository.CreateAsync(create);
|
|
await _pmUserRepository.SaveChangesAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
|
|
_unitOfWork.RollbackAccountContext();
|
|
return operation.Failed("خطا در ویرایش کاربر پروگرام منیجر");
|
|
}
|
|
|
|
|
|
//var parameters = new CreateProgramManagerUser(
|
|
// command.Fullname,
|
|
// command.Username,
|
|
// account.Password,
|
|
// command.Mobile,
|
|
// command.Email,
|
|
// account.id,
|
|
// command.UserRoles
|
|
//);
|
|
|
|
//var url = "api/user/Create";
|
|
|
|
|
|
//var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
|
|
// url,
|
|
// key,
|
|
// parameters
|
|
//);
|
|
|
|
//if (!response.Success)
|
|
//{
|
|
// _unitOfWork.RollbackAccountContext();
|
|
// return operation.Failed(response.Error);
|
|
|
|
//}
|
|
|
|
//if (!response.Result.isSuccess)
|
|
//{
|
|
// _unitOfWork.RollbackAccountContext();
|
|
// return operation.Failed(response.Error);
|
|
//}
|
|
}
|
|
|
|
}
|
|
|
|
_unitOfWork.CommitAccountContext();
|
|
return operation.Succcedded();
|
|
}
|
|
|
|
public EditAccount GetDetails(long id)
|
|
{
|
|
return _accountRepository.GetDetails(id);
|
|
}
|
|
|
|
public OperationResult Login(Login command)
|
|
{
|
|
|
|
long idAutoriz = 0;
|
|
var operation = new OperationResult();
|
|
if (string.IsNullOrWhiteSpace(command.Password))
|
|
return operation.Failed(ApplicationMessages.EmptyPassword);
|
|
|
|
if (string.IsNullOrWhiteSpace(command.Username))
|
|
return operation.Failed(ApplicationMessages.EmptyUsername);
|
|
|
|
var account = _accountRepository.GetBy(command.Username);
|
|
var cameraAccount = _cameraAccountRepository.GetBy(command.Username);
|
|
SubAccount subAccount = _subAccountRepository.GetBy(command.Username);
|
|
if (account == null && cameraAccount == null && subAccount == null)
|
|
return operation.Failed(ApplicationMessages.WrongUserPass);
|
|
|
|
if (account != null)
|
|
{
|
|
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(account.Password, command.Password);
|
|
if (!result.Verified)
|
|
return operation.Failed(ApplicationMessages.WrongUserPass);
|
|
var permissions = _roleRepository.Get(account.RoleId)
|
|
.Permissions
|
|
.Select(x => x.Code)
|
|
.ToList();
|
|
int? positionValue;
|
|
if (account.PositionId != null)
|
|
{
|
|
positionValue = _positionRepository.Get((long)account.PositionId).PositionValue;
|
|
}
|
|
else
|
|
{
|
|
positionValue = null;
|
|
}
|
|
var pmUserId = _pmUserQueryService.GetCurrentPmUserIdFromAccountId(account.id).GetAwaiter().GetResult();
|
|
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
|
|
, account.Username, account.Mobile, account.ProfilePhoto,
|
|
permissions, account.RoleName, account.AdminAreaPermission,
|
|
account.ClientAriaPermission, positionValue,0,pmUserId);
|
|
|
|
if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false" &&
|
|
account.IsActiveString == "true")
|
|
{
|
|
var clientPermissions = _accountPermissionSubtitle1Repository.GetAllPermissionCodes();
|
|
authViewModel.Permissions = clientPermissions;
|
|
var workshopList = _workshopRepository.GetWorkshopsByClientAccountId(account.id).Select(x => new WorkshopClaim
|
|
{
|
|
PersonnelCount = x.PersonnelCount,
|
|
Id = x.Id,
|
|
Name = x.WorkshopFullName,
|
|
Slug = _passwordHasher.SlugHasher(x.Id)
|
|
}).OrderByDescending(x => x.PersonnelCount).ToList();
|
|
authViewModel.WorkshopList = workshopList;
|
|
if (workshopList.Any())
|
|
{
|
|
var workshop = workshopList.First();
|
|
authViewModel.WorkshopName = workshop.Name;
|
|
authViewModel.WorkshopSlug = _passwordHasher.SlugHasher(workshop.Id);
|
|
authViewModel.WorkshopId = workshop.Id;
|
|
}
|
|
}
|
|
|
|
_authHelper.Signin(authViewModel);
|
|
|
|
if ((account.AdminAreaPermission == "true" && account.ClientAriaPermission == "true" && account.IsActiveString == "true") || (account.AdminAreaPermission == "true" && account.ClientAriaPermission == "false" && account.IsActiveString == "true"))
|
|
idAutoriz = 1;
|
|
|
|
if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false" && account.IsActiveString == "true")
|
|
idAutoriz = 2;
|
|
}
|
|
|
|
if (cameraAccount != null)
|
|
{
|
|
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(cameraAccount.Password, command.Password);
|
|
if (!result.Verified)
|
|
return operation.Failed(ApplicationMessages.WrongUserPass);
|
|
|
|
var mobile = string.IsNullOrWhiteSpace(cameraAccount.Mobile) ? " " : cameraAccount.Mobile;
|
|
var authViewModel = new CameraAuthViewModel(cameraAccount.id, cameraAccount.WorkshopId,
|
|
cameraAccount.Username, mobile, cameraAccount.WorkshopName, cameraAccount.AccountId, cameraAccount.IsActiveSting);
|
|
if (cameraAccount.IsActiveSting == "true")
|
|
{
|
|
_authHelper.CameraSignIn(authViewModel);
|
|
idAutoriz = 3;
|
|
}
|
|
else
|
|
{
|
|
idAutoriz = 0;
|
|
}
|
|
|
|
}
|
|
|
|
if (subAccount != null)
|
|
{
|
|
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(subAccount.Password, command.Password);
|
|
if (!result.Verified)
|
|
return operation.Failed(ApplicationMessages.WrongUserPass);
|
|
var role = _subAccountRoleRepository.Get(subAccount.SubAccountRoleId);
|
|
|
|
var permissions = role.RolePermissions.Select(x => x.PermissionCode).ToList();
|
|
var authViewModel = new AuthViewModel(subAccount.AccountId, subAccount.SubAccountRoleId, subAccount.FullName
|
|
, subAccount.Username, subAccount.PhoneNumber, "", permissions, role.Title, "false",
|
|
"true", 0, subAccount.id);
|
|
var workshopList = _workshopSubAccountRepository.GetWorkshopsBySubAccountId(subAccount.id);
|
|
authViewModel.WorkshopList = workshopList.Select(x => new WorkshopClaim()
|
|
{
|
|
Slug = _passwordHasher.SlugHasher(x.WorkshopId),
|
|
Name = x.WorkshopName,
|
|
PersonnelCount = x.PersonnelCount,
|
|
Id = x.WorkshopId
|
|
}).ToList();
|
|
|
|
if (workshopList.Any())
|
|
{
|
|
var workshop = workshopList.First();
|
|
authViewModel.WorkshopName = workshop.WorkshopName;
|
|
authViewModel.WorkshopSlug = _passwordHasher.SlugHasher(workshop.WorkshopId);
|
|
authViewModel.WorkshopId = workshop.WorkshopId;
|
|
}
|
|
_authHelper.Signin(authViewModel);
|
|
idAutoriz = 2;
|
|
}
|
|
|
|
return operation.Succcedded(idAutoriz);
|
|
}
|
|
public OperationResult LoginWithMobile(long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.GetById(id);
|
|
if (account == null)
|
|
return operation.Failed(ApplicationMessages.WrongUserPass);
|
|
|
|
|
|
|
|
var permissions = _roleRepository.Get(account.RoleId)
|
|
.Permissions
|
|
.Select(x => x.Code)
|
|
.ToList();
|
|
int? positionValue;
|
|
if (account.PositionId != null)
|
|
{
|
|
positionValue = _positionRepository.Get((long)account.PositionId).PositionValue;
|
|
}
|
|
else
|
|
{
|
|
positionValue = null;
|
|
}
|
|
|
|
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
|
|
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, account.AdminAreaPermission, account.ClientAriaPermission, positionValue);
|
|
|
|
if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false" &&
|
|
account.IsActiveString == "true")
|
|
{
|
|
var clientPermissions = _accountPermissionSubtitle1Repository.GetAllPermissionCodes();
|
|
authViewModel.Permissions = clientPermissions;
|
|
var workshopList = _workshopRepository.GetWorkshopsByClientAccountId(account.id).Select(x => new WorkshopClaim
|
|
{
|
|
PersonnelCount = x.PersonnelCount,
|
|
Id = x.Id,
|
|
Name = x.WorkshopFullName,
|
|
Slug = _passwordHasher.SlugHasher(x.Id)
|
|
}).OrderByDescending(x => x.PersonnelCount).ToList();
|
|
authViewModel.WorkshopList = workshopList;
|
|
if (workshopList.Any())
|
|
{
|
|
var workshop = workshopList.First();
|
|
authViewModel.WorkshopName = workshop.Name;
|
|
authViewModel.WorkshopSlug = _passwordHasher.SlugHasher(workshop.Id);
|
|
authViewModel.WorkshopId = workshop.Id;
|
|
}
|
|
}
|
|
|
|
_authHelper.Signin(authViewModel);
|
|
long idAutoriz = 0;
|
|
if (account.AdminAreaPermission == "true" && account.ClientAriaPermission == "true" || account.AdminAreaPermission == "true" && account.ClientAriaPermission == "false")
|
|
idAutoriz = 1;
|
|
|
|
if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false")
|
|
idAutoriz = 2;
|
|
return operation.Succcedded(idAutoriz);
|
|
}
|
|
public void Logout()
|
|
{
|
|
_authHelper.SignOut();
|
|
}
|
|
|
|
public List<AccountViewModel> GetAccounts()
|
|
{
|
|
return _accountRepository.GetAccounts();
|
|
}
|
|
|
|
public List<AccountViewModel> GetClientsAccount()
|
|
{
|
|
return _accountRepository.GetClientsAccount();
|
|
}
|
|
|
|
public async Task<OperationResult> SendVerifyCodeToChangingPass(string phone, long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.Get(id);
|
|
if (account == null)
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
//var verifyCodeHash = _passwordHasher.Hash(verifyCode);
|
|
Random generator = new Random();
|
|
String r = generator.Next(1, 1000000).ToString("D6");
|
|
account.SetVerifyCode(r);
|
|
_accountRepository.SaveChanges();
|
|
_smsService.VerifySend(phone, r);
|
|
|
|
TimeSpan delay = TimeSpan.FromSeconds(130);
|
|
await Task.Delay(delay);
|
|
|
|
account.SetVerifyCode("");
|
|
_accountRepository.SaveChanges();
|
|
return operation.Succcedded();
|
|
}
|
|
public EditAccount GetByVerifyCode(string code, string phone)
|
|
{
|
|
return _accountRepository.GetByVerifyCode(code, phone);
|
|
}
|
|
|
|
public EditAccount GetByUserNameAndId(long id, string username)
|
|
{
|
|
return _accountRepository.GetByUserNameAndId(id, username);
|
|
}
|
|
|
|
public async Task<OperationResult> SetVerifyCode(string phone, long id)
|
|
{
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.Get(id);
|
|
if (account == null || account.IsActiveString == "false")
|
|
return operation.Failed(ApplicationMessages.RecordNotFound);
|
|
//var verifyCodeHash = _passwordHasher.Hash(verifyCode);
|
|
Random generator = new Random();
|
|
String r = generator.Next(1, 1000000).ToString("D6");
|
|
account.SetVerifyCode(r);
|
|
_accountRepository.SaveChanges();
|
|
_smsService.LoginSend(phone, r);
|
|
|
|
//TimeSpan delay = TimeSpan.FromSeconds(30);
|
|
|
|
await _accountRepository.RemoveCode(id);
|
|
|
|
return operation.Succcedded();
|
|
|
|
}
|
|
|
|
|
|
public List<AccountViewModel> Search(AccountSearchModel searchModel)
|
|
{
|
|
return _accountRepository.Search(searchModel);
|
|
}
|
|
|
|
public OperationResult Active(long id)
|
|
{
|
|
var opration = new OperationResult();
|
|
var acc = _accountRepository.Get(id);
|
|
if (acc == null)
|
|
return opration.Failed("رکورد مورد نظر یافت نشد");
|
|
|
|
acc.Active();
|
|
|
|
_accountRepository.SaveChanges();
|
|
return opration.Succcedded();
|
|
}
|
|
|
|
public OperationResult DeActive(long id)
|
|
{
|
|
var opration = new OperationResult();
|
|
var acc = _accountRepository.Get(id);
|
|
if (acc == null)
|
|
return opration.Failed("رکورد مورد نظر یافت نشد");
|
|
|
|
acc.DeActive();
|
|
|
|
|
|
_accountRepository.SaveChanges();
|
|
return opration.Succcedded();
|
|
}
|
|
|
|
public OperationResult DirectLogin(long id)
|
|
{
|
|
var prAcc = _authHelper.CurrentAccountInfo();
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.GetById(id);
|
|
if (account == null)
|
|
return operation.Failed("این اکانت وجود ندارد");
|
|
|
|
|
|
|
|
var permissions = _roleRepository.Get(account.RoleId)
|
|
.Permissions
|
|
.Select(x => x.Code)
|
|
.ToList();
|
|
|
|
|
|
_authHelper.SignOut();
|
|
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
|
|
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, "false", "true", null);
|
|
var workshopList = _workshopRepository.GetWorkshopsByClientAccountId(account.id).Select(x => new WorkshopClaim
|
|
{
|
|
PersonnelCount = x.PersonnelCount,
|
|
Id = x.Id,
|
|
Name = x.WorkshopFullName,
|
|
Slug = _passwordHasher.SlugHasher(x.Id)
|
|
}).OrderByDescending(x => x.PersonnelCount).ToList();
|
|
|
|
authViewModel.WorkshopList = workshopList;
|
|
|
|
var clientPermissions = _accountPermissionSubtitle1Repository.GetAllPermissionCodes();
|
|
authViewModel.Permissions = clientPermissions;
|
|
if (authViewModel.WorkshopList.Any())
|
|
{
|
|
var workshop = authViewModel.WorkshopList.First();
|
|
authViewModel.WorkshopSlug = _passwordHasher.SlugHasher(workshop.Id);
|
|
authViewModel.WorkshopName = workshop.Name;
|
|
authViewModel.WorkshopId = workshop.Id;
|
|
}
|
|
_authHelper.Signin(authViewModel);
|
|
return operation.Succcedded(2);
|
|
}
|
|
public OperationResult DirectCameraLogin(long cameraAccountId)
|
|
{
|
|
var prAcc = _authHelper.CurrentAccountInfo();
|
|
var operation = new OperationResult();
|
|
var cameraAccount = _cameraAccountRepository.GetById(cameraAccountId);
|
|
if (cameraAccount == null)
|
|
return operation.Failed("این اکانت وجود ندارد");
|
|
|
|
|
|
|
|
|
|
|
|
_authHelper.SignOut();
|
|
|
|
|
|
var mobile = string.IsNullOrWhiteSpace(cameraAccount.Mobile) ? " " : cameraAccount.Mobile;
|
|
var authViewModel = new CameraAuthViewModel(cameraAccount.id, cameraAccount.WorkshopId,
|
|
cameraAccount.Username, mobile, cameraAccount.WorkshopName, cameraAccount.AccountId, cameraAccount.IsActiveSting);
|
|
if (cameraAccount.IsActiveSting == "true")
|
|
{
|
|
_authHelper.CameraSignIn(authViewModel);
|
|
|
|
}
|
|
else
|
|
{
|
|
return operation.Failed("این اکانت غیر فعال شده است");
|
|
}
|
|
return operation.Succcedded(2);
|
|
}
|
|
|
|
|
|
public AccountLeftWorkViewModel WorkshopList(long accountId)
|
|
{
|
|
string fullname = this._accountRepository.GetById(accountId).Fullname;
|
|
List<WorkshopAccountlistViewModel> source = _accountLeftworkRepository.WorkshopList(accountId);
|
|
List<long> userWorkshopIds = source.Select(x => x.WorkshopId).ToList();
|
|
List<WorkshopSelectList> allWorkshops = this._accountLeftworkRepository.GetAllWorkshops();
|
|
List<AccountViewModel> accountSelectList = this._accountRepository.GetAdminAccountSelectList();
|
|
(string StartWorkFa, string LeftWorkFa) byAccountId = this._accountLeftworkRepository.GetByAccountId(accountId);
|
|
return new AccountLeftWorkViewModel()
|
|
{
|
|
AccountId = accountId,
|
|
AccountFullName = fullname,
|
|
StartDateFa = byAccountId.StartWorkFa,
|
|
LeftDateFa = byAccountId.LeftWorkFa,
|
|
WorkshopAccountlist = source,
|
|
WorkshopSelectList = new SelectList(allWorkshops.Where(x => !userWorkshopIds.Contains(x.Id)), "Id", "WorkshopFullName"),
|
|
AccountSelectList = new SelectList(accountSelectList, "Id", "Fullname")
|
|
};
|
|
}
|
|
|
|
public OperationResult SaveWorkshopAccount(
|
|
List<WorkshopAccountlistViewModel> workshopAccountList,
|
|
string startDate,
|
|
string leftDate,
|
|
long accountId)
|
|
{
|
|
return this._accountLeftworkRepository.SaveWorkshopAccount(workshopAccountList, startDate, leftDate, accountId);
|
|
}
|
|
public OperationResult CreateNewWorkshopAccount(long currentAccountId, long newAccountId)
|
|
{
|
|
return this._accountLeftworkRepository.CopyWorkshopToNewAccount(currentAccountId, newAccountId);
|
|
}
|
|
#region Mahan
|
|
|
|
public List<AccountViewModel> AccountsForAssign(long taskId)
|
|
{
|
|
return _accountRepository.AccountsForAssign(taskId);
|
|
}
|
|
|
|
public List<AccountViewModel> GetAccountsByPositionId(long positionId)
|
|
{
|
|
if (!_positionRepository.Exists(x => x.id == positionId))
|
|
{
|
|
return new List<AccountViewModel>();
|
|
}
|
|
return _accountRepository.GetAccountsByPositionId(positionId);
|
|
}
|
|
|
|
public List<AccountViewModel> GetAccountEqualToLowerPositionValue()
|
|
{
|
|
return _accountRepository.GetAccountEqualToLowerPositionValue();
|
|
}
|
|
|
|
public OperationResult ReLogin()
|
|
{
|
|
var prAcc = _authHelper.CurrentAccountInfo();
|
|
var operation = new OperationResult();
|
|
var account = _accountRepository.GetIncludePositions(prAcc.Id);
|
|
if (account == null)
|
|
return operation.Failed("این اکانت وجود ندارد");
|
|
|
|
|
|
|
|
var permissions = _roleRepository.Get(account.RoleId)
|
|
.Permissions
|
|
.Select(x => x.Code)
|
|
.ToList();
|
|
|
|
|
|
_authHelper.SignOut();
|
|
var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
|
|
, account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, account.AdminAreaPermission, account.ClientAriaPermission, account.Position.PositionValue);
|
|
_authHelper.Signin(authViewModel);
|
|
return operation.Succcedded(2);
|
|
|
|
}
|
|
|
|
public async Task<List<AccountSelectListViewModel>> GetAdminSelectList()
|
|
{
|
|
return await _accountRepository.GetAdminSelectList();
|
|
}
|
|
|
|
#endregion
|
|
#region Pooya
|
|
public OperationResult IsPhoneNumberAndPasswordValid(long accountId, string phoneNumber, string password, string rePassword)
|
|
{
|
|
OperationResult op = new();
|
|
|
|
var entity = _accountRepository.Get(accountId);
|
|
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
|
|
if (!string.IsNullOrWhiteSpace(rePassword) || !string.IsNullOrWhiteSpace(password))
|
|
{
|
|
if (rePassword != password)
|
|
return op.Failed("تکرار رمز عبور با رمز عبور مطابقت ندارد");
|
|
|
|
if (password.Length < 8)
|
|
return op.Failed("رمز عبور نمی تواند کمتر از 8 کاراکتر باشد");
|
|
}
|
|
|
|
if ((string.IsNullOrWhiteSpace(phoneNumber) || entity.Mobile == phoneNumber) && string.IsNullOrWhiteSpace(rePassword))
|
|
return op.Failed("چیزی برای تغییر وجود ندارد");
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(phoneNumber) && entity.Mobile != phoneNumber)
|
|
{
|
|
phoneNumber = phoneNumber.Trim();
|
|
if (phoneNumber.Length != 11)
|
|
return op.Failed("شماره تلفن همراه به درستی وارد نشده است");
|
|
if (_accountRepository.Exists(x => x.Mobile == phoneNumber && x.id != accountId) ||
|
|
_subAccountRepository.Exists(x => x.PhoneNumber == phoneNumber) ||
|
|
_cameraAccountRepository.Exists(x => x.Mobile == phoneNumber))
|
|
return op.Failed("قبلا یک حساب با این شماره ثبت شده است");
|
|
}
|
|
|
|
|
|
return op.Succcedded();
|
|
}
|
|
|
|
public OperationResult ChangePasswordAndPhoneNumber(AccountChangePasswordAndPhoneNumber command)
|
|
{
|
|
OperationResult op = new();
|
|
command.PhoneNumber = command.PhoneNumber.Trim();
|
|
var entity = _accountRepository.Get(command.AccountId);
|
|
if (entity == null)
|
|
return op.Failed(ApplicationMessages.RecordNotFound);
|
|
var validationResult = IsPhoneNumberAndPasswordValid(command.AccountId, command.PhoneNumber, command.Password, command.RePassword);
|
|
if (validationResult.IsSuccedded == false)
|
|
return validationResult;
|
|
|
|
if (!string.IsNullOrWhiteSpace(command.RePassword))
|
|
{
|
|
|
|
entity.ChangePassword(_passwordHasher.Hash(command.Password));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(command.PhoneNumber))
|
|
{
|
|
entity.Edit(entity.Fullname, entity.Username, command.PhoneNumber, entity.RoleId, entity.ProfilePhoto, entity.RoleName);
|
|
}
|
|
_accountRepository.SaveChanges();
|
|
return op.Succcedded();
|
|
}
|
|
//public UserClaimsResponseDTO GetClaimsForSignIn(Login command)
|
|
//{
|
|
// var operation = new OperationResult();
|
|
// var claimsResponse = new UserClaimsResponseDTO() { UserType = UserType.Anonymous };
|
|
|
|
// if (string.IsNullOrWhiteSpace(command.Password))
|
|
// return claimsResponse.Failed(ApplicationMessages.EmptyPassword);
|
|
|
|
// if (string.IsNullOrWhiteSpace(command.Username))
|
|
// return claimsResponse.Failed(ApplicationMessages.EmptyUsername);
|
|
|
|
|
|
// var account = _accountRepository.GetBy(command.Username);
|
|
// var cameraAccount = _cameraAccountRepository.GetBy(command.Username);
|
|
// if (account == null && cameraAccount == null)
|
|
// return claimsResponse.Failed(ApplicationMessages.WrongUserPass);
|
|
|
|
// if (account != null)
|
|
// {
|
|
// (bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(account.Password, command.Password);
|
|
// if (!result.Verified)
|
|
// return claimsResponse.Failed(ApplicationMessages.WrongUserPass);
|
|
// var permissions = _roleRepository.Get(account.RoleId)
|
|
// .Permissions
|
|
// .Select(x => x.Code)
|
|
// .ToList();
|
|
// int? positionValue;
|
|
// if (account.PositionId != null)
|
|
// {
|
|
// positionValue = _positionRepository.Get((long)account.PositionId).PositionValue;
|
|
// }
|
|
// else
|
|
// {
|
|
// positionValue = null;
|
|
// }
|
|
// var authViewModel = new AuthViewModel(account.id, account.RoleId, account.Fullname
|
|
// , account.Username, account.Mobile, account.ProfilePhoto, permissions, account.RoleName, account.AdminAreaPermission, account.ClientAriaPermission, positionValue);
|
|
|
|
// if (account.ClientAriaPermission == "true" && account.AdminAreaPermission == "false" &&
|
|
// account.IsActiveString == "true")
|
|
// {
|
|
// var workshopList = _workshopRepository.SearchForClient(new WorkshopSearchModel() { AccountId = account.id })
|
|
// .OrderByDescending(x => x.PersonnelCount).ToList().Select(x => new WorkshopClaim()
|
|
// {
|
|
// Slug = _passwordHasher.SlugHasher(x.Id),
|
|
// Name = x.WorkshopFullName,
|
|
// PersonnelCount = x.PersonnelCount,
|
|
// Id = x.Id
|
|
// }
|
|
// ).ToList();
|
|
// authViewModel.WorkshopList = workshopList;
|
|
// if (workshopList.Any())
|
|
// authViewModel.WorkshopSlug = _passwordHasher.SlugHasher(workshopList.First().Id);
|
|
// ClaimsIdentity claims = _authHelper.GetClaimsIdentityForSignIn(authViewModel);
|
|
// var encryptedClaim = Tools.SerializeToBson(claims);
|
|
// return claimsResponse.Succeeded(UserType.Client, encryptedClaim);
|
|
|
|
// }
|
|
|
|
|
|
// if ((account.AdminAreaPermission == "true" && account.ClientAriaPermission == "true" &&
|
|
// account.IsActiveString == "true") || (account.AdminAreaPermission == "true" &&
|
|
// account.ClientAriaPermission == "false" &&
|
|
// account.IsActiveString == "true"))
|
|
// {
|
|
// ClaimsIdentity claims = _authHelper.GetClaimsIdentityForSignIn(authViewModel);
|
|
// var encryptedClaim = Tools.SerializeToBson(claims);
|
|
// return claimsResponse.Succeeded(UserType.Admin, encryptedClaim);
|
|
// }
|
|
|
|
// }
|
|
|
|
// if (cameraAccount != null)
|
|
// {
|
|
// (bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(cameraAccount.Password, command.Password);
|
|
// if (!result.Verified)
|
|
// return claimsResponse.Failed(ApplicationMessages.WrongUserPass);
|
|
|
|
// var mobile = string.IsNullOrWhiteSpace(cameraAccount.Mobile) ? " " : cameraAccount.Mobile;
|
|
// var authViewModel = new CameraAuthViewModel(cameraAccount.id, cameraAccount.WorkshopId,
|
|
// cameraAccount.Username, mobile, cameraAccount.WorkshopName, cameraAccount.AccountId, cameraAccount.IsActiveSting);
|
|
// if (cameraAccount.IsActiveSting == "true")
|
|
// {
|
|
// var claims = _authHelper.GetCameraClaimsIdentityForSignIn(authViewModel);
|
|
// var serializedClaims = Tools.SerializeToBson(claims);
|
|
// return claimsResponse.Succeeded(UserType.Admin, serializedClaims);
|
|
|
|
// }
|
|
// }
|
|
|
|
// return claimsResponse.Failed(ApplicationMessages.WrongUserPass);
|
|
//}
|
|
#endregion
|
|
|
|
|
|
public bool CheckExistClientAccount(string userName)
|
|
{
|
|
return _accountRepository.CheckExistClientAccount(userName);
|
|
}
|
|
|
|
public List<AccountViewModel> GetAdminAccountsNew()
|
|
{
|
|
return _accountRepository.GetAdminAccountsNew();
|
|
}
|
|
|
|
public void CameraLogin(CameraLoginRequest request)
|
|
{
|
|
var cameraAccount = _cameraAccountRepository.GetBy(request.UserName);
|
|
|
|
if (cameraAccount == null)
|
|
{
|
|
throw new BadRequestException(ApplicationMessages.WrongUserPass);
|
|
}
|
|
|
|
(bool Verified, bool NeedUpgrade) result = _passwordHasher.Check(cameraAccount.Password, request.Password);
|
|
|
|
if (!result.Verified)
|
|
throw new BadRequestException(ApplicationMessages.WrongUserPass);
|
|
|
|
var mobile = string.IsNullOrWhiteSpace(cameraAccount.Mobile) ? " " : cameraAccount.Mobile;
|
|
|
|
var authViewModel = new CameraAuthViewModel(cameraAccount.id, cameraAccount.WorkshopId,
|
|
cameraAccount.Username, mobile, cameraAccount.WorkshopName, cameraAccount.AccountId,
|
|
cameraAccount.IsActiveSting);
|
|
if (cameraAccount.IsActiveSting != "true")
|
|
throw new BadRequestException(ApplicationMessages.WrongUserPass);
|
|
|
|
_authHelper.CameraSignIn(authViewModel);
|
|
}
|
|
|
|
public async Task<GetPmUserDto> GetPmUserByAccountId(long accountId)
|
|
{
|
|
return await _pmUserRepository.GetPmUserByAccountId(accountId);
|
|
}
|
|
} |