Skip to content

Commit

Permalink
permission to tree permission add permissiontype
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 23, 2024
1 parent faf7466 commit e8862af
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 129 deletions.
9 changes: 0 additions & 9 deletions src/LinCms.Application.Contracts/Cms/Admins/IAdminService.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace LinCms.Cms.Permissions;

public interface IPermissionService
{
IDictionary<string, List<PermissionDto>> GetAllStructualPermissions();
Task<List<PermissionTreeNode>> GetPermissionTreeNodes();
Task<bool> CheckPermissionAsync(string module, string permission);
Task DeletePermissionsAsync(RemovePermissionDto permissionDto);

Expand All @@ -19,6 +19,4 @@ public interface IPermissionService

Task<LinPermission> GetAsync(string permissionName);

Task<List<TreePermissionDto>> GetTreePermissionListAsync();

}
43 changes: 31 additions & 12 deletions src/LinCms.Application.Contracts/Cms/Permissions/PermissionDto.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
using System;
using System.Collections.Generic;
using IGeekFan.FreeKit.Extras.AuditEntity;
using LinCms.Entities;

namespace LinCms.Cms.Permissions;

public class PermissionDto : EntityDto<long>
{
public PermissionDto(string name, string module, string router)
{
Name = name;
Module = module;
Router = router;
}

public string Name { get; set; }
public string Module { get; set; }

public PermissionType PermissionType { get; set; }

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

public string Router { get; set; }

}

public class TreePermissionDto
public class PermissionTreeNode : PermissionNode
{
public string Rowkey { get; set; }
public string Name { get; set; }
public string Router { get; set; }

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

public PermissionTreeNode()
{
Children = new List<PermissionTreeNode>();
}

}

public class PermissionNode
{
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 PermissionNode()
{
Children = new List<PermissionNode>();
}
}

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

23 changes: 0 additions & 23 deletions src/LinCms.Application/Cms/Admin/AdminService.cs

This file was deleted.

2 changes: 2 additions & 0 deletions src/LinCms.Application/Cms/Permissions/PermissionProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ public class PermissionProfile : Profile
public PermissionProfile()
{
CreateMap<LinPermission, PermissionDto>();
CreateMap<LinPermission, PermissionNode>();
CreateMap<LinPermission, PermissionTreeNode>();
}
}
75 changes: 34 additions & 41 deletions src/LinCms.Application/Cms/Permissions/PermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,47 @@

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
{
private readonly ICurrentUser _currentUser = currentUser;

public IDictionary<string, List<PermissionDto>> GetAllStructualPermissions()
public async Task<List<PermissionTreeNode>> GetPermissionTreeNodes()
{
return permissionRepository.Select.ToList()
.GroupBy(r => r.Module)
.ToDictionary(
group => group.Key,
group =>
Mapper.Map<List<PermissionDto>>(group.ToList())
);

var permissionList = await permissionRepository.Select.ToListAsync();
var nodes = Mapper.Map<List<LinPermission>, List<PermissionTreeNode>>(permissionList);
return new PermissionTreeBuilder().BuildTree(nodes);
}


/// <summary>
/// 检查当前登录的用户的分组权限
/// </summary>
Expand All @@ -42,7 +65,7 @@ public async Task<bool> CheckPermissionAsync(string module, string permission)
if (CurrentUser.IsInGroup(LinConsts.Group.Admin)) return true;
long[] groups = CurrentUser.FindGroupIds().Select(long.Parse).ToArray();

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

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

Expand Down Expand Up @@ -85,7 +108,7 @@ public async Task<List<LinPermission>> GetPermissionByGroupIds(List<long> groupI

public List<IDictionary<string, object>> StructuringPermissions(List<LinPermission> permissions)
{
var groupPermissions = permissions.GroupBy(r => r.Module).Select(r => new
var groupPermissions = permissions.Where(r => r.PermissionType == PermissionType.Folder).GroupBy(r => r.Name).Select(r => new
{
r.Key,
Children = r.Select(u => u.Name).ToList()
Expand Down Expand Up @@ -116,34 +139,4 @@ public Task<LinPermission> GetAsync(string permissionName)
{
return permissionRepository.Where(r => r.Name == permissionName).FirstAsync();
}

public async Task<List<TreePermissionDto>> GetTreePermissionListAsync()
{
var permissions = await permissionRepository.Select.ToListAsync();

List<TreePermissionDto> treePermissionDtos = permissions.GroupBy(r => r.Module).Select(r =>
new TreePermissionDto
{
Rowkey = Guid.NewGuid().ToString(),
Children = new List<TreePermissionDto>(),
Name = r.Key,
}).ToList();


foreach (var permission in treePermissionDtos)
{
var childrens = permissions.Where(u => u.Module == permission.Name)
.Select(r => new TreePermissionDto
{
Rowkey = r.Id.ToString(),
Name = r.Name,
Router = r.Router,
CreateTime = r.CreateTime
})
.ToList();
permission.Children = childrens;
}

return treePermissionDtos;
}
}
4 changes: 4 additions & 0 deletions src/LinCms.Application/LinCms.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
</ItemGroup>

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

</Project>
31 changes: 16 additions & 15 deletions src/LinCms.Core/Entities/LinPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,35 +14,36 @@ public LinPermission()
{
}

public LinPermission(string name, string module, string router)
public LinPermission(string name, PermissionType permissionType, string router)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Module = module ?? throw new ArgumentNullException(nameof(module));
Router = router ?? throw new ArgumentNullException(nameof(router));
Name = name;
PermissionType = permissionType;
Router = router;
}

public PermissionType PermissionType { get; set; }

/// <summary>
/// 所属权限、权限名称,例如:访问首页
/// 父级Id
/// </summary>
[Column(StringLength = 60)]
public string Name { get; set; }
public long ParentId { get; set; }

/// <summary>
/// 权限所属模块,例如:人员管理
/// 所属权限、权限名称,例如:访问首页
/// </summary>
[Column(StringLength = 50)]
public string Module { get; set; }
[Column(StringLength = 60)]
public string Name { get; set; }

/// <summary>
/// 后台路由
/// </summary>
[Column(StringLength = 200)]
public string Router { get; set; }

public override string ToString()
{
return base.ToString() + $" Auth:{Name}、Module:{Module}";
}
}

public enum PermissionType
{
Folder = 0,
Permission = 1
}
}
8 changes: 4 additions & 4 deletions src/LinCms.Core/LinCms.Core.xml

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

41 changes: 37 additions & 4 deletions src/LinCms.Infrastructure/FreeSql/DataSeedContributor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection.PortableExecutable;
using System.Threading;
using System.Threading.Tasks;
using IGeekFan.FreeKit.Extras.FreeSql;
Expand Down Expand Up @@ -47,7 +48,7 @@ public async Task SeedPermissionAsync(List<PermissionDefinition> linCmsAttribute
List<LinPermission> insertPermissions = new();
List<LinPermission> updatePermissions = new();

List<LinPermission> allPermissions = await _permissionRepository.Select.ToListAsync(cancellationToken);
List<LinPermission> allPermissions = await _permissionRepository.Select.Where(r => r.PermissionType == PermissionType.Permission).ToListAsync(cancellationToken);

Expression<Func<LinGroupPermission, bool>> expression = u => false;
Expression<Func<LinPermission, bool>> permissionExpression = u => false;
Expand All @@ -64,19 +65,51 @@ public async Task SeedPermissionAsync(List<PermissionDefinition> linCmsAttribute
effectRows += await _groupPermissionRepository.DeleteAsync(expression, cancellationToken);
_logger.LogInformation($"删除了{effectRows}条数据");


#region Module 目录
var allModules = await _permissionRepository.Select.Where(r => r.PermissionType == PermissionType.Folder).ToListAsync(cancellationToken);

var permissionDefinitionsByModules = linCmsAttributes.GroupBy(r => r.Module).ToList();

var insertMoudles = new List<LinPermission>();
foreach (var module in permissionDefinitionsByModules)
{
LinPermission permissionEntity = allModules.FirstOrDefault(u => u.Name == module.Key);
if (permissionEntity == null)
{
insertMoudles.Add(new LinPermission()
{
PermissionType = PermissionType.Folder,
Name = module.Key,
ParentId = 0
});
}
}
await _permissionRepository.InsertAsync(insertMoudles, cancellationToken);
#endregion

allModules = await _permissionRepository.Select.Where(r => r.PermissionType == PermissionType.Folder).ToListAsync(cancellationToken);

linCmsAttributes.ForEach(r =>
{
LinPermission permissionEntity = allPermissions.FirstOrDefault(u => u.Module == r.Module && u.Name == r.Permission);
LinPermission permissionEntity = allPermissions.FirstOrDefault(u => u.Name == r.Permission);

var parentId = allModules.Where(u => u.Name == r.Module).First().Id;
if (permissionEntity == null)
{
insertPermissions.Add(new LinPermission(r.Permission, r.Module, r.Router));
insertPermissions.Add(new LinPermission(r.Permission, PermissionType.Permission, r.Router)
{
ParentId = parentId
});
}
else
{
bool routerExist = allPermissions.Any(u => u.Module == r.Module && u.Name == r.Permission && u.Router == r.Router);
bool routerExist = allPermissions.Any(u => u.Name == r.Permission && u.Router == r.Router);
if (!routerExist)
{
permissionEntity.Router = r.Router;
permissionEntity.ParentId = parentId;
permissionEntity.PermissionType = PermissionType.Permission;
updatePermissions.Add(permissionEntity);
}
}
Expand Down
Loading

0 comments on commit e8862af

Please sign in to comment.