Skip to content

Commit

Permalink
Use FrozenDictionary instead of Dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
InCerryGit committed Feb 28, 2024
1 parent 3a517c2 commit 733fe2f
Showing 1 changed file with 158 additions and 129 deletions.
287 changes: 158 additions & 129 deletions src/AgileConfig.Server.Apisite/Filters/PremissionCheckAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -33,135 +34,162 @@ private static string GetEnvFromArgs(IDictionary<string, object> args, IConfigSe
/// <summary>
/// 因为 attribute 不能传递 func 参数,所有从 action 的参数内获取 appId 的操作只能提前内置在一个静态字典内。
/// </summary>
protected static Dictionary<string, Func<ActionExecutingContext, IPremissionService, IConfigService, string>> _getAppIdParamFuncs = new Dictionary<string, Func<ActionExecutingContext, IPremissionService, IConfigService, string>>
{
{
"Config.Add",(args, premission, config)=> { var model = args.ActionArguments["model"]; return (model as IAppIdModel)?.AppId; }
},
{
"Config.AddRange",(args, premission, config)=> { var model = args.ActionArguments["model"]; return (model as List<ConfigVM>)?.FirstOrDefault()?.AppId; }
},
{
"Config.EnvSync",(args, premission, config)=> { var appId = args.ActionArguments["appId"]; return appId?.ToString(); }
},
{
"Config.Edit",(args, premission, config)=> { var model = args.ActionArguments["model"]; return (model as IAppIdModel)?.AppId; }
},
{
"Config.Delete", (args, premission, configService) => {
var id = args.ActionArguments["id"];
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(id.ToString(),env).GetAwaiter().GetResult();
protected static readonly
FrozenDictionary<string, Func<ActionExecutingContext, IPremissionService, IConfigService, string>>
GetAppIdParamFuncs =
new Dictionary<string, Func<ActionExecutingContext, IPremissionService, IConfigService, string>>
{
{
"Config.Add", (args, premission, config) =>
{
var model = args.ActionArguments["model"];
return (model as IAppIdModel)?.AppId;
}
},
{
"Config.AddRange", (args, premission, config) =>
{
var model = args.ActionArguments["model"];
return (model as List<ConfigVM>)?.FirstOrDefault()?.AppId;
}
},
{
"Config.EnvSync", (args, premission, config) =>
{
var appId = args.ActionArguments["appId"];
return appId?.ToString();
}
},
{
"Config.Edit", (args, premission, config) =>
{
var model = args.ActionArguments["model"];
return (model as IAppIdModel)?.AppId;
}
},
{
"Config.Delete", (args, premission, configService) =>
{
var id = args.ActionArguments["id"];
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(id.ToString(), env).GetAwaiter().GetResult();

return config.AppId;
}
},
{
"Config.DeleteSome", (args, premission, configService) => {
var ids = args.ActionArguments["ids"] as List<string>;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(ids.FirstOrDefault(),env).GetAwaiter().GetResult();

return config.AppId;
}
},
{
"Config.Offline", (args, premission, configService) => {
var id = args.ActionArguments["configId"] ;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(id.ToString(),env).GetAwaiter().GetResult();
return config.AppId;
}
},
{
"Config.DeleteSome", (args, premission, configService) =>
{
var ids = args.ActionArguments["ids"] as List<string>;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(ids.FirstOrDefault(), env).GetAwaiter().GetResult();

return config.AppId;
}
}
,
{
"Config.OfflineSome", (args, premission, configService) => {
var ids = args.ActionArguments["configIds"] as List<string>;
var id = ids?.FirstOrDefault();
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(ids.FirstOrDefault(),env).GetAwaiter().GetResult();
return config.AppId;
}
},
{
"Config.Offline", (args, premission, configService) =>
{
var id = args.ActionArguments["configId"];
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(id.ToString(), env).GetAwaiter().GetResult();

return config.AppId;
}
}
,
{
"Config.Publish", (args, premission, configService) => {
var model = args.ActionArguments["model"] as IAppIdModel;
return config.AppId;
}
},
{
"Config.OfflineSome", (args, premission, configService) =>
{
var ids = args.ActionArguments["configIds"] as List<string>;
var id = ids?.FirstOrDefault();
var env = GetEnvFromArgs(args.ActionArguments, configService);
var config = configService.GetAsync(ids.FirstOrDefault(), env).GetAwaiter().GetResult();

return model?.AppId;
}
}
,
{
"Config.Publish_API", (args, premission, configService) => {
var appId = args.ActionArguments["appId"];
return config.AppId;
}
},
{
"Config.Publish", (args, premission, configService) =>
{
var model = args.ActionArguments["model"] as IAppIdModel;

return appId.ToString();
}
}
,
{
"Config.Rollback", (args, premission, configService) => {
var timelineId = args.ActionArguments["publishTimelineId"] as string;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var detail = configService.GetPublishDetailByPublishTimelineIdAsync(timelineId, env).GetAwaiter().GetResult();
return detail.FirstOrDefault()?.AppId;
}
}
,
{
"Config.Rollback_API", (args, premission, configService) => {
var timelineId = args.ActionArguments["historyId"] as string;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var detail = configService.GetPublishDetailByPublishTimelineIdAsync(timelineId, env).GetAwaiter().GetResult();
return detail.FirstOrDefault()?.AppId;
}
}
,
{
"App.Add", (args, premission, configService) => {
return "";
return model?.AppId;
}
},
{
"Config.Publish_API", (args, premission, configService) =>
{
var appId = args.ActionArguments["appId"];

return appId.ToString();
}
},
{
"Config.Rollback", (args, premission, configService) =>
{
var timelineId = args.ActionArguments["publishTimelineId"] as string;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var detail = configService.GetPublishDetailByPublishTimelineIdAsync(timelineId, env)
.GetAwaiter().GetResult();
return detail.FirstOrDefault()?.AppId;
}
},
{
"Config.Rollback_API", (args, premission, configService) =>
{
var timelineId = args.ActionArguments["historyId"] as string;
var env = GetEnvFromArgs(args.ActionArguments, configService);
var detail = configService.GetPublishDetailByPublishTimelineIdAsync(timelineId, env)
.GetAwaiter().GetResult();
return detail.FirstOrDefault()?.AppId;
}
},
{
"App.Add", (args, premission, configService) => { return ""; }
},
{
"App.Edit", (args, premission, configService) =>
{
var app = args.ActionArguments["model"] as IAppModel;
return app.Id;
}
},
{
"App.Delete", (args, premission, configService) =>
{
var id = args.ActionArguments["id"] as string;
return id;
}
},
{
"App.DisableOrEanble", (args, premission, configService) =>
{
var id = args.ActionArguments["id"] as string;
return id;
}
},
{
"App.Auth", (args, premission, configService) =>
{
var model = args.ActionArguments["model"] as IAppIdModel;
return model?.AppId;
}
},
{
"Node.Add", (args, premission, configService) =>
{
var id = args.ActionArguments["id"] as string;
return id;
}
},
{
"Node.Delete", (args, premission, configService) =>
{
var model = args.ActionArguments["model"] as IAppIdModel;
return model?.AppId;
}
}
},
{
"App.Edit", (args, premission, configService) => {
var app = args.ActionArguments["model"] as IAppModel;
return app.Id;
}
},
{
"App.Delete", (args, premission, configService) => {
var id = args.ActionArguments["id"] as string;
return id;
}
},
{
"App.DisableOrEanble", (args, premission, configService) => {
var id = args.ActionArguments["id"] as string;
return id;
}
},
{
"App.Auth", (args, premission, configService) => {
var model = args.ActionArguments["model"] as IAppIdModel;
return model?.AppId;
}
},
{
"Node.Add", (args, premission, configService) => {
var id = args.ActionArguments["id"] as string;
return id;
}
},
{
"Node.Delete", (args, premission, configService) => {
var model = args.ActionArguments["model"] as IAppIdModel;
return model?.AppId;
}
}
};
}.ToFrozenDictionary();

protected const string GlobalMatchPatten = "GLOBAL_{0}";
protected const string AppMatchPatten = "APP_{0}_{1}";
Expand All @@ -171,7 +199,9 @@ private static string GetEnvFromArgs(IDictionary<string, object> args, IConfigSe

private string _actionName;
private string _functionKey;
public PremissionCheckAttribute(IPremissionService premissionService, IConfigService configService, string actionName, string functionKey)

public PremissionCheckAttribute(IPremissionService premissionService, IConfigService configService,
string actionName, string functionKey)
{
_premissionService = premissionService;
_configService = configService;
Expand Down Expand Up @@ -208,12 +238,13 @@ public override async Task OnActionExecutionAsync(ActionExecutingContext context
await base.OnActionExecutionAsync(context, next);
return;
}

var appId = "";
if (_getAppIdParamFuncs.ContainsKey(_actionName))
if (GetAppIdParamFuncs.TryGetValue(_actionName, out var func))
{
var func = _getAppIdParamFuncs[_actionName];
appId = func(context, _premissionService, _configService);
}

if (!string.IsNullOrEmpty(appId))
{
matchKey = string.Format(AppMatchPatten, appId, _functionKey);
Expand All @@ -229,7 +260,5 @@ public override async Task OnActionExecutionAsync(ActionExecutingContext context
context.Result = new ContentResult();
await base.OnActionExecutionAsync(context, next);
}


}
}
}

0 comments on commit 733fe2f

Please sign in to comment.