Squashed commit of the following:

commit ae30d4b2cb
Author: 陈淳 <454313500@qq.com>
Date:   Fri Apr 26 19:08:18 2024 +0800

    fix: 修复值对象查询导致问题,已同步sqlsugar更新

commit 4c12626b44
Author: 陈淳 <454313500@qq.com>
Date:   Mon Apr 22 18:15:57 2024 +0800

    feat: 添加值对象

commit d389dcbedf
Author: 陈淳 <454313500@qq.com>
Date:   Mon Apr 22 18:06:09 2024 +0800

    feat: 添加值对象

commit 58ff8f45cf
Author: 陈淳 <454313500@qq.com>
Date:   Mon Apr 22 15:54:25 2024 +0800

    feat: 去除新增的缓存操作

commit 826271c84d
Author: 陈淳 <454313500@qq.com>
Date:   Mon Apr 22 15:39:41 2024 +0800

    feat: 添加缓存crud
This commit is contained in:
陈淳
2024-04-26 19:09:11 +08:00
parent 3a158c8249
commit 777aa64153
9 changed files with 236 additions and 60 deletions

View File

@@ -1,5 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Yi.Framework.Ddd.Application;
using Yi.Framework.Rbac.Application.Contracts.Dtos.DictionaryType;
using Yi.Framework.Rbac.Application.Contracts.IServices;
@@ -36,6 +38,5 @@ namespace Yi.Framework.Rbac.Application.Services
Items = await MapToGetListOutputDtosAsync(entities)
};
}
}
}

View File

@@ -3,6 +3,7 @@ using SqlSugar;
using TencentCloud.Tcr.V20190924.Models;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Caching;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Users;
using Yi.Framework.Ddd.Application;
@@ -12,6 +13,7 @@ using Yi.Framework.Rbac.Domain.Authorization;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Managers;
using Yi.Framework.Rbac.Domain.Repositories;
using Yi.Framework.Rbac.Domain.Shared.Caches;
using Yi.Framework.Rbac.Domain.Shared.Consts;
using Yi.Framework.Rbac.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Shared.OperLog;
@@ -25,10 +27,11 @@ namespace Yi.Framework.Rbac.Application.Services.System
public class UserService : YiCrudAppService<UserEntity, UserGetOutputDto, UserGetListOutputDto, Guid, UserGetListInputVo, UserCreateInputVo, UserUpdateInputVo>,IUserService
//IUserService
{
public UserService(ISqlSugarRepository<UserEntity, Guid> repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus) : base(repository)
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
public UserService(ISqlSugarRepository<UserEntity, Guid> repository, UserManager userManager, IUserRepository userRepository, ICurrentUser currentUser, IDeptService deptService, ILocalEventBus localEventBus, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache) : base(repository)
=>
(_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus) =
(userManager, userRepository, currentUser, deptService, repository, localEventBus);
(_userManager, _userRepository, _currentUser, _deptService, _repository, _localEventBus, _userCache) =
(userManager, userRepository, currentUser, deptService, repository, localEventBus, userCache);
private UserManager _userManager { get; set; }
private ISqlSugarRepository<UserEntity, Guid> _repository;
private IUserRepository _userRepository { get; set; }
@@ -77,6 +80,14 @@ namespace Yi.Framework.Rbac.Application.Services.System
return result;
}
protected override UserEntity MapToEntity(UserCreateInputVo createInput)
{
var output= base.MapToEntity(createInput);
output.EncryPassword = new Domain.Entities.ValueObjects.EncryPasswordValueObject(createInput.Password);
return output;
}
/// <summary>
/// 添加用户
/// </summary>
@@ -99,13 +110,13 @@ namespace Yi.Framework.Rbac.Application.Services.System
{
throw new UserFriendlyException(UserConst.User_Exist);
}
var entities = await MapToEntityAsync(input);
entities.BuildPassword();
var entitiy = await MapToEntityAsync(input);
entitiy.BuildPassword();
//using (var uow = _unitOfWorkManager.CreateContext())
//{
var returnEntity = await _repository.InsertReturnEntityAsync(entities);
var returnEntity = await _repository.InsertReturnEntityAsync(entitiy);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { returnEntity.Id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { returnEntity.Id }, input.PostIds);
//uow.Commit();
@@ -152,17 +163,17 @@ namespace Yi.Framework.Rbac.Application.Services.System
//更新密码,特殊处理
if (input.Password is not null)
{
entity.Password = input.Password;
entity.EncryPassword.Password = input.Password;
entity.BuildPassword();
}
await MapToEntityAsync(input, entity);
//using (var uow = _unitOfWorkManager.CreateContext())
//{
var res1 = await _repository.UpdateAsync(entity);
await _userManager.GiveUserSetRoleAsync(new List<Guid> { id }, input.RoleIds);
await _userManager.GiveUserSetPostAsync(new List<Guid> { id }, input.PostIds);
// uow.Commit();
//}
await _userCache.RefreshAsync(new UserInfoCacheKey(_currentUser.GetId()));
return await MapToGetOutputDtoAsync(entity);
}
@@ -179,6 +190,7 @@ namespace Yi.Framework.Rbac.Application.Services.System
await _repository.UpdateAsync(entity);
var dto = await MapToGetOutputDtoAsync(entity);
await _userCache.RefreshAsync(new UserInfoCacheKey(_currentUser.GetId()));
return dto;
}
@@ -200,13 +212,16 @@ namespace Yi.Framework.Rbac.Application.Services.System
}
entity.State = state;
await _repository.UpdateAsync(entity);
await _userCache.RefreshAsync(new UserInfoCacheKey(id));
return await MapToGetOutputDtoAsync(entity);
}
[OperLog("删除用户", OperEnum.Delete)]
[Permission("system:user:delete")]
public override async Task DeleteAsync(Guid id)
{
await base.DeleteAsync(id);
await _userCache.RefreshAsync(new UserInfoCacheKey(id));
}
[Permission("system:user:export")]

View File

@@ -4,6 +4,7 @@ using Volo.Abp.Auditing;
using Volo.Abp.Domain.Entities;
using Yi.Framework.Core.Data;
using Yi.Framework.Core.Helper;
using Yi.Framework.Rbac.Domain.Entities.ValueObjects;
using Yi.Framework.Rbac.Domain.Shared.Enums;
namespace Yi.Framework.Rbac.Domain.Entities
@@ -22,7 +23,7 @@ namespace Yi.Framework.Rbac.Domain.Entities
public UserEntity(string userName, string password, long phone, string nick = "萌新")
{
UserName = userName;
Password = password;
EncryPassword.Password = password;
Phone = phone;
Nick = nick;
BuildPassword();
@@ -55,14 +56,20 @@ namespace Yi.Framework.Rbac.Domain.Entities
public string UserName { get; set; } = string.Empty;
/// <summary>
/// 密码
/// 加密密码
/// </summary>
public string Password { get; set; } = string.Empty;
[SugarColumn(IsOwnsOne = true)]
public EncryPasswordValueObject EncryPassword { get; set; } = new EncryPasswordValueObject();
/// <summary>
/// 加密盐值
/// </summary>
public string Salt { get; set; } = string.Empty;
///// <summary>
///// 密码
///// </summary>
//public string Password { get; set; } = string.Empty;
///// <summary>
///// 加密盐值
///// </summary>
//public string Salt { get; set; } = string.Empty;
/// <summary>
/// 头像
@@ -175,14 +182,14 @@ namespace Yi.Framework.Rbac.Domain.Entities
//如果不传值那就把自己的password当作传进来的password
if (password == null)
{
if (Password == null)
if (EncryPassword?.Password == null)
{
throw new ArgumentNullException(nameof(Password));
throw new ArgumentNullException(nameof(EncryPassword.Password));
}
password = Password;
password = EncryPassword.Password;
}
Salt = MD5Helper.GenerateSalt();
Password = MD5Helper.SHA2Encode(password, Salt);
EncryPassword.Salt = MD5Helper.GenerateSalt();
EncryPassword.Password = MD5Helper.SHA2Encode(password, EncryPassword.Salt);
return this;
}
@@ -193,12 +200,12 @@ namespace Yi.Framework.Rbac.Domain.Entities
/// <returns></returns>
public bool JudgePassword(string password)
{
if (Salt is null)
if (EncryPassword.Salt is null)
{
throw new ArgumentNullException(Salt);
throw new ArgumentNullException(EncryPassword.Salt);
}
var p = MD5Helper.SHA2Encode(password, Salt);
if (Password == MD5Helper.SHA2Encode(password, Salt))
var p = MD5Helper.SHA2Encode(password, EncryPassword.Salt);
if (EncryPassword.Password == MD5Helper.SHA2Encode(password, EncryPassword.Salt))
{
return true;
}

View File

@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Values;
namespace Yi.Framework.Rbac.Domain.Entities.ValueObjects
{
public class EncryPasswordValueObject : ValueObject
{
public EncryPasswordValueObject() { }
public EncryPasswordValueObject(string password) { this.Password = password; }
/// <summary>
/// 密码
/// </summary>
public string Password { get; set; } = string.Empty;
/// <summary>
/// 加密盐值
/// </summary>
public string Salt { get; set; } = string.Empty;
protected override IEnumerable<object> GetAtomicValues()
{
yield return Password;
yield return Salt;
}
}
}

View File

@@ -154,7 +154,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
userAction.Invoke(user);
}
if (user.Password == MD5Helper.SHA2Encode(password, user.Salt))
if (user.EncryPassword.Password == MD5Helper.SHA2Encode(password, user.EncryPassword.Salt))
{
return;
}
@@ -247,7 +247,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
throw new UserFriendlyException("无效更新!原密码错误!");
}
user.Password = newPassword;
user.EncryPassword.Password = newPassword;
user.BuildPassword();
await _repository.UpdateAsync(user);
}
@@ -262,7 +262,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
var user = await _repository.GetByIdAsync(userId);
// EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
user.Password = password;
user.EncryPassword.Password = password;
user.BuildPassword();
return await _repository.UpdateAsync(user);
}

View File

@@ -95,42 +95,26 @@ namespace Yi.Framework.Rbac.Domain.Managers
/// <returns></returns>
public async Task<UserRoleMenuDto> GetInfoAsync(Guid userId)
{
var user = await _userRepository.GetUserAllInfoAsync(userId);
var output = await GetInfoByCacheAsync(user);
var output = await GetInfoByCacheAsync(userId);
return output;
}
/// <summary>
/// 批量查询用户信息
/// </summary>
/// <param name="userIds"></param>
/// <returns></returns>
public async Task<List<UserRoleMenuDto>> GetInfoListAsync(List<Guid> userIds)
{
List<UserRoleMenuDto> output = new List<UserRoleMenuDto>();
var users = await _userRepository.GetListUserAllInfoAsync(userIds);
foreach (var user in users)
{
output.Add(await GetInfoByCacheAsync(user));
}
return output;
}
private async Task<UserRoleMenuDto> GetInfoByCacheAsync(UserEntity user)
private async Task<UserRoleMenuDto> GetInfoByCacheAsync(Guid userId)
{
//此处优先从缓存中获取
UserRoleMenuDto output = null;
var tokenExpiresMinuteTime = LazyServiceProvider.GetRequiredService<IOptions<JwtOptions>>().Value.ExpiresMinuteTime;
var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(user.Id),
var cacheData = await _userCache.GetOrAddAsync(new UserInfoCacheKey(userId),
async () =>
{
var user = await _userRepository.GetUserAllInfoAsync(userId);
var data = EntityMapToDto(user);
//系统用户数据被重置,老前端访问重新授权
if (data is null)
{
throw new AbpAuthorizationException();
}
data.Menus.Clear();
//data.Menus.Clear();
output = data;
return new UserInfoCacheItem(data);
},
@@ -143,6 +127,24 @@ namespace Yi.Framework.Rbac.Domain.Managers
return output!;
}
/// <summary>
/// 批量查询用户信息
/// </summary>
/// <param name="userIds"></param>
/// <returns></returns>
public async Task<List<UserRoleMenuDto>> GetInfoListAsync(List<Guid> userIds)
{
List<UserRoleMenuDto> output = new List<UserRoleMenuDto>();
foreach (var userId in userIds)
{
output.Add(await GetInfoByCacheAsync(userId));
}
return output;
}
private UserRoleMenuDto EntityMapToDto(UserEntity user)
{
@@ -152,8 +154,8 @@ namespace Yi.Framework.Rbac.Domain.Managers
//{
// throw new UserFriendlyException($"数据错误用户id{nameof(userId)} 不存在,请重新登录");
//}
user.Password = string.Empty;
user.Salt = string.Empty;
user.EncryPassword.Password = string.Empty;
user.EncryPassword.Salt = string.Empty;
//超级管理员特殊处理
if (UserConst.Admin.Equals(user.UserName))

View File

@@ -2,6 +2,7 @@
using Volo.Abp.Data;
using Volo.Abp.DependencyInjection;
using Yi.Framework.Rbac.Domain.Entities;
using Yi.Framework.Rbac.Domain.Entities.ValueObjects;
using Yi.Framework.Rbac.Domain.Shared.Enums;
using Yi.Framework.Rbac.Domain.Shared.Options;
using Yi.Framework.SqlSugarCore.Abstractions;
@@ -27,7 +28,7 @@ namespace Yi.Framework.Rbac.SqlSugarCore.DataSeeds
Name = "大橙子",
UserName = "cc",
Nick = "橙子",
Password = _options.AdminPassword,
EncryPassword = new EncryPasswordValueObject(_options.AdminPassword),
Email = "454313500@qq.com",
Phone = 13800000000,
Sex = SexEnum.Male,
@@ -47,7 +48,7 @@ namespace Yi.Framework.Rbac.SqlSugarCore.DataSeeds
Name = "大测试",
UserName = "test",
Nick = "测试",
Password = "123456",
EncryPassword=new EncryPasswordValueObject("123456"),
Email = "454313500@qq.com",
Phone = 15900000000,
Sex = SexEnum.Woman,
@@ -68,7 +69,7 @@ namespace Yi.Framework.Rbac.SqlSugarCore.DataSeeds
Name = "游客",
UserName = "guest",
Nick = "测试",
Password = "123456",
EncryPassword = new EncryPasswordValueObject("123456"),
Email = "454313500@qq.com",
Phone = 15900000000,
Sex = SexEnum.Woman,