2018-10-25 17:52:04 +08:00
using Bootstrap.Security.SQLServer ;
using Longbow ;
2018-10-21 10:02:59 +08:00
using Longbow.Data ;
2018-10-19 23:09:52 +08:00
using System ;
using System.Collections.Generic ;
using System.Data ;
using System.Data.Common ;
2018-10-21 10:02:59 +08:00
using System.Data.SqlClient ;
using System.Linq ;
2018-10-19 23:09:52 +08:00
namespace Bootstrap.DataAccess
2016-10-28 13:36:43 +08:00
{
2017-03-30 16:15:45 +08:00
/// <summary>
///
/// </summary>
2016-10-28 13:36:43 +08:00
public class Role
{
/// <summary>
/// 获得/设置 角色主键ID
/// </summary>
2017-03-30 16:15:45 +08:00
public int Id { get ; set ; }
2016-10-28 13:36:43 +08:00
/// <summary>
/// 获得/设置 角色名称
/// </summary>
public string RoleName { get ; set ; }
/// <summary>
/// 获得/设置 角色描述
/// </summary>
public string Description { get ; set ; }
/// <summary>
/// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get ; set ; }
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询所有角色
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Role > RetrieveRoles ( int id = 0 )
{
2018-10-28 15:03:59 +08:00
string sql = "select * from Roles" ;
var roles = new List < Role > ( ) ;
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
roles . Add ( new Role ( )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
Id = LgbConvert . ReadValue ( reader [ 0 ] , 0 ) ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return roles ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 保存用户角色关系
/// </summary>
2018-10-23 14:41:23 +08:00
/// <param name="userId"></param>
2018-10-19 23:09:52 +08:00
/// <param name="roleIds"></param>
/// <returns></returns>
2018-10-23 14:41:23 +08:00
public virtual bool SaveRolesByUserId ( int userId , IEnumerable < int > roleIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "UserID" , typeof ( int ) ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
//判断用户是否选定角色
2018-10-23 14:41:23 +08:00
roleIds . ToList ( ) . ForEach ( roleId = > dt . Rows . Add ( userId , roleId ) ) ;
2018-10-21 10:02:59 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
{
try
{
// delete user from config table
2018-10-23 14:41:23 +08:00
string sql = $"delete from UserRole where UserID = {userId}" ;
2018-10-21 10:02:59 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
if ( dt . Rows . Count > 0 )
{
// 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 ) ;
}
}
transaction . CommitTransaction ( ) ;
}
2018-10-23 14:41:23 +08:00
CacheCleanUtility . ClearCache ( userIds : new List < int > ( ) { userId } , roleIds : roleIds ) ;
2018-10-21 10:02:59 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Role > RetrieveRolesByUserId ( int userId )
{
2018-10-28 15:03:59 +08:00
List < Role > roles = new List < Role > ( ) ;
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" ;
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@UserID" , userId ) ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
roles . Add ( new Role ( )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
Id = LgbConvert . ReadValue ( reader [ 0 ] , 0 ) ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return roles ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 删除角色表
/// </summary>
/// <param name="value"></param>
2018-10-21 10:02:59 +08:00
public virtual bool DeleteRole ( IEnumerable < int > value )
{
bool ret = false ;
var ids = string . Join ( "," , value ) ;
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . StoredProcedure , "Proc_DeleteRoles" ) )
{
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@ids" , ids ) ) ;
ret = DbAccessManager . DBAccess . ExecuteNonQuery ( cmd ) = = - 1 ;
}
CacheCleanUtility . ClearCache ( roleIds : value ) ;
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual 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 ) ;
string sql = p . Id = = 0 ?
"Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
"Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID" ;
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@ID" , p . Id ) ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@RoleName" , p . RoleName ) ) ;
2018-10-21 21:08:35 +08:00
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@Description" , DbAdapterManager . ToDBValue ( p . Description ) ) ) ;
2018-10-21 10:02:59 +08:00
ret = DbAccessManager . DBAccess . ExecuteNonQuery ( cmd ) = = 1 ;
}
CacheCleanUtility . ClearCache ( roleIds : p . Id = = 0 ? new List < int > ( ) : new List < int > { p . Id } ) ;
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Role > RetrieveRolesByMenuId ( int menuId )
{
2018-10-28 15:03:59 +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" ;
List < Role > roles = new List < Role > ( ) ;
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@NavigationID" , menuId ) ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
roles . Add ( new Role ( )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
Id = LgbConvert . ReadValue ( reader [ 0 ] , 0 ) ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return roles ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
2018-10-23 14:41:23 +08:00
/// <param name="menuId"></param>
2018-10-19 23:09:52 +08:00
/// <param name="roleIds"></param>
/// <returns></returns>
2018-10-23 14:41:23 +08:00
public virtual bool SavaRolesByMenuId ( int menuId , IEnumerable < int > roleIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "NavigationID" , typeof ( int ) ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
//判断用户是否选定角色
2018-10-23 14:41:23 +08:00
roleIds . ToList ( ) . ForEach ( roleId = > dt . Rows . Add ( menuId , roleId ) ) ;
2018-10-21 10:02:59 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
{
try
{
// delete role from config table
2018-10-23 14:41:23 +08:00
string sql = $"delete from NavigationRole where NavigationID = {menuId}" ;
2018-10-21 10:02:59 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
// 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 = "NavigationRole" ;
bulk . ColumnMappings . Add ( "NavigationID" , "NavigationID" ) ;
bulk . ColumnMappings . Add ( "RoleID" , "RoleID" ) ;
bulk . WriteToServer ( dt ) ;
transaction . CommitTransaction ( ) ;
}
}
2018-10-23 14:41:23 +08:00
CacheCleanUtility . ClearCache ( roleIds : roleIds , menuIds : new List < int > ( ) { menuId } ) ;
2018-10-21 10:02:59 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据GroupId查询和该Group有关的所有Roles
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
2018-10-21 10:02:59 +08:00
public virtual IEnumerable < Role > RetrieveRolesByGroupId ( int groupId )
{
2018-10-28 15:03:59 +08:00
List < Role > roles = new List < Role > ( ) ;
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" ;
DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) ;
cmd . Parameters . Add ( DbAccessManager . DBAccess . CreateParameter ( "@GroupID" , groupId ) ) ;
using ( DbDataReader reader = DbAccessManager . DBAccess . ExecuteReader ( cmd ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
while ( reader . Read ( ) )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
roles . Add ( new Role ( )
2018-10-21 10:02:59 +08:00
{
2018-10-28 15:03:59 +08:00
Id = LgbConvert . ReadValue ( reader [ 0 ] , 0 ) ,
RoleName = ( string ) reader [ 1 ] ,
Description = reader . IsDBNull ( 2 ) ? string . Empty : ( string ) reader [ 2 ] ,
Checked = ( string ) reader [ 3 ]
} ) ;
2018-10-21 10:02:59 +08:00
}
2018-10-28 15:03:59 +08:00
}
return roles ;
2018-10-21 10:02:59 +08:00
}
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据GroupId更新Roles信息, 删除旧的Roles信息, 插入新的Roles信息
/// </summary>
2018-10-23 14:41:23 +08:00
/// <param name="groupId"></param>
2018-10-19 23:09:52 +08:00
/// <param name="roleIds"></param>
/// <returns></returns>
2018-10-23 14:41:23 +08:00
public virtual bool SaveRolesByGroupId ( int groupId , IEnumerable < int > roleIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
//构造表格
DataTable dt = new DataTable ( ) ;
dt . Columns . Add ( "RoleID" , typeof ( int ) ) ;
dt . Columns . Add ( "GroupID" , typeof ( int ) ) ;
2018-10-23 14:41:23 +08:00
roleIds . ToList ( ) . ForEach ( roleId = > dt . Rows . Add ( roleId , groupId ) ) ;
2018-10-21 10:02:59 +08:00
using ( TransactionPackage transaction = DbAccessManager . DBAccess . BeginTransaction ( ) )
{
try
{
// delete user from config table
2018-10-23 14:41:23 +08:00
string sql = $"delete from RoleGroup where GroupID = {groupId}" ;
2018-10-21 10:02:59 +08:00
using ( DbCommand cmd = DbAccessManager . DBAccess . CreateCommand ( CommandType . Text , sql ) )
{
DbAccessManager . DBAccess . ExecuteNonQuery ( cmd , transaction ) ;
// 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 ( ) ;
}
}
2018-10-23 14:41:23 +08:00
CacheCleanUtility . ClearCache ( roleIds : roleIds , groupIds : new List < int > ( ) { groupId } ) ;
2018-10-21 10:02:59 +08:00
ret = true ;
}
catch ( Exception ex )
{
transaction . RollbackTransaction ( ) ;
throw ex ;
}
}
return ret ;
}
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2018-10-25 17:52:04 +08:00
public virtual IEnumerable < string > RetrieveRolesByUserName ( string userName ) = > BASQLHelper . RetrieveRolesByUserName ( userName ) ;
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查
/// 从Navigators-〉GroupNavigatorRole-〉Role查查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
2018-10-25 17:52:04 +08:00
public virtual IEnumerable < string > RetrieveRolesByUrl ( string url ) = > BASQLHelper . RetrieveRolesByUrl ( url ) ;
2016-10-28 13:36:43 +08:00
}
}