2017-04-03 15:56:17 +08:00
using Bootstrap.Security ;
2018-10-28 23:35:23 +08:00
using Bootstrap.Security.DataAccess ;
2018-10-28 08:29:15 +08:00
using Longbow.Security.Cryptography ;
2017-04-03 15:56:17 +08:00
using System ;
2018-10-19 23:09:52 +08:00
using System.Collections.Generic ;
2018-10-21 10:02:59 +08:00
using System.Linq ;
2016-11-06 16:01:14 +08:00
namespace Bootstrap.DataAccess
2016-10-22 16:58:31 +08:00
{
/// <summary>
/// 用户表实体类
/// </summary>
2017-04-03 15:56:17 +08:00
public class User : BootstrapUser
2016-10-22 16:58:31 +08:00
{
/// <summary>
/// 获得/设置 用户主键ID
/// </summary>
2018-10-30 13:07:29 +08:00
public string Id { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-10-22 16:58:31 +08:00
/// <summary>
/// 获取/设置 密码
/// </summary>
public string Password { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-10-22 16:58:31 +08:00
/// <summary>
2016-10-24 19:59:07 +08:00
/// 获取/设置 密码盐
2016-10-22 16:58:31 +08:00
/// </summary>
public string PassSalt { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-10-24 01:01:29 +08:00
/// <summary>
2016-10-28 20:18:12 +08:00
/// 获取/设置 角色用户关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-06 16:01:14 +08:00
/// <summary>
/// 获得/设置 用户注册时间
/// </summary>
public DateTime RegisterTime { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-06 16:01:14 +08:00
/// <summary>
/// 获得/设置 用户被批复时间
/// </summary>
2019-01-11 23:20:28 +08:00
public DateTime ? ApprovedTime { get ; set ; }
2016-11-10 10:14:27 +08:00
/// <summary>
2016-11-11 14:47:54 +08:00
/// 获得/设置 用户批复人
/// </summary>
public string ApprovedBy { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-11 14:47:54 +08:00
/// <summary>
2016-11-10 10:14:27 +08:00
/// 获得/设置 用户的申请理由
/// </summary>
public string Description { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-11 14:32:52 +08:00
/// <summary>
2018-09-07 15:53:16 +08:00
/// 获得/设置 用户当前状态 0 表示管理员注册用户 1 表示用户注册 2 表示更改密码 3 表示更改个人皮肤 4 表示更改显示名称 5 批复新用户注册操作
2016-11-11 14:32:52 +08:00
/// </summary>
2018-09-07 15:53:16 +08:00
public UserStates UserStatus { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-11 18:02:27 +08:00
/// <summary>
/// 获得/设置 通知描述 2分钟内为刚刚
/// </summary>
public string Period { get ; set ; }
2019-01-11 23:20:28 +08:00
2016-11-12 15:26:40 +08:00
/// <summary>
2016-11-16 20:06:28 +08:00
/// 获得/设置 新密码
/// </summary>
public string NewPassword { get ; set ; }
2019-01-11 23:20:28 +08:00
2017-01-03 19:47:37 +08:00
/// <summary>
2018-10-19 23:09:52 +08:00
/// 验证用户登陆账号与密码正确
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <returns></returns>
public virtual bool Authenticate ( string userName , string password )
{
2019-01-11 23:20:28 +08:00
var user = DbManager . Db . SingleOrDefault < User > ( "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 ) ;
2018-10-19 23:09:52 +08:00
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
2018-10-21 10:02:59 +08:00
///
/// </summary>
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="newPass"></param>
/// <returns></returns>
public virtual bool ChangePassword ( string userName , string password , string newPass )
{
bool ret = false ;
if ( Authenticate ( userName , password ) )
{
2019-01-11 23:20:28 +08:00
string sql = "set Password = @0, PassSalt = @1 where UserName = @2" ;
2018-10-21 10:02:59 +08:00
var passSalt = LgbCryptography . GenerateSalt ( ) ;
var newPassword = LgbCryptography . ComputeHash ( newPass , passSalt ) ;
2019-01-11 23:20:28 +08:00
ret = DbManager . Db . Update < User > ( sql , newPassword , passSalt , userName ) = = 1 ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2019-01-11 23:20:28 +08:00
2018-10-21 10:02:59 +08:00
/// <summary>
2018-10-19 23:09:52 +08:00
/// 查询所有用户
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < User > Retrieves ( ) = > DbManager . Db . Fetch < User > ( "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null" ) ;
2018-10-21 10:02:59 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询所有的新注册用户
/// </summary>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < User > RetrieveNewUsers ( ) = > DbManager . Db . Fetch < User > ( "select ID, UserName, DisplayName, RegisterTime, Description from Users Where ApprovedTime is null order by RegisterTime desc" ) ;
2018-10-19 23:09:52 +08:00
/// <summary>
/// 删除用户
/// </summary>
/// <param name="value"></param>
2019-01-11 23:20:28 +08:00
public virtual bool Delete ( IEnumerable < string > value )
2018-10-21 10:02:59 +08:00
{
2019-01-12 00:34:12 +08:00
if ( ! value . Any ( ) ) return true ;
2018-10-21 10:02:59 +08:00
bool ret = false ;
2019-01-11 23:20:28 +08:00
var db = DbManager . Db ;
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
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 ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
2018-10-30 17:02:36 +08:00
/// 新建前台User View调用/注册用户调用
2018-10-19 23:09:52 +08:00
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool Save ( User p )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
2018-11-02 19:35:55 +08:00
if ( string . IsNullOrEmpty ( p . Id ) & & p . Description . Length > 500 ) p . Description = p . Description . Substring ( 0 , 500 ) ;
2018-10-21 10:02:59 +08:00
if ( p . UserName . Length > 50 ) p . UserName = p . UserName . Substring ( 0 , 50 ) ;
p . PassSalt = LgbCryptography . GenerateSalt ( ) ;
p . Password = LgbCryptography . ComputeHash ( p . Password , p . PassSalt ) ;
2019-01-11 23:20:28 +08:00
p . RegisterTime = DateTime . Now ;
2018-11-02 19:35:55 +08:00
2019-01-11 23:20:28 +08:00
var db = DbManager . Db ;
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
db . BeginTransaction ( ) ;
if ( ! db . Exists < User > ( "where UserName = @0" , p . UserName ) )
2018-11-02 19:35:55 +08:00
{
2019-01-11 23:20:28 +08:00
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 ) ;
2018-11-02 19:35:55 +08:00
}
2019-01-11 23:20:28 +08:00
db . CompleteTransaction ( ) ;
ret = true ;
}
catch ( Exception ex )
{
db . AbortTransaction ( ) ;
throw ex ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
2018-10-30 17:02:36 +08:00
/// User List 视图保存按钮调用
2018-10-19 23:09:52 +08:00
/// </summary>
/// <param name="id"></param>
/// <param name="password"></param>
/// <param name="displayName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool Update ( string id , string password , string displayName )
2018-10-21 10:02:59 +08:00
{
var passSalt = LgbCryptography . GenerateSalt ( ) ;
var newPassword = LgbCryptography . ComputeHash ( password , passSalt ) ;
2019-01-11 23:20:28 +08:00
return DbManager . Db . Update < User > ( "set Password = @1, PassSalt = @2, DisplayName = @3 where ID = @0" , id , newPassword , passSalt , displayName ) = = 1 ;
2018-10-21 10:02:59 +08:00
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="approvedBy"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool Approve ( string id , string approvedBy ) = > DbManager . Db . Update < User > ( "set ApprovedTime = @1, ApprovedBy = @2 where ID = @0" , id , DateTime . Now , approvedBy ) = = 1 ;
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="rejectBy"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool Reject ( string id , string rejectBy )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
2019-01-11 23:20:28 +08:00
var db = DbManager . Db ;
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
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 ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过roleId获取所有用户
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < User > RetrievesByRoleId ( string roleId ) = > DbManager . Db . Fetch < User > ( "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 ) ;
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过角色ID保存当前授权用户( 插入)
/// </summary>
2018-10-23 15:37:42 +08:00
/// <param name="roleId">角色ID</param>
2018-10-19 23:09:52 +08:00
/// <param name="userIds">用户ID数组</param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool SaveByRoleId ( string roleId , IEnumerable < string > userIds )
2018-10-21 10:02:59 +08:00
{
bool ret = false ;
2019-01-11 23:20:28 +08:00
var db = DbManager . Db ;
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
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 ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过groupId获取所有用户
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual IEnumerable < User > RetrievesByGroupId ( string groupId ) = > DbManager . Db . Fetch < User > ( "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 ) ;
2018-10-19 23:09:52 +08:00
/// <summary>
/// 通过部门ID保存当前授权用户( 插入)
/// </summary>
2018-10-23 15:37:42 +08:00
/// <param name="groupId">GroupID</param>
2018-10-19 23:09:52 +08:00
/// <param name="userIds">用户ID数组</param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool SaveByGroupId ( string groupId , IEnumerable < string > userIds )
2018-10-21 10:02:59 +08:00
{
bool ret = false ;
2019-01-11 23:20:28 +08:00
var db = DbManager . Db ;
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
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 ;
2018-10-21 10:02:59 +08:00
}
return ret ;
}
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据用户名修改用户头像
/// </summary>
/// <param name="userName"></param>
/// <param name="iconName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool SaveUserIconByName ( string userName , string iconName ) = > DbManager . Db . Update < User > ( "set Icon = @1 where UserName = @0" , userName , iconName ) = = 1 ;
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
2018-10-21 10:02:59 +08:00
/// <param name="userName"></param>
2018-10-19 23:09:52 +08:00
/// <param name="displayName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool SaveDisplayName ( string userName , string displayName ) = > DbManager . Db . Update < User > ( "set DisplayName = @1 where UserName = @0" , userName , displayName ) = = 1 ;
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据用户名更改用户皮肤
/// </summary>
/// <param name="userName"></param>
/// <param name="cssName"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool SaveUserCssByName ( string userName , string cssName ) = > DbManager . Db . Update < User > ( "set Css = @1 where UserName = @0" , userName , cssName ) = = 1 ;
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
2018-10-21 10:02:59 +08:00
/// <param name="userName"></param>
2018-10-19 23:09:52 +08:00
/// <returns></returns>
2018-10-28 23:35:23 +08:00
public virtual BootstrapUser RetrieveUserByUserName ( string userName ) = > DbHelper . RetrieveUserByUserName ( userName ) ;
2019-01-11 23:20:28 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
2017-01-03 19:47:37 +08:00
///
/// </summary>
/// <returns></returns>
public override string ToString ( )
{
return string . Format ( "{0} ({1})" , UserName , DisplayName ) ;
}
2016-10-22 16:58:31 +08:00
}
2019-01-11 23:20:28 +08:00
2018-09-07 15:53:16 +08:00
/// <summary>
///
/// </summary>
public enum UserStates
{
/// <summary>
///
/// </summary>
ChangePassword ,
/// <summary>
///
/// </summary>
ChangeTheme ,
/// <summary>
///
/// </summary>
ChangeDisplayName ,
/// <summary>
///
/// </summary>
ApproveUser ,
/// <summary>
///
/// </summary>
RejectUser
}
2016-10-22 16:58:31 +08:00
}