From ed5196d1a14d96b0d811a19f83e8a14e352d0134 Mon Sep 17 00:00:00 2001 From: liguoyang Date: Thu, 21 Mar 2024 21:02:52 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E7=94=A8=E6=88=B7=E5=88=9B=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Identity/Controllers/UsersController.cs | 12 +++++++++ .../Identity/Request/UserCreateRequest.cs | 25 +++++++++++++++++++ .../Identity/Services/IUserService.cs | 4 ++- .../Services/Implements/UserService.cs | 22 ++++++++++++++++ 4 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 Src/Dlhis.Sys/Identity/Request/UserCreateRequest.cs diff --git a/Src/Dlhis.Sys/Identity/Controllers/UsersController.cs b/Src/Dlhis.Sys/Identity/Controllers/UsersController.cs index 2a87166e..c3f4ffa5 100644 --- a/Src/Dlhis.Sys/Identity/Controllers/UsersController.cs +++ b/Src/Dlhis.Sys/Identity/Controllers/UsersController.cs @@ -63,6 +63,18 @@ public class UsersController : ControllerBase return await _userService.GetUserByIdsAsync(ids); } + /// + /// + /// + /// + /// + [Authorize(Roles = "admin")] + [HttpPost("create")] + public async Task CreateUserAsync(UserCreateRequest request) + { + await _userService.CreateUserAsync(request); + } + /// /// 上传头像 /// diff --git a/Src/Dlhis.Sys/Identity/Request/UserCreateRequest.cs b/Src/Dlhis.Sys/Identity/Request/UserCreateRequest.cs new file mode 100644 index 00000000..b080a1ad --- /dev/null +++ b/Src/Dlhis.Sys/Identity/Request/UserCreateRequest.cs @@ -0,0 +1,25 @@ +using System.ComponentModel.DataAnnotations; + +namespace Dlhis.Sys.Identity.Request; + +public class UserCreateRequest +{ + /// + /// 用户名 + /// + [Required] + public string Username { get; set; } + + /// + /// 密码 + /// + [MinLength(6)] + [Required] + public string Password { get; set; } + + /// + /// 厂区 + /// + [Required] + public string Tenant { get; set; } +} diff --git a/Src/Dlhis.Sys/Identity/Services/IUserService.cs b/Src/Dlhis.Sys/Identity/Services/IUserService.cs index 619f6e78..02622619 100644 --- a/Src/Dlhis.Sys/Identity/Services/IUserService.cs +++ b/Src/Dlhis.Sys/Identity/Services/IUserService.cs @@ -18,4 +18,6 @@ public interface IUserService : IScopeDependency Task> GetUserByIdsAsync(List ids); -} + + Task CreateUserAsync(UserCreateRequest request); +} \ No newline at end of file diff --git a/Src/Dlhis.Sys/Identity/Services/Implements/UserService.cs b/Src/Dlhis.Sys/Identity/Services/Implements/UserService.cs index 1f61bff3..2868ad2a 100644 --- a/Src/Dlhis.Sys/Identity/Services/Implements/UserService.cs +++ b/Src/Dlhis.Sys/Identity/Services/Implements/UserService.cs @@ -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().Where(x => ids.Contains(x.Id)).Select().ToListAsync(); return users; } + + public async Task CreateUserAsync(UserCreateRequest request) + { + var hasher = new PasswordHasher(); + + 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 + }); + } }