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