Skip to content

Commit

Permalink
更新权限管理和数据种子贡献者代码
Browse files Browse the repository at this point in the history
  • Loading branch information
luoyunchong committed May 24, 2024
1 parent 257f84b commit e771b84
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
3 changes: 1 addition & 2 deletions src/LinCms.Application/Cms/Permissions/PermissionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
namespace LinCms.Cms.Permissions;

public class PermissionService(IAuditBaseRepository<LinPermission, long> permissionRepository,
IAuditBaseRepository<LinGroupPermission, long> groupPermissionRepository, ICurrentUser currentUser)
IAuditBaseRepository<LinGroupPermission, long> groupPermissionRepository)
: ApplicationService, IPermissionService
{
private readonly ICurrentUser _currentUser = currentUser;

public async Task<List<PermissionTreeNode>> GetPermissionTreeNodes()
{
Expand Down
6 changes: 3 additions & 3 deletions src/LinCms.Core/Data/PermissionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace LinCms.Data
{
public class PermissionDefinition(string permission, string module, string router)
{
public string Permission { get; } = permission ?? throw new ArgumentNullException(nameof(permission));
public string Module { get; } = module ?? throw new ArgumentNullException(nameof(module));
public string Router { get; } = router ?? throw new ArgumentNullException(nameof(router));
public string Permission { get; set; } = permission ?? throw new ArgumentNullException(nameof(permission));
public string Module { get; set; } = module ?? throw new ArgumentNullException(nameof(module));
public string Router { get; set; } = router ?? throw new ArgumentNullException(nameof(router));

public override string ToString()
{
Expand Down
20 changes: 15 additions & 5 deletions src/LinCms.Infrastructure/FreeSql/DataSeedContributor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public async Task SeedPermissionAsync(List<PermissionDefinition> linCmsAttribute
var permissionDefinitionsByModules = linCmsAttributes.GroupBy(r => r.Module).ToList();

var insertMoudles = new List<LinPermission>();
var sortCode = 10;
foreach (var module in permissionDefinitionsByModules)
{
LinPermission permissionEntity = allModules.FirstOrDefault(u => u.Name == module.Key);
Expand All @@ -81,38 +82,47 @@ public async Task SeedPermissionAsync(List<PermissionDefinition> linCmsAttribute
{
PermissionType = PermissionType.Folder,
Name = module.Key,
ParentId = 0
ParentId = 0,
SortCode = sortCode
});
sortCode += 10;
}
}
await _permissionRepository.InsertAsync(insertMoudles, cancellationToken);
#endregion

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

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

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

sortCode += 1;
});

await _permissionRepository.InsertAsync(insertPermissions, cancellationToken);
Expand Down
39 changes: 27 additions & 12 deletions src/LinCms.Web/Utils/ReflexHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,33 @@ public static List<PermissionDefinition> GetAssemblyLinCmsAttributes()

foreach (Attribute attribute in methodInfo.GetCustomAttributes())
{
if (attribute is LinCmsAuthorizeAttribute linAttribute && linAttribute.Permission.IsNotNullOrEmpty() && linAttribute.Module.IsNotNullOrEmpty())
if (attribute is LinCmsAuthorizeAttribute linAttribute &&
linAttribute.Permission.IsNotNullOrEmpty() && linAttribute.Module.IsNotNullOrEmpty())
{
string actionTemplate = methodAttribute.Template != null ? "/" + methodAttribute.Template + " " : " ";
string router = $"{routerAttribute.Template}{actionTemplate}{methodAttribute.HttpMethods.FirstOrDefault()}";
linAuths.Add(
new PermissionDefinition(
linAttribute.Permission,
linAttribute.Module,
router
)
);
//methodInfo.Name.ToSnakeCase()
string actionTemplate = methodAttribute.Template != null
? "/" + methodAttribute.Template + " "
: " ";
string router =
$"{routerAttribute.Template}{actionTemplate}{methodAttribute.HttpMethods.FirstOrDefault()}";

// 判断 linAuths中的权限中是否已存在,如果存在,router增加现有router值

if (linAuths.Any(definition => definition.Permission == linAttribute.Permission))
{
PermissionDefinition permissionDefinition = linAuths.First(definition => definition.Permission == linAttribute.Permission);
permissionDefinition.Router += "," + router;
}
else
{

linAuths.Add(
new PermissionDefinition(
linAttribute.Permission,
linAttribute.Module,
router
)
);
}
}
}
}
Expand Down Expand Up @@ -172,7 +187,7 @@ public static dynamic AuthorizationConvertToTree(List<PermissionDefinition> list

public static List<IDictionary<string, object>> AuthsConvertToTree(List<LinPermission> listAuths)
{
var groupAuths = listAuths.Where(r=>r.PermissionType==PermissionType.Folder).GroupBy(r => r.Name).Select(r => new
var groupAuths = listAuths.Where(r => r.PermissionType == PermissionType.Folder).GroupBy(r => r.Name).Select(r => new
{
r.Key,
Children = r.Select(u => u.Name).ToList()
Expand Down

0 comments on commit e771b84

Please sign in to comment.