2018-10-21 10:27:43 +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;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace Bootstrap.DataAccess.SQLite
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class Role : DataAccess.Role
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 保存用户角色关系
|
|
|
|
|
/// </summary>
|
2018-10-23 14:41:23 +08:00
|
|
|
|
/// <param name="userId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public override bool SaveRolesByUserId(string userId, IEnumerable<string> roleIds)
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
var ret = false;
|
|
|
|
|
//判断用户是否选定角色
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// delete user from config table
|
2018-10-23 14:41:23 +08:00
|
|
|
|
string sql = $"delete from UserRole where UserID = {userId}";
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-20 22:25:53 +08:00
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
2018-10-23 14:41:23 +08:00
|
|
|
|
roleIds.ToList().ForEach(rId =>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-23 14:41:23 +08:00
|
|
|
|
cmd.CommandText = $"Insert Into UserRole (UserID, RoleID) Values ( {userId}, {rId})";
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
});
|
2018-10-19 23:09:52 +08:00
|
|
|
|
transaction.CommitTransaction();
|
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.RollbackTransaction();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除角色表
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="value"></param>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public override bool DeleteRole(IEnumerable<string> value)
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
bool ret = false;
|
|
|
|
|
var ids = string.Join(",", value);
|
2018-10-23 14:41:23 +08:00
|
|
|
|
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-23 14:41:23 +08:00
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"delete from UserRole where RoleID in ({ids})"))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
|
|
|
|
|
cmd.CommandText = $"delete from RoleGroup where RoleID in ({ids})";
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
|
|
|
|
|
cmd.CommandText = $"delete from NavigationRole where RoleID in ({ids})";
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
|
|
|
|
|
cmd.CommandText = $"delete from Roles where ID in ({ids})";
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
|
|
|
|
|
transaction.CommitTransaction();
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.RollbackTransaction();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2018-10-23 14:41:23 +08:00
|
|
|
|
/// <param name="menuId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public override bool SavaRolesByMenuId(string menuId, IEnumerable<string> roleIds)
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
var ret = false;
|
|
|
|
|
//判断用户是否选定角色
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// delete role from config table
|
2018-10-23 14:41:23 +08:00
|
|
|
|
string sql = $"delete from NavigationRole where NavigationID = {menuId}";
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-20 22:25:53 +08:00
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
2018-10-19 23:09:52 +08:00
|
|
|
|
// insert batch data into config table
|
2018-10-23 14:41:23 +08:00
|
|
|
|
roleIds.ToList().ForEach(rId =>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-23 14:41:23 +08:00
|
|
|
|
cmd.CommandText = $"Insert Into NavigationRole (NavigationID, RoleID) Values ( {menuId}, {rId})";
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
});
|
|
|
|
|
transaction.CommitTransaction();
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.RollbackTransaction();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
2018-10-31 11:15:43 +08:00
|
|
|
|
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息
|
|
|
|
|
/// </summary>
|
2018-10-23 14:41:23 +08:00
|
|
|
|
/// <param name="groupId"></param>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
/// <param name="roleIds"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-10-30 13:07:29 +08:00
|
|
|
|
public override bool SaveRolesByGroupId(string groupId, IEnumerable<string> roleIds)
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
var ret = false;
|
|
|
|
|
//构造表格
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// delete user from config table
|
2018-10-23 14:41:23 +08:00
|
|
|
|
string sql = $"delete from RoleGroup where GroupID = {groupId}";
|
2018-10-20 22:25:53 +08:00
|
|
|
|
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-20 22:25:53 +08:00
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
2018-10-19 23:09:52 +08:00
|
|
|
|
|
|
|
|
|
// insert batch data into config table
|
2018-10-23 14:41:23 +08:00
|
|
|
|
roleIds.ToList().ForEach(rId =>
|
2018-10-19 23:09:52 +08:00
|
|
|
|
{
|
2018-10-23 14:41:23 +08:00
|
|
|
|
cmd.CommandText = $"Insert Into RoleGroup (GroupID, RoleID) Values ( {groupId}, {rId})";
|
|
|
|
|
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
|
|
|
|
|
});
|
|
|
|
|
transaction.CommitTransaction();
|
2018-10-19 23:09:52 +08:00
|
|
|
|
}
|
|
|
|
|
ret = true;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
transaction.RollbackTransaction();
|
|
|
|
|
throw ex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-25 18:47:33 +08:00
|
|
|
|
}
|