|
@@ -1,25 +1,16 @@
|
|
|
-using Hotline.Application.Contracts.Configurations;
|
|
|
-using Hotline.Caches;
|
|
|
+using Hotline.Caches;
|
|
|
using Hotline.CallCenter.Tels;
|
|
|
+using Hotline.Identity.Accounts;
|
|
|
using Hotline.Permissions;
|
|
|
using Hotline.Share.Dtos.User;
|
|
|
using Hotline.Users;
|
|
|
-using Identity.Admin.HttpClient;
|
|
|
-using Identity.Shared.Dtos;
|
|
|
-using Identity.Shared.Dtos.Account;
|
|
|
-using Identity.Shared.Dtos.Identity;
|
|
|
-using Identity.Shared.Dtos.Role;
|
|
|
using MapsterMapper;
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
using XF.Domain.Authentications;
|
|
|
using XF.Domain.Exceptions;
|
|
|
-using Microsoft.Extensions.Options;
|
|
|
using XF.Utility.AppIdentityModel;
|
|
|
-using XF.Utility.UnifyResponse;
|
|
|
-using Hotline.Settings;
|
|
|
-using Microsoft.AspNetCore.Mvc.Formatters;
|
|
|
-using Microsoft.AspNetCore.Authorization;
|
|
|
using Hotline.Share.Dtos;
|
|
|
+using Hotline.Share.Dtos.Role;
|
|
|
|
|
|
namespace Hotline.Api.Controllers;
|
|
|
|
|
@@ -34,10 +25,9 @@ public class UserController : BaseController
|
|
|
private readonly IUserRepository _userRepository;
|
|
|
private readonly ITelCacheManager _telCacheManager;
|
|
|
private readonly IUserCacheManager _userCacheManager;
|
|
|
- private readonly IIdentityClient _identityClient;
|
|
|
- private readonly IOptionsSnapshot<IdentityConfigs> _identityConfigs;
|
|
|
private readonly IMapper _mapper;
|
|
|
- private readonly IOrgUserRepository _orgUserRepository;
|
|
|
+ private readonly IAccountRepository _accountRepository;
|
|
|
+ private readonly IAccountDomainService _accountDomainService;
|
|
|
|
|
|
public UserController(
|
|
|
ISessionContext sessionContext,
|
|
@@ -46,10 +36,9 @@ public class UserController : BaseController
|
|
|
IUserRepository userRepository,
|
|
|
ITelCacheManager telCacheManager,
|
|
|
IUserCacheManager userCacheManager,
|
|
|
- IIdentityClient identityClient,
|
|
|
- IOptionsSnapshot<IdentityConfigs> identityConfigs,
|
|
|
IMapper mapper,
|
|
|
- IOrgUserRepository orgUserRepository)
|
|
|
+ IAccountRepository accountRepository,
|
|
|
+ IAccountDomainService accountDomainService)
|
|
|
{
|
|
|
_sessionContext = sessionContext;
|
|
|
_userDomainService = userDomainService;
|
|
@@ -57,10 +46,9 @@ public class UserController : BaseController
|
|
|
_userRepository = userRepository;
|
|
|
_telCacheManager = telCacheManager;
|
|
|
_userCacheManager = userCacheManager;
|
|
|
- _identityClient = identityClient;
|
|
|
- _identityConfigs = identityConfigs;
|
|
|
_mapper = mapper;
|
|
|
- _orgUserRepository = orgUserRepository;
|
|
|
+ _accountRepository = accountRepository;
|
|
|
+ _accountDomainService = accountDomainService;
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -116,92 +104,57 @@ public class UserController : BaseController
|
|
|
/// <summary>
|
|
|
/// 更新用户
|
|
|
/// </summary>
|
|
|
- /// <param name="userDto"></param>
|
|
|
+ /// <param name="dto"></param>
|
|
|
/// <returns></returns>
|
|
|
[Permission(EPermission.UpdateUser)]
|
|
|
[HttpPut]
|
|
|
- public async Task Update([FromBody] UpdateUserDto userDto)
|
|
|
+ public async Task Update([FromBody] UpdateUserDto dto)
|
|
|
{
|
|
|
- var user = await _userRepository.GetAsync(userDto.Id, HttpContext.RequestAborted);
|
|
|
- if (user is null || user.IsDeleted)
|
|
|
- throw UserFriendlyException.SameMessage("无效用户编号");
|
|
|
- if (await IsAccountLock(user.Id))
|
|
|
+ var account = await _accountRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
|
|
|
+ if (account is null)
|
|
|
+ throw UserFriendlyException.SameMessage("该账号不存在");
|
|
|
+ if (_accountDomainService.IsLockedOut(account))
|
|
|
throw UserFriendlyException.SameMessage("该账号已被锁定");
|
|
|
|
|
|
- _mapper.Map(userDto, user);
|
|
|
+ var user = await _userRepository.GetAsync(dto.Id, HttpContext.RequestAborted);
|
|
|
+ if (user is null)
|
|
|
+ throw UserFriendlyException.SameMessage("无效用户编号");
|
|
|
+
|
|
|
+ _mapper.Map(dto, user);
|
|
|
await _userRepository.UpdateAsync(user, HttpContext.RequestAborted);
|
|
|
- //查询用户组织架构
|
|
|
- var orgUser = await _orgUserRepository.GetAsync(x => x.UserId == user.Id);
|
|
|
- if (orgUser is null)
|
|
|
- {
|
|
|
- //新增
|
|
|
- if (!string.IsNullOrEmpty(userDto.OrgId) && !string.IsNullOrEmpty(userDto.OrgCode))
|
|
|
- {
|
|
|
- await _orgUserRepository.AddAsync(new OrgUser() { OrgId = userDto.OrgId, OrgCode = userDto.OrgCode, UserId = user.Id });
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //修改
|
|
|
- if (!string.IsNullOrEmpty(userDto.OrgId) && !string.IsNullOrEmpty(userDto.OrgCode))
|
|
|
- {
|
|
|
- orgUser.OrgId = orgUser.Id;
|
|
|
- orgUser.OrgCode = orgUser.OrgCode;
|
|
|
- await _orgUserRepository.UpdateAsync(orgUser, HttpContext.RequestAborted);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //删除
|
|
|
- await _orgUserRepository.RemoveAsync(orgUser.Id, false, HttpContext.RequestAborted);
|
|
|
- }
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 新增用户
|
|
|
/// </summary>
|
|
|
- /// <param name="userDto"></param>
|
|
|
+ /// <param name="dto"></param>
|
|
|
/// <returns></returns>
|
|
|
[Permission(EPermission.AddUser)]
|
|
|
[HttpPost]
|
|
|
- public async Task<string> Add([FromBody] AddUserDto userDto)
|
|
|
+ public async Task<string> Add([FromBody] AddUserDto dto)
|
|
|
{
|
|
|
- var getAccountRsp = await _identityClient.GetUserAsync(userDto.UserName, HttpContext.RequestAborted);
|
|
|
- CheckHttpRequestSuccess(getAccountRsp, "GetUserAsync");
|
|
|
- var account = getAccountRsp.Result;
|
|
|
+ var account = await _accountRepository.GetAsync(d => d.UserName == dto.UserName, HttpContext.RequestAborted);
|
|
|
if (account is null)
|
|
|
{
|
|
|
- var identityConfigs = _identityConfigs.Value;
|
|
|
+ account = _mapper.Map<Account>(dto);
|
|
|
+ await _accountRepository.AddAsync(account, HttpContext.RequestAborted);
|
|
|
+ var user = _mapper.Map<User>(dto);
|
|
|
+ user.Id = account.Id;
|
|
|
+ await _userRepository.AddAsync(user, HttpContext.RequestAborted);
|
|
|
|
|
|
- var addAccountRsp = await _identityClient.AddUserAsync(new IdentityUserDto
|
|
|
- {
|
|
|
- ClientId = identityConfigs.ClientId,
|
|
|
- UserName = userDto.UserName,
|
|
|
- Email = $"{userDto.UserName}@fw.com",
|
|
|
- DislayName = userDto.Name ?? userDto.UserName
|
|
|
- }, HttpContext.RequestAborted);
|
|
|
- if (addAccountRsp is null || !addAccountRsp.IsSuccess)
|
|
|
- throw new UserFriendlyException("identity service insert fail: AddUserAsync", "新增用户失败!");
|
|
|
-
|
|
|
- var user = _mapper.Map<User>(userDto);
|
|
|
- user.Id = addAccountRsp.Result;
|
|
|
- string userid = await _userRepository.AddAsync(user, HttpContext.RequestAborted);
|
|
|
- //如果有组织架构就新增一条数据
|
|
|
- if (!string.IsNullOrEmpty(userDto.OrgId) && !string.IsNullOrEmpty(userDto.OrgCode))
|
|
|
- {
|
|
|
- await _orgUserRepository.AddAsync(new OrgUser() { OrgId = userDto.OrgId, OrgCode = userDto.OrgCode, UserId = userid });
|
|
|
- }
|
|
|
- return userid;
|
|
|
+ //initial pwd
|
|
|
+ await _accountDomainService.InitialPasswordAsync(account, HttpContext.RequestAborted);
|
|
|
+ return account.Id;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if (await IsAccountLock(account.Id))
|
|
|
+ if (_accountDomainService.IsLockedOut(account))
|
|
|
throw UserFriendlyException.SameMessage("该账号已被锁定,请联系管理员");
|
|
|
|
|
|
var user = await _userRepository.GetAsync(account.Id, HttpContext.RequestAborted);
|
|
|
if (user is null)
|
|
|
{
|
|
|
- user = _mapper.Map<User>(userDto);
|
|
|
+ user = _mapper.Map<User>(dto);
|
|
|
user.Id = account.Id;
|
|
|
return await _userRepository.AddAsync(user, HttpContext.RequestAborted);
|
|
|
}
|
|
@@ -209,6 +162,7 @@ public class UserController : BaseController
|
|
|
if (user.IsDeleted)
|
|
|
{
|
|
|
user.Recover();
|
|
|
+ _mapper.Map(dto, user);
|
|
|
await _userRepository.UpdateAsync(user);
|
|
|
return user.Id;
|
|
|
}
|
|
@@ -230,9 +184,10 @@ public class UserController : BaseController
|
|
|
if (work is not null)
|
|
|
throw UserFriendlyException.SameMessage("该用户正在工作中,请下班以后再删除");
|
|
|
|
|
|
- var response = await _identityClient.LockUserAsync(new UserLockDto(id), HttpContext.RequestAborted);
|
|
|
- CheckHttpRequestSuccess(response, "LockUserAsync");
|
|
|
-
|
|
|
+ var account = await _accountRepository.GetAsync(id, HttpContext.RequestAborted);
|
|
|
+ if (account is null)
|
|
|
+ throw UserFriendlyException.SameMessage("该账号不存在");
|
|
|
+ await _accountDomainService.LockOutAsync(account, cancellationToken: HttpContext.RequestAborted);
|
|
|
await _userRepository.RemoveAsync(id, true, HttpContext.RequestAborted);
|
|
|
}
|
|
|
|
|
@@ -240,7 +195,6 @@ public class UserController : BaseController
|
|
|
/// 查询用户当前状态
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
- //[AllowAnonymous]
|
|
|
[HttpGet("state")]
|
|
|
public async Task<UserStateDto> GetUserState()
|
|
|
{
|
|
@@ -259,18 +213,19 @@ public class UserController : BaseController
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 分页查询用户角色
|
|
|
+ /// 查询用户角色
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
[Permission(EPermission.GetUserRoles)]
|
|
|
- [HttpGet("roles")]
|
|
|
- public async Task<PagedDto<IdentityRoleDto>> GetUserRoles([FromQuery] UserRolesPagedDto dto)
|
|
|
+ [HttpGet("{id}/roles")]
|
|
|
+ public async Task<IReadOnlyList<RoleDto>> GetUserRoles(string id)
|
|
|
{
|
|
|
- var pageDto = _mapper.Map<PageDto>(dto);
|
|
|
- var getUserRolesRsp = await _identityClient.GetUserRolesAsync(dto.UserId, pageDto, HttpContext.RequestAborted);
|
|
|
- CheckHttpRequestSuccess(getUserRolesRsp, "GetUserRolesAsync");
|
|
|
- var result = getUserRolesRsp.Result;
|
|
|
- return new PagedDto<IdentityRoleDto>(result.TotalCount, result.Roles);
|
|
|
+ var account = await _accountRepository.Queryable()
|
|
|
+ .Includes(d => d.Roles)
|
|
|
+ .FirstAsync(d => d.Id == id);
|
|
|
+ if (account == null)
|
|
|
+ throw UserFriendlyException.SameMessage("无效账号编号");
|
|
|
+ return _mapper.Map<IReadOnlyList<RoleDto>>(account.Roles);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -281,8 +236,7 @@ public class UserController : BaseController
|
|
|
[HttpPost("roles")]
|
|
|
public async Task SetUserRoles([FromBody] SetUserRolesDto dto)
|
|
|
{
|
|
|
- var setUserRolesRsp = await _identityClient.SetUserRolesAsync(dto, HttpContext.RequestAborted);
|
|
|
- CheckHttpRequestSuccess(setUserRolesRsp, "SetUserRolesAsync");
|
|
|
+ await _accountRepository.SetAccountRolesAsync(dto.UserId, dto.RoleIds, HttpContext.RequestAborted);
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -307,10 +261,13 @@ public class UserController : BaseController
|
|
|
[HttpPost("change-pwd")]
|
|
|
public async Task ChangePassword([FromBody] ChangePasswordDto dto)
|
|
|
{
|
|
|
- var changepwdDto = _mapper.Map<UserChangePasswordDto>(dto);
|
|
|
- changepwdDto.UserId = _sessionContext.RequiredUserId;
|
|
|
- var changepwdRsp = await _identityClient.ChangePasswordAsync(changepwdDto, HttpContext.RequestAborted);
|
|
|
- CheckHttpRequestSuccess(changepwdRsp, "ChangePasswordAsync");
|
|
|
+ var account = await _accountRepository.GetAsync(_sessionContext.RequiredUserId, HttpContext.RequestAborted);
|
|
|
+ if (account == null)
|
|
|
+ throw UserFriendlyException.SameMessage("无效账号编号");
|
|
|
+ var result = await _accountDomainService.ResetPasswordAsync(account, dto.CurrentPassword, dto.NewPassword,
|
|
|
+ HttpContext.RequestAborted);
|
|
|
+ if (!result.Succeeded)
|
|
|
+ throw new UserFriendlyException(string.Join(',', result.Errors.Select(d => d.Description).ToList()));
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -320,26 +277,17 @@ public class UserController : BaseController
|
|
|
[HttpPost("initial-pwd/{userId}")]
|
|
|
public async Task InitialPassword(string userId)
|
|
|
{
|
|
|
- var initpwdRsp = await _identityClient.InitialPasswordAsync(userId, HttpContext.RequestAborted);
|
|
|
- CheckHttpRequestSuccess(initpwdRsp, "InitialPasswordAsync");
|
|
|
+ var account = await _accountRepository.GetAsync(_sessionContext.RequiredUserId, HttpContext.RequestAborted);
|
|
|
+ if (account == null)
|
|
|
+ throw UserFriendlyException.SameMessage("无效账号编号");
|
|
|
+ await _accountDomainService.InitialPasswordAsync(account, HttpContext.RequestAborted);
|
|
|
}
|
|
|
|
|
|
- #region private
|
|
|
-
|
|
|
- private async Task<bool> IsAccountLock(string userId)
|
|
|
+ [HttpGet]
|
|
|
+ public async Task<IReadOnlyList<UserDto>> Query([FromQuery]IReadOnlyList<string> ids)
|
|
|
{
|
|
|
- var response = await _identityClient.IsAccountLockAsync(userId, HttpContext.RequestAborted);
|
|
|
- if (response is null || !response.IsSuccess)
|
|
|
- throw new UserFriendlyException("identity service request fail: IsAccountLockAsync");
|
|
|
- return response.Result;
|
|
|
+ var users = await _userRepository.Queryable().ToListAsync(d => ids.Contains(d.Id));
|
|
|
+ return _mapper.Map<IReadOnlyList<UserDto>>(users);
|
|
|
}
|
|
|
|
|
|
- private void CheckHttpRequestSuccess(ApiResponse response, string msg)
|
|
|
- {
|
|
|
- if (response == null || !response.IsSuccess)
|
|
|
- throw new UserFriendlyException($"identity service request failed: {msg}");
|
|
|
- }
|
|
|
-
|
|
|
- #endregion
|
|
|
-
|
|
|
}
|