feat: 用户account领域与user领域关系

This commit is contained in:
陈淳
2024-04-29 14:22:45 +08:00
parent df5224cf12
commit cabdd55cf5
4 changed files with 99 additions and 87 deletions

View File

@@ -79,7 +79,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
{
throw new UserFriendlyException(UserConst.No_Role);
}
if (userInfo.PermissionCodes.Count() ==0)
if (userInfo.PermissionCodes.Count() == 0)
{
throw new UserFriendlyException(UserConst.No_Permission);
}
@@ -181,7 +181,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
userAction.Invoke(user);
}
//这里为了兼容解决数据库开启了大小写不敏感问题,还要将用户名进行二次校验
if (user != null&&user.UserName==userName)
if (user != null && user.UserName == userName)
{
return true;
}
@@ -225,7 +225,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
dto.PermissionCodes?.ForEach(per => AddToClaim(claims, TokenTypeConst.Permission, per));
dto.RoleCodes?.ForEach(role => AddToClaim(claims, AbpClaimTypes.Role, role));
}
return claims;
}
@@ -265,34 +265,24 @@ namespace Yi.Framework.Rbac.Domain.Managers
public async Task<bool> RestPasswordAsync(Guid userId, string password)
{
var user = await _repository.GetByIdAsync(userId);
// EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
// EntityHelper.TrySetId(user, () => GuidGenerator.Create(), true);
user.EncryPassword.Password = password;
user.BuildPassword();
return await _repository.UpdateAsync(user);
}
/// <summary>
/// 注册用户,创建用户之后设置默认角色
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="phone"></param>
/// <returns></returns>
public async Task RegisterAsync(string userName, string password, long phone)
{
//输入的用户名与电话号码都不能在数据库中存在
UserEntity user = new();
var isExist = await _repository.IsAnyAsync(x => x.UserName == userName || x.Phone == phone);
if (isExist)
{
throw new UserFriendlyException("用户已存在,注册失败");
}
var newUser = new UserEntity(userName, password, phone);
var entity = await _repository.InsertReturnEntityAsync(newUser);
//赋上一个初始角色
var role = await _roleRepository.GetFirstAsync(x => x.RoleCode == UserConst.DefaultRoleCode);
if (role is not null)
{
await _userManager.GiveUserSetRoleAsync(new List<Guid> { entity.Id }, new List<Guid> { role.Id });
}
await _localEventBus.PublishAsync(new UserCreateEventArgs(entity.Id));
var user = new UserEntity(userName, password, phone);
await _userManager.CreateAsync(user);
await _userManager.SetDefautRoleAsync(user.Id);
}
}

View File

@@ -1,17 +1,22 @@
using Mapster;
using System.Text.RegularExpressions;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using TencentCloud.Ame.V20190916.Models;
using Volo.Abp.Authorization;
using Volo.Abp.Caching;
using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus.Local;
using Volo.Abp.Guids;
using Yi.Framework.Rbac.Domain.Entities;
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.Dtos;
using Yi.Framework.Rbac.Domain.Shared.Etos;
using Yi.Framework.Rbac.Domain.Shared.Options;
using Yi.Framework.SqlSugarCore.Abstractions;
@@ -22,12 +27,14 @@ namespace Yi.Framework.Rbac.Domain.Managers
public readonly ISqlSugarRepository<UserEntity> _repository;
public readonly ISqlSugarRepository<UserRoleEntity> _repositoryUserRole;
public readonly ISqlSugarRepository<UserPostEntity> _repositoryUserPost;
private readonly ISqlSugarRepository<RoleEntity> _roleRepository;
private IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> _userCache;
private readonly IGuidGenerator _guidGenerator;
private IUserRepository _userRepository;
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository) =>
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository) =
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository);
private ILocalEventBus _localEventBus;
public UserManager(ISqlSugarRepository<UserEntity> repository, ISqlSugarRepository<UserRoleEntity> repositoryUserRole, ISqlSugarRepository<UserPostEntity> repositoryUserPost, IGuidGenerator guidGenerator, IDistributedCache<UserInfoCacheItem, UserInfoCacheKey> userCache, IUserRepository userRepository, ILocalEventBus localEventBus, ISqlSugarRepository<RoleEntity> roleRepository) =>
(_repository, _repositoryUserRole, _repositoryUserPost, _guidGenerator, _userCache, _userRepository, _localEventBus, _roleRepository) =
(repository, repositoryUserRole, repositoryUserPost, guidGenerator, userCache, userRepository, localEventBus, roleRepository);
/// <summary>
/// 给用户设置角色
@@ -88,6 +95,65 @@ namespace Yi.Framework.Rbac.Domain.Managers
}
}
/// <summary>
/// 创建用户
/// </summary>
/// <returns></returns>
public async Task CreateAsync(UserEntity userEntity)
{
//校验用户名
ValidateUserName(userEntity);
if (userEntity.EncryPassword?.Password.Length < 6)
{
throw new UserFriendlyException("密码需大于等于6位");
}
var isExist = await _repository.IsAnyAsync(x => x.UserName == userEntity.UserName || x.Phone == userEntity.Phone);
if (isExist)
{
throw new UserFriendlyException("用户已存在,创建失败");
}
var entity = await _repository.InsertReturnEntityAsync(userEntity);
userEntity = entity;
await _localEventBus.PublishAsync(new UserCreateEventArgs(entity.Id));
}
public async Task SetDefautRoleAsync(Guid userId)
{
var role = await _roleRepository.GetFirstAsync(x => x.RoleCode == UserConst.DefaultRoleCode);
if (role is not null)
{
await GiveUserSetRoleAsync(new List<Guid> { userId }, new List<Guid> { role.Id });
}
}
private void ValidateUserName(UserEntity input)
{
if (input.UserName == UserConst.Admin || input.UserName == UserConst.TenantAdmin)
{
throw new UserFriendlyException("用户名无效注册!");
}
if (input.UserName.Length < 2)
{
throw new UserFriendlyException("账号名需大于等于2位");
}
// 正则表达式,匹配只包含数字和字母的字符串
string pattern = @"^[a-zA-Z0-9]+$";
bool isMatch = Regex.IsMatch(input.UserName, pattern);
if (!isMatch)
{
throw new UserFriendlyException("用户名不能包含除【字母】与【数字】的其他字符");
}
}
/// <summary>
/// 查询用户信息,已缓存
@@ -95,7 +161,7 @@ namespace Yi.Framework.Rbac.Domain.Managers
/// <returns></returns>
public async Task<UserRoleMenuDto> GetInfoAsync(Guid userId)
{
var output = await GetInfoByCacheAsync(userId);
return output;
}