Add ProgramManager Context

This commit is contained in:
SamSys
2025-12-07 15:21:53 +03:30
parent fb97d3453d
commit acd96bcdc7
22 changed files with 781 additions and 406 deletions

View File

@@ -2,15 +2,21 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using AccountManagement.Application.Contracts.ProgramManager;
namespace AccountManagement.Application.Contracts.Account;
public interface IAccountApplication
{
AccountViewModel GetAccountBy(long id);
OperationResult Create(CreateAccount command);
/// <summary>
/// ایجاد کاربر گزارشگیر و پروگرام منیجر
/// </summary>
/// <param name="command"></param>
/// <returns></returns>
Task<OperationResult> Create(CreateAccount command);
OperationResult RegisterClient(RegisterAccount command);
OperationResult Edit(EditAccount command);
Task<OperationResult> Edit(EditAccount command);
OperationResult EditClient(EditClientAccount command);
OperationResult ChangePassword(ChangePassword command);
OperationResult Login(Login command);
@@ -67,6 +73,13 @@ public interface IAccountApplication
List<AccountViewModel> GetAdminAccountsNew();
void CameraLogin(CameraLoginRequest request);
/// <summary>
/// دریافت کاربر پروگرام منیجر با اکانت آی دی
/// </summary>
/// <param name="accountId"></param>
/// <returns></returns>
Task<GetPmUserDto> GetPmUserByAccountId(long accountId);
}
public class CameraLoginRequest

View File

@@ -0,0 +1,56 @@
using System.Collections.Generic;
namespace AccountManagement.Application.Contracts.ProgramManager;
public class GetPmUserDto
{
/// <summary>
/// نام و نام خانوادگی
/// </summary>
public string FullName { get; set; }
/// <summary>
/// نام کاربری
/// </summary>
public string UserName { get; set; }
/// <summary>
/// مسیر عکس پروفایل
/// </summary>
public string ProfilePhotoPath { get; set; }
/// <summary>
/// شماره موبایل
/// </summary>
public string Mobile { get; set; }
/// <summary>
/// فعال/غیر فعال بودن یوزر
/// </summary>
public bool IsActive { get; set; }
/// <summary>
/// آی دی کاربر در گزارشگیر
/// </summary>
public long? AccountId { get; set; }
/// <summary>
/// نقش ها
/// </summary>
public List<long> Roles { get; set; }
public List<RoleListDto> RoleListDto { get; set; }
}
public record RoleListDto
{
public string RoleName { get; set; }
public long RoleId { get; set; }
public List<int> Permissions { get; set; }
}

View File

@@ -0,0 +1,28 @@
using System.Collections.Generic;
namespace AccountManagement.Application.Contracts.ProgramManager;
public record GetPmRolesDto
{
/// <summary>
/// آی دی نقش
/// </summary>
public long Id { get; set; }
/// <summary>
/// نام نقش
/// </summary>
public string RoleName { get; set; }
/// <summary>
/// آی دی نقش در گزارشگیر
/// </summary>
public long? GozareshgirRoleId { get; set; }
/// <summary>
/// لیست کدهای دسترسی
/// </summary>
public List<int> Permissions { get; set; }
}

View File

@@ -1,13 +1,25 @@
using _0_Framework.Application;
using AccountManagement.Application.Contracts.ProgramManager;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace AccountManagement.Application.Contracts.Role
{
public interface IRoleApplication
{
OperationResult Create(CreateRole command);
OperationResult Edit(EditRole command);
Task<OperationResult> Create(CreateRole command);
Task<OperationResult> Edit(EditRole command);
List<RoleViewModel> List();
EditRole GetDetails(long id);
#region ProgramManager
Task<SelectList> GetPmRoleList(long? gozareshgirRoleId);
Task<List<GetPmRolesDto>> GetPmRoleListToEdit(long? gozareshgirRoleId);
#endregion
}
}

View File

@@ -1,36 +1,40 @@
using System;
using System.Collections;
using _0_Framework.Application;
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.Threading;
using System.Threading.Tasks;
using _0_Framework.Application.Sms;
using AccountManagement.Domain.AccountLeftWorkAgg;
using AccountManagement.Domain.CameraAccountAgg;
using AccountManagement.Domain.RoleAgg;
using CompanyManagment.App.Contracts.Workshop;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Rendering;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
using Company.Domain.WorkshopAgg;
using System.Security.Claims;
using System.Text;
using _0_Framework.Exceptions;
using AccountManagement.Domain.PositionAgg;
using AccountManagement.Domain.SubAccountAgg;
using AccountManagement.Domain.SubAccountPermissionSubtitle1Agg;
using AccountManagement.Domain.SubAccountRoleAgg;
using Company.Domain.WorkshopSubAccountAgg;
using Newtonsoft.Json;
using Microsoft.EntityFrameworkCore;
using Company.Domain._common;
using AccountManagement.Domain.InternalApiCaller;
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
using System.Threading;
using System.Threading.Tasks;
using AccountManagement.Application.Contracts.ProgramManager;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
//using AccountManagement.Domain.RoleAgg;
@@ -52,10 +56,11 @@ public class AccountApplication : IAccountApplication
private readonly ISubAccountRoleRepository _subAccountRoleRepository;
private readonly IWorkshopSubAccountRepository _workshopSubAccountRepository;
private readonly ISubAccountPermissionSubtitle1Repository _accountPermissionSubtitle1Repository;
private readonly IPmUserRepository _pmUserRepository;
private readonly IUnitOfWork _unitOfWork;
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)
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)
{
_authHelper = authHelper;
_roleRepository = roleRepository;
@@ -69,6 +74,7 @@ public class AccountApplication : IAccountApplication
_workshopSubAccountRepository = workshopSubAccountRepository;
_accountPermissionSubtitle1Repository = accountPermissionSubtitle1Repository;
_unitOfWork = unitOfWork;
_pmUserRepository = pmUserRepository;
_fileUploader = fileUploader;
_passwordHasher = passwordHasher;
_accountRepository = accountRepository;
@@ -132,7 +138,7 @@ public class AccountApplication : IAccountApplication
};
}
public OperationResult Create(CreateAccount command)
public async Task<OperationResult> Create(CreateAccount command)
{
var operation = new OperationResult();
@@ -159,36 +165,61 @@ public class AccountApplication : IAccountApplication
if (command.IsProgramManagerUser)
{
var parameters = new CreateProgramManagerUser(
command.Fullname,
command.Username,
password,
command.Mobile,
command.Email,
account.id,
command.UserRoles
);
var url = "api/user/create";
var key = SecretKeys.ProgramManagerInternalApi;
var response = InternalApiCaller.PostAsync<CreateProgramManagerUser, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
try
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
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("خطا در ایجاد کاربر پروگرام منیجر");
}
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Result.errorMessage);
}
//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();
@@ -230,7 +261,7 @@ public class AccountApplication : IAccountApplication
return opreation.Succcedded(register.id, message: "ثبت نام شما با موفقیت انجام شد");
}
public OperationResult Edit(EditAccount command)
public async Task<OperationResult> Edit(EditAccount command)
{
var operation = new OperationResult();
var account = _accountRepository.Get(command.Id);
@@ -249,47 +280,62 @@ public class AccountApplication : IAccountApplication
_accountRepository.SaveChanges();
var key = SecretKeys.ProgramManagerInternalApi;
var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
$"api/user/{account.id}",
key
);
//var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
// $"api/user/{account.id}",
// key
//);
var userResult = _pmUserRepository.GetByPmUsertoEditbyAccountId(account.id).GetAwaiter().GetResult();
//اگر کاربر در پروگرام منیجر قبلا ایجاد شده
if (apiResult.Success && apiResult.Result.Data.accountId == account.id)
if (userResult != null)
{
if (!command.UserRoles.Any())
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("حداقل یک نقش باید انتخاب شود");
}
try
{
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)
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(response.Error);
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.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
}
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Error);
//}
}
else //اگر کاربر قبلا ایجاد نشده
@@ -298,38 +344,77 @@ public class AccountApplication : IAccountApplication
if (command.IsProgramManagerUser)
{
if (!command.UserRoles.Any())
{
_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)
if (_pmUserRepository.Exists(x => x.FullName == command.Fullname))
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Error);
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);
//}
}
}
@@ -968,4 +1053,9 @@ public class AccountApplication : IAccountApplication
_authHelper.CameraSignIn(authViewModel);
}
public async Task<GetPmUserDto> GetPmUserByAccountId(long accountId)
{
return await _pmUserRepository.GetPmUserByAccountId(accountId);
}
}

View File

@@ -3,25 +3,36 @@ using AccountManagement.Application.Contracts.Role;
using AccountManagement.Domain.RoleAgg;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AccountManagement.Application.Contracts.ProgramManager;
using AccountManagement.Application.Contracts.ProgramManagerApiResult;
using AccountManagement.Domain.InternalApiCaller;
using Company.Domain._common;
using AccountManagement.Application.Contracts.Ticket;
using AccountManagement.Domain.PmDomains.PmPermissionAgg;
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using AccountManagement.Domain.PmDomains.PmUserAgg;
using Microsoft.AspNetCore.Mvc.Rendering;
using Role = AccountManagement.Domain.RoleAgg.Role;
namespace AccountManagement.Application;
public class RoleApplication : IRoleApplication
{
private readonly IRoleRepository _roleRepository;
private readonly IPmRoleRepository _pmRoleRepository;
private readonly IPmUserRepository _pmUserRepository;
private readonly IUnitOfWork _unitOfWork;
public RoleApplication(IRoleRepository roleRepository, IUnitOfWork unitOfWork)
public RoleApplication(IRoleRepository roleRepository, IUnitOfWork unitOfWork, IPmRoleRepository pmRoleRepository, IPmUserRepository pmUserRepository)
{
_roleRepository = roleRepository;
_unitOfWork = unitOfWork;
_pmRoleRepository = pmRoleRepository;
_pmUserRepository = pmUserRepository;
}
public OperationResult Create(CreateRole command)
public async Task<OperationResult> Create(CreateRole command)
{
var operation = new OperationResult();
if (_roleRepository.Exists(x => x.Name == command.Name))
@@ -36,35 +47,49 @@ public class RoleApplication : IRoleApplication
var pmPermissions = command.PmPermissions.Where(x => x > 0).ToList();
if (pmPermissions.Any())
{
var parameters = new CreateProgramManagerRole
try
{
RoleName = command.Name,
Permissions = pmPermissions,
GozareshgirRoleId = role.id
var pmPermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList();
var pmRole = new PmRole(command.Name, role.id, pmPermissionsData);
await _pmRoleRepository.CreateAsync(pmRole);
await _pmRoleRepository.SaveChangesAsync();
};
var url = "api/role";
var key = SecretKeys.ProgramManagerInternalApi;
var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
}
catch (System.Exception)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
}
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Result.errorMessage);
}
//var parameters = new CreateProgramManagerRole
//{
// RoleName = command.Name,
// Permissions = pmPermissions,
// GozareshgirRoleId = role.id
//};
//var url = "api/role";
//var key = SecretKeys.ProgramManagerInternalApi;
//var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
// url,
// key,
// parameters
//);
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Result.errorMessage);
//}
}
@@ -74,7 +99,7 @@ public class RoleApplication : IRoleApplication
return operation.Succcedded();
}
public OperationResult Edit(EditRole command)
public async Task<OperationResult> Edit(EditRole command)
{
var operation = new OperationResult();
var role = _roleRepository.Get(command.Id);
@@ -89,7 +114,7 @@ public class RoleApplication : IRoleApplication
var permissions = command.Permissions.Where(x => x > 0).Select(x => new Permission(x)).ToList();
_unitOfWork.BeginAccountContext();
role.Edit(command.Name, permissions);
@@ -99,117 +124,120 @@ public class RoleApplication : IRoleApplication
//یافتن نقش در پروگرام منیجر
var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
"api/role",
key,
new Dictionary<string, object>
{
{ "RoleName", "" },
//var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
{ "GozareshgirRoleId", command.Id}
}
);
// { "GozareshgirRoleId", command.Id}
// }
//);
var pmRoleResult = await _pmRoleRepository.GetPmRoleToEdit(command.Id);
if (apiResult.Success)
//اگر این نقش در پروگرام منیجر وجود داشت ویرایش کن
if (pmRoleResult != null)
{
if (apiResult.Result.isSuccess)
try
{
//اگر این نقش در پروگرام منیجر وجود داشت ویرایش کن
if (apiResult.Result.data.role.Any())
{
var parameters = new CreateProgramManagerRole
{
RoleName = command.Name,
Permissions = pmPermissions,
GozareshgirRoleId = role.id
};
var url = "api/role/edit";
var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
}
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Result.errorMessage);
}
}
else //اگر نقش در پروگرام منیجر وجود نداشت
{
//اگر تیک پرمیشن های پروگرام منیجر زده شده
//این نقش را سمت پروگرام منیجر بساز
if (pmPermissions.Any())
{
var parameters = new CreateProgramManagerRole
{
RoleName = command.Name,
Permissions = pmPermissions,
GozareshgirRoleId = role.id
};
var url = "api/role";
var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
url,
key,
parameters
);
if (!response.Success)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
}
if (!response.Result.isSuccess)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed(response.Result.errorMessage);
}
}
}
var pmpermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList();
pmRoleResult.Edit(command.Name, pmpermissionsData);
await _pmRoleRepository.SaveChangesAsync();
}
else
catch (System.Exception)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
}
//var parameters = new CreateProgramManagerRole
//{
// RoleName = command.Name,
// Permissions = pmPermissions,
// GozareshgirRoleId = role.id
//};
//var url = "api/role/edit";
//var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
// url,
// key,
// parameters
//);
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Result.errorMessage);
//}
}
else
else //اگر نقش در پروگرام منیجر وجود نداشت
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
//اگر تیک پرمیشن های پروگرام منیجر زده شده
//این نقش را سمت پروگرام منیجر بساز
if (pmPermissions.Any())
{
try
{
var pmPermissionsData = pmPermissions.Where(x => x > 0).Select(x => new PmPermission(x)).ToList();
var pmRole = new PmRole(command.Name, command.Id, pmPermissionsData);
await _pmRoleRepository.CreateAsync(pmRole);
await _pmRoleRepository.SaveChangesAsync();
}
catch (System.Exception)
{
_unitOfWork.RollbackAccountContext();
return operation.Failed("خطا در ویرایش دسترسی ها در پروگرام منیجر");
}
//var parameters = new CreateProgramManagerRole
//{
// RoleName = command.Name,
// Permissions = pmPermissions,
// GozareshgirRoleId = role.id
//};
//var url = "api/role";
//var response = InternalApiCaller.PostAsync<CreateProgramManagerRole, ApiResponse>(
// url,
// key,
// parameters
//);
//if (!response.Success)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed("ارتباط با اپلیکیش پروگرام منیجر برقرار نشد");
//}
//if (!response.Result.isSuccess)
//{
// _unitOfWork.RollbackAccountContext();
// return operation.Failed(response.Result.errorMessage);
//}
}
}
_unitOfWork.CommitAccountContext();
return operation.Succcedded();
}
@@ -222,4 +250,23 @@ public class RoleApplication : IRoleApplication
{
return _roleRepository.List();
}
public async Task<SelectList> GetPmRoleList(long? gozareshgirRoleId)
{
var rolse = await _pmRoleRepository.GetPmRoleList(gozareshgirRoleId);
return new SelectList(rolse, "Id", "RoleName");
}
public async Task<List<GetPmRolesDto>> GetPmRoleListToEdit(long? gozareshgirRoleId)
{
return await _pmRoleRepository.GetPmRoleList(gozareshgirRoleId);
}
}

View File

@@ -0,0 +1,18 @@
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using AccountManagement.Domain.PmDomains.PmUserAgg;
using AccountMangement.Infrastructure.EFCore.PmDbConetxt;
using AccountMangement.Infrastructure.EFCore.Repository.PmRepositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace AccountManagement.Configuration;
public class PmDbBootstrapper
{
public static void Configure(IServiceCollection services, string connectionString)
{
services.AddTransient<IPmRoleRepository, PmRoleRepository>();
services.AddTransient<IPmUserRepository, PmUserRepository>();
services.AddDbContext<PmDbContext>(x => x.UseSqlServer(connectionString));
}
}

View File

@@ -7,13 +7,13 @@ using AccountManagement.Domain.PmDomains.PmRoleAgg;
namespace AccountManagement.Domain.PmDomains.PmPermissionAgg;
public class Permission
public class PmPermission
{
public long Id { get; private set; }
public int Code { get; private set; }
public Role Role { get; private set; }
public PmRole Role { get; private set; }
public Permission(int code)
public PmPermission(int code)
{
Code = code;
}

View File

@@ -0,0 +1,15 @@
using _0_Framework.Domain;
using System.Collections.Generic;
using System.Threading.Tasks;
using AccountManagement.Application.Contracts.ProgramManager;
namespace AccountManagement.Domain.PmDomains.PmRoleAgg;
public interface IPmRoleRepository :IRepository<long, PmRole>
{
Task<List<GetPmRolesDto>> GetPmRoleList(long? gozareshgirRoleId);
Task<PmRole?> GetPmRoleToEdit(long gozareshgirRoleId);
}

View File

@@ -4,7 +4,7 @@ using AccountManagement.Domain.PmDomains.PmPermissionAgg;
namespace AccountManagement.Domain.PmDomains.PmRoleAgg;
public class Role : EntityBase
public class PmRole : EntityBase
{
/// <summary>
/// نام نقش
@@ -15,7 +15,7 @@ public class Role : EntityBase
/// <summary>
/// لیست پرمیشن کد ها
/// </summary>
public List<Permission> Permissions { get; private set; }
public List<PmPermission> PmPermission { get; private set; }
/// <summary>
/// ای دی نقش در گزارشگیر
@@ -23,23 +23,23 @@ public class Role : EntityBase
public long? GozareshgirRoleId { get; private set; }
protected Role()
protected PmRole()
{
}
public Role(string roleName,long? gozareshgirRolId, List<Permission> permissions)
public PmRole(string roleName,long? gozareshgirRolId, List<PmPermission> permissions)
{
RoleName = roleName;
Permissions = permissions;
PmPermission = permissions;
GozareshgirRoleId = gozareshgirRolId;
}
public void Edit(string roleName, List<Permission> permissions)
public void Edit(string roleName, List<PmPermission> permissions)
{
RoleName = roleName;
Permissions = permissions;
PmPermission = permissions;
}

View File

@@ -2,9 +2,9 @@
namespace AccountManagement.Domain.PmDomains.PmRoleUserAgg;
public class RoleUser
public class PmRoleUser
{
public RoleUser(long roleId)
public PmRoleUser(long roleId)
{
RoleId = roleId;
}
@@ -13,7 +13,7 @@ public class RoleUser
public long RoleId { get; private set; }
public User User { get; set; }
public PmUser User { get; set; }
}

View File

@@ -0,0 +1,21 @@
using _0_Framework.Domain;
using AccountManagement.Application.Contracts.ProgramManager;
using System.Threading.Tasks;
namespace AccountManagement.Domain.PmDomains.PmUserAgg;
public interface IPmUserRepository : IRepository<long, PmUser>
{
/// <summary>
/// دریافت کاربر پروگرام منیجر جهتد ویرایش
/// </summary>
/// <param name="accountId"></param>
/// <returns></returns>
Task<PmUser?> GetByPmUsertoEditbyAccountId(long accountId);
/// <summary>
/// دریافت کرابر پروگرام منیجر با اکانت آی دی در گزارشگیر
/// </summary>
/// <param name="accountId"></param>
/// <returns></returns>
Task<GetPmUserDto> GetPmUserByAccountId(long accountId);
}

View File

@@ -9,7 +9,7 @@ namespace AccountManagement.Domain.PmDomains.PmUserAgg;
/// <summary>
/// کاربر
/// </summary>
public class User : EntityBase
public class PmUser : EntityBase
{
/// <summary>
/// ایجاد
@@ -21,7 +21,7 @@ public class User : EntityBase
/// <param name="email"></param>
/// <param name="accountId"></param>
/// <param name="roles"></param>
public User(string fullName, string userName, string password, string mobile, string email, long? accountId, List<RoleUser> roles)
public PmUser(string fullName, string userName, string password, string mobile, string email, long? accountId, List<PmRoleUser> roles)
{
FullName = fullName;
UserName = userName;
@@ -33,7 +33,7 @@ public class User : EntityBase
RoleUser = roles;
}
protected User()
protected PmUser()
{
}
@@ -87,7 +87,7 @@ public class User : EntityBase
/// <summary>
/// لیست پرمیشن کد ها
/// </summary>
public List<RoleUser> RoleUser { get; private set; }
public List<PmRoleUser> RoleUser { get; private set; }
/// <summary>
@@ -98,7 +98,7 @@ public class User : EntityBase
/// <param name="mobile"></param>
/// <param name="roles"></param>
/// <param name="isActive"></param>
public void Edit(string fullName, string userName, string mobile, List<RoleUser> roles, bool isActive)
public void Edit(string fullName, string userName, string mobile, List<PmRoleUser> roles, bool isActive)
{
FullName = fullName;
UserName = userName;

View File

@@ -0,0 +1,24 @@
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace AccountMangement.Infrastructure.EFCore.Mappings.PmMappings;
public class PmRoleMapping : IEntityTypeConfiguration<PmRole>
{
public void Configure(EntityTypeBuilder<PmRole> builder)
{
builder.ToTable("PmRoles", t => t.ExcludeFromMigrations());
builder.HasKey(x => x.id);
builder.Property(x => x.RoleName).HasMaxLength(100).IsRequired();
builder.OwnsMany(x => x.PmPermission, navigationBuilder =>
{
navigationBuilder.HasKey(x => x.Id);
navigationBuilder.ToTable("PmRolePermissions", t => t.ExcludeFromMigrations());
navigationBuilder.WithOwner(x => x.Role);
});
}
}

View File

@@ -1,15 +1,15 @@
using GozareshgirProgramManager.Domain.UserAgg.Entities;
using AccountManagement.Domain.PmDomains.PmUserAgg;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace GozareshgirProgramManager.Infrastructure.Persistence.Mappings;
namespace AccountMangement.Infrastructure.EFCore.Mappings.PmMappings;
public class UserMapping :IEntityTypeConfiguration<User>
public class PmUserMapping :IEntityTypeConfiguration<PmUser>
{
public void Configure(EntityTypeBuilder<User> builder)
public void Configure(EntityTypeBuilder<PmUser> builder)
{
builder.ToTable("Users");
builder.HasKey(x => x.Id);
builder.HasKey(x => x.id);
builder.Property(x => x.FullName).HasMaxLength(100).IsRequired();
builder.Property(x => x.UserName).HasMaxLength(100).IsRequired();
@@ -24,11 +24,7 @@ public class UserMapping :IEntityTypeConfiguration<User>
navigationBuilder.ToTable("RoleUsers");
navigationBuilder.WithOwner(x => x.User);
});
builder.HasMany(x=>x.RefreshTokens)
.WithOne(x=>x.User)
.HasForeignKey(x=>x.UserId)
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@@ -0,0 +1,32 @@
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using AccountManagement.Domain.PmDomains.PmUserAgg;
using AccountMangement.Infrastructure.EFCore.Mappings;
using AccountMangement.Infrastructure.EFCore.Mappings.PmMappings;
using Microsoft.EntityFrameworkCore;
namespace AccountMangement.Infrastructure.EFCore.PmDbConetxt;
public class PmDbContext : DbContext
{
public PmDbContext(DbContextOptions<PmDbContext> options) : base(options)
{
}
public DbSet<PmUser> Users { get; set; } = null!;
public DbSet<PmRole> PmRoles { get; set; } = null!;
public PmDbContext()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var assembly = typeof(PmUserMapping).Assembly;
modelBuilder.ApplyConfigurationsFromAssembly(assembly);
//SubAccountPermissionSeeder.Seed(modelBuilder);
base.OnModelCreating(modelBuilder);
}
}

View File

@@ -0,0 +1,49 @@
using _0_Framework.InfraStructure;
using AccountManagement.Application.Contracts.ProgramManager;
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using AccountMangement.Infrastructure.EFCore.PmDbConetxt;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
namespace AccountMangement.Infrastructure.EFCore.Repository.PmRepositories;
public class PmRoleRepository : RepositoryBase<long, PmRole>, IPmRoleRepository
{
private readonly PmDbContext _pmDbContext;
public PmRoleRepository(PmDbContext context) : base(context)
{
_pmDbContext = context;
}
public async Task<List<GetPmRolesDto>> GetPmRoleList(long? gozareshgirRoleId)
{
var query = _pmDbContext.PmRoles.AsQueryable();
if (gozareshgirRoleId != null && gozareshgirRoleId > 0)
query = query.Where(x => x.GozareshgirRoleId == gozareshgirRoleId);
var res = await query
.Select(p => new GetPmRolesDto()
{
Id = p.id,
RoleName = p.RoleName,
GozareshgirRoleId = p.GozareshgirRoleId,
Permissions = p.PmPermission.Select(x => x.Code).ToList()
})
.ToListAsync();
return res;
}
public async Task<PmRole?> GetPmRoleToEdit(long gozareshgirRoleId)
{
return await _pmDbContext.PmRoles.FirstOrDefaultAsync(x => x.GozareshgirRoleId == gozareshgirRoleId);
}
}

View File

@@ -0,0 +1,47 @@
using _0_Framework.InfraStructure;
using AccountManagement.Application.Contracts.ProgramManager;
using AccountManagement.Domain.PmDomains.PmUserAgg;
using AccountMangement.Infrastructure.EFCore.PmDbConetxt;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace AccountMangement.Infrastructure.EFCore.Repository.PmRepositories;
public class PmUserRepository :RepositoryBase<long, PmUser>, IPmUserRepository
{
private readonly PmDbContext _pmDbContext;
public PmUserRepository(PmDbContext context, PmDbContext pmDbContext) : base(context)
{
_pmDbContext = pmDbContext;
}
public async Task<PmUser?> GetByPmUsertoEditbyAccountId(long accountId)
{
return await _pmDbContext.Users.FirstOrDefaultAsync(x => x.AccountId == accountId);
}
public async Task<GetPmUserDto> GetPmUserByAccountId(long accountId)
{
var query = await _pmDbContext.Users.FirstOrDefaultAsync(x => x.AccountId == accountId);
if (query == null)
return new GetPmUserDto();
List<long> roles = query.RoleUser.Select(x => x.RoleId).ToList();
return new GetPmUserDto()
{
FullName = query.FullName,
UserName = query.UserName,
ProfilePhotoPath = query.ProfilePhotoPath,
Mobile = query.Mobile,
IsActive = query.IsActive,
AccountId = query.AccountId,
Roles = roles,
RoleListDto = await _pmDbContext.PmRoles.Where(x => roles.Contains(x.id)).Select(x => new RoleListDto()
{
RoleName = x.RoleName,
RoleId = x.id,
Permissions = x.PmPermission.Select(x => x.Code).ToList()
}).ToListAsync(),
};
}
}

View File

@@ -1,23 +0,0 @@
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace CompanyManagment.EFCore.Mapping.PmMappings;
public class RoleMapping : IEntityTypeConfiguration<Role>
{
public void Configure(EntityTypeBuilder<Role> builder)
{
builder.ToTable("Roles");
builder.HasKey(x => x.id);
builder.Property(x => x.RoleName).HasMaxLength(100).IsRequired();
builder.OwnsMany(x => x.Permissions, navigationBuilder =>
{
navigationBuilder.HasKey(x => x.Id);
navigationBuilder.ToTable("RolePermissions");
navigationBuilder.WithOwner(x => x.Role);
});
}
}

View File

@@ -1,30 +0,0 @@
using Microsoft.EntityFrameworkCore;
using System;
using AccountManagement.Domain.PmDomains.PmRoleAgg;
using AccountManagement.Domain.PmDomains.PmUserAgg;
namespace CompanyManagment.EFCore;
public class PmDbContext : DbContext
{
public PmDbContext(DbContextOptions<PmDbContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; } = null!;
public DbSet<Role> Roles { get; set; } = null!;
public PmDbContext()
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfigurationsFromAssembly(typeof(PmDbContext).Assembly);
base.OnModelCreating(modelBuilder);
}
}

View File

@@ -1,18 +0,0 @@
using Company.Domain.InsuranceJobItemAgg;
using Company.Domain.InsurancJobAgg;
using CompanyManagment.App.Contracts.InsuranceJob;
using CompanyManagment.Application;
using CompanyManagment.EFCore;
using CompanyManagment.EFCore.Repository;
using Microsoft.Extensions.DependencyInjection;
namespace PersonalContractingParty.Config;
public class PmDbBootstrapper
{
public static void Configure(IServiceCollection services, string connectionString)
{
services.AddDbContext<TestDbContext>(x => x.UseSqlServer(connectionString));
}
}

View File

@@ -55,41 +55,42 @@ public class IndexModel : PageModel
public IActionResult OnGetCreate()
{
{
var pmRolseSelectList = _roleApplication.GetPmRoleList(null).GetAwaiter().GetResult();
var command = new CreateAccount
{
Roles = _roleApplication.List()
};
var key = SecretKeys.ProgramManagerInternalApi;
var response = InternalApiCaller.GetAsync<RoleResponse>(
"api/role",
key,
new Dictionary<string, object>
{
{ "RoleName", "" },
Roles = _roleApplication.List(),
RoleList = pmRolseSelectList,
};
//var key = SecretKeys.ProgramManagerInternalApi;
//var response = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
{ "GozareshgirRoleId", "" }
}
);
// { "GozareshgirRoleId", "" }
// }
//);
if (response.Success)
{
if (response.Result.isSuccess)
{
command.RoleList = new SelectList(response.Result.data.role, "id", "roleName");
//if (response.Success)
//{
// if (response.Result.isSuccess)
// {
// command.RoleList = new SelectList(response.Result.data.role, "id", "roleName");
}
// }
}
//}
return Partial("./Create", command);
}
public IActionResult OnPostCreate(CreateAccount command)
{
var result = _accountApplication.Create(command);
var result = _accountApplication.Create(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
@@ -102,7 +103,7 @@ public class IndexModel : PageModel
public IActionResult OnPostCreateRole(CreateRole command)
{
var result = _roleApplication.Create(command);
var result = _roleApplication.Create(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
@@ -112,55 +113,51 @@ public class IndexModel : PageModel
var account = _accountApplication.GetDetails(id);
//var key = SecretKeys.ProgramManagerInternalApi;
//var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
// $"api/user/{account.Id}",
// key
//);
var result = _accountApplication.GetPmUserByAccountId(account.Id).GetAwaiter().GetResult();
var key = SecretKeys.ProgramManagerInternalApi;
var apiResult = InternalApiCaller.GetAsync<SingleUserResponseResult>(
$"api/user/{account.Id}",
key
);
// حالا نتیجه دیسریال شده در apiResult.Result قرار دارد
var result = apiResult.Result;
// مثل قبل:
if (result != null && result.isSuccess)
if (result != null)
{
account.IsProgramManagerUser = (result.Data.accountId == account.Id && result.Data.isActive);
account.UserRoles = apiResult.Result.Data.Roles;
account.IsProgramManagerUser = (result.AccountId== account.Id && result.IsActive);
account.UserRoles = result.Roles;
}
else
{
account.IsProgramManagerUser = false;
}
var pmRolseSelectList = _roleApplication.GetPmRoleList(null).GetAwaiter().GetResult();
account.RoleList = pmRolseSelectList;
//var response = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
var response = InternalApiCaller.GetAsync<RoleResponse>(
"api/role",
key,
new Dictionary<string, object>
{
{ "RoleName", "" },
// { "GozareshgirRoleId", "" }
// }
//);
{ "GozareshgirRoleId", "" }
}
);
//if (response.Success)
//{
// if (response.Result.isSuccess)
// {
// account.RoleList = new SelectList(response.Result.data.role, "id", "roleName");
if (response.Success)
{
if (response.Result.isSuccess)
{
account.RoleList = new SelectList(response.Result.data.role, "id", "roleName");
}
// }
}
//}
@@ -172,7 +169,7 @@ public class IndexModel : PageModel
public JsonResult OnPostEdit(EditAccount command)
{
var result = _accountApplication.Edit(command);
var result = _accountApplication.Edit(command).GetAwaiter().GetResult();
return new JsonResult(result);
}
@@ -182,26 +179,27 @@ public class IndexModel : PageModel
var rol = new List<int>();
foreach (var item in role.MappedPermissions) rol.Add(item.Code);
var key = SecretKeys.ProgramManagerInternalApi;
//var key = SecretKeys.ProgramManagerInternalApi;
var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
"api/role",
key,
new Dictionary<string, object>
{
{ "RoleName", "" },
//var apiResult = InternalApiCaller.GetAsync<RoleResponse>(
// "api/role",
// key,
// new Dictionary<string, object>
// {
// { "RoleName", "" },
{ "GozareshgirRoleId", role.Id }
}
);
// { "GozareshgirRoleId", role.Id }
// }
//);
var pmRoleResult = _roleApplication.GetPmRoleListToEdit(role.Id).GetAwaiter().GetResult();
role.Permissions = rol;
if (apiResult.Success)
if (pmRoleResult != null)
{
if (apiResult.Result.isSuccess && apiResult.Result.data.role.Any())
if (pmRoleResult.Any())
{
var pmPermission = apiResult.Result.data.role.FirstOrDefault()!.permissions;
var pmPermission = pmRoleResult.FirstOrDefault()!.Permissions;
role.Permissions.AddRange(pmPermission);
}
@@ -213,7 +211,7 @@ public class IndexModel : PageModel
public JsonResult OnPostEditRole(EditRole command)
{
var result = _roleApplication.Edit(command);
var result = _roleApplication.Edit(command).GetAwaiter().GetResult();
return new JsonResult(result);
}