From f781c5ffc229ffa7ef6c76494c4b6238a061e6c2 Mon Sep 17 00:00:00 2001 From: Argo Windows Date: Mon, 28 Oct 2019 14:52:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20RetrieveUserBy?= =?UTF-8?q?UserName=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/admin/Bootstrap.Admin/Controllers/AccountController.cs | 2 +- .../Bootstrap.Admin/Controllers/Api/InterfaceController.cs | 2 +- .../Bootstrap.Admin/Controllers/Api/RegisterController.cs | 4 ++-- src/admin/Bootstrap.Admin/Controllers/HomeController.cs | 2 +- src/admin/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs | 2 +- src/admin/Bootstrap.Admin/Models/HeaderBarModel.cs | 6 +++--- src/admin/Bootstrap.Admin/Models/LockModel.cs | 4 ++-- src/admin/Bootstrap.Admin/Models/NavigatorBarModel.cs | 2 +- src/admin/Bootstrap.DataAccess.MongoDB/User.cs | 2 +- src/admin/Bootstrap.DataAccess/Helper/TraceHelper.cs | 2 +- src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs | 4 ++-- test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs | 2 +- 12 files changed, 17 insertions(+), 17 deletions(-) diff --git a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs index 8e2cbb87..2491ad4f 100644 --- a/src/admin/Bootstrap.Admin/Controllers/AccountController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/AccountController.cs @@ -41,7 +41,7 @@ namespace Bootstrap.Admin.Controllers var authenticationType = User.Identity.AuthenticationType; await HttpContext.SignOutAsync(); var urlReferrer = Request.Headers["Referer"].FirstOrDefault(); - return View(new LockModel(this) + return View(new LockModel(User.Identity.Name) { AuthenticationType = authenticationType, ReturnUrl = WebUtility.UrlEncode(string.IsNullOrEmpty(urlReferrer) ? CookieAuthenticationDefaults.LoginPath.Value : urlReferrer) diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/InterfaceController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/InterfaceController.cs index 1bc8257b..4b3ddcde 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/InterfaceController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/InterfaceController.cs @@ -54,7 +54,7 @@ namespace Bootstrap.Admin.Controllers [HttpPost] public BootstrapUser RetrieveUserByUserName([FromBody]string userName) { - return UserHelper.RetrieveUserByUserName(new GenericIdentity(userName)); + return UserHelper.RetrieveUserByUserName(userName); } /// diff --git a/src/admin/Bootstrap.Admin/Controllers/Api/RegisterController.cs b/src/admin/Bootstrap.Admin/Controllers/Api/RegisterController.cs index aa2d475c..d3e610e3 100644 --- a/src/admin/Bootstrap.Admin/Controllers/Api/RegisterController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/Api/RegisterController.cs @@ -25,7 +25,7 @@ namespace Bootstrap.Admin.Controllers.Api [HttpGet] public bool Get(string userName) { - return UserHelper.RetrieveUserByUserName(new GenericIdentity(userName)) == null && !UserHelper.RetrieveNewUsers().Any(u => u.UserName == userName); + return UserHelper.RetrieveUserByUserName(userName) == null && !UserHelper.RetrieveNewUsers().Any(u => u.UserName == userName); } /// @@ -58,7 +58,7 @@ namespace Bootstrap.Admin.Controllers.Api [HttpPut] public bool Put([FromBody]ResetUser user) { - if (UserHelper.RetrieveUserByUserName(new GenericIdentity(user.UserName)) == null) return true; + if (UserHelper.RetrieveUserByUserName(user.UserName) == null) return true; return UserHelper.ForgotPassword(user); } } diff --git a/src/admin/Bootstrap.Admin/Controllers/HomeController.cs b/src/admin/Bootstrap.Admin/Controllers/HomeController.cs index eaf5e393..719657b2 100644 --- a/src/admin/Bootstrap.Admin/Controllers/HomeController.cs +++ b/src/admin/Bootstrap.Admin/Controllers/HomeController.cs @@ -18,7 +18,7 @@ namespace Bootstrap.Admin.Controllers /// public IActionResult Index() { - var model = new HeaderBarModel(User.Identity); + var model = new HeaderBarModel(User.Identity.Name); if (string.IsNullOrEmpty(model.UserName)) return Redirect(Request.PathBase + CookieAuthenticationDefaults.LogoutPath); var homeUrl = DictHelper.RetrieveHomeUrl(model.AppId); diff --git a/src/admin/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs b/src/admin/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs index 16354921..65993bec 100644 --- a/src/admin/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs +++ b/src/admin/Bootstrap.Admin/HealthChecks/DBHealthCheck.cs @@ -69,7 +69,7 @@ namespace Bootstrap.Admin.HealthChecks try { DbContextManager.Exception = null; - var user = UserHelper.RetrieveUserByUserName(new GenericIdentity(userName)); + var user = UserHelper.RetrieveUserByUserName(userName); displayName = user?.DisplayName; roles = string.Join(",", RoleHelper.RetrievesByUserName(userName) ?? new string[0]); menusCount = MenuHelper.RetrieveMenusByUserName(userName)?.Count() ?? 0; diff --git a/src/admin/Bootstrap.Admin/Models/HeaderBarModel.cs b/src/admin/Bootstrap.Admin/Models/HeaderBarModel.cs index 21c4394f..f476e335 100644 --- a/src/admin/Bootstrap.Admin/Models/HeaderBarModel.cs +++ b/src/admin/Bootstrap.Admin/Models/HeaderBarModel.cs @@ -13,10 +13,10 @@ namespace Bootstrap.Admin.Models /// /// 默认构造函数 /// - /// - public HeaderBarModel(IIdentity identity) + /// + public HeaderBarModel(string userName) { - var user = UserHelper.RetrieveUserByUserName(identity); + var user = UserHelper.RetrieveUserByUserName(userName); if (user != null) { Icon = user.Icon.Contains("://", StringComparison.OrdinalIgnoreCase) ? user.Icon : string.Format("{0}{1}", DictHelper.RetrieveIconFolderPath(), user.Icon); diff --git a/src/admin/Bootstrap.Admin/Models/LockModel.cs b/src/admin/Bootstrap.Admin/Models/LockModel.cs index 7ba85d35..17408302 100644 --- a/src/admin/Bootstrap.Admin/Models/LockModel.cs +++ b/src/admin/Bootstrap.Admin/Models/LockModel.cs @@ -10,8 +10,8 @@ namespace Bootstrap.Admin.Models /// /// 构造函数 /// - /// - public LockModel(ControllerBase controller) : base(controller.User.Identity) + /// + public LockModel(string userName) : base(userName) { } diff --git a/src/admin/Bootstrap.Admin/Models/NavigatorBarModel.cs b/src/admin/Bootstrap.Admin/Models/NavigatorBarModel.cs index 60fb1537..9ef31894 100644 --- a/src/admin/Bootstrap.Admin/Models/NavigatorBarModel.cs +++ b/src/admin/Bootstrap.Admin/Models/NavigatorBarModel.cs @@ -16,7 +16,7 @@ namespace Bootstrap.Admin.Models /// 构造函数 /// /// - public NavigatorBarModel(ControllerBase controller) : base(controller.User.Identity) + public NavigatorBarModel(ControllerBase controller) : base(controller.User.Identity.Name) { Navigations = MenuHelper.RetrieveSystemMenus(UserName, $"~{controller.HttpContext.Request.Path}"); var authApps = AppHelper.RetrievesByUserName(controller.User.Identity.Name); diff --git a/src/admin/Bootstrap.DataAccess.MongoDB/User.cs b/src/admin/Bootstrap.DataAccess.MongoDB/User.cs index 89ffcfda..952b4453 100644 --- a/src/admin/Bootstrap.DataAccess.MongoDB/User.cs +++ b/src/admin/Bootstrap.DataAccess.MongoDB/User.cs @@ -105,7 +105,7 @@ namespace Bootstrap.DataAccess.MongoDB public override bool Save(DataAccess.User user) { // 已经存在或者已经在新用户中了 - if (UserHelper.RetrieveUserByUserName(new GenericIdentity(user.UserName)) != null || UserHelper.RetrieveNewUsers().Any(u => u.UserName == user.UserName)) return false; + if (UserHelper.RetrieveUserByUserName(user.UserName) != null || UserHelper.RetrieveNewUsers().Any(u => u.UserName == user.UserName)) return false; user.PassSalt = LgbCryptography.GenerateSalt(); user.Password = LgbCryptography.ComputeHash(user.Password, user.PassSalt); diff --git a/src/admin/Bootstrap.DataAccess/Helper/TraceHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/TraceHelper.cs index 203aa6f0..5c8ac959 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/TraceHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/TraceHelper.cs @@ -22,7 +22,7 @@ namespace Bootstrap.DataAccess { if (context.User.Identity.IsAuthenticated) { - var user = UserHelper.RetrieveUserByUserName(context.User.Identity); + var user = UserHelper.RetrieveUserByUserName(context.User.Identity.Name); // user == null 以前登录过客户端保留了 Cookie 但是用户名可能被系统删除 // link bug: https://gitee.com/LongbowEnterprise/BootstrapAdmin/issues/I123MH diff --git a/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs b/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs index 68c5628f..519d0fbf 100644 --- a/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs +++ b/src/admin/Bootstrap.DataAccess/Helper/UserHelper.cs @@ -293,9 +293,9 @@ namespace Bootstrap.DataAccess /// /// 通过登录名获取登录用户方法 /// - /// + /// /// - public static BootstrapUser RetrieveUserByUserName(IIdentity identity) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, identity.Name), k => DbContextManager.Create().RetrieveUserByUserName(identity.Name), RetrieveUsersByNameDataKey); + public static BootstrapUser RetrieveUserByUserName(string userName) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName), k => DbContextManager.Create().RetrieveUserByUserName(userName), RetrieveUsersByNameDataKey); /// /// 通过登录账号获得用户信息 diff --git a/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs b/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs index de6094be..c87be5b1 100644 --- a/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs +++ b/test/UnitTest/Bootstrap.DataAccess/SQLServer/UsersTest.cs @@ -135,7 +135,7 @@ namespace Bootstrap.DataAccess.SqlServer [Fact] public void RetrieveUserByUserName_Ok() { - var usr = UserHelper.RetrieveUserByUserName(new GenericIdentity("Admin")); + var usr = UserHelper.RetrieveUserByUserName("Admin"); Assert.Equal("Administrator", usr.DisplayName); }