Skip to content

Commit

Permalink
feat: Permission Tree Add Create And Update Delete
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 24, 2024
1 parent e8862af commit 257f84b
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LinCms.Cms.Permissions;
public interface IPermissionService
{
Task<List<PermissionTreeNode>> GetPermissionTreeNodes();
Task<bool> CheckPermissionAsync(string module, string permission);
Task<bool> CheckPermissionAsync(string permission);
Task DeletePermissionsAsync(RemovePermissionDto permissionDto);

Task DispatchPermissions(DispatchPermissionsDto permissionDto, List<PermissionDefinition> permissionDefinition);
Expand All @@ -17,6 +17,11 @@ public interface IPermissionService

List<IDictionary<string, object>> StructuringPermissions(List<LinPermission> permissions);

Task UpdateAsync(int id,PermissioCreateUpdateDto createUpdateDto);
Task CreateAsync(PermissioCreateUpdateDto createUpdateDto);

Task DeleteAsync(int id);

Task<LinPermission> GetAsync(string permissionName);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using LinCms.Entities;

namespace LinCms.Cms.Permissions;

public class PermissioCreateUpdateDto
{

public PermissionType PermissionType { get; set; }

/// <summary>
/// 父级Id
/// </summary>
public long ParentId { get; set; }

/// <summary>
/// 所属权限、权限名称,例如:访问首页
/// </summary>
public string Name { get; set; }

/// <summary>
/// 后台路由
/// </summary>
public string Router { get; set; }

/// <summary>
/// 排序
/// </summary>
public int SortCode { get; set; }
}
21 changes: 10 additions & 11 deletions src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ namespace LinCms.Cms.Permissions;

public class PermissionDto : EntityDto<long>
{
public string Name { get; set; }

public PermissionType PermissionType { get; set; }

/// <summary>
/// 父级Id
/// </summary>
public long ParentId { get; set; }
public string Name { get; set; }

public PermissionType PermissionType { get; set; }

public string Router { get; set; }

}

public class PermissionTreeNode : PermissionNode
public class PermissionTreeNode : TreeNode
{
public int SortCode { get; set; }
public string Router { get; set; }

public PermissionType PermissionType { get; set; }
public DateTime? CreateTime { get; set; }
public List<PermissionTreeNode> Children { get; set; }

Expand All @@ -34,16 +34,15 @@ public PermissionTreeNode()

}

public class PermissionNode
public class TreeNode
{
public long Id { get; set; }
public PermissionType PermissionType { get; set; }
public long ParentId { get; set; }
public string Name { get; set; }
public List<PermissionNode> Children { get; set; }
public List<TreeNode> Children { get; set; }

public PermissionNode()
public TreeNode()
{
Children = new List<PermissionNode>();
Children = new List<TreeNode>();
}
}
20 changes: 20 additions & 0 deletions src/LinCms.Application.Contracts/LinCms.Application.Contracts.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/LinCms.Application/Cms/Permissions/PermissionProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ public class PermissionProfile : Profile
public PermissionProfile()
{
CreateMap<LinPermission, PermissionDto>();
CreateMap<LinPermission, PermissionNode>();
CreateMap<LinPermission, TreeNode>();
CreateMap<LinPermission, PermissionTreeNode>();
CreateMap<PermissioCreateUpdateDto, LinPermission>();
}
}
67 changes: 28 additions & 39 deletions src/LinCms.Application/Cms/Permissions/PermissionService.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,18 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Threading.Tasks;
using IGeekFan.FreeKit.Extras.FreeSql;
using IGeekFan.FreeKit.Extras.FreeSql;
using IGeekFan.FreeKit.Extras.Security;
using LinCms.Common;
using LinCms.Data;
using LinCms.Entities;
using LinCms.Security;
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;

namespace LinCms.Cms.Permissions;

public class PermissionTreeBuilder
{
public List<PermissionTreeNode> BuildTree(List<PermissionTreeNode> nodes)
{
var nodeDict = nodes.ToDictionary(n => n.Id);
List<PermissionTreeNode> roots = new List<PermissionTreeNode>();

foreach (var node in nodes)
{
if (node.ParentId == 0) // 假定ParentId为0表示根节点
{
roots.Add(node);
}
else
{
if (nodeDict.TryGetValue(node.ParentId, out PermissionTreeNode parentNode))
{
parentNode.Children.Add(node);
}
}
}

return roots; // 返回森林中所有根节点的列表
}

}

public class PermissionService(IAuditBaseRepository<LinPermission, long> permissionRepository,
IAuditBaseRepository<LinGroupPermission, long> groupPermissionRepository, ICurrentUser currentUser)
: ApplicationService, IPermissionService
Expand All @@ -47,27 +21,25 @@ public class PermissionService(IAuditBaseRepository<LinPermission, long> permiss

public async Task<List<PermissionTreeNode>> GetPermissionTreeNodes()
{
var permissionList = await permissionRepository.Select.ToListAsync();
var permissionList = await permissionRepository.Select.OrderBy(r => r.SortCode).ToListAsync();
var nodes = Mapper.Map<List<LinPermission>, List<PermissionTreeNode>>(permissionList);
return new PermissionTreeBuilder().BuildTree(nodes);
return new TreeBuilder().BuildPermissionTree(nodes);
}


/// <summary>
/// 检查当前登录的用户的分组权限
/// </summary>
/// <param name="module">模块</param>
/// <param name="permission">权限名</param>
/// <returns></returns>
public async Task<bool> CheckPermissionAsync(string module, string permission)
public async Task<bool> CheckPermissionAsync(string permission)
{
//默认Admin角色拥有所有权限
if (CurrentUser.IsInGroup(LinConsts.Group.Admin)) return true;
long[] groups = CurrentUser.FindGroupIds().Select(long.Parse).ToArray();

LinPermission linPermission = await permissionRepository.Where(r => r.PermissionType == PermissionType.Permission && r.Name == permission).FirstAsync();

if (linPermission == null || groups == null || groups.Length == 0) return false;
if (linPermission == null || groups.Length == 0) return false;

bool existPermission = await groupPermissionRepository.Select
.AnyAsync(r => groups.Contains(r.GroupId) && r.PermissionId == linPermission.Id);
Expand Down Expand Up @@ -135,6 +107,23 @@ public List<IDictionary<string, object>> StructuringPermissions(List<LinPermissi
return list;
}

public async Task UpdateAsync(int id, PermissioCreateUpdateDto createUpdateDto)
{
LinPermission permission = await permissionRepository.GetAsync(id);
Mapper.Map(createUpdateDto, permission);
await permissionRepository.UpdateAsync(permission);
}

public async Task CreateAsync(PermissioCreateUpdateDto createUpdateDto)
{
await permissionRepository.UpdateAsync(Mapper.Map<LinPermission>(createUpdateDto));
}

public async Task DeleteAsync(int id)
{
await permissionRepository.DeleteAsync(id);
}

public Task<LinPermission> GetAsync(string permissionName)
{
return permissionRepository.Where(r => r.Name == permissionName).FirstAsync();
Expand Down
51 changes: 51 additions & 0 deletions src/LinCms.Application/Cms/Permissions/TreeBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Collections.Generic;
using System.Linq;

namespace LinCms.Cms.Permissions;

public record TreeBuilder
{
public List<PermissionTreeNode> BuildPermissionTree(List<PermissionTreeNode> nodes)
{
var nodeDict = nodes.ToDictionary(n => n.Id);
List<PermissionTreeNode> roots = new List<PermissionTreeNode>();

foreach (var node in nodes)
{
if (node.ParentId == 0) // 假定ParentId为0表示根节点
{
roots.Add(node);
}
else
{
if (nodeDict.TryGetValue(node.ParentId, out PermissionTreeNode parentNode))
{
parentNode.Children.Add(node);
}
}
}
return roots; // 返回森林中所有根节点的列表
}

public List<TreeNode> BuildTree(List<TreeNode> nodes)
{
var nodeDict = nodes.ToDictionary(n => n.Id);
List<TreeNode> roots = new List<TreeNode>();

foreach (var node in nodes)
{
if (node.ParentId == 0) // 假定ParentId为0表示根节点
{
roots.Add(node);
}
else
{
if (nodeDict.TryGetValue(node.ParentId, out TreeNode parentNode))
{
parentNode.Children.Add(node);
}
}
}
return roots; // 返回森林中所有根节点的列表
}
}
3 changes: 0 additions & 3 deletions src/LinCms.Application/LinCms.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,5 @@
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Cms\Admin\" />
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/LinCms.Core/Entities/LinPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public LinPermission(string name, PermissionType permissionType, string router)
/// </summary>
[Column(StringLength = 200)]
public string Router { get; set; }

/// <summary>
/// 排序
/// </summary>
public int SortCode { get; set; }

}

public enum PermissionType
Expand Down
5 changes: 5 additions & 0 deletions src/LinCms.Core/LinCms.Core.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 257f84b

Please sign in to comment.