2018-10-19 23:09:52 +08:00
|
|
|
|
using Bootstrap.Security;
|
2018-10-28 23:35:23 +08:00
|
|
|
|
using Bootstrap.Security.DataAccess;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using Longbow.Data;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Data.Common;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using System.Data.SqlClient;
|
2018-10-19 23:09:52 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Menu : BootstrapMenu
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除菜单信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public virtual bool DeleteMenu(IEnumerable<string> value)
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
var ids = string.Join(",", value);
|
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteMenus"))
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids));
|
|
|
|
|
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1;
|
|
|
|
|
}
|
|
|
|
|
CacheCleanUtility.ClearCache(menuIds: value);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存新建/更新的菜单信息
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-21 10:02:59 +08:00
|
|
|
|
public virtual bool SaveMenu(BootstrapMenu p)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(p.Name)) return false;
|
|
|
|
|
bool ret = false;
|
|
|
|
|
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);
|
2018-10-30 13:07:29 +08:00
|
|
|
|
string sql = string.IsNullOrEmpty(p.Id) ?
|
2018-10-21 10:02:59 +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";
|
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ID", p.Id));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ParentId", p.ParentId));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Name", p.Name));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Order", p.Order));
|
2018-10-21 21:08:35 +08:00
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Icon", DbAdapterManager.ToDBValue(p.Icon)));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Url", DbAdapterManager.ToDBValue(p.Url)));
|
2018-10-21 10:02:59 +08:00
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Category", p.Category));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Target", p.Target));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@IsResource", p.IsResource));
|
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ApplicationCode", p.ApplicationCode));
|
|
|
|
|
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(menuIds: string.IsNullOrEmpty(p.Id) ? new List<string>() : new List<string>() { p.Id });
|
2018-10-21 10:02:59 +08:00
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 查询某个角色所配置的菜单
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="roleId"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public virtual IEnumerable<BootstrapMenu> RetrieveMenusByRoleId(string roleId)
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
var menus = new List<BootstrapMenu>();
|
|
|
|
|
string sql = "select NavigationID from NavigationRole where RoleID = @RoleID";
|
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", roleId));
|
|
|
|
|
using (DbDataReader reader = DbAccessManager.DBAccess.ExecuteReader(cmd))
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
while (reader.Read())
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-28 15:03:59 +08:00
|
|
|
|
menus.Add(new BootstrapMenu()
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
2018-10-30 13:07:29 +08:00
|
|
|
|
Id = reader[0].ToString()
|
2018-10-28 15:03:59 +08:00
|
|
|
|
});
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-28 15:03:59 +08:00
|
|
|
|
}
|
|
|
|
|
return menus;
|
2018-10-21 10:02:59 +08:00
|
|
|
|
}
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过角色ID保存当前授权菜单
|
|
|
|
|
/// </summary>
|
2018-10-23 14:28:25 +08:00
|
|
|
|
/// <param name="roleId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="menuIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public virtual bool SaveMenusByRoleId(string roleId, IEnumerable<string> menuIds)
|
2018-10-21 10:02:59 +08:00
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
DataTable dt = new DataTable();
|
|
|
|
|
dt.Columns.Add("RoleID", typeof(int));
|
|
|
|
|
dt.Columns.Add("NavigationID", typeof(int));
|
2018-10-23 14:28:25 +08:00
|
|
|
|
menuIds.ToList().ForEach(menuId => dt.Rows.Add(roleId, menuId));
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//删除菜单角色表该角色所有的菜单
|
2018-10-23 14:28:25 +08:00
|
|
|
|
string sql = $"delete from NavigationRole where RoleID = {roleId}";
|
2018-10-21 10:02:59 +08:00
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
|
|
|
|
{
|
|
|
|
|
DbAccessManager.DBAccess.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();
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-30 13:07:29 +08:00
|
|
|
|
CacheCleanUtility.ClearCache(menuIds: menuIds, roleIds: new List<string>() { roleId });
|
2018-10-21 10:02:59 +08:00
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.RollbackTransaction();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
/// 通过当前用户名获得所有菜单
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="userName">当前登陆的用户名</param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-28 23:35:23 +08:00
|
|
|
|
public virtual IEnumerable<BootstrapMenu> RetrieveAllMenus(string userName) => DbHelper.RetrieveAllMenus(userName);
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|