重构用户注册、保存、更新模块

This commit is contained in:
Argo-Lenovo 2016-11-11 14:32:52 +08:00
parent 942d063e45
commit 676bf963e6
9 changed files with 34 additions and 53 deletions

View File

@ -73,9 +73,11 @@ namespace Bootstrap.Admin.Controllers
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
[AllowAnonymous] [AllowAnonymous]
public ActionResult Register(string userName, string displayName, string password, string description) public ActionResult Register(User p)
{ {
var result = UserHelper.RegisterUser(userName, displayName, password, description); if (string.IsNullOrEmpty(p.UserName) || string.IsNullOrEmpty(p.Password) || string.IsNullOrEmpty(p.DisplayName) || string.IsNullOrEmpty(p.Description)) return View();
p.UserStatus = 1;
var result = UserHelper.SaveUser(p);
if (result) if (result)
{ {
return Redirect("/Content/html/RegResult.html"); return Redirect("/Content/html/RegResult.html");

View File

@ -101,7 +101,7 @@ namespace Bootstrap.DataAccess
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
} }
ret = true; ret = true;
CacheCleanUtility.ClearCache(dictIds: p.ID == 0 ? "" : p.ID.ToString()); CacheCleanUtility.ClearCache(dictIds: p.ID == 0 ? string.Empty : p.ID.ToString());
} }
catch (DbException ex) catch (DbException ex)
{ {

View File

@ -101,7 +101,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccess.ToDBValue(p.Description), ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccess.ToDBValue(p.Description), ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
} }
CacheCleanUtility.ClearCache(groupIds: p.ID == 0 ? "" : p.ID.ToString()); CacheCleanUtility.ClearCache(groupIds: p.ID == 0 ? string.Empty : p.ID.ToString());
ret = true; ret = true;
} }
catch (DbException ex) catch (DbException ex)

View File

@ -96,7 +96,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
} }
CacheCleanUtility.ClearCache(logIds: p.ID == 0 ? "" : p.ID.ToString()); CacheCleanUtility.ClearCache(logIds: p.ID == 0 ? string.Empty : p.ID.ToString());
ret = true; ret = true;
} }
catch (DbException ex) catch (DbException ex)

View File

@ -143,7 +143,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
} }
CacheCleanUtility.ClearCache(menuIds: p.ID == 0 ? "" : p.ID.ToString()); CacheCleanUtility.ClearCache(menuIds: p.ID == 0 ? string.Empty : p.ID.ToString());
ret = true; ret = true;
} }
catch (DbException ex) catch (DbException ex)

View File

@ -184,7 +184,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
} }
CacheCleanUtility.ClearCache(roleIds: p.ID == 0 ? "" : p.ID.ToString()); CacheCleanUtility.ClearCache(roleIds: p.ID == 0 ? string.Empty : p.ID.ToString());
ret = true; ret = true;
} }
catch (DbException ex) catch (DbException ex)

View File

@ -35,5 +35,9 @@ namespace Bootstrap.DataAccess
/// 获得/设置 用户的申请理由 /// 获得/设置 用户的申请理由
/// </summary> /// </summary>
public string Description { get; set; } public string Description { get; set; }
/// <summary>
/// 获得/设置 用户当前状态 0 表示管理员注册用户 1 表示用户自己注册 2 表示管理员批复
/// </summary>
public int UserStatus { get; set; }
} }
} }

View File

@ -126,17 +126,11 @@ namespace Bootstrap.DataAccess
/// <returns></returns> /// <returns></returns>
public static bool SaveUser(User p) public static bool SaveUser(User p)
{ {
string type = "";
if (p == null) throw new ArgumentNullException("p");
if (p.UserName.Length > 50) p.UserName.Substring(0, 50); if (p.UserName.Length > 50) p.UserName.Substring(0, 50);
p.PassSalt = LgbCryptography.GenerateSalt(); p.PassSalt = LgbCryptography.GenerateSalt();
p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt);
if (p.ID == 0 && p.Description.Length > 500) p.Description.Substring(0, 500); if (p.ID == 0 && p.Description.Length > 500) p.Description.Substring(0, 500);
if ((p.ApprovedTime == null) || (p.ApprovedTime == DateTime.MinValue)) bool ret = false;
type = "0";
else
type = "1";
bool ret = false;
try try
{ {
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers")) using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers"))
@ -146,11 +140,11 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@password", p.Password, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@password", p.Password, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@passSalt", p.PassSalt, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@passSalt", p.PassSalt, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@displayName", p.DisplayName, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@displayName", p.DisplayName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", p.Description, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", DBAccess.ToDBValue(p.Description), ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@type", type, ParameterDirection.Input)); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userStatus", p.UserStatus, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
} }
CacheCleanUtility.ClearCache(userIds: p.ID.ToString()); CacheCleanUtility.ClearCache(userIds: p.ID == 0 ? string.Empty : p.ID.ToString());
ret = true; ret = true;
} }
catch (DbException ex) catch (DbException ex)
@ -326,15 +320,5 @@ namespace Bootstrap.DataAccess
} }
return ret; return ret;
} }
/// <summary>
///
/// </summary>
/// <returns></returns>
public static bool RegisterUser(string userName, string displayName, string password, string description)
{
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(displayName) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(description))
return false;
return SaveUser(new User() { UserName = userName, DisplayName = displayName, Password = password, Description = description });
}
} }
} }

View File

@ -162,7 +162,6 @@ GO
SET QUOTED_IDENTIFIER ON SET QUOTED_IDENTIFIER ON
GO GO
Drop PROCEDURE Proc_SaveUsers Drop PROCEDURE Proc_SaveUsers
GO GO
-- ============================================= -- =============================================
@ -173,13 +172,12 @@ GO
CREATE PROCEDURE [dbo].[Proc_SaveUsers] CREATE PROCEDURE [dbo].[Proc_SaveUsers]
-- Add the parameters for the stored procedure here -- Add the parameters for the stored procedure here
@id int, @id int,
@userName varchar(max), @userName varchar(50),
@password varchar(max), @password varchar(50),
@passSalt varchar(max), @passSalt varchar(50),
@displayName varchar(max), @displayName nvarchar(50),
@description varchar(max), @description nvarchar(500),
--type=0type=1 @userStatus int = 0 --0 1 2
@type varchar(max)
WITH ENCRYPTION WITH ENCRYPTION
AS AS
BEGIN BEGIN
@ -187,27 +185,20 @@ BEGIN
-- interfering with SELECT statements. -- interfering with SELECT statements.
SET NOCOUNT ON; SET NOCOUNT ON;
SET XACT_ABORT ON; SET XACT_ABORT ON;
-- Insert statements for procedure here -- Insert statements for procedure here
if @id=0 if @userStatus = 2
begin begin
if @type='0' update Users set ApprovedTime = GETDATE() where ID = @id
begin end
Insert Into Users (UserName, Password, PassSalt, DisplayName, RegisterTime, Description)values(@userName,@password,@passSalt,@displayName,GETDATE(),@description) else
insert into Notifications(Category,Title,Content,RegisterTime,Status)values(0,@userName,@description,GETDATE(),0) begin
end declare @approveTime datetime = null
else if @userStatus = 0 set @approveTime = GETDATE()
begin Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, GETDATE(), @approveTime, @description)
Insert Into Users (UserName, Password, PassSalt, DisplayName, RegisterTime,ApprovedTime,Description)values(@userName,@password,@passSalt,@displayName,GETDATE(),GETDATE(),@description) end
end
end
else
begin
Update Users set UserName =@userName, Password =@password, PassSalt =@passSalt, DisplayName =@displayName where ID = @id
end
END END
GO GO
Drop PROCEDURE Proc_ProcessRegisterUser Drop PROCEDURE Proc_ProcessRegisterUser
GO GO
-- ============================================= -- =============================================