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

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>
/// <returns></returns>
[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)
{
return Redirect("/Content/html/RegResult.html");

View File

@ -101,7 +101,7 @@ namespace Bootstrap.DataAccess
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
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)
{

View File

@ -101,7 +101,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", DBAccess.ToDBValue(p.Description), ParameterDirection.Input));
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;
}
catch (DbException ex)

View File

@ -96,7 +96,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RequestUrl", p.RequestUrl, ParameterDirection.Input));
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;
}
catch (DbException ex)

View File

@ -143,7 +143,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
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;
}
catch (DbException ex)

View File

@ -184,7 +184,7 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
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;
}
catch (DbException ex)

View File

@ -35,5 +35,9 @@ namespace Bootstrap.DataAccess
/// 获得/设置 用户的申请理由
/// </summary>
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>
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);
p.PassSalt = LgbCryptography.GenerateSalt();
p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt);
if (p.ID == 0 && p.Description.Length > 500) p.Description.Substring(0, 500);
if ((p.ApprovedTime == null) || (p.ApprovedTime == DateTime.MinValue))
type = "0";
else
type = "1";
bool ret = false;
bool ret = false;
try
{
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("@passSalt", p.PassSalt, 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("@type", type, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", DBAccess.ToDBValue(p.Description), ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userStatus", p.UserStatus, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
CacheCleanUtility.ClearCache(userIds: p.ID.ToString());
CacheCleanUtility.ClearCache(userIds: p.ID == 0 ? string.Empty : p.ID.ToString());
ret = true;
}
catch (DbException ex)
@ -326,15 +320,5 @@ namespace Bootstrap.DataAccess
}
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
GO
Drop PROCEDURE Proc_SaveUsers
GO
-- =============================================
@ -173,13 +172,12 @@ GO
CREATE PROCEDURE [dbo].[Proc_SaveUsers]
-- Add the parameters for the stored procedure here
@id int,
@userName varchar(max),
@password varchar(max),
@passSalt varchar(max),
@displayName varchar(max),
@description varchar(max),
--type=0type=1
@type varchar(max)
@userName varchar(50),
@password varchar(50),
@passSalt varchar(50),
@displayName nvarchar(50),
@description nvarchar(500),
@userStatus int = 0 --0 1 2
WITH ENCRYPTION
AS
BEGIN
@ -187,27 +185,20 @@ BEGIN
-- interfering with SELECT statements.
SET NOCOUNT ON;
SET XACT_ABORT ON;
-- Insert statements for procedure here
if @id=0
begin
if @type='0'
begin
Insert Into Users (UserName, Password, PassSalt, DisplayName, RegisterTime, Description)values(@userName,@password,@passSalt,@displayName,GETDATE(),@description)
insert into Notifications(Category,Title,Content,RegisterTime,Status)values(0,@userName,@description,GETDATE(),0)
end
else
begin
Insert Into Users (UserName, Password, PassSalt, DisplayName, RegisterTime,ApprovedTime,Description)values(@userName,@password,@passSalt,@displayName,GETDATE(),GETDATE(),@description)
end
end
else
begin
Update Users set UserName =@userName, Password =@password, PassSalt =@passSalt, DisplayName =@displayName where ID = @id
end
if @userStatus = 2
begin
update Users set ApprovedTime = GETDATE() where ID = @id
end
else
begin
declare @approveTime datetime = null
if @userStatus = 0 set @approveTime = GETDATE()
Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, GETDATE(), @approveTime, @description)
end
END
GO
Drop PROCEDURE Proc_ProcessRegisterUser
GO
-- =============================================