feat: 添加用户、角色功能
This commit is contained in:
@@ -669,6 +669,14 @@
|
||||
<param name="userId"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.RBAC.Domain.Identity.RoleManager.GiveRoleSetMenuAsync(System.Collections.Generic.List{System.Int64},System.Collections.Generic.List{System.Int64})">
|
||||
<summary>
|
||||
给角色设置菜单
|
||||
</summary>
|
||||
<param name="roleIds"></param>
|
||||
<param name="menuIds"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:Yi.RBAC.Domain.Identity.UserManager.GiveUserSetRoleAsync(System.Collections.Generic.List{System.Int64},System.Collections.Generic.List{System.Int64})">
|
||||
<summary>
|
||||
给用户设置角色
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Mapster;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -103,6 +104,30 @@ namespace Yi.RBAC.Domain.Identity
|
||||
return claims;
|
||||
}
|
||||
|
||||
public async Task UpdatePasswordAsync(long userId, string newPassword, string oldPassword)
|
||||
{
|
||||
var user = await _repository.GetByIdAsync(userId);
|
||||
|
||||
if (!user.JudgePassword(oldPassword))
|
||||
{
|
||||
throw new UserFriendlyException("无效更新!新密码不能与老密码相同");
|
||||
}
|
||||
user.Password = newPassword;
|
||||
user.BuildPassword();
|
||||
await _repository.UpdateAsync(user);
|
||||
}
|
||||
|
||||
|
||||
public async Task<bool> RestPasswordAsync(long userId, string password)
|
||||
{
|
||||
var user = await _repository.GetByIdAsync(userId);
|
||||
user.Id = userId;
|
||||
user.Password = password;
|
||||
user.BuildPassword();
|
||||
return await _repository.UpdateAsync(user);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
using Yi.RBAC.Domain.Identity.Entities;
|
||||
|
||||
namespace Yi.RBAC.Domain.Identity
|
||||
{
|
||||
[AppService]
|
||||
public class RoleManager
|
||||
{
|
||||
private IRepository<RoleEntity> _repository;
|
||||
private IRepository<RoleMenuEntity> _roleMenuRepository;
|
||||
public RoleManager(IRepository<RoleEntity> repository, IRepository<RoleMenuEntity> roleMenuRepository)
|
||||
{
|
||||
_repository = repository;
|
||||
_roleMenuRepository = roleMenuRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 给角色设置菜单
|
||||
/// </summary>
|
||||
/// <param name="roleIds"></param>
|
||||
/// <param name="menuIds"></param>
|
||||
/// <returns></returns>
|
||||
public async Task GiveRoleSetMenuAsync(List<long> roleIds, List<long> menuIds)
|
||||
{
|
||||
//这个是需要事务的,在service中进行工作单元
|
||||
await _roleMenuRepository.DeleteAsync(u => roleIds.Contains(u.RoleId));
|
||||
//遍历用户
|
||||
foreach (var roleId in roleIds)
|
||||
{
|
||||
//添加新的关系
|
||||
List<RoleMenuEntity> roleMenuEntity = new();
|
||||
foreach (var menu in menuIds)
|
||||
{
|
||||
roleMenuEntity.Add(new RoleMenuEntity() {Id=SnowflakeHelper.NextId, RoleId = roleId, MenuId = menu });
|
||||
}
|
||||
//一次性批量添加
|
||||
await _roleMenuRepository.InsertRangeAsync(roleMenuEntity);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user