BootstrapAdmin/Bootstrap.DataAccess/RoleHelper.cs

424 lines
22 KiB
C#
Raw Normal View History

2016-10-25 18:47:33 +08:00
using Longbow;
using Longbow.Caching;
using Longbow.Caching.Configuration;
2016-10-26 21:32:54 +08:00
using Longbow.Data;
2016-10-25 18:47:33 +08:00
using Longbow.ExceptionManagement;
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.Globalization;
using System.Linq;
namespace Bootstrap.DataAccess
{
/// <summary>
///
/// </summary>
2016-11-04 16:06:40 +08:00
public static class RoleHelper
2016-10-25 18:47:33 +08:00
{
2016-11-04 16:06:40 +08:00
private const string RetrieveRolesDataKey = "RoleHelper-RetrieveRoles";
private const string RetrieveRolesByUrlDataKey = "RoleHelper-RetrieveRolesByUrl";
private const string RetrieveRolesByUserNameDataKey = "RoleHelper-RetrieveRolesByUserName";
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>
/// <param name="tId"></param>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRoles(string tId = null)
{
2016-11-04 16:06:40 +08:00
var ret = CacheManager.GetOrAdd(RetrieveRolesDataKey, CacheSection.RetrieveIntervalByKey(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);
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
roles.Add(new Role()
{
ID = (int)reader[0],
RoleName = LgbConvert.ReadValue(reader[1], string.Empty),
Description = LgbConvert.ReadValue(reader[2], string.Empty)
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return roles;
2016-11-04 16:06:40 +08:00
}, CacheSection.RetrieveDescByKey(RetrieveRolesDataKey));
2016-10-25 18:47:33 +08:00
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
}
/// <summary>
2016-10-26 21:32:54 +08:00
/// 保存用户角色关系
2016-10-25 18:47:33 +08:00
/// </summary>
/// <param name="id"></param>
/// <param name="roleIds"></param>
2016-10-25 18:47:33 +08:00
/// <returns></returns>
public static bool SaveRolesByUserId(int id, string roleIds)
2016-10-25 18:47:33 +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));
//判断用户是否选定角色
if (!string.IsNullOrEmpty(roleIds)) roleIds.Split(',').ToList().ForEach(roleId => dt.Rows.Add(id, roleId));
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
2016-10-26 21:32:54 +08:00
{
try
2016-10-26 21:32:54 +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
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
2016-10-25 18:47:33 +08:00
// insert batch data into config table
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
2016-10-26 21:32:54 +08:00
{
bulk.BatchSize = 1000;
bulk.DestinationTableName = "UserRole";
bulk.ColumnMappings.Add("UserID", "UserID");
bulk.ColumnMappings.Add("RoleID", "RoleID");
2016-10-26 21:32:54 +08:00
bulk.WriteToServer(dt);
transaction.CommitTransaction();
}
}
2016-11-04 16:06:40 +08:00
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(r => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveRolesByUserIDDataKey, id) || key == string.Format("{0}-{1}", UserHelper.RetrieveUsersByRoleIDDataKey, r)));
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
2016-10-26 21:32:54 +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>
public static IEnumerable<Role> RetrieveRolesByUserId(int userId)
2016-10-25 18:47:33 +08:00
{
2016-11-04 16:06:40 +08:00
string key = string.Format("{0}-{1}", RetrieveRolesByUserIDDataKey, userId);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByUserIDDataKey), k =>
2016-10-26 21:32:54 +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";
2016-10-26 21:32:54 +08:00
try
{
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId, ParameterDirection.Input));
2016-10-26 21:32:54 +08:00
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Roles.Add(new Role()
{
ID = (int)reader[0],
RoleName = (string)reader[1],
Description = (string)reader[2],
Checked = (string)reader[3]
2016-10-26 21:32:54 +08:00
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
2016-11-04 16:06:40 +08:00
}, CacheSection.RetrieveDescByKey(RetrieveRolesByUserIDDataKey));
}
2016-10-25 18:47:33 +08:00
/// <summary>
/// 删除角色表
/// </summary>
/// <param name="IDs"></param>
public static bool DeleteRole(string IDs)
{
bool ret = false;
if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret;
try
{
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles where ID in ({0})", IDs);
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
2016-11-04 16:06:40 +08:00
CacheManager.Clear(key => key == RetrieveRolesDataKey);
2016-10-25 18:47:33 +08:00
ret = true;
}
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
/// <summary>
/// 保存新建/更新的角色信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveRole(Role p)
{
if (p == null) throw new ArgumentNullException("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";
try
{
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName, ParameterDirection.Input));
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
}
2016-11-04 16:06:40 +08:00
CacheManager.Clear(key => key == RetrieveRolesDataKey);
2016-10-25 18:47:33 +08:00
ret = true;
}
catch (DbException ex)
{
ExceptionManager.Publish(ex);
}
return ret;
}
/// <summary>
/// 查询某个菜单所拥有的角色
/// </summary>
/// <param name="menuId"></param>
/// <returns></returns>
2016-10-28 14:45:09 +08:00
public static IEnumerable<Role> RetrieveRolesByMenuId(int menuId)
{
2016-11-04 16:06:40 +08:00
string key = string.Format("{0}-{1}", RetrieveRolesByMenuIDDataKey, menuId);
var ret = CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByMenuIDDataKey), k =>
{
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";
List<Role> Roles = new List<Role>();
2016-10-27 17:56:00 +08:00
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input));
try
{
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Roles.Add(new Role()
{
ID = (int)reader[0],
RoleName = (string)reader[1],
Description = (string)reader[2],
2016-10-28 14:45:09 +08:00
Checked = (string)reader[3]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
2016-11-04 16:06:40 +08:00
}, CacheSection.RetrieveDescByKey(RetrieveRolesByMenuIDDataKey));
2016-10-27 17:56:00 +08:00
return ret;
}
public static bool SavaRolesByMenuId(int id, string roleIds)
{
var ret = false;
DataTable dt = new DataTable();
dt.Columns.Add("NavigationID", typeof(int));
dt.Columns.Add("RoleID", typeof(int));
//判断用户是否选定角色
if (!string.IsNullOrEmpty(roleIds)) roleIds.Split(',').ToList().ForEach(roleId => dt.Rows.Add(id, roleId));
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
{
try
{
// delete role from config table
string sql = "delete from NavigationRole where NavigationID=@NavigationID;";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.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();
}
}
2016-11-04 16:06:40 +08:00
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(r => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveRolesByMenuIDDataKey, id) || key.Contains(MenuHelper.RetrieveMenusByUserIDDataKey)));
ret = true;
}
catch (Exception ex)
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
}
}
return ret;
}
2016-10-29 09:24:55 +08:00
/// <summary>
2016-10-27 17:56:00 +08:00
/// 根据GroupId查询和该Group有关的所有Roles
/// author:liuchun
/// <param name="tId"></param>
2016-10-29 09:24:55 +08:00
/// <returns></returns>
2016-10-27 17:56:00 +08:00
public static IEnumerable<Role> RetrieveRolesByGroupId(int groupID)
2016-10-29 09:24:55 +08:00
{
2016-11-04 16:06:40 +08:00
string key = string.Format("{0}-{1}", RetrieveRolesByGroupIDDataKey, groupID);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByGroupIDDataKey), k =>
2016-10-27 17:56:00 +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";
2016-10-27 17:56:00 +08:00
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
try
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupID, ParameterDirection.Input));
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Roles.Add(new Role()
{
ID = (int)reader[0],
RoleName = (string)reader[1],
Description = (string)reader[2],
Checked = (string)reader[3]
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
2016-11-04 16:06:40 +08:00
}, CacheSection.RetrieveDescByKey(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>
2016-10-27 17:56:00 +08:00
/// <param name="p"></param>
2016-10-29 09:24:55 +08:00
/// <returns></returns>
public static bool SaveRolesByGroupId(int id, string roleIds)
2016-10-29 09:24:55 +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));
if (!string.IsNullOrEmpty(roleIds)) roleIds.Split(',').ToList().ForEach(roleId => dt.Rows.Add(roleId, id));
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
2016-10-27 17:56:00 +08:00
{
try
2016-10-27 17:56:00 +08:00
{
// delete user from config table
string sql = "delete from RoleGroup where GroupID=@GroupID";
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
{
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", id, ParameterDirection.Input));
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
2016-10-27 17:56:00 +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-04 16:06:40 +08:00
roleIds.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).AsParallel()
.ForAll(r => CacheManager.Clear(key => key == string.Format("{0}-{1}", RetrieveRolesByGroupIDDataKey, id) || key == string.Format("{0}-{1}", GroupHelper.RetrieveGroupsByRoleIDDataKey, r)));
ret = true;
}
catch (Exception ex)
2016-10-27 17:56:00 +08:00
{
ExceptionManager.Publish(ex);
transaction.RollbackTransaction();
2016-10-27 17:56:00 +08:00
}
}
return ret;
2016-10-29 09:24:55 +08:00
}
/// <summary>
/// 根据用户名查询某个用户所拥有的角色
/// 从UserRole表查
/// 从User-〉Group-〉GroupRole查
/// </summary>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUserName(string username)
{
2016-11-04 16:06:40 +08:00
string key = string.Format("{0}-{1}", RetrieveRolesByUserNameDataKey, username);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesByUserNameDataKey), k =>
{
List<Role> Roles = new List<Role>();
try
{
string sql = "select r.ID, r.RoleName, r.[Description] from Roles r inner join UserRole ur on r.ID=ur.RoleID inner join Users u on ur.UserID=u.ID and u.UserName=@UserName union select r.ID, r.RoleName, r.[Description] from Roles r inner join RoleGroup rg on r.ID=rg.RoleID inner join Groups g on rg.GroupID=g.ID inner join UserGroup ug on ug.GroupID=g.ID inner join Users u on ug.UserID=u.ID and u.UserName=@UserName";
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserName", username, ParameterDirection.Input));
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Roles.Add(new Role()
{
ID = (int)reader[0],
RoleName = (string)reader[1],
Description = (string)reader[2],
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
2016-11-04 16:06:40 +08:00
}, CacheSection.RetrieveDescByKey(RetrieveRolesByUserNameDataKey));
}
/// <summary>
/// 根据菜单url查询某个所拥有的角色
/// 从NavigatorRole表查
/// 从Navigators-〉GroupNavigatorRole-〉Role查查询某个用户所拥有的角色
/// </summary>
/// <returns></returns>
public static IEnumerable<Role> RetrieveRolesByUrl(string url)
{
2016-11-04 16:06:40 +08:00
string key = string.Format("{0}-{1}", RetrieveRolesDataKey, url);
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveRolesDataKey), k =>
{
string sql = "select r.ID, r.RoleName, r.[Description] from Roles r inner join NavigationRole nr on r.ID = nr.RoleID inner join Navigations n on nr.NavigationID = n.ID and n.Url = @URl";
List<Role> Roles = new List<Role>();
try
{
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@URl", url, ParameterDirection.Input));
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
{
while (reader.Read())
{
Roles.Add(new Role()
{
ID = (int)reader[0],
RoleName = (string)reader[1],
Description = (string)reader[2],
});
}
}
}
catch (Exception ex) { ExceptionManager.Publish(ex); }
return Roles;
2016-11-04 16:06:40 +08:00
}, CacheSection.RetrieveDescByKey(RetrieveRolesDataKey));
}
2016-10-25 18:47:33 +08:00
}
}