修改BUG:用户管理页面保存按钮对已经存在的用户保存时出错,增加UpdateUser方法

This commit is contained in:
Argo-MacBookPro 2018-09-13 19:51:11 +08:00
parent 3e74b7bf07
commit 0ce25143fc
2 changed files with 49 additions and 20 deletions

View File

@ -77,11 +77,20 @@ namespace Bootstrap.Admin.Controllers.Api
/// <param name="value"></param> /// <param name="value"></param>
[HttpPost] [HttpPost]
public bool Post([FromBody]User value) public bool Post([FromBody]User value)
{
var ret = false;
if (value.Id == 0)
{ {
value.Description = string.Format("管理员{0}创建用户", User.Identity.Name); value.Description = string.Format("管理员{0}创建用户", User.Identity.Name);
value.ApprovedBy = User.Identity.Name; value.ApprovedBy = User.Identity.Name;
value.ApprovedTime = DateTime.Now; value.ApprovedTime = DateTime.Now;
return UserHelper.SaveUser(value); ret = UserHelper.SaveUser(value);
}
else
{
ret = UserHelper.UpdateUser(value.Id, value.Password, value.DisplayName);
}
return ret;
} }
/// <summary> /// <summary>
/// ///

View File

@ -92,14 +92,13 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers")) using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers"))
{ {
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); 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; return ret;
} }
/// <summary> /// <summary>
/// 保存新建/更新的用户信息 /// 保存新建
/// </summary> /// </summary>
/// <param name="p"></param> /// <param name="p"></param>
/// <returns></returns> /// <returns></returns>
@ -122,8 +121,32 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedTime", approvedTime)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedTime", approvedTime));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", p.Description)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", p.Description));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1;
if (ret) CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="password"></param>
/// <param name="displayName"></param>
/// <returns></returns>
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<int>() : new List<int>() { id });
} }
CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
return ret; return ret;
} }
/// <summary> /// <summary>
@ -141,8 +164,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", id)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", id));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", approvedBy)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", approvedBy));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
if (ret) CacheCleanUtility.ClearCache(userIds: new List<int>() { id });
} }
CacheCleanUtility.ClearCache(userIds: new List<int>() { id });
return ret; return ret;
} }
/// <summary> /// <summary>
@ -165,8 +188,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", newPassword)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", newPassword));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", passSalt)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", passSalt));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
ret = true;
} }
} }
return ret; return ret;
@ -187,8 +209,8 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", rejectBy)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", rejectBy));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", "未填写")); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", "未填写"));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1; ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == -1;
if (ret) CacheCleanUtility.ClearCache(userIds: new List<int>() { id });
} }
CacheCleanUtility.ClearCache(userIds: new List<int>() { id });
return ret; return ret;
} }
/// <summary> /// <summary>
@ -353,9 +375,8 @@ namespace Bootstrap.DataAccess
{ {
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
ret = true;
} }
return ret; return ret;
} }
@ -374,7 +395,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@DisplayName", displayName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@DisplayName", displayName));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1; ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
} }
return ret; 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("@cssName", DBAccessFactory.ToDBValue(cssName)));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); ret = DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd) == 1;
CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*"); if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
ret = true;
} }
return ret; return ret;
} }