feat 用户创建
This commit is contained in:
@@ -63,6 +63,18 @@ public class UsersController : ControllerBase
|
||||
return await _userService.GetUserByIdsAsync(ids);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Roles = "admin")]
|
||||
[HttpPost("create")]
|
||||
public async Task CreateUserAsync(UserCreateRequest request)
|
||||
{
|
||||
await _userService.CreateUserAsync(request);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 上传头像
|
||||
/// </summary>
|
||||
|
||||
25
Src/Dlhis.Sys/Identity/Request/UserCreateRequest.cs
Normal file
25
Src/Dlhis.Sys/Identity/Request/UserCreateRequest.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Dlhis.Sys.Identity.Request;
|
||||
|
||||
public class UserCreateRequest
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户名
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Username { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 密码
|
||||
/// </summary>
|
||||
[MinLength(6)]
|
||||
[Required]
|
||||
public string Password { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 厂区
|
||||
/// </summary>
|
||||
[Required]
|
||||
public string Tenant { get; set; }
|
||||
}
|
||||
@@ -18,4 +18,6 @@ public interface IUserService : IScopeDependency
|
||||
|
||||
|
||||
Task<List<UserResponse>> GetUserByIdsAsync(List<int> ids);
|
||||
|
||||
Task CreateUserAsync(UserCreateRequest request);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Kurisu.Core.DataAccess.Dto;
|
||||
using Kurisu.SqlSugar.Extensions;
|
||||
using Kurisu.SqlSugar.Services;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
|
||||
namespace Dlhis.Sys.Identity.Services.Implements;
|
||||
|
||||
@@ -67,4 +68,25 @@ public class UserService : IUserService
|
||||
var users = await _dbContext.Queryable<UserEntity>().Where(x => ids.Contains(x.Id)).Select<UserResponse>().ToListAsync();
|
||||
return users;
|
||||
}
|
||||
|
||||
public async Task CreateUserAsync(UserCreateRequest request)
|
||||
{
|
||||
var hasher = new PasswordHasher<UserEntity>();
|
||||
|
||||
var user = new UserEntity
|
||||
{
|
||||
UserName = request.Username,
|
||||
NormalizedUserName = request.Username.ToUpper(),
|
||||
};
|
||||
request.Password = hasher.HashPassword(user, request.Password);
|
||||
user.PasswordHash = request.Password;
|
||||
|
||||
var id = await _dbContext.InsertReturnIdentityAsync(user);
|
||||
|
||||
await _dbContext.InsertAsync(new UserTenantEntity
|
||||
{
|
||||
TenantCode = request.Tenant,
|
||||
UserId = (int)id
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user