2017-01-16 11:42:41 +08:00
using Bootstrap.Security ;
using Longbow ;
2018-06-07 00:45:47 +08:00
using Longbow.Cache ;
2016-10-29 09:24:55 +08:00
using Longbow.Data ;
2016-10-23 15:46:18 +08:00
using Longbow.Security ;
2016-10-22 16:58:31 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
2016-10-28 20:18:12 +08:00
using System.Data.SqlClient ;
2016-10-22 16:58:31 +08:00
using System.Linq ;
namespace Bootstrap.DataAccess
{
/// <summary>
/// 用户表相关操作类
/// </summary>
public static class UserHelper
{
2018-09-10 18:35:25 +08:00
internal const string RetrieveUsersDataKey = "BootstrapUser-RetrieveUsers" ;
2017-04-03 15:56:17 +08:00
internal const string RetrieveUsersByRoleIdDataKey = "BootstrapUser-RetrieveUsersByRoleId" ;
internal const string RetrieveUsersByGroupIdDataKey = "BootstrapUser-RetrieveUsersByGroupId" ;
2016-11-11 21:05:41 +08:00
internal const string RetrieveNewUsersDataKey = "UserHelper-RetrieveNewUsers" ;
2018-09-10 18:35:25 +08:00
2016-10-22 16:58:31 +08:00
/// <summary>
/// 查询所有用户
/// </summary>
2016-11-10 12:20:26 +08:00
/// <param name="id"></param>
2016-10-22 16:58:31 +08:00
/// <returns></returns>
2017-04-03 15:56:17 +08:00
public static IEnumerable < User > RetrieveUsers ( )
2016-10-22 16:58:31 +08:00
{
2018-09-10 18:35:25 +08:00
return CacheManager . GetOrAdd ( RetrieveUsersDataKey , key = >
2016-10-22 16:58:31 +08:00
{
2017-03-30 16:15:45 +08:00
List < User > users = new List < User > ( ) ;
2017-04-03 15:56:17 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null" ) ;
2018-06-12 11:17:21 +08:00
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-22 16:58:31 +08:00
{
2018-06-12 11:17:21 +08:00
while ( reader . Read ( ) )
2016-10-22 16:58:31 +08:00
{
2018-06-12 11:17:21 +08:00
users . Add ( new User ( )
2016-10-22 16:58:31 +08:00
{
2018-06-12 11:17:21 +08:00
Id = ( int ) reader [ 0 ] ,
UserName = ( string ) reader [ 1 ] ,
DisplayName = ( string ) reader [ 2 ] ,
RegisterTime = ( DateTime ) reader [ 3 ] ,
ApprovedTime = LgbConvert . ReadValue ( reader [ 4 ] , DateTime . MinValue ) ,
ApprovedBy = reader . IsDBNull ( 5 ) ? string . Empty : ( string ) reader [ 5 ] ,
Description = ( string ) reader [ 6 ]
} ) ;
2016-10-22 16:58:31 +08:00
}
}
2017-03-30 16:15:45 +08:00
return users ;
2017-04-05 11:54:21 +08:00
} ) ;
2016-10-23 15:46:18 +08:00
}
2016-11-11 18:02:27 +08:00
/// <summary>
/// 查询所有的新注册用户
/// </summary>
/// <returns></returns>
2016-11-11 21:05:41 +08:00
public static IEnumerable < User > RetrieveNewUsers ( )
2016-11-11 18:02:27 +08:00
{
2017-04-05 11:54:21 +08:00
return CacheManager . GetOrAdd ( RetrieveNewUsersDataKey , key = >
2016-11-11 18:02:27 +08:00
{
2018-09-08 16:16:51 +08:00
string sql = "select ID, UserName, DisplayName, RegisterTime, [Description] from Users Where ApprovedTime is null order by RegisterTime desc" ;
2017-03-30 16:15:45 +08:00
List < User > users = new List < User > ( ) ;
2016-11-11 18:02:27 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2018-06-12 11:17:21 +08:00
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-11-11 18:02:27 +08:00
{
2018-06-12 11:17:21 +08:00
while ( reader . Read ( ) )
2016-11-11 18:02:27 +08:00
{
2018-06-12 11:17:21 +08:00
users . Add ( new User ( )
2016-11-11 18:02:27 +08:00
{
2018-06-12 11:17:21 +08:00
Id = ( int ) reader [ 0 ] ,
UserName = ( string ) reader [ 1 ] ,
DisplayName = ( string ) reader [ 2 ] ,
RegisterTime = ( DateTime ) reader [ 3 ] ,
Description = ( string ) reader [ 4 ]
} ) ;
2016-11-11 18:02:27 +08:00
}
}
2017-03-30 16:15:45 +08:00
return users ;
2017-04-05 11:54:21 +08:00
} ) ;
2016-11-11 18:02:27 +08:00
}
2016-10-23 15:46:18 +08:00
/// <summary>
2016-10-22 16:58:31 +08:00
/// 删除用户
/// </summary>
2018-06-08 12:54:05 +08:00
/// <param name="value"></param>
public static bool DeleteUser ( IEnumerable < int > value )
2016-10-22 16:58:31 +08:00
{
2016-10-22 20:55:07 +08:00
bool ret = false ;
2018-06-12 11:17:21 +08:00
var ids = string . Join ( "," , value ) ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . StoredProcedure , "Proc_DeleteUsers" ) )
2016-11-07 12:27:04 +08:00
{
2018-06-12 11:17:21 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ids" , ids ) ) ;
2018-09-13 19:51:11 +08:00
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = - 1 ;
if ( ret ) CacheCleanUtility . ClearCache ( userIds : value ) ;
2016-10-22 20:55:07 +08:00
}
return ret ;
2016-10-22 16:58:31 +08:00
}
/// <summary>
2018-09-13 19:51:11 +08:00
/// 保存新建
2016-10-22 16:58:31 +08:00
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveUser ( User p )
{
2018-09-08 18:10:29 +08:00
var ret = false ;
2017-03-30 16:15:45 +08:00
if ( p . Id = = 0 & & p . Description . Length > 500 ) p . Description = p . Description . Substring ( 0 , 500 ) ;
2018-09-07 15:53:16 +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 ) ;
2018-06-12 11:17:21 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . StoredProcedure , "Proc_SaveUsers" ) )
2016-10-22 16:58:31 +08:00
{
2018-09-07 15:53:16 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@userName" , p . UserName ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@password" , p . Password ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@passSalt" , p . PassSalt ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@displayName" , p . DisplayName ) ) ;
2018-06-12 11:17:21 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@approvedBy" , DBAccessFactory . ToDBValue ( p . ApprovedBy ) ) ) ;
2018-09-07 15:53:16 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@description" , p . Description ) ) ;
2018-09-08 18:10:29 +08:00
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = - 1 ;
2018-09-13 19:51:11 +08:00
if ( ret ) CacheCleanUtility . ClearCache ( userIds : p . Id = = 0 ? new List < int > ( ) : new List < int > ( ) { p . Id } ) ;
}
return ret ;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="password"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public static bool UpdateUser ( int id , string password , string displayName )
{
bool ret = false ;
string sql = "Update Users set Password = @Password, PassSalt = @PassSalt, DisplayName = @DisplayName where ID = @id" ;
var passSalt = LgbCryptography . GenerateSalt ( ) ;
var newPassword = LgbCryptography . ComputeHash ( password , passSalt ) ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@id" , id ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@DisplayName" , displayName ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Password" , newPassword ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@PassSalt" , passSalt ) ) ;
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
if ( ret ) CacheCleanUtility . ClearCache ( userIds : id = = 0 ? new List < int > ( ) : new List < int > ( ) { id } ) ;
2016-10-22 16:58:31 +08:00
}
2018-09-08 18:10:29 +08:00
return ret ;
2018-09-07 15:53:16 +08:00
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="approvedBy"></param>
/// <returns></returns>
public static bool ApproveUser ( int id , string approvedBy )
{
var ret = false ;
var sql = "update Users set ApprovedTime = GETDATE(), ApprovedBy = @approvedBy where ID = @id" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@id" , id ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@approvedBy" , approvedBy ) ) ;
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
2018-09-13 19:51:11 +08:00
if ( ret ) CacheCleanUtility . ClearCache ( userIds : new List < int > ( ) { id } ) ;
2018-09-07 15:53:16 +08:00
}
return ret ;
}
/// <summary>
///
/// </summary>
2018-09-10 18:35:25 +08:00
/// <param name="userName"></param>
/// <param name="password"></param>
/// <param name="newPass"></param>
/// <returns></returns>
public static bool ChangePassword ( string userName , string password , string newPass )
{
bool ret = false ;
if ( BootstrapUser . Authenticate ( userName , password ) )
{
string sql = "Update Users set Password = @Password, PassSalt = @PassSalt where UserName = @userName" ;
var passSalt = LgbCryptography . GenerateSalt ( ) ;
var newPassword = LgbCryptography . ComputeHash ( newPass , passSalt ) ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Password" , newPassword ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@PassSalt" , passSalt ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@userName" , userName ) ) ;
2018-09-13 19:51:11 +08:00
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
2018-09-10 18:35:25 +08:00
}
}
return ret ;
}
/// <summary>
///
/// </summary>
2018-09-07 15:53:16 +08:00
/// <param name="id"></param>
/// <param name="rejectBy"></param>
/// <param name="reason"></param>
/// <returns></returns>
public static bool RejectUser ( int id , string rejectBy )
{
var ret = false ;
2018-09-08 16:16:51 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . StoredProcedure , "Proc_RejectUsers" ) )
2018-09-07 15:53:16 +08:00
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@id" , id ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@rejectedBy" , rejectBy ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@rejectedReason" , "未填写" ) ) ;
2018-09-08 16:16:51 +08:00
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = - 1 ;
2018-09-13 19:51:11 +08:00
if ( ret ) CacheCleanUtility . ClearCache ( userIds : new List < int > ( ) { id } ) ;
2018-09-07 15:53:16 +08:00
}
2016-10-22 16:58:31 +08:00
return ret ;
}
2016-10-23 15:46:18 +08:00
/// <summary>
2016-10-28 20:18:12 +08:00
/// 通过roleId获取所有用户
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public static IEnumerable < User > RetrieveUsersByRoleId ( int roleId )
{
2017-03-30 16:15:45 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveUsersByRoleIdDataKey , roleId ) ;
2017-04-05 11:54:21 +08:00
return CacheManager . GetOrAdd ( key , k = >
2016-10-28 20:18:12 +08:00
{
2017-03-30 16:15:45 +08:00
List < User > users = new List < User > ( ) ;
2016-11-06 16:01:14 +08:00
string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @RoleID where u.ApprovedTime is not null" ;
2016-10-28 20:18:12 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@RoleID" , roleId ) ) ;
2018-06-12 11:17:21 +08:00
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-28 20:18:12 +08:00
{
2018-06-12 11:17:21 +08:00
while ( reader . Read ( ) )
2016-10-28 20:18:12 +08:00
{
2018-06-12 11:17:21 +08:00
users . Add ( new User ( )
2016-10-28 20:18:12 +08:00
{
2018-06-12 11:17:21 +08:00
Id = ( int ) reader [ 0 ] ,
UserName = ( string ) reader [ 1 ] ,
DisplayName = ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2016-10-28 20:18:12 +08:00
}
}
2017-03-30 16:15:45 +08:00
return users ;
2017-04-05 11:54:21 +08:00
} , RetrieveUsersByRoleIdDataKey ) ;
2016-10-28 20:18:12 +08:00
}
/// <summary>
/// 通过角色ID保存当前授权用户( 插入)
/// </summary>
/// <param name="id">角色ID</param>
2017-03-30 16:15:45 +08:00
/// <param name="userIds">用户ID数组</param>
2016-10-28 20:18:12 +08:00
/// <returns></returns>
2018-09-13 19:21:35 +08:00
public static bool SaveUsersByRoleId ( int id , IEnumerable < int > userIds )
2016-10-28 20:18:12 +08:00
{
2016-10-29 09:24:55 +08:00
bool ret = false ;
2016-10-28 20:18:12 +08:00
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
2018-09-13 19:21:35 +08:00
userIds . ToList ( ) . ForEach ( userId = > dt . Rows . Add ( id , userId ) ) ;
2016-10-29 09:24:55 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
2016-10-28 20:18:12 +08:00
{
2016-10-29 09:24:55 +08:00
try
2016-10-28 20:18:12 +08:00
{
2016-10-29 09:24:55 +08:00
//删除用户角色表该角色所有的用户
2016-10-31 15:55:31 +08:00
string sql = "delete from UserRole where RoleID=@RoleID" ;
2016-10-29 09:24:55 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2016-10-28 20:18:12 +08:00
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@RoleID" , id ) ) ;
2016-10-29 09:24:55 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
//批插入用户角色表
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
bulk . DestinationTableName = "UserRole" ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
bulk . ColumnMappings . Add ( "UserID" , "UserID" ) ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
2016-10-28 20:18:12 +08:00
}
2018-09-13 19:21:35 +08:00
CacheCleanUtility . ClearCache ( userIds : userIds , roleIds : new List < int > ( ) { id } ) ;
2016-11-01 16:53:17 +08:00
ret = true ;
2016-10-29 09:24:55 +08:00
}
2016-11-01 16:53:17 +08:00
catch ( Exception ex )
2016-10-29 09:24:55 +08:00
{
transaction . RollbackTransaction ( ) ;
2018-06-12 11:17:21 +08:00
throw ex ;
2016-10-28 20:18:12 +08:00
}
}
2016-10-29 09:24:55 +08:00
return ret ;
2016-10-22 16:58:31 +08:00
}
2016-10-29 17:38:23 +08:00
/// <summary>
/// 通过groupId获取所有用户
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="groupId"></param>
2016-10-29 17:38:23 +08:00
/// <returns></returns>
public static IEnumerable < User > RetrieveUsersByGroupId ( int groupId )
{
2017-03-30 16:15:45 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveUsersByGroupIdDataKey , groupId ) ;
2017-04-05 11:54:21 +08:00
return CacheManager . GetOrAdd ( key , k = >
2016-10-29 17:38:23 +08:00
{
2017-03-30 16:15:45 +08:00
List < User > users = new List < User > ( ) ;
2016-11-06 16:01:14 +08:00
string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID =@groupId where u.ApprovedTime is not null" ;
2016-10-29 17:38:23 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@GroupID" , groupId ) ) ;
2018-06-12 11:17:21 +08:00
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-29 17:38:23 +08:00
{
2018-06-12 11:17:21 +08:00
while ( reader . Read ( ) )
2016-10-29 17:38:23 +08:00
{
2018-06-12 11:17:21 +08:00
users . Add ( new User ( )
2016-10-29 17:38:23 +08:00
{
2018-06-12 11:17:21 +08:00
Id = ( int ) reader [ 0 ] ,
UserName = ( string ) reader [ 1 ] ,
DisplayName = ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2016-10-29 17:38:23 +08:00
}
}
2017-03-30 16:15:45 +08:00
return users ;
2017-04-05 11:54:21 +08:00
} , RetrieveUsersByRoleIdDataKey ) ;
2016-10-29 17:38:23 +08:00
}
/// <summary>
/// 通过部门ID保存当前授权用户( 插入)
/// </summary>
/// <param name="id">GroupID</param>
2017-03-30 16:15:45 +08:00
/// <param name="userIds">用户ID数组</param>
2016-10-29 17:38:23 +08:00
/// <returns></returns>
2018-09-13 19:21:35 +08:00
public static bool SaveUsersByGroupId ( int id , IEnumerable < int > userIds )
2016-10-29 17:38:23 +08:00
{
bool ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
2016-11-01 16:53:17 +08:00
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
2018-09-13 19:21:35 +08:00
userIds . ToList ( ) . ForEach ( userId = > dt . Rows . Add ( userId , id ) ) ;
2016-10-29 17:38:23 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
{
try
{
//删除用户角色表该角色所有的用户
2016-11-06 16:01:14 +08:00
string sql = "delete from UserGroup where GroupID = @GroupID" ;
2016-10-29 17:38:23 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@GroupID" , id ) ) ;
2016-10-29 17:38:23 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
//批插入用户角色表
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
bulk . DestinationTableName = "UserGroup" ;
bulk . ColumnMappings . Add ( "UserID" , "UserID" ) ;
bulk . ColumnMappings . Add ( "GroupID" , "GroupID" ) ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
2018-09-13 19:21:35 +08:00
CacheCleanUtility . ClearCache ( userIds : userIds , groupIds : new List < int > ( ) { id } ) ;
2016-10-29 17:38:23 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
2018-06-12 11:17:21 +08:00
throw ex ;
2016-10-29 17:38:23 +08:00
}
}
return ret ;
}
2016-11-15 11:15:38 +08:00
/// <summary>
/// 根据用户名修改用户头像
/// </summary>
/// <param name="userName"></param>
2016-11-16 17:27:03 +08:00
/// <param name="iconName"></param>
2016-11-15 11:15:38 +08:00
/// <returns></returns>
2016-11-16 17:27:03 +08:00
public static bool SaveUserIconByName ( string userName , string iconName )
2016-11-15 11:15:38 +08:00
{
bool ret = false ;
2018-06-12 11:17:21 +08:00
string sql = "Update Users set Icon = @iconName where UserName = @userName" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2017-06-13 11:34:06 +08:00
{
2018-06-12 11:17:21 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@iconName" , iconName ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@userName" , userName ) ) ;
2018-09-13 19:51:11 +08:00
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
if ( ret ) CacheCleanUtility . ClearCache ( cacheKey : $"{RetrieveUsersDataKey}*" ) ;
2017-06-13 11:34:06 +08:00
}
return ret ;
}
/// <summary>
2018-09-10 18:35:25 +08:00
///
/// </summary>
/// <param name="userName"></param>
/// <param name="displayName"></param>
/// <returns></returns>
public static bool SaveDisplayName ( string userName , string displayName )
{
bool ret = false ;
string sql = "Update Users set DisplayName = @DisplayName where UserName = @userName" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@DisplayName" , displayName ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@userName" , userName ) ) ;
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
2018-09-13 19:51:11 +08:00
if ( ret ) CacheCleanUtility . ClearCache ( cacheKey : $"{RetrieveUsersDataKey}*" ) ;
2018-09-10 18:35:25 +08:00
}
return ret ;
}
/// <summary>
2017-06-13 11:34:06 +08:00
/// 根据用户名更改用户皮肤
/// </summary>
/// <param name="userName"></param>
/// <param name="cssName"></param>
/// <returns></returns>
public static bool SaveUserCssByName ( string userName , string cssName )
{
bool ret = false ;
2018-06-12 11:17:21 +08:00
string sql = "Update Users set Css = @cssName where UserName = @userName" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2017-06-13 11:34:06 +08:00
{
2018-06-12 11:17:21 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@cssName" , DBAccessFactory . ToDBValue ( cssName ) ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@userName" , userName ) ) ;
2018-09-13 19:51:11 +08:00
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
if ( ret ) CacheCleanUtility . ClearCache ( cacheKey : $"{RetrieveUsersDataKey}*" ) ;
2016-11-16 20:06:28 +08:00
}
return ret ;
}
2016-10-22 16:58:31 +08:00
}
2017-03-30 16:15:45 +08:00
}