using Bootstrap.Security; using Bootstrap.Security.DataAccess; using Longbow.Security.Cryptography; using System; using System.Collections.Generic; using System.Linq; namespace Bootstrap.DataAccess { /// /// 用户表实体类 /// public class User : BootstrapUser { /// /// 获得/设置 用户主键ID /// public string Id { get; set; } /// /// 获取/设置 密码 /// public string Password { get; set; } /// /// 获取/设置 密码盐 /// public string PassSalt { get; set; } /// /// 获取/设置 角色用户关联状态 checked 标示已经关联 '' 标示未关联 /// 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 批复新用户注册操作 /// public UserStates UserStatus { get; set; } /// /// 获得/设置 通知描述 2分钟内为刚刚 /// public string Period { get; set; } /// /// 获得/设置 新密码 /// public string NewPassword { get; set; } /// /// 验证用户登陆账号与密码正确 /// /// /// /// public virtual bool Authenticate(string userName, string password) { var user = DbManager.Db.SingleOrDefault("select Password, PassSalt from Users where ApprovedTime is not null and UserName = @0", userName); return !string.IsNullOrEmpty(user.PassSalt) && user.Password == LgbCryptography.ComputeHash(password, user.PassSalt); } /// /// /// /// /// /// /// 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.Db.Update(sql, newPassword, passSalt, userName) == 1; } return ret; } /// /// 查询所有用户 /// /// /// public virtual IEnumerable Retrieves() => DbManager.Db.Fetch("select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null"); /// /// 查询所有的新注册用户 /// /// public virtual IEnumerable RetrieveNewUsers() => DbManager.Db.Fetch("select ID, UserName, DisplayName, RegisterTime, Description from Users Where ApprovedTime is null order by RegisterTime desc"); /// /// 删除用户 /// /// public virtual bool Delete(IEnumerable value) { bool ret = false; var db = DbManager.Db; 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; } /// /// 新建前台User View调用/注册用户调用 /// /// /// public virtual bool Save(User p) { var ret = false; if (string.IsNullOrEmpty(p.Id) && p.Description.Length > 500) p.Description = p.Description.Substring(0, 500); if (p.UserName.Length > 50) p.UserName = p.UserName.Substring(0, 50); p.PassSalt = LgbCryptography.GenerateSalt(); p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); p.RegisterTime = DateTime.Now; var db = DbManager.Db; try { db.BeginTransaction(); if (!db.Exists("where UserName = @0", p.UserName)) { db.Insert(p); db.Execute("insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = @0", p.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.Db.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.Db.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.Db; 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.Db.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.Db; try { db.BeginTransaction(); //删除用户角色表该角色所有的用户 db.Execute("delete from UserRole where RoleID = @0", roleId); db.InsertBulk("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.Db.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.Db; try { db.BeginTransaction(); //删除用户角色表该角色所有的用户 db.Execute("delete from UserGroup where GroupID = @0", groupId); db.InsertBulk("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.Db.Update("set Icon = @1 where UserName = @0", userName, iconName) == 1; /// /// /// /// /// /// public virtual bool SaveDisplayName(string userName, string displayName) => DbManager.Db.Update("set DisplayName = @1 where UserName = @0", userName, displayName) == 1; /// /// 根据用户名更改用户皮肤 /// /// /// /// public virtual bool SaveUserCssByName(string userName, string cssName) => DbManager.Db.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 } }