添加关系表业务
This commit is contained in:
@@ -18,7 +18,7 @@ using Yi.Framework.WebCore.AuthorizationPolicy;
|
||||
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 账户控制器
|
||||
/// 账户管理
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
|
||||
@@ -15,6 +15,9 @@ using Yi.Framework.WebCore.AuthorizationPolicy;
|
||||
|
||||
namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
/// <summary>
|
||||
/// 菜单管理
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class MenuController : BaseCrudController<MenuEntity>
|
||||
@@ -24,5 +27,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
_iMenuService = iMenuService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到树形菜单
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
//暂未制作逻辑删除与多租户的过滤
|
||||
public async Task<List<MenuEntity>> GetMenuTree()
|
||||
{
|
||||
return await _iMenuService.GetMenuTreeAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 角色管理
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class RoleController : BaseCrudController<RoleEntity>
|
||||
@@ -24,5 +28,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
_iRoleService = iRoleService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给多用户设置多角色
|
||||
/// </summary>
|
||||
/// <param name="giveRoleSetMenuDto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task<Result> GiveRoleSetMenu(GiveRoleSetMenuDto giveRoleSetMenuDto)
|
||||
{
|
||||
return Result.Success().SetStatus(await _iRoleService.GiveRoleSetMenu(giveRoleSetMenuDto.RoleIds, giveRoleSetMenuDto.MenuIds));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// 用户管理
|
||||
/// </summary>
|
||||
[ApiController]
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class UserController : BaseCrudController<UserEntity>
|
||||
@@ -24,5 +28,16 @@ namespace Yi.Framework.ApiMicroservice.Controllers
|
||||
{
|
||||
_iUserService = iUserService;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给多用户设置多角色
|
||||
/// </summary>
|
||||
/// <param name="giveUserSetRoleDto"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task<Result> GiveUserSetRole(GiveUserSetRoleDto giveUserSetRoleDto)
|
||||
{
|
||||
return Result.Success().SetStatus(await _iUserService.GiveUserSetRole(giveUserSetRoleDto.UserIds,giveUserSetRoleDto.RoleIds));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<long> RoleIds { get; set; }
|
||||
public List<long> MenuIds { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -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<long> UserIds { get; set; }
|
||||
public List<long> RoleIds { get; set;}
|
||||
}
|
||||
}
|
||||
12
Yi.Framework.Net6/Yi.Framework.Interface/IMenuService.cs
Normal file
12
Yi.Framework.Net6/Yi.Framework.Interface/IMenuService.cs
Normal file
@@ -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<MenuEntity>
|
||||
{
|
||||
Task<List<MenuEntity>> GetMenuTreeAsync();
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,14 @@ namespace Yi.Framework.Interface
|
||||
/// DbTest
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<List<RoleEntity>> DbTest();
|
||||
Task<List<RoleEntity>> DbTest();
|
||||
|
||||
/// <summary>
|
||||
/// 给角色设置菜单,多角色,多菜单
|
||||
/// </summary>
|
||||
/// <param name="roleIds"></param>
|
||||
/// <param name="menuIds"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> GiveRoleSetMenu(List<long> roleIds, List<long> menuIds);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<MenuEntity>
|
||||
{
|
||||
public partial interface IMenuService : IBaseService<MenuEntity>
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace Yi.Framework.Interface
|
||||
/// 测试仓储的上下文对象
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<List<UserEntity>> DbTest();
|
||||
Task<List<UserEntity>> DbTest();
|
||||
|
||||
/// <summary>
|
||||
/// 登录方法
|
||||
@@ -21,7 +21,7 @@ namespace Yi.Framework.Interface
|
||||
/// <param name="password"></param>
|
||||
/// <param name="userAction"></param>
|
||||
/// <returns></returns>
|
||||
public Task<bool> Login(string userName, string password, Action<UserEntity> userAction = null);
|
||||
Task<bool> Login(string userName, string password, Action<UserEntity> userAction = null);
|
||||
|
||||
/// <summary>
|
||||
/// 注册方法
|
||||
@@ -29,13 +29,20 @@ namespace Yi.Framework.Interface
|
||||
/// <param name="userEntity"></param>
|
||||
/// <param name="userAction"></param>
|
||||
/// <returns></returns>
|
||||
public Task<bool> Register(UserEntity userEntity, Action<UserEntity> userAction = null);
|
||||
|
||||
Task<bool> Register(UserEntity userEntity, Action<UserEntity> userAction = null);
|
||||
|
||||
/// <summary>
|
||||
/// 导航属性关联角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public Task<List<UserEntity>> GetListInRole();
|
||||
Task<List<UserEntity>> GetListInRole();
|
||||
|
||||
/// <summary>
|
||||
/// 给用户设置角色,多用户,多角色
|
||||
/// </summary>
|
||||
/// <param name="userIds"></param>
|
||||
/// <param name="roleIds"></param>
|
||||
/// <returns></returns>
|
||||
Task<bool> GiveUserSetRole(List<long> userIds, List<long> roleIds);
|
||||
}
|
||||
}
|
||||
|
||||
19
Yi.Framework.Net6/Yi.Framework.Service/MenuService.cs
Normal file
19
Yi.Framework.Net6/Yi.Framework.Service/MenuService.cs
Normal file
@@ -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<MenuEntity>, IMenuService
|
||||
{
|
||||
public async Task<List<MenuEntity>> GetMenuTreeAsync()
|
||||
{
|
||||
//ParentId 0,代表为根目录,只能存在一个
|
||||
//复杂查询直接使用db代理
|
||||
return await _repository._Db.Queryable<MenuEntity>().ToTreeAsync(it=>it.Childs,it=>it.ParentId,0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,5 +13,33 @@ namespace Yi.Framework.Service
|
||||
{
|
||||
return await _repository._Db.Queryable<RoleEntity>().ToListAsync();
|
||||
}
|
||||
public async Task<bool> GiveRoleSetMenu(List<long> roleIds, List<long> menuIds)
|
||||
{
|
||||
var _repositoryRoleMenu = _repository.ChangeRepository<Repository<RoleMenuEntity>>();
|
||||
|
||||
//多次操作,需要事务确保原子性
|
||||
return await _repositoryRoleMenu.UseTranAsync(async () =>
|
||||
{
|
||||
|
||||
//遍历用户
|
||||
foreach (var roleId in roleIds)
|
||||
{
|
||||
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
|
||||
await _repositoryRoleMenu.DeleteAsync(u => u.RoleId==roleId);
|
||||
|
||||
//添加新的关系
|
||||
List<RoleMenuEntity> roleMenuEntity = new();
|
||||
foreach (var menu in menuIds)
|
||||
{
|
||||
roleMenuEntity.Add(new RoleMenuEntity() { RoleId = roleId,MenuId=menu });
|
||||
}
|
||||
|
||||
//一次性批量添加
|
||||
await _repositoryRoleMenu.InsertRangeAsync(roleMenuEntity);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace Yi.Framework.Service
|
||||
}
|
||||
public async Task<bool> Exist(string userName, Action<UserEntity> 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<bool> Login(string userName, string password,Action<UserEntity> userAction = null)
|
||||
public async Task<bool> Login(string userName, string password, Action<UserEntity> 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<bool> Register(UserEntity userEntity, Action<UserEntity> 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<List<UserEntity>> GetListInRole()
|
||||
{
|
||||
return await _repository._Db.Queryable<UserEntity>().Includes(u => u.Roles).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<bool> GiveUserSetRole(List<long> userIds, List<long> roleIds)
|
||||
{
|
||||
var _repositoryUserRole = _repository.ChangeRepository<Repository<UserRoleEntity>>();
|
||||
|
||||
//多次操作,需要事务确保原子性
|
||||
return await _repositoryUserRole.UseTranAsync(async () =>
|
||||
{
|
||||
|
||||
//遍历用户
|
||||
foreach (var userId in userIds)
|
||||
{
|
||||
//删除用户之前所有的用户角色关系(物理删除,没有恢复的必要)
|
||||
await _repositoryUserRole.DeleteAsync(u => u.UserId == userId);
|
||||
|
||||
//添加新的关系
|
||||
List<UserRoleEntity> userRoleEntities = new();
|
||||
foreach (var roleId in roleIds)
|
||||
{
|
||||
userRoleEntities.Add(new UserRoleEntity() { UserId = userId, RoleId = roleId });
|
||||
}
|
||||
|
||||
//一次性批量添加
|
||||
await _repositoryUserRole.InsertRangeAsync(userRoleEntities);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user