Skip to content

Commit

Permalink
新增用户密码格式验证,避免简单密码设置
Browse files Browse the repository at this point in the history
  • Loading branch information
zhontai committed Nov 1, 2023
1 parent 8261dce commit 83a9ab0
Show file tree
Hide file tree
Showing 7 changed files with 315 additions and 165 deletions.
2 changes: 1 addition & 1 deletion src/hosts/ZhonTai.Host/Configs/appconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"fonts": [ "Times New Roman", "Verdana", "Arial", "Gungsuh", "Impact" ]
},
//默认密码
"defaultPassword": "111111",
"defaultPassword": "123asd",
//动态api
"dynamicApi": {
//结果格式化
Expand Down
31 changes: 31 additions & 0 deletions src/platform/ZhonTai.Admin/Core/Helpers/UserHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using FileInfo = ZhonTai.Common.Files.FileInfo;
using ZhonTai.Admin.Core.Attributes;
using ZhonTai.Common.Helpers;
using ZhonTai.Admin.Core.Dto;

namespace ZhonTai.Admin.Core.Helpers;

/// <summary>
/// 用户帮助类
/// </summary>
[SingleInstance]
public class UserHelper
{
/// <summary>
/// 检查密码
/// </summary>
/// <param name="password"></param>
public void CheckPassword(string password)
{
if (!PasswordHelper.Verify(password))
{
throw ResultOutput.Exception("密码为字母+数字+可选特殊字符,长度在6-16之间");
}
}
}
246 changes: 126 additions & 120 deletions src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using ZhonTai.Admin.Domain.Pkg;
using ZhonTai.Admin.Domain.TenantPkg;
using ZhonTai.Admin.Services.Pkg;
using ZhonTai.Admin.Core.Helpers;

namespace ZhonTai.Admin.Services.Tenant;

Expand All @@ -36,6 +37,8 @@ namespace ZhonTai.Admin.Services.Tenant;
[DynamicApi(Area = AdminConsts.AreaName)]
public class TenantService : BaseService, ITenantService, IDynamicApi
{
private readonly Lazy<UserHelper> _userHelper;

private AppConfig _appConfig => LazyGetRequiredService<AppConfig>();
private ITenantRepository _tenantRepository => LazyGetRequiredService<ITenantRepository>();
private IRoleRepository _roleRepository => LazyGetRequiredService<IRoleRepository>();
Expand All @@ -48,8 +51,9 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
private IPasswordHasher<UserEntity> _passwordHasher => LazyGetRequiredService<IPasswordHasher<UserEntity>>();
private ITenantPkgRepository _tenantPkgRepository => LazyGetRequiredService<ITenantPkgRepository>();

public TenantService()
public TenantService(Lazy<UserHelper> userHelper)
{
_userHelper = userHelper;
}

/// <summary>
Expand Down Expand Up @@ -113,7 +117,7 @@ public async Task<PageOutput<TenantListOutput>> GetPageAsync(PageInput<TenantGet
List = Mapper.Map<List<TenantListOutput>>(list),
Total = total
};

return data;
}

Expand All @@ -125,131 +129,134 @@ public async Task<PageOutput<TenantListOutput>> GetPageAsync(PageInput<TenantGet
[AdminTransaction]
public virtual async Task<long> AddAsync(TenantAddInput input)
{
using (_tenantRepository.DataFilter.Disable(FilterNames.Tenant))
{
var existsOrg = await _orgRepository.Select
.Where(a => (a.Name == input.Name || a.Code == input.Code) && a.ParentId == 0)
.FirstAsync(a => new { a.Name, a.Code });

if (existsOrg != null)
{
if (existsOrg.Name == input.Name)
{
throw ResultOutput.Exception($"企业名称已存在");
}
_userHelper.Value.CheckPassword(input.Password);

if (existsOrg.Code == input.Code)
{
throw ResultOutput.Exception($"企业编码已存在");
}
}

Expression<Func<UserEntity, bool>> where = (a => a.UserName == input.UserName);
where = where.Or(input.Phone.NotNull(), a => a.Mobile == input.Phone)
.Or(input.Email.NotNull(), a => a.Email == input.Email);
using var _ = _tenantRepository.DataFilter.Disable(FilterNames.Tenant);

var existsUser = await _userRepository.Select.Where(where)
.FirstAsync(a => new { a.UserName, a.Mobile, a.Email });
var existsOrg = await _orgRepository.Select
.Where(a => (a.Name == input.Name || a.Code == input.Code) && a.ParentId == 0)
.FirstAsync(a => new { a.Name, a.Code });

if (existsUser != null)
if (existsOrg != null)
{
if (existsOrg.Name == input.Name)
{
if (existsUser.UserName == input.UserName)
{
throw ResultOutput.Exception($"企业账号已存在");
}

if (input.Phone.NotNull() && existsUser.Mobile == input.Phone)
{
throw ResultOutput.Exception($"企业手机号已存在");
}

if (input.Email.NotNull() && existsUser.Email == input.Email)
{
throw ResultOutput.Exception($"企业邮箱已存在");
}
throw ResultOutput.Exception($"企业名称已存在");
}

//添加租户
TenantEntity entity = Mapper.Map<TenantEntity>(input);
TenantEntity tenant = await _tenantRepository.InsertAsync(entity);
long tenantId = tenant.Id;

//添加租户套餐
if (input.PkgIds != null && input.PkgIds.Any())
if (existsOrg.Code == input.Code)
{
var pkgs = input.PkgIds.Select(pkgId => new TenantPkgEntity
{
TenantId = tenantId,
PkgId = pkgId
}).ToList();

await _tenantPkgRepository.InsertAsync(pkgs);
throw ResultOutput.Exception($"企业编码已存在");
}
}

//添加部门
var org = new OrgEntity
{
TenantId = tenantId,
Name = input.Name,
Code = input.Code,
ParentId = 0,
MemberCount = 1,
Sort = 1,
Enabled = true
};
await _orgRepository.InsertAsync(org);
Expression<Func<UserEntity, bool>> where = (a => a.UserName == input.UserName);
where = where.Or(input.Phone.NotNull(), a => a.Mobile == input.Phone)
.Or(input.Email.NotNull(), a => a.Email == input.Email);

var existsUser = await _userRepository.Select.Where(where)
.FirstAsync(a => new { a.UserName, a.Mobile, a.Email });

//添加用户
if (input.Password.IsNull())
if (existsUser != null)
{
if (existsUser.UserName == input.UserName)
{
input.Password = _appConfig.DefaultPassword;
throw ResultOutput.Exception($"企业账号已存在");
}
var user = new UserEntity
{
TenantId = tenantId,
UserName = input.UserName,
Name = input.RealName,
Mobile = input.Phone,
Email = input.Email,
Type = UserType.TenantAdmin,
OrgId = org.Id,
Enabled = true
};
if (_appConfig.PasswordHasher)

if (input.Phone.NotNull() && existsUser.Mobile == input.Phone)
{
user.Password = _passwordHasher.HashPassword(user, input.Password);
user.PasswordEncryptType = PasswordEncryptType.PasswordHasher;
throw ResultOutput.Exception($"企业手机号已存在");
}
else

if (input.Email.NotNull() && existsUser.Email == input.Email)
{
user.Password = MD5Encrypt.Encrypt32(input.Password);
user.PasswordEncryptType = PasswordEncryptType.MD5Encrypt32;
throw ResultOutput.Exception($"企业邮箱已存在");
}
await _userRepository.InsertAsync(user);
}

long userId = user.Id;
//添加租户
TenantEntity entity = Mapper.Map<TenantEntity>(input);
TenantEntity tenant = await _tenantRepository.InsertAsync(entity);
long tenantId = tenant.Id;

//添加用户员工
var emp = new UserStaffEntity
//添加租户套餐
if (input.PkgIds != null && input.PkgIds.Any())
{
var pkgs = input.PkgIds.Select(pkgId => new TenantPkgEntity
{
Id = userId,
TenantId = tenantId
};
await _userStaffRepository.InsertAsync(emp);
TenantId = tenantId,
PkgId = pkgId
}).ToList();

//添加用户部门
var userOrg = new UserOrgEntity
{
UserId = userId,
OrgId = org.Id
};
await _userOrgRepository.InsertAsync(userOrg);
await _tenantPkgRepository.InsertAsync(pkgs);
}

//添加角色分组和角色
var roleGroupId = YitIdHelper.NextId();
var roleId = YitIdHelper.NextId();
var jobGroupId = YitIdHelper.NextId();
var roles = new List<RoleEntity>{
//添加部门
var org = new OrgEntity
{
TenantId = tenantId,
Name = input.Name,
Code = input.Code,
ParentId = 0,
MemberCount = 1,
Sort = 1,
Enabled = true
};
await _orgRepository.InsertAsync(org);

//添加用户
if (input.Password.IsNull())
{
input.Password = _appConfig.DefaultPassword;
}

var user = new UserEntity
{
TenantId = tenantId,
UserName = input.UserName,
Name = input.RealName,
Mobile = input.Phone,
Email = input.Email,
Type = UserType.TenantAdmin,
OrgId = org.Id,
Enabled = true
};
if (_appConfig.PasswordHasher)
{
user.Password = _passwordHasher.HashPassword(user, input.Password);
user.PasswordEncryptType = PasswordEncryptType.PasswordHasher;
}
else
{
user.Password = MD5Encrypt.Encrypt32(input.Password);
user.PasswordEncryptType = PasswordEncryptType.MD5Encrypt32;
}
await _userRepository.InsertAsync(user);

long userId = user.Id;

//添加用户员工
var emp = new UserStaffEntity
{
Id = userId,
TenantId = tenantId
};
await _userStaffRepository.InsertAsync(emp);

//添加用户部门
var userOrg = new UserOrgEntity
{
UserId = userId,
OrgId = org.Id
};
await _userOrgRepository.InsertAsync(userOrg);

//添加角色分组和角色
var roleGroupId = YitIdHelper.NextId();
var roleId = YitIdHelper.NextId();
var jobGroupId = YitIdHelper.NextId();
var roles = new List<RoleEntity>{
new RoleEntity
{
Id = roleGroupId,
Expand Down Expand Up @@ -290,23 +297,22 @@ public virtual async Task<long> AddAsync(TenantAddInput input)
Sort = 1
}
};
await _roleRepository.InsertAsync(roles);
await _roleRepository.InsertAsync(roles);

//添加用户角色
var userRole = new UserRoleEntity()
{
UserId = userId,
RoleId = roleId
};
await _userRoleRepository.InsertAsync(userRole);
//添加用户角色
var userRole = new UserRoleEntity()
{
UserId = userId,
RoleId = roleId
};
await _userRoleRepository.InsertAsync(userRole);

//更新租户的用户和部门
tenant.UserId = userId;
tenant.OrgId = org.Id;
await _tenantRepository.UpdateAsync(tenant);
//更新租户的用户和部门
tenant.UserId = userId;
tenant.OrgId = org.Id;
await _tenantRepository.UpdateAsync(tenant);

return tenant.Id;
}
return tenant.Id;
}

/// <summary>
Expand Down
Loading

0 comments on commit 83a9ab0

Please sign in to comment.