2017-04-03 15:56:17 +08:00
|
|
|
|
using Bootstrap.Security;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
using Longbow.Cache;
|
2016-10-26 21:51:38 +08:00
|
|
|
|
using Longbow.Data;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
using Longbow.Logging;
|
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
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2016-10-26 14:02:40 +08:00
|
|
|
|
public static class MenuHelper
|
|
|
|
|
{
|
2017-04-13 16:49:48 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal const string RetrieveMenusByRoleIdDataKey = "MenuHelper-RetrieveMenusByRoleId";
|
2016-10-26 14:02:40 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除菜单信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="ids"></param>
|
|
|
|
|
public static bool DeleteMenu(string ids)
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return false;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
bool ret = false;
|
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
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids));
|
2016-11-07 15:17:10 +08:00
|
|
|
|
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>
|
2017-04-03 15:56:17 +08:00
|
|
|
|
public static bool SaveMenu(BootstrapMenu p)
|
2016-10-26 14:02:40 +08:00
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
if (string.IsNullOrEmpty(p.Name)) return false;
|
2016-10-26 14:02:40 +08:00
|
|
|
|
bool ret = false;
|
2017-03-30 16:15:45 +08:00
|
|
|
|
if (p.Name.Length > 50) p.Name = p.Name.Substring(0, 50);
|
|
|
|
|
if (p.Icon != null && p.Icon.Length > 50) p.Icon = p.Icon.Substring(0, 50);
|
|
|
|
|
if (p.Url != null && p.Url.Length > 4000) p.Url = p.Url.Substring(0, 4000);
|
|
|
|
|
string sql = p.Id == 0 ?
|
2017-03-03 13:34:53 +08:00
|
|
|
|
"Insert Into Navigations (ParentId, Name, [Order], Icon, Url, Category, Target, IsResource, [Application]) Values (@ParentId, @Name, @Order, @Icon, @Url, @Category, @Target, @IsResource, @ApplicationCode)" :
|
|
|
|
|
"Update Navigations set ParentId = @ParentId, Name = @Name, [Order] = @Order, Icon = @Icon, Url = @Url, Category = @Category, Target = @Target, IsResource = @IsResource, Application = @ApplicationCode where ID = @ID";
|
2016-10-26 14:02:40 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.Id));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ParentId", p.ParentId));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Name", p.Name));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Order", p.Order));
|
2018-06-07 00:45:47 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Icon", DBAccessFactory.ToDBValue(p.Icon)));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Url", DBAccessFactory.ToDBValue(p.Url)));
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Category", p.Category));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Target", p.Target));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@IsResource", p.IsResource));
|
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode));
|
2016-10-26 14:02:40 +08:00
|
|
|
|
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
|
|
|
|
}
|
2017-03-30 16:15:45 +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>
|
2017-04-03 15:56:17 +08:00
|
|
|
|
public static IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(int roleId)
|
2016-11-07 21:41:17 +08:00
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
string key = string.Format("{0}-{1}", RetrieveMenusByRoleIdDataKey, roleId);
|
2017-04-05 11:54:21 +08:00
|
|
|
|
return CacheManager.GetOrAdd(key, k =>
|
2016-11-07 21:41:17 +08:00
|
|
|
|
{
|
2017-04-03 15:56:17 +08:00
|
|
|
|
var menus = new List<BootstrapMenu>();
|
2016-11-07 21:41:17 +08:00
|
|
|
|
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
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId));
|
2016-11-08 20:37:14 +08:00
|
|
|
|
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
|
|
|
|
{
|
2017-04-03 15:56:17 +08:00
|
|
|
|
menus.Add(new BootstrapMenu()
|
2016-11-08 20:37:14 +08:00
|
|
|
|
{
|
2017-03-30 16:15:45 +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); }
|
2017-03-30 16:15:45 +08:00
|
|
|
|
return menus;
|
2017-04-05 11:54:21 +08:00
|
|
|
|
}, RetrieveMenusByRoleIdDataKey);
|
2016-11-07 21:41:17 +08:00
|
|
|
|
}
|
|
|
|
|
/// <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))
|
|
|
|
|
{
|
2017-03-30 16:15:45 +08:00
|
|
|
|
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", id));
|
2016-11-07 21:41:17 +08:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|