2018-06-07 00:45:47 +08:00
using Longbow.Cache ;
2016-10-26 21:32:54 +08:00
using Longbow.Data ;
2016-10-25 18:47:33 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
2016-10-26 21:32:54 +08:00
using System.Data.SqlClient ;
2016-10-25 18:47:33 +08:00
using System.Linq ;
namespace Bootstrap.DataAccess
{
2016-10-28 13:36:43 +08:00
/// <summary>
///
/// </summary>
2016-11-04 16:06:40 +08:00
public static class RoleHelper
2016-10-25 18:47:33 +08:00
{
2016-12-06 15:49:50 +08:00
internal const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles" ;
2017-03-30 16:15:45 +08:00
internal const string RetrieveRolesByUserIdDataKey = "RoleHelper-RetrieveRolesByUserId" ;
internal const string RetrieveRolesByMenuIdDataKey = "RoleHelper-RetrieveRolesByMenuId" ;
internal const string RetrieveRolesByGroupIdDataKey = "RoleHelper-RetrieveRolesByGroupId" ;
2016-10-25 18:47:33 +08:00
/// <summary>
/// 查询所有角色
/// </summary>
2016-11-10 12:20:26 +08:00
/// <param name="id"></param>
2016-10-25 18:47:33 +08:00
/// <returns></returns>
2016-11-10 12:20:26 +08:00
public static IEnumerable < Role > RetrieveRoles ( int id = 0 )
2016-10-25 18:47:33 +08:00
{
2017-04-05 11:54:21 +08:00
var ret = CacheManager . GetOrAdd ( RetrieveRolesDataKey , key = >
2016-10-25 18:47:33 +08:00
{
2016-11-04 16:06:40 +08:00
string sql = "select * from Roles" ;
2016-10-25 18:47:33 +08:00
List < Role > roles = new List < Role > ( ) ;
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2018-09-08 19:57:42 +08:00
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-25 18:47:33 +08:00
{
2018-09-08 19:57:42 +08:00
while ( reader . Read ( ) )
2016-10-25 18:47:33 +08:00
{
2018-09-08 19:57:42 +08:00
roles . Add ( new Role ( )
2016-10-25 18:47:33 +08:00
{
2018-09-08 19:57:42 +08:00
Id = ( int ) reader [ 0 ] ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ]
} ) ;
2016-10-25 18:47:33 +08:00
}
}
return roles ;
2017-04-05 11:54:21 +08:00
} ) ;
2017-03-30 16:15:45 +08:00
return id = = 0 ? ret : ret . Where ( t = > id = = t . Id ) ;
2016-10-25 18:47:33 +08:00
}
/// <summary>
2016-10-26 21:32:54 +08:00
/// 保存用户角色关系
2016-10-25 18:47:33 +08:00
/// </summary>
/// <param name="id"></param>
2016-10-28 13:36:43 +08:00
/// <param name="roleIds"></param>
2016-10-25 18:47:33 +08:00
/// <returns></returns>
2016-10-28 13:36:43 +08:00
public static bool SaveRolesByUserId ( int id , string roleIds )
2016-10-25 18:47:33 +08:00
{
2016-10-28 13:36:43 +08:00
var ret = false ;
2016-10-26 21:32:54 +08:00
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
//判断用户是否选定角色
2016-11-02 12:12:34 +08:00
if ( ! string . IsNullOrEmpty ( roleIds ) ) roleIds . Split ( ',' ) . ToList ( ) . ForEach ( roleId = > dt . Rows . Add ( id , roleId ) ) ;
2016-10-28 13:36:43 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
2016-10-26 21:32:54 +08:00
{
2016-10-28 13:36:43 +08:00
try
2016-10-26 21:32:54 +08:00
{
2016-10-28 13:36:43 +08:00
// delete user from config table
string sql = "delete from UserRole where UserID = @UserID;" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2016-10-26 21:32:54 +08:00
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@UserID" , id ) ) ;
2016-10-28 13:36:43 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2016-11-10 14:06:00 +08:00
if ( dt . Rows . Count > 0 )
2016-10-26 21:32:54 +08:00
{
2016-11-10 14:06:00 +08:00
// insert batch data into config table
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
bulk . DestinationTableName = "UserRole" ;
bulk . ColumnMappings . Add ( "UserID" , "UserID" ) ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
bulk . WriteToServer ( dt ) ;
}
2016-10-26 21:32:54 +08:00
}
2016-11-10 14:06:00 +08:00
transaction . CommitTransaction ( ) ;
2016-10-26 21:32:54 +08:00
}
2016-11-10 14:06:00 +08:00
CacheCleanUtility . ClearCache ( userIds : id . ToString ( ) , roleIds : roleIds ) ;
2016-10-28 13:36:43 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
2018-09-08 19:57:42 +08:00
throw ex ;
2016-10-26 21:32:54 +08:00
}
}
2016-10-28 13:36:43 +08:00
return ret ;
}
2016-10-25 18:47:33 +08:00
/// <summary>
2016-10-26 21:32:54 +08:00
/// 查询某个用户所拥有的角色
2016-10-25 18:47:33 +08:00
/// </summary>
/// <returns></returns>
2016-10-28 13:36:43 +08:00
public static IEnumerable < Role > RetrieveRolesByUserId ( int userId )
2016-10-25 18:47:33 +08:00
{
2017-03-30 16:15:45 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveRolesByUserIdDataKey , userId ) ;
2017-04-05 11:54:21 +08:00
return CacheManager . GetOrAdd ( key , k = >
2016-10-26 21:32:54 +08:00
{
2017-03-30 16:15:45 +08:00
List < Role > roles = new List < Role > ( ) ;
2016-10-28 13:36:43 +08:00
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @UserID" ;
2018-09-08 19:57:42 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@UserID" , userId ) ) ;
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-26 21:32:54 +08:00
{
2018-09-08 19:57:42 +08:00
while ( reader . Read ( ) )
2016-10-26 21:32:54 +08:00
{
2018-09-08 19:57:42 +08:00
roles . Add ( new Role ( )
2016-10-26 21:32:54 +08:00
{
2018-09-08 19:57:42 +08:00
Id = ( int ) reader [ 0 ] ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2016-10-26 21:32:54 +08:00
}
}
2017-03-30 16:15:45 +08:00
return roles ;
2017-04-05 11:54:21 +08:00
} , RetrieveRolesByUserIdDataKey ) ;
2016-10-28 13:36:43 +08:00
}
2016-10-25 18:47:33 +08:00
/// <summary>
/// 删除角色表
/// </summary>
2018-06-08 12:54:05 +08:00
/// <param name="value"></param>
public static bool DeleteRole ( IEnumerable < int > value )
2016-10-25 18:47:33 +08:00
{
bool ret = false ;
2018-09-08 19:57:42 +08:00
var ids = string . Join ( "," , value ) ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . StoredProcedure , "Proc_DeleteRoles" ) )
2016-11-07 15:17:10 +08:00
{
2018-09-08 19:57:42 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ids" , ids ) ) ;
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = - 1 ;
2016-10-25 18:47:33 +08:00
}
2018-09-08 19:57:42 +08:00
CacheCleanUtility . ClearCache ( roleIds : ids ) ;
2016-10-25 18:47:33 +08:00
return ret ;
}
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveRole ( Role p )
{
bool ret = false ;
if ( ! string . IsNullOrEmpty ( p . RoleName ) & & p . RoleName . Length > 50 ) p . RoleName = p . RoleName . Substring ( 0 , 50 ) ;
if ( ! string . IsNullOrEmpty ( p . Description ) & & p . Description . Length > 50 ) p . Description = p . Description . Substring ( 0 , 500 ) ;
2017-03-30 16:15:45 +08:00
string sql = p . Id = = 0 ?
2016-10-25 18:47:33 +08:00
"Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
"Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID" ;
2018-09-08 19:57:42 +08:00
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2016-10-25 18:47:33 +08:00
{
2018-09-08 19:57:42 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@ID" , p . Id ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@RoleName" , p . RoleName ) ) ;
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@Description" , DBAccessFactory . ToDBValue ( p . Description ) ) ) ;
ret = DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
2016-10-25 18:47:33 +08:00
}
2018-09-08 19:57:42 +08:00
CacheCleanUtility . ClearCache ( roleIds : p . Id = = 0 ? string . Empty : p . Id . ToString ( ) ) ;
2016-10-25 18:47:33 +08:00
return ret ;
}
2016-10-28 11:12:25 +08:00
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
2016-10-28 14:45:09 +08:00
public static IEnumerable < Role > RetrieveRolesByMenuId ( int menuId )
2016-10-28 11:12:25 +08:00
{
2017-03-30 16:15:45 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveRolesByMenuIdDataKey , menuId ) ;
2017-04-05 11:54:21 +08:00
var ret = CacheManager . GetOrAdd ( key , k = >
2016-10-28 11:12:25 +08:00
{
2016-11-04 16:06:40 +08:00
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID" ;
2017-03-30 16:15:45 +08:00
List < Role > roles = new List < Role > ( ) ;
2016-10-27 17:56:00 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@NavigationID" , menuId ) ) ;
2018-09-08 19:57:42 +08:00
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-28 11:12:25 +08:00
{
2018-09-08 19:57:42 +08:00
while ( reader . Read ( ) )
2016-10-28 11:12:25 +08:00
{
2018-09-08 19:57:42 +08:00
roles . Add ( new Role ( )
2016-10-28 11:12:25 +08:00
{
2018-09-08 19:57:42 +08:00
Id = ( int ) reader [ 0 ] ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2016-10-28 11:12:25 +08:00
}
}
2017-03-30 16:15:45 +08:00
return roles ;
2017-04-05 11:54:21 +08:00
} , RetrieveRolesByMenuIdDataKey ) ;
2016-10-27 17:56:00 +08:00
return ret ;
2016-10-28 11:12:25 +08:00
}
2016-10-31 15:55:31 +08:00
public static bool SavaRolesByMenuId ( int id , string roleIds )
2016-10-28 11:12:25 +08:00
{
2016-10-31 15:55:31 +08:00
var ret = false ;
2016-10-28 11:12:25 +08:00
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "NavigationID" , typeof ( int ) ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
//判断用户是否选定角色
2016-11-02 12:12:34 +08:00
if ( ! string . IsNullOrEmpty ( roleIds ) ) roleIds . Split ( ',' ) . ToList ( ) . ForEach ( roleId = > dt . Rows . Add ( id , roleId ) ) ;
2016-10-31 15:55:31 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
2016-10-28 11:12:25 +08:00
{
2016-10-31 15:55:31 +08:00
try
2016-10-28 11:12:25 +08:00
{
2016-10-31 15:55:31 +08:00
// delete role from config table
string sql = "delete from NavigationRole where NavigationID=@NavigationID;" ;
using ( DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) )
2016-10-28 11:12:25 +08:00
{
2017-03-30 16:15:45 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@NavigationID" , id ) ) ;
2016-10-31 15:55:31 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2016-10-28 11:12:25 +08:00
2016-10-31 15:55:31 +08:00
// insert batch data into config table
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
2016-10-28 11:12:25 +08:00
{
2016-10-31 15:55:31 +08:00
bulk . BatchSize = 1000 ;
bulk . DestinationTableName = "NavigationRole" ;
bulk . ColumnMappings . Add ( "NavigationID" , "NavigationID" ) ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
2016-10-28 11:12:25 +08:00
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
2016-11-05 12:11:16 +08:00
CacheCleanUtility . ClearCache ( roleIds : roleIds , menuIds : id . ToString ( ) ) ;
2016-10-31 15:55:31 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
2018-09-08 19:57:42 +08:00
throw ex ;
2016-10-28 11:12:25 +08:00
}
}
2016-10-31 15:55:31 +08:00
return ret ;
2016-10-28 11:12:25 +08:00
}
2016-10-29 09:24:55 +08:00
/// <summary>
2016-10-27 17:56:00 +08:00
/// 根据GroupId查询和该Group有关的所有Roles
2017-03-30 16:15:45 +08:00
/// </summary>
/// <param name="groupId"></param>
2016-10-29 09:24:55 +08:00
/// <returns></returns>
2017-03-30 16:15:45 +08:00
public static IEnumerable < Role > RetrieveRolesByGroupId ( int groupId )
2016-10-29 09:24:55 +08:00
{
2017-03-30 16:15:45 +08:00
string key = string . Format ( "{0}-{1}" , RetrieveRolesByGroupIdDataKey , groupId ) ;
2017-04-05 11:54:21 +08:00
return CacheManager . GetOrAdd ( key , k = >
2016-10-27 17:56:00 +08:00
{
2017-03-30 16:15:45 +08:00
List < Role > roles = new List < Role > ( ) ;
2016-11-02 12:12:34 +08:00
string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @GroupID" ;
2016-10-27 17:56:00 +08:00
DbCommand cmd = DBAccessManager . SqlDBAccess . CreateCommand ( CommandType . Text , sql ) ;
2018-09-08 19:57:42 +08:00
cmd . Parameters . Add ( DBAccessManager . SqlDBAccess . CreateParameter ( "@GroupID" , groupId ) ) ;
using ( DbDataReader reader = DBAccessManager . SqlDBAccess . ExecuteReader ( cmd ) )
2016-10-27 17:56:00 +08:00
{
2018-09-08 19:57:42 +08:00
while ( reader . Read ( ) )
2016-10-27 17:56:00 +08:00
{
2018-09-08 19:57:42 +08:00
roles . Add ( new Role ( )
2016-10-27 17:56:00 +08:00
{
2018-09-08 19:57:42 +08:00
Id = ( int ) reader [ 0 ] ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2016-10-27 17:56:00 +08:00
}
}
2017-03-30 16:15:45 +08:00
return roles ;
2017-04-05 11:54:21 +08:00
} , RetrieveRolesByGroupIdDataKey ) ;
2016-10-29 09:24:55 +08:00
}
2016-10-27 17:56:00 +08:00
2016-10-29 09:24:55 +08:00
/// <summary>
2016-10-27 17:56:00 +08:00
/// 根据GroupId更新Roles信息, 删除旧的Roles信息, 插入新的Roles信息
2016-10-29 09:24:55 +08:00
/// </summary>
2017-03-30 16:15:45 +08:00
/// <param name="id"></param>
/// <param name="roleIds"></param>
2016-10-29 09:24:55 +08:00
/// <returns></returns>
2016-10-31 15:55:31 +08:00
public static bool SaveRolesByGroupId ( int id , string roleIds )
2016-10-29 09:24:55 +08:00
{
2016-10-31 15:55:31 +08:00
var ret = false ;
2016-10-27 17:56:00 +08:00
//构造表格
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
2016-11-02 12:12:34 +08:00
if ( ! string . IsNullOrEmpty ( roleIds ) ) roleIds . Split ( ',' ) . ToList ( ) . ForEach ( roleId = > dt . Rows . Add ( roleId , id ) ) ;
2016-10-31 15:55:31 +08:00
using ( TransactionPackage transaction = DBAccessManager . SqlDBAccess . BeginTransaction ( ) )
2016-10-27 17:56:00 +08:00
{
2016-10-31 15:55:31 +08:00
try
2016-10-27 17:56:00 +08:00
{
2016-10-31 15:55:31 +08:00
// delete user from config table
string sql = "delete from RoleGroup where GroupID=@GroupID" ;
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-31 15:55:31 +08:00
DBAccessManager . SqlDBAccess . ExecuteNonQuery ( cmd , transaction ) ;
2016-10-27 17:56:00 +08:00
2016-10-31 15:55:31 +08:00
// insert batch data into config table
using ( SqlBulkCopy bulk = new SqlBulkCopy ( ( SqlConnection ) transaction . Transaction . Connection , SqlBulkCopyOptions . Default , ( SqlTransaction ) transaction . Transaction ) )
{
bulk . BatchSize = 1000 ;
bulk . DestinationTableName = "RoleGroup" ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
bulk . ColumnMappings . Add ( "GroupID" , "GroupID" ) ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
2016-11-05 12:11:16 +08:00
CacheCleanUtility . ClearCache ( roleIds : roleIds , groupIds : id . ToString ( ) ) ;
2016-10-31 15:55:31 +08:00
ret = true ;
}
catch ( Exception ex )
2016-10-27 17:56:00 +08:00
{
2016-10-31 15:55:31 +08:00
transaction . RollbackTransaction ( ) ;
2018-09-08 19:57:42 +08:00
throw ex ;
2016-10-27 17:56:00 +08:00
}
}
2016-10-31 15:55:31 +08:00
return ret ;
2016-10-29 09:24:55 +08:00
}
2016-10-25 18:47:33 +08:00
}
}