using Bootstrap.Security;
using Bootstrap.Security.DataAccess;
using Longbow.Security.Cryptography;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
///
/// 用户表实体类
///
[TableName("Users")]
public class User : BootstrapUser
{
///
/// 获得/设置 用户主键ID
///
public string Id { get; set; }
///
/// 获取/设置 密码
///
public string Password { get; set; }
///
/// 获取/设置 密码盐
///
public string PassSalt { get; set; }
///
/// 获取/设置 角色用户关联状态 checked 标示已经关联 '' 标示未关联
///
[ResultColumn]
public string Checked { get; set; }
///
/// 获得/设置 用户注册时间
///
public DateTime RegisterTime { get; set; }
///
/// 获得/设置 用户被批复时间
///
public DateTime? ApprovedTime { get; set; }
///
/// 获得/设置 用户批复人
///
public string ApprovedBy { get; set; }
///
/// 获得/设置 用户的申请理由
///
public string Description { get; set; }
///
/// 获得/设置 用户当前状态 0 表示管理员注册用户 1 表示用户注册 2 表示更改密码 3 表示更改个人皮肤 4 表示更改显示名称 5 批复新用户注册操作
///
[ResultColumn]
public UserStates UserStatus { get; set; }
///
/// 获得/设置 通知描述 2分钟内为刚刚
///
[ResultColumn]
public string Period { get; set; }
///
/// 获得/设置 新密码
///
[ResultColumn]
public string NewPassword { get; set; }
///
/// 获得/设置 是否重置密码
///
[Ignore]
public int IsReset { get; set; }
///
/// 验证用户登陆账号与密码正确
///
///
///
///
public virtual bool Authenticate(string userName, string password)
{
var user = DbManager.Create().SingleOrDefault("select Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName);
return user != null && !string.IsNullOrEmpty(user.PassSalt) && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt);
}
///
///
///
///
///
///
public virtual bool SaveApp(string userName, string app) => DbManager.Create().Update("set App = @1 where UserName = @0", userName, app) == 1;
///
///
///
///
///
///
///
public virtual bool ChangePassword(string userName, string password, string newPass)
{
bool ret = false;
if (Authenticate(userName, password))
{
string sql = "set Password = @0, PassSalt = @1 where UserName = @2";
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(newPass, passSalt);
ret = DbManager.Create().Update(sql, newPassword, passSalt, userName) == 1;
}
return ret;
}
///
/// 查询所有用户
///
///
public virtual IEnumerable Retrieves() => DbManager.Create().Fetch("select u.ID, u.UserName, u.DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description, ru.IsReset from Users u left join (select 1 as IsReset, UserName from ResetUsers group by UserName) ru on u.UserName = ru.UserName Where ApprovedTime is not null");
///
/// 查询所有的新注册用户
///
///
public virtual IEnumerable RetrieveNewUsers() => DbManager.Create().Fetch("select ID, UserName, DisplayName, RegisterTime, Description from Users Where ApprovedTime is null order by RegisterTime desc");
///
/// 删除用户
///
///
public virtual bool Delete(IEnumerable value)
{
if (!value.Any()) return true;
bool ret = false;
var db = DbManager.Create();
try
{
var ids = string.Join(",", value);
db.BeginTransaction();
db.Execute($"Delete from UserRole where UserID in ({ids})");
db.Execute($"delete from UserGroup where UserID in ({ids})");
db.Execute($"delete from Users where ID in ({ids})");
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
///
///
///
///
///
public virtual bool ResetPassword(string userName, string password)
{
var ret = false;
var resetUser = UserHelper.RetrieveResetUserByUserName(userName);
if (resetUser == null) return ret;
string sql = "set Password = @0, PassSalt = @1 where UserName = @2";
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
var db = DbManager.Create();
try
{
db.BeginTransaction();
ret = db.Update(sql, newPassword, passSalt, userName) == 1;
if (ret) db.Execute("delete from ResetUsers where UserName = @0", userName);
db.CompleteTransaction();
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
///
///
///
///
public virtual bool ForgotPassword(ResetUser user) => ResetUserHelper.Save(user);
///
/// 新建前台User View调用/注册用户调用
///
///
///
public virtual bool Save(User user)
{
var ret = false;
user.PassSalt = LgbCryptography.GenerateSalt();
user.Password = LgbCryptography.ComputeHash(user.Password, user.PassSalt);
user.RegisterTime = DateTime.Now;
var db = DbManager.Create();
try
{
db.BeginTransaction();
if (!db.Exists("UserName = @0", user.UserName))
{
db.Insert(user);
db.Execute("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @0", user.UserName);
}
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// User List 视图保存按钮调用
///
///
///
///
///
public virtual bool Update(string id, string password, string displayName)
{
var passSalt = LgbCryptography.GenerateSalt();
var newPassword = LgbCryptography.ComputeHash(password, passSalt);
return DbManager.Create().Update("set Password = @1, PassSalt = @2, DisplayName = @3 where ID = @0", id, newPassword, passSalt, displayName) == 1;
}
///
///
///
///
///
///
public virtual bool Approve(string id, string approvedBy) => DbManager.Create().Update("set ApprovedTime = @1, ApprovedBy = @2 where ID = @0", id, DateTime.Now, approvedBy) == 1;
///
///
///
///
///
///
public virtual bool Reject(string id, string rejectBy)
{
var ret = false;
var db = DbManager.Create();
try
{
db.BeginTransaction();
db.Execute("insert into RejectUsers (UserName, DisplayName, RegisterTime, RejectedBy, RejectedTime, RejectedReason) select UserName, DisplayName, Registertime, @1, @2, @3 from Users where ID = @0", id, rejectBy, DateTime.Now, "未填写");
db.Execute("delete from UserRole where UserId = @0", id);
db.Execute("delete from UserGroup where UserId = @0", id);
db.Execute("delete from users where ID = @0", id);
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// 通过roleId获取所有用户
///
///
///
public virtual IEnumerable RetrievesByRoleId(string roleId) => DbManager.Create().Fetch("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @0 where u.ApprovedTime is not null", roleId);
///
/// 通过角色ID保存当前授权用户(插入)
///
/// 角色ID
/// 用户ID数组
///
public virtual bool SaveByRoleId(string roleId, IEnumerable userIds)
{
bool ret = false;
var db = DbManager.Create();
try
{
db.BeginTransaction();
//删除用户角色表该角色所有的用户
db.Execute("delete from UserRole where RoleID = @0", roleId);
db.InsertBatch("UserRole", userIds.Select(g => new { UserID = g, RoleID = roleId }));
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// 通过groupId获取所有用户
///
///
///
public virtual IEnumerable RetrievesByGroupId(string groupId) => DbManager.Create().Fetch("select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end Checked from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID = @0 where u.ApprovedTime is not null", groupId);
///
/// 通过部门ID保存当前授权用户(插入)
///
/// GroupID
/// 用户ID数组
///
public virtual bool SaveByGroupId(string groupId, IEnumerable userIds)
{
bool ret = false;
var db = DbManager.Create();
try
{
db.BeginTransaction();
//删除用户角色表该角色所有的用户
db.Execute("delete from UserGroup where GroupID = @0", groupId);
db.InsertBatch("UserGroup", userIds.Select(g => new { UserID = g, GroupID = groupId }));
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
///
/// 根据用户名修改用户头像
///
///
///
///
public virtual bool SaveUserIconByName(string userName, string iconName) => DbManager.Create().Update("set Icon = @1 where UserName = @0", userName, iconName) == 1;
///
///
///
///
///
///
public virtual bool SaveDisplayName(string userName, string displayName) => DbManager.Create().Update("set DisplayName = @1 where UserName = @0", userName, displayName) == 1;
///
/// 根据用户名更改用户皮肤
///
///
///
///
public virtual bool SaveUserCssByName(string userName, string cssName) => DbManager.Create().Update("set Css = @1 where UserName = @0", userName, cssName) == 1;
///
///
///
///
///
public virtual BootstrapUser RetrieveUserByUserName(string userName) => DbHelper.RetrieveUserByUserName(userName);
///
///
///
///
public override string ToString()
{
return string.Format("{0} ({1})", UserName, DisplayName);
}
}
///
///
///
public enum UserStates
{
///
///
///
ChangePassword,
///
///
///
ChangeTheme,
///
///
///
ChangeDisplayName,
///
///
///
ApproveUser,
///
///
///
RejectUser,
///
///
///
SaveApp
}
}