2018-10-28 23:35:23 +08:00
using Bootstrap.Security.DataAccess ;
2019-01-14 12:33:09 +08:00
using Longbow.Data ;
2018-10-19 23:09:52 +08:00
using System ;
using System.Collections.Generic ;
2018-10-21 10:02:59 +08:00
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>
2018-10-30 13:07:29 +08:00
public string Id { get ; set ; }
2018-10-31 11:15:43 +08:00
2016-10-28 13:36:43 +08:00
/// <summary>
/// 获得/设置 角色名称
/// </summary>
public string RoleName { get ; set ; }
2018-10-31 11:15:43 +08:00
2016-10-28 13:36:43 +08:00
/// <summary>
/// 获得/设置 角色描述
/// </summary>
public string Description { get ; set ; }
2018-10-31 11:15:43 +08:00
2016-10-28 13:36:43 +08:00
/// <summary>
/// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get ; set ; }
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询所有角色
/// </summary>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Role > Retrieves ( ) = > DbManager . Create ( ) . Fetch < Role > ( ) ;
2018-10-31 11:15:43 +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>
2019-01-11 23:20:28 +08:00
public virtual bool SaveByUserId ( string userId , IEnumerable < string > roleIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
db . BeginTransaction ( ) ;
// delete user from config table
db . Execute ( "delete from UserRole where UserID = @0" , userId ) ;
2019-01-14 12:33:09 +08:00
db . InsertBatch ( "UserRole" , roleIds . Select ( g = > new { UserID = userId , RoleID = g } ) ) ;
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 ;
}
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Role > RetrievesByUserId ( string userId ) = > DbManager . Create ( ) . Fetch < Role > ( "select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join UserRole ur on r.ID = ur.RoleID and UserID = @0" , userId ) ;
2018-10-31 11:15:43 +08:00
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-17 13:28:23 +08:00
if ( ! value . Any ( ) ) return true ;
2018-10-21 10:02:59 +08:00
bool ret = false ;
var ids = string . Join ( "," , value ) ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
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 in ({ids})" ) ;
db . Execute ( $"delete from RoleGroup where RoleID in ({ids})" ) ;
db . Execute ( $"delete from NavigationRole where RoleID in ({ids})" ) ;
db . Execute ( $"delete from Roles 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 ;
}
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
2019-01-11 23:20:28 +08:00
public virtual bool Save ( Role p )
2018-10-21 10:02:59 +08:00
{
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 ) ;
2019-01-11 23:20:28 +08:00
2019-01-14 12:33:09 +08:00
DbManager . Create ( ) . Save ( p ) ;
2019-01-11 23:20:28 +08:00
return true ;
2018-10-21 10:02:59 +08:00
}
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Role > RetrievesByMenuId ( string menuId ) = > DbManager . Create ( ) . Fetch < Role > ( "select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @0" , menuId ) ;
2018-10-31 11:15:43 +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>
2019-01-11 23:20:28 +08:00
public virtual bool SavaByMenuId ( string menuId , IEnumerable < string > roleIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
db . BeginTransaction ( ) ;
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
// delete role from config table
db . Execute ( "delete from NavigationRole where NavigationID = @0" , menuId ) ;
2019-01-14 12:33:09 +08:00
db . InsertBatch ( "NavigationRole" , roleIds . Select ( g = > new { NavigationID = menuId , RoleID = g } ) ) ;
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 ;
}
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据GroupId查询和该Group有关的所有Roles
/// </summary>
/// <param name="groupId"></param>
/// <returns></returns>
2019-01-14 12:33:09 +08:00
public virtual IEnumerable < Role > RetrievesByGroupId ( string groupId ) = > DbManager . Create ( ) . Fetch < Role > ( "select r.ID, r.RoleName, r.Description, case ur.RoleID when r.ID then 'checked' else '' end Checked from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @0" , groupId ) ;
2018-10-31 11:15:43 +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>
2019-01-11 23:20:28 +08:00
public virtual bool SaveByGroupId ( string groupId , IEnumerable < string > roleIds )
2018-10-21 10:02:59 +08:00
{
var ret = false ;
2019-01-14 12:33:09 +08:00
var db = DbManager . Create ( ) ;
2019-01-11 23:20:28 +08:00
try
2018-10-21 10:02:59 +08:00
{
2019-01-11 23:20:28 +08:00
// delete user from config table
db . Execute ( "delete from RoleGroup where GroupID = @0" , groupId ) ;
2019-01-14 12:33:09 +08:00
db . InsertBatch ( "RoleGroup" , roleIds . Select ( g = > new { GroupID = groupId , RoleID = g } ) ) ;
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 ;
}
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
///
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
2018-10-28 23:35:23 +08:00
public virtual IEnumerable < string > RetrieveRolesByUserName ( string userName ) = > DbHelper . RetrieveRolesByUserName ( userName ) ;
2018-10-31 11:15:43 +08:00
2018-10-19 23:09:52 +08:00
/// <summary>
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查
/// 从Navigators-〉GroupNavigatorRole-〉Role查查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
2018-10-28 23:35:23 +08:00
public virtual IEnumerable < string > RetrieveRolesByUrl ( string url ) = > DbHelper . RetrieveRolesByUrl ( url ) ;
2016-10-28 13:36:43 +08:00
}
}