Skip to content

Commit

Permalink
修复启用接口数据权限未按接口地址缓存的问题 #61
Browse files Browse the repository at this point in the history
升级npm包
  • Loading branch information
zhontai committed Mar 11, 2024
1 parent c757c95 commit 9f266a9
Show file tree
Hide file tree
Showing 10 changed files with 1,631 additions and 1,049 deletions.
12 changes: 12 additions & 0 deletions src/platform/ZhonTai.Admin/Core/AppInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading;
using ZhonTai.Admin.Core.Auth;

namespace ZhonTai.Admin.Core;
Expand Down Expand Up @@ -82,6 +83,17 @@ public static bool IsRun
/// </summary>
public static Logger Log => LogManager.Setup().LoadConfigurationFromAppSettings().GetCurrentClassLogger();

static readonly AsyncLocal<string> _asyncLocal = new();

/// <summary>
/// 数据权限接口路径
/// </summary>
public static string CurrentDataPermissionApiPath
{
get => _asyncLocal.Value;
set => _asyncLocal.Value = value;
}

#region private

private static IEnumerable<Type> GetTypes(Assembly ass)
Expand Down
2 changes: 1 addition & 1 deletion src/platform/ZhonTai.Admin/Core/Auth/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ DataPermissionDto GetDataPermission()
}
else
{
return cache.Get<DataPermissionDto>(CacheKeys.DataPermission + Id);
return cache.Get<DataPermissionDto>(CacheKeys.GetDataPermissionKey(Id));
}
}

Expand Down
26 changes: 25 additions & 1 deletion src/platform/ZhonTai.Admin/Core/Consts/CacheKeys.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using SixLabors.ImageSharp.Drawing;
using System.ComponentModel;
using ZhonTai.Admin.Core.Attributes;

namespace ZhonTai.Admin.Core.Consts;
Expand Down Expand Up @@ -46,4 +47,27 @@ public static partial class CacheKeys
/// <param name="code">唯一码</param>
/// <returns></returns>
public static string GetSmsCodeKey(string mobile, string code) => $"{SmsCode}{mobile}:{code}";

/// <summary>
/// 获取数据权限缓存键
/// </summary>
/// <param name="userId">用户Id</param>
/// <param name="apiPath">请求接口路径</param>
/// <returns></returns>
public static string GetDataPermissionKey(long userId, string apiPath = null)
{
if(apiPath.IsNull())
{
apiPath = AppInfo.CurrentDataPermissionApiPath;
}

return $"{DataPermission}{userId}{(apiPath.NotNull() ? (":" + apiPath) : "")}";
}

/// <summary>
/// 获取数据权限模板
/// </summary>
/// <param name="userId">用户Id</param>
/// <returns></returns>
public static string GetDataPermissionPattern(long userId) => $"{DataPermission}{userId}*";
}
9 changes: 4 additions & 5 deletions src/platform/ZhonTai.Admin/Core/HostApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.IdentityModel.Logging;
using FreeScheduler;
using SixLabors.ImageSharp.Drawing;

namespace ZhonTai.Admin.Core;

Expand Down Expand Up @@ -771,19 +772,17 @@ private void ConfigureMiddleware(WebApplication app, IWebHostEnvironment env, IC
var user = ctx.RequestServices.GetRequiredService<IUser>();
if (user?.Id > 0)
{
var endpoint = ctx.GetEndpoint();
string path = null;

//排除匿名或者登录接口
var endpoint = ctx.GetEndpoint();
if (appConfig.Validate.ApiDataPermission && endpoint != null && !endpoint.Metadata.Any(m => m.GetType() == typeof(AllowAnonymousAttribute) || m.GetType() == typeof(LoginAttribute)))
{
var actionDescriptor = endpoint.Metadata.GetMetadata<ControllerActionDescriptor>();
var template = actionDescriptor?.AttributeRouteInfo?.Template;
path = template.NotNull() ? $"/{template}" : null;
AppInfo.CurrentDataPermissionApiPath = template.NotNull() ? $"/{template}" : null;
}

var userService = ctx.RequestServices.GetRequiredService<IUserService>();
await userService.GetDataPermissionAsync(path);
await userService.GetDataPermissionAsync(AppInfo.CurrentDataPermissionApiPath);
}

await next();
Expand Down
6 changes: 0 additions & 6 deletions src/platform/ZhonTai.Admin/Services/Pkg/PkgService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,6 @@ public async Task UpdateAsync(PkgUpdateInput input)

Mapper.Map(input, entity);
await _pkgRepository.UpdateAsync(entity);

var tenantIds = await _tenantPkgRepository.Select.Where(a => a.PkgId == entity.Id).ToListAsync(a => a.TenantId);
foreach (var tenantId in tenantIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + tenantId);
}
}

/// <summary>
Expand Down
16 changes: 8 additions & 8 deletions src/platform/ZhonTai.Admin/Services/Role/RoleService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public async Task AddRoleUserAsync(RoleAddRoleUserListInput input)
var clearUserIds = userIds.Concat(input.UserIds).Distinct();
foreach (var userId in clearUserIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand All @@ -174,7 +174,7 @@ public async Task RemoveRoleUserAsync(RoleAddRoleUserListInput input)

foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand Down Expand Up @@ -245,7 +245,7 @@ public async Task UpdateAsync(RoleUpdateInput input)
var userIds = await _userRoleRepository.Select.Where(a => a.RoleId == entity.Id).ToListAsync(a => a.UserId);
foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand All @@ -269,7 +269,7 @@ public virtual async Task DeleteAsync(long id)

foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand All @@ -293,7 +293,7 @@ public virtual async Task BatchDeleteAsync(long[] ids)

foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand All @@ -312,7 +312,7 @@ public virtual async Task SoftDeleteAsync(long id)
await _roleRepository.SoftDeleteRecursiveAsync(a => roleIdList.Contains(a.Id));
foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand All @@ -331,7 +331,7 @@ public virtual async Task BatchSoftDeleteAsync(long[] ids)
await _roleRepository.SoftDeleteRecursiveAsync(a => roleIdList.Contains(a.Id));
foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand Down Expand Up @@ -359,7 +359,7 @@ public async Task SetDataScopeAsync(RoleSetDataScopeInput input)
var userIds = await _userRoleRepository.Select.Where(a => a.RoleId == entity.Id).ToListAsync(a => a.UserId);
foreach (var userId in userIds)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}
}
15 changes: 7 additions & 8 deletions src/platform/ZhonTai.Admin/Services/User/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ public async Task<DataPermissionDto> GetDataPermissionAsync(string? apiPath)
{
return null;
}

var key = CacheKeys.DataPermission + User.Id;
return await Cache.GetOrSetAsync(key, async () =>

return await Cache.GetOrSetAsync(CacheKeys.GetDataPermissionKey(User.Id, apiPath), async () =>
{
using var _ = _userRepository.DataFilter.Disable(FilterNames.Self, FilterNames.Data);

Expand Down Expand Up @@ -525,7 +524,7 @@ public virtual async Task UpdateAsync(UserUpdateInput input)
await _userOrgRepository.InsertAsync(orgs);
}

await Cache.DelAsync(CacheKeys.DataPermission + user.Id);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}

/// <summary>
Expand Down Expand Up @@ -769,7 +768,7 @@ public virtual async Task DeleteAsync(long id)
await _userRepository.DeleteAsync(a => a.Id == id);

//删除用户数据权限缓存
await Cache.DelAsync(CacheKeys.DataPermission + id);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(id));
}

/// <summary>
Expand Down Expand Up @@ -799,7 +798,7 @@ public virtual async Task BatchDeleteAsync(long[] ids)

foreach (var userId in ids)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand Down Expand Up @@ -827,7 +826,7 @@ public virtual async Task SoftDeleteAsync(long id)
await _staffRepository.SoftDeleteAsync(a => a.Id == id);
await _userRepository.SoftDeleteAsync(id);

await Cache.DelAsync(CacheKeys.DataPermission + id);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(id));
}

/// <summary>
Expand All @@ -853,7 +852,7 @@ public virtual async Task BatchSoftDeleteAsync(long[] ids)

foreach (var userId in ids)
{
await Cache.DelAsync(CacheKeys.DataPermission + userId);
await Cache.DelByPatternAsync(CacheKeys.GetDataPermissionPattern(userId));
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

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

Loading

0 comments on commit 9f266a9

Please sign in to comment.