2018-10-19 23:09:52 +08:00
using Bootstrap.Security ;
using Longbow.Data ;
2018-10-28 08:29:15 +08:00
using Longbow.Security.Cryptography ;
2018-10-19 23:09:52 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
using System.Linq ;
namespace Bootstrap.DataAccess.SQLite
{
/// <summary>
/// 用户表实体类
/// </summary>
public class User : DataAccess . User
{
/// <summary>
/// <summary>
/// 删除用户
/// </summary>
/// <param name="value"></param>
public override bool DeleteUser ( IEnumerable < int > value )
{
bool ret = false ;
var ids = string . Join ( "," , value ) ;
2018-10-23 15:37:42 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
try
{
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , $"Delete from UserRole where UserID in ({ids})" ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd ) ;
cmd . CommandText = $"delete from UserGroup where UserID in ({ids})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"delete from Users where ID in ({ids})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
transaction . CommitTransaction ( ) ;
CacheCleanUtility . ClearCache ( userIds : value ) ;
ret = true ;
}
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
2018-10-19 23:09:52 +08:00
}
return ret ;
}
/// <summary>
/// 保存新建
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public override bool SaveUser ( DataAccess . User p )
{
var ret = false ;
if ( p . Id = = 0 & & 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 ) ;
2018-10-23 15:37:42 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
try
{
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , "select UserName from Users Where UserName = @userName" ) )
{
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@userName" , p . UserName ) ) ;
var un = DbAccessManager . DBAccess . ExecuteScalar ( cmd , transaction ) ;
if ( DbAdapterManager . ToObjectValue ( un ) = = null )
{
cmd . CommandText = "Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, datetime('now', 'localtime'), @approvedBy, datetime('now', 'localtime'), @description)" ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@password" , p . Password ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@passSalt" , p . PassSalt ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@displayName" , p . DisplayName ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@approvedBy" , DbAdapterManager . ToDBValue ( p . ApprovedBy ) ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@description" , p . Description ) ) ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = '{p.UserName}'" ;
cmd . Parameters . Clear ( ) ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
transaction . CommitTransaction ( ) ;
CacheCleanUtility . ClearCache ( userIds : p . Id = = 0 ? new List < int > ( ) : new List < int > ( ) { p . Id } ) ;
ret = true ;
}
}
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
2018-10-19 23:09:52 +08:00
}
return ret ;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="approvedBy"></param>
/// <returns></returns>
public override bool ApproveUser ( int id , string approvedBy )
{
var ret = false ;
var sql = "update Users set ApprovedTime = datetime('now', 'localtime'), ApprovedBy = @approvedBy where ID = @id" ;
2018-10-20 22:25:53 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
2018-10-19 23:09:52 +08:00
{
2018-10-20 22:25:53 +08:00
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@id" , id ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@approvedBy" , approvedBy ) ) ;
ret = DbAccessManager . DBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
2018-10-19 23:09:52 +08:00
if ( ret ) CacheCleanUtility . ClearCache ( userIds : new List < int > ( ) { id } ) ;
}
return ret ;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="rejectBy"></param>
/// <param name="reason"></param>
/// <returns></returns>
public override bool RejectUser ( int id , string rejectBy )
{
var ret = false ;
2018-10-23 15:37:42 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
try
{
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , $"insert into RejectUsers (UserName, DisplayName, RegisterTime, RejectedBy, RejectedTime, RejectedReason) select UserName, DisplayName, Registertime, '{rejectBy}', datetime('now', 'localtime'), '未填写' from Users where ID = {id}" ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"delete from UserRole where UserId = {id}" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"delete from UserGroup where UserId = {id}" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
cmd . CommandText = $"delete from users where ID = {id}" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
transaction . CommitTransaction ( ) ;
CacheCleanUtility . ClearCache ( userIds : new List < int > ( ) { id } ) ;
ret = true ;
}
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
2018-10-19 23:09:52 +08:00
}
return ret ;
}
/// <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>
2018-10-23 15:37:42 +08:00
public override bool SaveUsersByRoleId ( int roleId , IEnumerable < int > userIds )
2018-10-19 23:09:52 +08:00
{
bool ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
2018-10-23 15:37:42 +08:00
userIds . ToList ( ) . ForEach ( userId = > dt . Rows . Add ( roleId , userId ) ) ;
2018-10-20 22:25:53 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
try
{
//删除用户角色表该角色所有的用户
2018-10-23 15:37:42 +08:00
string sql = $"delete from UserRole where RoleID = {roleId}" ;
2018-10-20 22:25:53 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
2018-10-19 23:09:52 +08:00
{
2018-10-20 22:25:53 +08:00
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2018-10-19 23:09:52 +08:00
//批插入用户角色表
2018-10-23 15:37:42 +08:00
userIds . ToList ( ) . ForEach ( uId = >
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
cmd . CommandText = $"Insert Into UserRole (UserID, RoleID) Values ( {uId}, {roleId})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
} ) ;
transaction . CommitTransaction ( ) ;
2018-10-19 23:09:52 +08:00
}
2018-10-23 15:37:42 +08:00
CacheCleanUtility . ClearCache ( userIds : userIds , roleIds : new List < int > ( ) { roleId } ) ;
2018-10-19 23:09:52 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
/// <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>
2018-10-23 15:37:42 +08:00
public override bool SaveUsersByGroupId ( int groupId , IEnumerable < int > userIds )
2018-10-19 23:09:52 +08:00
{
bool ret = false ;
2018-10-20 22:25:53 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
2018-10-19 23:09:52 +08:00
{
try
{
//删除用户角色表该角色所有的用户
2018-10-23 15:37:42 +08:00
string sql = $"delete from UserGroup where GroupID = {groupId}" ;
2018-10-20 22:25:53 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
2018-10-19 23:09:52 +08:00
{
2018-10-20 22:25:53 +08:00
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2018-10-19 23:09:52 +08:00
//批插入用户角色表
2018-10-23 15:37:42 +08:00
userIds . ToList ( ) . ForEach ( uId = >
2018-10-19 23:09:52 +08:00
{
2018-10-23 15:37:42 +08:00
cmd . CommandText = $"Insert Into UserGroup (UserID, GroupID) Values ( {uId}, {groupId})" ;
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
} ) ;
transaction . CommitTransaction ( ) ;
2018-10-19 23:09:52 +08:00
}
2018-10-23 15:37:42 +08:00
CacheCleanUtility . ClearCache ( userIds : userIds , groupIds : new List < int > ( ) { groupId } ) ;
2018-10-19 23:09:52 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public override BootstrapUser RetrieveUserByUserName ( string userName )
{
2018-10-28 15:03:59 +08:00
BootstrapUser user = null ;
var sql = "select UserName, DisplayName, case ifnull(d.Code, '') when '' then '~/images/uploader/' else d.Code end || ifnull(Icon, 'default.jpg') Icon, u.Css from Users u left join Dicts d on d.Define = '0' and d.Category = '头像地址' and Name = '头像路径' where ApprovedTime is not null and UserName = @UserName" ;
var db = DbAccessManager . DBAccess ;
var cmd = db . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( db . CreateParameter ( "@UserName" , userName ) ) ;
using ( DbDataReader reader = db . ExecuteReader ( cmd ) )
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
if ( reader . Read ( ) )
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
user = new BootstrapUser
2018-10-19 23:09:52 +08:00
{
2018-10-28 15:03:59 +08:00
UserName = ( string ) reader [ 0 ] ,
DisplayName = ( string ) reader [ 1 ] ,
Icon = ( string ) reader [ 2 ] ,
Css = reader . IsDBNull ( 3 ) ? string . Empty : ( string ) reader [ 3 ]
} ;
2018-10-19 23:09:52 +08:00
}
2018-10-28 15:03:59 +08:00
}
return user ;
2018-10-19 23:09:52 +08:00
}
}
}