2016-10-26 14:02:40 +08:00
|
|
|
|
using Longbow;
|
|
|
|
|
using Longbow.Caching;
|
|
|
|
|
using Longbow.Caching.Configuration;
|
2016-10-26 21:51:38 +08:00
|
|
|
|
using Longbow.Data;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
using Longbow.ExceptionManagement;
|
2016-11-08 20:37:14 +08:00
|
|
|
|
using Longbow.Security.Principal;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
2016-11-07 21:41:17 +08:00
|
|
|
|
using System.Data.SqlClient;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
|
|
|
|
public static class MenuHelper
|
|
|
|
|
{
|
2016-11-05 12:11:16 +08:00
|
|
|
|
internal const string RetrieveMenusDataKey = "MenuHelper-RetrieveMenus";
|
2016-11-07 21:41:17 +08:00
|
|
|
|
internal const string RetrieveMenusByRoleIDDataKey = "MenuHelper-RetrieveMenusByRoleId";
|
2016-10-26 14:02:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询所有菜单信息
|
|
|
|
|
/// </summary>
|
2016-11-08 20:37:14 +08:00
|
|
|
|
/// <param name="userName"></param>
|
2016-10-26 14:02:40 +08:00
|
|
|
|
/// <returns></returns>
|
2016-11-08 20:37:14 +08:00
|
|
|
|
public static IEnumerable<Menu> RetrieveMenus(string userName = null)
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
userName = LgbPrincipal.IsAdmin(userName) ? string.Empty : userName;
|
|
|
|
|
string key = string.Format("{0}-{1}", RetrieveMenusDataKey, userName);
|
|
|
|
|
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusDataKey), k =>
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
|
|
|
|
List<Menu> Menus = new List<Menu>();
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_RetrieveMenus"))
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", DBAccess.ToDBValue(userName), ParameterDirection.Input));
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
while (reader.Read())
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
Menus.Add(new Menu()
|
|
|
|
|
{
|
|
|
|
|
ID = (int)reader[0],
|
|
|
|
|
ParentId = (int)reader[1],
|
|
|
|
|
Name = (string)reader[2],
|
|
|
|
|
Order = (int)reader[3],
|
|
|
|
|
Icon = LgbConvert.ReadValue(reader[4], string.Empty),
|
|
|
|
|
Url = LgbConvert.ReadValue(reader[5], string.Empty),
|
|
|
|
|
Category = (string)reader[6],
|
2016-11-24 20:58:47 +08:00
|
|
|
|
Target = (string)reader[7],
|
|
|
|
|
CategoryName = (string)reader[8],
|
|
|
|
|
ParentName = LgbConvert.ReadValue(reader[9], string.Empty)
|
2016-11-08 20:37:14 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-10-26 14:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return Menus;
|
2016-11-04 16:06:40 +08:00
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveMenusDataKey));
|
2016-10-26 14:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2016-11-08 20:37:14 +08:00
|
|
|
|
///
|
2016-10-26 14:02:40 +08:00
|
|
|
|
/// </summary>
|
2016-11-15 14:31:53 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Menu> RetrieveAllMenusByUserName(string userName)
|
|
|
|
|
{
|
|
|
|
|
var navs = RetrieveMenus(userName);
|
|
|
|
|
var root = navs.Where(m => m.ParentId == 0).OrderBy(m => m.Order);
|
|
|
|
|
CascadeMenu(navs, root);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-10-26 14:02:40 +08:00
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-11-08 20:37:14 +08:00
|
|
|
|
public static IEnumerable<Menu> RetrieveNavigationsByUserName(string userName)
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
var navs = RetrieveMenus(userName).Where(m => m.Category == "0");
|
|
|
|
|
var root = navs.Where(m => m.ParentId == 0).OrderBy(m => m.Order);
|
|
|
|
|
CascadeMenu(navs, root);
|
|
|
|
|
return root;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2016-11-05 23:15:07 +08:00
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userId"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-11-08 20:37:14 +08:00
|
|
|
|
public static IEnumerable<Menu> RetrieveLinksByUserName(string userName)
|
2016-11-05 23:15:07 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
var navs = RetrieveMenus(userName).Where(m => m.Category == "1");
|
2016-11-06 00:32:55 +08:00
|
|
|
|
var root = navs.Where(m => m.ParentId == 0).OrderBy(m => m.Order);
|
2016-11-05 23:15:07 +08:00
|
|
|
|
CascadeMenu(navs, root);
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
private static void CascadeMenu(IEnumerable<Menu> navs, IEnumerable<Menu> level)
|
|
|
|
|
{
|
|
|
|
|
level.ToList().ForEach(m =>
|
|
|
|
|
{
|
2016-11-06 00:32:55 +08:00
|
|
|
|
m.Menus = navs.Where(sub => sub.ParentId == m.ID).OrderBy(sub => sub.Order);
|
2016-11-05 23:15:07 +08:00
|
|
|
|
CascadeMenu(navs, m.Menus);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
2016-10-26 14:02:40 +08:00
|
|
|
|
/// 删除菜单信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
public static bool DeleteMenu(string ids)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return ret;
|
2016-11-07 15:17:10 +08:00
|
|
|
|
try
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-07 15:17:10 +08:00
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus"))
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2016-11-07 15:17:10 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids, ParameterDirection.Input));
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
2016-10-26 14:02:40 +08:00
|
|
|
|
}
|
2016-11-07 15:17:10 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(menuIds: ids);
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
2016-10-26 14:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新建/更新的菜单信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool SaveMenu(Menu p)
|
|
|
|
|
{
|
|
|
|
|
if (p == null) throw new ArgumentNullException("p");
|
|
|
|
|
bool ret = false;
|
2016-10-26 21:51:38 +08:00
|
|
|
|
if (string.IsNullOrEmpty(p.Name)) return ret;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
if (p.Name.Length > 50) p.Name.Substring(0, 50);
|
2016-10-26 21:51:38 +08:00
|
|
|
|
if (p.Icon != null && p.Icon.Length > 50) p.Icon.Substring(0, 50);
|
|
|
|
|
if (p.Url != null && p.Url.Length > 50) p.Url.Substring(0, 50);
|
2016-10-26 14:02:40 +08:00
|
|
|
|
string sql = p.ID == 0 ?
|
2016-11-24 20:58:47 +08:00
|
|
|
|
"Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category, Target) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category, @Target)" :
|
|
|
|
|
"Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category, Target = @Target where ID = @ID";
|
2016-10-26 14:02:40 +08:00
|
|
|
|
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("@ParentId", p.ParentId, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name, ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order, ParameterDirection.Input));
|
2016-10-26 21:51:38 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccess.ToDBValue(p.Icon), ParameterDirection.Input));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccess.ToDBValue(p.Url), ParameterDirection.Input));
|
2016-10-26 14:02:40 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category, ParameterDirection.Input));
|
2016-11-24 20:58:47 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Target", p.Target, ParameterDirection.Input));
|
2016-10-26 14:02:40 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
2016-11-11 14:32:52 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(menuIds: p.ID == 0 ? string.Empty : p.ID.ToString());
|
2016-10-26 14:02:40 +08:00
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (DbException ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-11-07 21:41:17 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询某个角色所配置的菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static IEnumerable<Menu> RetrieveMenusByRoleId(int roleId)
|
|
|
|
|
{
|
|
|
|
|
string key = string.Format("{0}-{1}", RetrieveMenusByRoleIDDataKey, roleId);
|
|
|
|
|
return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RetrieveMenusByRoleIDDataKey), k =>
|
|
|
|
|
{
|
|
|
|
|
List<Menu> Menus = new List<Menu>();
|
|
|
|
|
try
|
|
|
|
|
{
|
2016-11-14 20:44:08 +08:00
|
|
|
|
string sql = "select NavigationID from NavigationRole where RoleID = @RoleID";
|
2016-11-08 20:37:14 +08:00
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
2016-11-07 21:41:17 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId, ParameterDirection.Input));
|
|
|
|
|
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
2016-11-07 21:41:17 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
while (reader.Read())
|
2016-11-07 21:41:17 +08:00
|
|
|
|
{
|
2016-11-08 20:37:14 +08:00
|
|
|
|
Menus.Add(new Menu()
|
|
|
|
|
{
|
2016-11-14 20:44:08 +08:00
|
|
|
|
ID = (int)reader[0]
|
2016-11-08 20:37:14 +08:00
|
|
|
|
});
|
|
|
|
|
}
|
2016-11-07 21:41:17 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
|
|
|
|
return Menus;
|
|
|
|
|
}, CacheSection.RetrieveDescByKey(RetrieveMenusByRoleIDDataKey));
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过角色ID保存当前授权菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id"></param>
|
|
|
|
|
/// <param name="menuIds"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool SaveMenusByRoleId(int id, string menuIds)
|
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
DataTable dt = new DataTable();
|
|
|
|
|
dt.Columns.Add("RoleID", typeof(int));
|
|
|
|
|
dt.Columns.Add("NavigationID", typeof(int));
|
|
|
|
|
if (!string.IsNullOrEmpty(menuIds)) menuIds.Split(',').ToList().ForEach(menuId => dt.Rows.Add(id, Convert.ToInt32(menuId)));
|
|
|
|
|
using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//删除菜单角色表该角色所有的菜单
|
|
|
|
|
string sql = "delete from NavigationRole where RoleID=@RoleID";
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", id, ParameterDirection.Input));
|
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
//批插入菜单角色表
|
|
|
|
|
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
|
|
|
|
|
{
|
|
|
|
|
bulk.DestinationTableName = "NavigationRole";
|
|
|
|
|
bulk.ColumnMappings.Add("RoleID", "RoleID");
|
|
|
|
|
bulk.ColumnMappings.Add("NavigationID", "NavigationID");
|
|
|
|
|
bulk.WriteToServer(dt);
|
|
|
|
|
transaction.CommitTransaction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: id.ToString());
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ExceptionManager.Publish(ex);
|
|
|
|
|
transaction.RollbackTransaction();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2016-10-26 14:02:40 +08:00
|
|
|
|
}
|
|
|
|
|
}
|