From f3061ed643e7f607c14674f11a27b9f202e0bd96 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com>
Date: Tue, 26 Apr 2022 01:31:14 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=85=B3=E7=B3=BB=E8=A1=A8?=
=?UTF-8?q?=E4=B8=9A=E5=8A=A1?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../Controllers/AccountController.cs | 2 +-
.../Controllers/MenuController.cs | 14 +++++++
.../Controllers/RoleController.cs | 15 +++++++
.../Controllers/UserController.cs | 15 +++++++
.../GiveRoleSetMenuDto.cs | 14 +++++++
.../GiveUserSetRoleDto.cs | 14 +++++++
.../Yi.Framework.Interface/IMenuService.cs | 12 ++++++
.../Yi.Framework.Interface/IRoleService.cs | 10 ++++-
.../IServiceTemplate/IMenuService.cs | 8 ++--
.../Yi.Framework.Interface/IUserService.cs | 17 +++++---
.../Yi.Framework.Service/MenuService.cs | 19 +++++++++
.../Yi.Framework.Service/RoleService.cs | 28 +++++++++++++
.../Yi.Framework.Service/UserService.cs | 40 +++++++++++++++----
13 files changed, 191 insertions(+), 17 deletions(-)
create mode 100644 Yi.Framework.Net6/Yi.Framework.DTOModel/GiveRoleSetMenuDto.cs
create mode 100644 Yi.Framework.Net6/Yi.Framework.DTOModel/GiveUserSetRoleDto.cs
create mode 100644 Yi.Framework.Net6/Yi.Framework.Interface/IMenuService.cs
create mode 100644 Yi.Framework.Net6/Yi.Framework.Service/MenuService.cs
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs
index 85f12094..8ce42cb1 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/AccountController.cs
@@ -18,7 +18,7 @@ using Yi.Framework.WebCore.AuthorizationPolicy;
namespace Yi.Framework.ApiMicroservice.Controllers
{
///
- /// 账户控制器
+ /// 账户管理
///
[ApiController]
[Route("api/[controller]/[action]")]
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/MenuController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/MenuController.cs
index 0de84e65..c7fcf91d 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/MenuController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/MenuController.cs
@@ -15,6 +15,9 @@ using Yi.Framework.WebCore.AuthorizationPolicy;
namespace Yi.Framework.ApiMicroservice.Controllers
{
+ ///
+ /// 菜单管理
+ ///
[ApiController]
[Route("api/[controller]/[action]")]
public class MenuController : BaseCrudController
@@ -24,5 +27,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
_iMenuService = iMenuService;
}
+
+ ///
+ /// 得到树形菜单
+ ///
+ ///
+ [HttpGet]
+ //暂未制作逻辑删除与多租户的过滤
+ public async Task> GetMenuTree()
+ {
+ return await _iMenuService.GetMenuTreeAsync();
+ }
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs
index cd030960..00eadff0 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/RoleController.cs
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
+using Yi.Framework.DTOModel;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
@@ -15,6 +16,9 @@ using Yi.Framework.WebCore.AuthorizationPolicy;
namespace Yi.Framework.ApiMicroservice.Controllers
{
+ ///
+ /// 角色管理
+ ///
[ApiController]
[Route("api/[controller]/[action]")]
public class RoleController : BaseCrudController
@@ -24,5 +28,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
_iRoleService = iRoleService;
}
+
+ ///
+ /// 给多用户设置多角色
+ ///
+ ///
+ ///
+ [HttpPut]
+ public async Task GiveRoleSetMenu(GiveRoleSetMenuDto giveRoleSetMenuDto)
+ {
+ return Result.Success().SetStatus(await _iRoleService.GiveRoleSetMenu(giveRoleSetMenuDto.RoleIds, giveRoleSetMenuDto.MenuIds));
+ }
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs
index 4b8631f4..69cdb8ec 100644
--- a/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs
+++ b/Yi.Framework.Net6/Yi.Framework.ApiMicroservice/Controllers/UserController.cs
@@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yi.Framework.Common.Models;
+using Yi.Framework.DTOModel;
using Yi.Framework.Interface;
using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
@@ -15,6 +16,9 @@ using Yi.Framework.WebCore.AuthorizationPolicy;
namespace Yi.Framework.ApiMicroservice.Controllers
{
+ ///
+ /// 用户管理
+ ///
[ApiController]
[Route("api/[controller]/[action]")]
public class UserController : BaseCrudController
@@ -24,5 +28,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
{
_iUserService = iUserService;
}
+
+ ///
+ /// 给多用户设置多角色
+ ///
+ ///
+ ///
+ [HttpPut]
+ public async Task GiveUserSetRole(GiveUserSetRoleDto giveUserSetRoleDto)
+ {
+ return Result.Success().SetStatus(await _iUserService.GiveUserSetRole(giveUserSetRoleDto.UserIds,giveUserSetRoleDto.RoleIds));
+ }
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.DTOModel/GiveRoleSetMenuDto.cs b/Yi.Framework.Net6/Yi.Framework.DTOModel/GiveRoleSetMenuDto.cs
new file mode 100644
index 00000000..488ab7c5
--- /dev/null
+++ b/Yi.Framework.Net6/Yi.Framework.DTOModel/GiveRoleSetMenuDto.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Yi.Framework.DTOModel
+{
+ public class GiveRoleSetMenuDto
+ {
+ public List RoleIds { get; set; }
+ public List MenuIds { get; set; }
+ }
+}
diff --git a/Yi.Framework.Net6/Yi.Framework.DTOModel/GiveUserSetRoleDto.cs b/Yi.Framework.Net6/Yi.Framework.DTOModel/GiveUserSetRoleDto.cs
new file mode 100644
index 00000000..123afd52
--- /dev/null
+++ b/Yi.Framework.Net6/Yi.Framework.DTOModel/GiveUserSetRoleDto.cs
@@ -0,0 +1,14 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Yi.Framework.DTOModel
+{
+ public class GiveUserSetRoleDto
+ {
+ public List UserIds { get; set; }
+ public List RoleIds { get; set;}
+ }
+}
diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IMenuService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IMenuService.cs
new file mode 100644
index 00000000..f6b2f880
--- /dev/null
+++ b/Yi.Framework.Net6/Yi.Framework.Interface/IMenuService.cs
@@ -0,0 +1,12 @@
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Yi.Framework.Model.Models;
+using Yi.Framework.Repository;
+
+namespace Yi.Framework.Interface
+{
+ public partial interface IMenuService:IBaseService
+ {
+ Task> GetMenuTreeAsync();
+ }
+}
diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IRoleService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IRoleService.cs
index 7b83b9d2..88f2aec3 100644
--- a/Yi.Framework.Net6/Yi.Framework.Interface/IRoleService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Interface/IRoleService.cs
@@ -11,6 +11,14 @@ namespace Yi.Framework.Interface
/// DbTest
///
///
- public Task> DbTest();
+ Task> DbTest();
+
+ ///
+ /// 给角色设置菜单,多角色,多菜单
+ ///
+ ///
+ ///
+ ///
+ Task GiveRoleSetMenu(List roleIds, List menuIds);
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IMenuService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IMenuService.cs
index 413a0ade..f93cf04b 100644
--- a/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IMenuService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Interface/IServiceTemplate/IMenuService.cs
@@ -1,9 +1,11 @@
-using Yi.Framework.Model.Models;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Yi.Framework.Model.Models;
using Yi.Framework.Repository;
namespace Yi.Framework.Interface
{
- public partial interface IMenuService:IBaseService
- {
+ public partial interface IMenuService : IBaseService
+ {
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs b/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs
index 9c6ebe80..be6b005f 100644
--- a/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Interface/IUserService.cs
@@ -12,7 +12,7 @@ namespace Yi.Framework.Interface
/// 测试仓储的上下文对象
///
///
- public Task> DbTest();
+ Task> DbTest();
///
/// 登录方法
@@ -21,7 +21,7 @@ namespace Yi.Framework.Interface
///
///
///
- public Task Login(string userName, string password, Action userAction = null);
+ Task Login(string userName, string password, Action userAction = null);
///
/// 注册方法
@@ -29,13 +29,20 @@ namespace Yi.Framework.Interface
///
///
///
- public Task Register(UserEntity userEntity, Action userAction = null);
-
+ Task Register(UserEntity userEntity, Action userAction = null);
///
/// 导航属性关联角色
///
///
- public Task> GetListInRole();
+ Task> GetListInRole();
+
+ ///
+ /// 给用户设置角色,多用户,多角色
+ ///
+ ///
+ ///
+ ///
+ Task GiveUserSetRole(List userIds, List roleIds);
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.Service/MenuService.cs b/Yi.Framework.Net6/Yi.Framework.Service/MenuService.cs
new file mode 100644
index 00000000..4d954c5f
--- /dev/null
+++ b/Yi.Framework.Net6/Yi.Framework.Service/MenuService.cs
@@ -0,0 +1,19 @@
+using SqlSugar;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Yi.Framework.Interface;
+using Yi.Framework.Model.Models;
+using Yi.Framework.Repository;
+
+namespace Yi.Framework.Service
+{
+ public partial class MenuService : BaseService, IMenuService
+ {
+ public async Task> GetMenuTreeAsync()
+ {
+ //ParentId 0,代表为根目录,只能存在一个
+ //复杂查询直接使用db代理
+ return await _repository._Db.Queryable().ToTreeAsync(it=>it.Childs,it=>it.ParentId,0);
+ }
+ }
+}
diff --git a/Yi.Framework.Net6/Yi.Framework.Service/RoleService.cs b/Yi.Framework.Net6/Yi.Framework.Service/RoleService.cs
index ead1ea03..252cd722 100644
--- a/Yi.Framework.Net6/Yi.Framework.Service/RoleService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Service/RoleService.cs
@@ -13,5 +13,33 @@ namespace Yi.Framework.Service
{
return await _repository._Db.Queryable().ToListAsync();
}
+ public async Task GiveRoleSetMenu(List roleIds, List menuIds)
+ {
+ var _repositoryRoleMenu = _repository.ChangeRepository>();
+
+ //多次操作,需要事务确保原子性
+ return await _repositoryRoleMenu.UseTranAsync(async () =>
+ {
+
+ //遍历用户
+ foreach (var roleId in roleIds)
+ {
+ //删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
+ await _repositoryRoleMenu.DeleteAsync(u => u.RoleId==roleId);
+
+ //添加新的关系
+ List roleMenuEntity = new();
+ foreach (var menu in menuIds)
+ {
+ roleMenuEntity.Add(new RoleMenuEntity() { RoleId = roleId,MenuId=menu });
+ }
+
+ //一次性批量添加
+ await _repositoryRoleMenu.InsertRangeAsync(roleMenuEntity);
+ }
+ });
+
+
+ }
}
}
diff --git a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs
index 0db1c3d5..773d1e8e 100644
--- a/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs
+++ b/Yi.Framework.Net6/Yi.Framework.Service/UserService.cs
@@ -27,7 +27,7 @@ namespace Yi.Framework.Service
}
public async Task Exist(string userName, Action userAction = null)
{
- var user = await _repository.GetFirstAsync(u=>u.UserName== userName);
+ var user = await _repository.GetFirstAsync(u => u.UserName == userName);
if (userAction != null)
{
userAction.Invoke(user);
@@ -38,18 +38,18 @@ namespace Yi.Framework.Service
}
return true;
}
- public async Task Login(string userName, string password,Action userAction = null)
+ public async Task Login(string userName, string password, Action userAction = null)
{
- var user=new UserEntity();
+ var user = new UserEntity();
if (await Exist(userName, o => user = o))
{
userAction.Invoke(user);
- if (user.Password== Common.Helper.MD5Helper.SHA2Encode(password, user.Salt))
+ if (user.Password == Common.Helper.MD5Helper.SHA2Encode(password, user.Salt))
{
return true;
}
}
- return false;
+ return false;
}
public async Task Register(UserEntity userEntity, Action userAction = null)
@@ -57,9 +57,9 @@ namespace Yi.Framework.Service
var user = new UserEntity();
if (!await Exist(userEntity.UserName))
{
- user.UserName= userEntity.UserName;
+ user.UserName = userEntity.UserName;
user.Salt = Common.Helper.MD5Helper.GenerateSalt();
- user.Password = Common.Helper.MD5Helper.SHA2Encode(userEntity.Password,user.Salt);
+ user.Password = Common.Helper.MD5Helper.SHA2Encode(userEntity.Password, user.Salt);
userAction.Invoke(await _repository.InsertReturnEntityAsync(user));
return true;
}
@@ -69,7 +69,33 @@ namespace Yi.Framework.Service
public async Task> GetListInRole()
{
return await _repository._Db.Queryable().Includes(u => u.Roles).ToListAsync();
+ }
+ public async Task GiveUserSetRole(List userIds, List roleIds)
+ {
+ var _repositoryUserRole = _repository.ChangeRepository>();
+
+ //多次操作,需要事务确保原子性
+ return await _repositoryUserRole.UseTranAsync(async () =>
+ {
+
+ //遍历用户
+ foreach (var userId in userIds)
+ {
+ //删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
+ await _repositoryUserRole.DeleteAsync(u => u.UserId == userId);
+
+ //添加新的关系
+ List userRoleEntities = new();
+ foreach (var roleId in roleIds)
+ {
+ userRoleEntities.Add(new UserRoleEntity() { UserId = userId, RoleId = roleId });
+ }
+
+ //一次性批量添加
+ await _repositoryUserRole.InsertRangeAsync(userRoleEntities);
+ }
+ });
}
}
}