📄 userbussiness.cs
字号:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// 关于用户的业务层操作
/// </summary>
public class UserBussiness
{
BBSTableTableAdapters.UserTableAdapter userTable;
public UserBussiness()
{
userTable = new BBSTableTableAdapters.UserTableAdapter();
}
/// <summary>
/// 获取所有注册用户列表
/// </summary>
/// <returns>注册用户列表</returns>
public DataTable GetUserList()
{
return userTable.GetUserList();
}
/// <summary>
/// 根据ID删除用户
/// </summary>
/// <param name="pUserID">用户ID</param>
public void DeleteUserByID(int UserID)
{
userTable.DeleteUserByID(UserID);
}
/// <summary>
/// 根据ID获取用户信息
/// </summary>
/// <param name="pUser">用户实体</param>
/// <returns>若指定ID用户信息不存在返回false,否则返回true</returns>
public bool GetUserByID(ref UserEntity pUser)
{
DataTable userInfo = userTable.GetUserByID(pUser.UserID);
if (userInfo.Rows.Count == 0)
return false;
else
{
SetUserInfo(userInfo, ref pUser);
return true;
}
}
/// <summary>
/// 设置用户信息
/// </summary>
/// <param name="pUserTable"></param>
private void SetUserInfo(DataTable pUserTable,ref UserEntity pUser)
{
pUser.UserName = pUserTable.Rows[0]["UserName"].ToString();
pUser.TrueName = pUserTable.Rows[0]["TrueName"].ToString();
if (Convert.ToInt32(pUserTable.Rows[0]["Actived"]) == 0)
pUser.Actived = false;
else
pUser.Actived = true;
if (!Convert.IsDBNull(pUserTable.Rows[0]["Address"]))
pUser.Address = pUserTable.Rows[0]["Address"].ToString();
if (!Convert.IsDBNull(pUserTable.Rows[0]["ProtectionAnswer"]))
pUser.Answer = pUserTable.Rows[0]["ProtectionAnswer"].ToString();
pUser.CreateDate = (DateTime)pUserTable.Rows[0]["CreateDate"];
pUser.Email = pUserTable.Rows[0]["Email"].ToString();
if (!Convert.IsDBNull(pUserTable.Rows[0]["LastLogin"]))
pUser.LastLogin = (DateTime)pUserTable.Rows[0]["LastLogin"];
pUser.Posted = Convert.ToInt32(pUserTable.Rows[0]["Posted"]);
if (!Convert.IsDBNull(pUserTable.Rows[0]["ProtectQuestion"]))
pUser.Question = pUserTable.Rows[0]["ProtectQuestion"].ToString();
pUser.RePosted = Convert.ToInt32(pUserTable.Rows[0]["RePosted"]);
if (!Convert.IsDBNull(pUserTable.Rows[0]["RoleID"]))
pUser.RoleID = Convert.ToInt32(pUserTable.Rows[0]["RoleID"]);
if (Convert.ToInt32(pUserTable.Rows[0]["Sex"]) == 0)
pUser.Sex = false;
else
pUser.Sex = true;
if (Convert.ToInt32(pUserTable.Rows[0]["ShowEmail"]) == 0)
pUser.ShowEmail = false;
else
pUser.ShowEmail = true;
pUser.RePosted = Convert.ToInt32(pUserTable.Rows[0]["RePosted"]);
}
/// <summary>
/// 判断指定ID用户是否激活
/// </summary>
/// <param name="pUserID">用户ID</param>
/// <returns>true已激活,false尚未激活</returns>
public bool IsUserActived(int pUserID)
{
if (userTable.IsUserActived(pUserID) == null)
return false;
else
return true;
}
/// <summary>
/// 判断指定用户是否存在,若存在返回其ID
/// </summary>
/// <returns>true存在,false不存在</returns>
public bool IsUserNameExist(ref UserEntity pUser)
{
object userID = userTable.IsUserNameExist(pUser.UserName);
if (userID == null)
return false;
else
{
pUser.UserID = Convert.ToInt32(userID);
return true;
}
}
/// <summary>
/// 用户登录
/// </summary>
/// <param name="pUser">用户实体</param>
/// <returns>true登录成功,false登录失败</returns>
public bool Login(ref UserEntity pUser)
{
//若用户名不存在
if (!IsUserNameExist(ref pUser))
return false;
//若用户尚未激活
if (!IsUserActived(pUser.UserID))
return false;
if (userTable.Login(pUser.UserName, Common.md5(pUser.Password)).Rows.Count == 0)
return false;
else
{
SetUserInfo(userTable.AfterLogin(pUser.UserName), ref pUser);
return true;
}
}
/// <summary>
/// 修改用户信息
/// </summary>
/// <param name="pUser">用户实体</param>
public void ModifyInfo(UserEntity pUser)
{
if (pUser.ShowEmail)
userTable.ModifyInfo(pUser.UserID, pUser.TrueName, pUser.Address, pUser.Email, pUser.Question, pUser.Answer, 1);
else
userTable.ModifyInfo(pUser.UserID, pUser.TrueName, pUser.Address, pUser.Email, pUser.Question, pUser.Answer, 0);
}
/// <summary>
/// 根据用户名修改用户密码
/// </summary>
/// <param name="pUser">用户实体</param>
public void ModifyPassword(UserEntity pUser)
{
userTable.ModifyPassword(pUser.UserName, Common.md5(pUser.Password));
}
/// <summary>
/// 修改用户角色ID
/// </summary>
/// <param name="pUserID">用户ID</param>
/// <param name="pRoleID">角色ID</param>
public void ModifyRoleByID(int pUserID, int pRoleID)
{
userTable.ModifyRoleByID(pUserID, pRoleID);
}
/// <summary>
/// 注册用户并返回激活码
/// </summary>
/// <param name="pUser">用户实体</param>
/// <returns>激活码</returns>
public string Register(ref UserEntity pUser)
{
int iSex = 0;
int iShowEmail = 0;
if (pUser.Sex)
iSex = 1;
if (pUser.ShowEmail)
iShowEmail = 1;
object userID= userTable.Register(pUser.UserName, Common.md5(pUser.Password), iSex, pUser.Email,
pUser.TrueName, pUser.Answer, pUser.Question, pUser.Answer, iShowEmail,pUser.RoleID);
if (userID == null)
{
return null;
}
else
{
ActivationBussiness actBuss = new ActivationBussiness();
ActivationEntity act = new ActivationEntity();
pUser.UserID = Convert.ToInt32(userID);
act.UserID = Convert.ToInt32(userID);
actBuss.AddActivation(ref act);
return act.ActivationCode;
}
}
/// <summary>
/// 设置用户激活状态,并获取用户信息
/// </summary>
/// <param name="pUser">用户实体</param>
/// <returns>已激活actived;激活码和用户ID不一致failed;激活成功success</returns>
public string SetUserActived(ref UserEntity pUser,ActivationEntity pAct)
{
object actived=userTable.IsUserActived(pUser.UserID);
if (actived!=null)
return "actived";
ActivationBussiness actBuss = new ActivationBussiness();
if (actBuss.IsActivationInfoCorrect(ref pAct))
{
pUser.UserID = pAct.UserID;
userTable.SetUserActived(pUser.UserID,pAct.ActivationID);
GetUserByID(ref pUser);
return "success";
}
else
{
return "failed";
}
}
/// <summary>
/// 获取用户密码保护问题
/// </summary>
/// <param name="pUser">用户实体</param>
/// <returns>若没有则返回false;若有则返回true</returns>
public bool GetUserQuesion(ref UserEntity pUser)
{
object userQuestion = userTable.GetUserQuestion(pUser.UserID);
if (userQuestion == null)
return false;
else
{
pUser.Question = (string)userQuestion;
return true;
}
}
/// <summary>
/// 检查用户密码保护答案是否正确
/// </summary>
/// <param name="pUser">用户实体</param>
/// <returns>true正确;false错误</returns>
public bool IsUserAnswerRight(UserEntity pUser)
{
object userAnswer = userTable.ChecUserAnswer(pUser.UserID, pUser.Answer);
if (userAnswer == null)
return false;
else
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -