From 0ce25143fcf57a77d1871476c367fa615ef93e84 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Thu, 13 Sep 2018 19:51:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BUG=EF=BC=9A=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=AE=A1=E7=90=86=E9=A1=B5=E9=9D=A2=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E6=8C=89=E9=92=AE=E5=AF=B9=E5=B7=B2=E7=BB=8F=E5=AD=98=E5=9C=A8?= =?UTF-8?q?=E7=9A=84=E7=94=A8=E6=88=B7=E4=BF=9D=E5=AD=98=E6=97=B6=E5=87=BA?= =?UTF-8?q?=E9=94=99=EF=BC=8C=E5=A2=9E=E5=8A=A0UpdateUser=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/UsersController.cs | 17 ++++-- Bootstrap.DataAccess/UserHelper.cs | 52 +++++++++++++------ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/UsersController.cs b/Bootstrap.Admin/Controllers/Api/UsersController.cs index 42a90f95..367eb3ac 100644 --- a/Bootstrap.Admin/Controllers/Api/UsersController.cs +++ b/Bootstrap.Admin/Controllers/Api/UsersController.cs @@ -78,10 +78,19 @@ namespace Bootstrap.Admin.Controllers.Api [HttpPost] public bool Post([FromBody]User value) { - value.Description = string.Format("管理员{0}创建用户", User.Identity.Name); - value.ApprovedBy = User.Identity.Name; - value.ApprovedTime = DateTime.Now; - return UserHelper.SaveUser(value); + var ret = false; + if (value.Id == 0) + { + value.Description = string.Format("管理员{0}创建用户", User.Identity.Name); + value.ApprovedBy = User.Identity.Name; + value.ApprovedTime = DateTime.Now; + ret = UserHelper.SaveUser(value); + } + else + { + ret = UserHelper.UpdateUser(value.Id, value.Password, value.DisplayName); + } + return ret; } /// /// diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs index 99acacdf..bc7e779e 100644 --- a/Bootstrap.DataAccess/UserHelper.cs +++ b/Bootstrap.DataAccess/UserHelper.cs @@ -92,14 +92,13 @@ namespace Bootstrap.DataAccess using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers")) { cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; + if (ret) CacheCleanUtility.ClearCache(userIds: value); } - CacheCleanUtility.ClearCache(userIds: value); - ret = true; return ret; } /// - /// 保存新建/更新的用户信息 + /// 保存新建 /// /// /// @@ -122,8 +121,32 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedTime", approvedTime)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", p.Description)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; + if (ret) CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? new List() : new List() { p.Id }); + } + return ret; + } + /// + /// + /// + /// + /// + /// + /// + public static bool UpdateUser(int id, string password, string displayName) + { + bool ret = false; + string sql = "Update Users set Password = @Password, PassSalt = @PassSalt, DisplayName = @DisplayName where ID = @id"; + var passSalt = LgbCryptography.GenerateSalt(); + var newPassword = LgbCryptography.ComputeHash(password, passSalt); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", id)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@DisplayName", displayName)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", newPassword)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", passSalt)); + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; + if (ret) CacheCleanUtility.ClearCache(userIds: id == 0 ? new List() : new List() { id }); } - CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? new List() : new List() { p.Id }); return ret; } /// @@ -141,8 +164,8 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", id)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", approvedBy)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; + if (ret) CacheCleanUtility.ClearCache(userIds: new List() { id }); } - CacheCleanUtility.ClearCache(userIds: new List() { id }); return ret; } /// @@ -165,8 +188,7 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", newPassword)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", passSalt)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - ret = true; + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; } } return ret; @@ -187,8 +209,8 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", rejectBy)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", "未填写")); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; + if (ret) CacheCleanUtility.ClearCache(userIds: new List() { id }); } - CacheCleanUtility.ClearCache(userIds: new List() { id }); return ret; } /// @@ -353,9 +375,8 @@ namespace Bootstrap.DataAccess { cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); - ret = true; + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; + if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); } return ret; } @@ -374,7 +395,7 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@DisplayName", displayName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; - CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); + if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); } return ret; } @@ -392,9 +413,8 @@ namespace Bootstrap.DataAccess { cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cssName", DBAccessFactory.ToDBValue(cssName))); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); - ret = true; + ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; + if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); } return ret; }