修改BUG:完善SQLite组件下User授权方法,移除对存储过程和SqlBulkCopy的依赖

This commit is contained in:
Argo-MacBookPro 2018-10-23 15:37:42 +08:00
parent b1d7e5bf59
commit c950fd744d
3 changed files with 113 additions and 61 deletions

View File

@ -6,7 +6,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Data.Common; using System.Data.Common;
using System.Data.SqlClient;
using System.Linq; using System.Linq;
@ -26,11 +25,30 @@ namespace Bootstrap.DataAccess.SQLite
{ {
bool ret = false; bool ret = false;
var ids = string.Join(",", value); var ids = string.Join(",", value);
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers")) using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@ids", ids)); try
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; {
if (ret) CacheCleanUtility.ClearCache(userIds: value); using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"Delete from UserRole where UserID in ({ids})"))
{
DbAccessManager.DBAccess.ExecuteNonQuery(cmd);
cmd.CommandText = $"delete from UserGroup where UserID in ({ids})";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
cmd.CommandText = $"delete from Users where ID in ({ids})";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(userIds: value);
ret = true;
}
}
catch (Exception ex)
{
transaction.RollbackTransaction();
throw ex;
}
} }
return ret; return ret;
} }
@ -46,16 +64,40 @@ namespace Bootstrap.DataAccess.SQLite
if (p.UserName.Length > 50) p.UserName = p.UserName.Substring(0, 50); if (p.UserName.Length > 50) p.UserName = p.UserName.Substring(0, 50);
p.PassSalt = LgbCryptography.GenerateSalt(); p.PassSalt = LgbCryptography.GenerateSalt();
p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt);
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers"))
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", p.UserName)); try
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@password", p.Password)); {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@passSalt", p.PassSalt)); using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, "select UserName from Users Where UserName = @userName"))
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@displayName", p.DisplayName)); {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy))); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@userName", p.UserName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description)); var un = DbAccessManager.DBAccess.ExecuteScalar(cmd, transaction);
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; if (DbAdapterManager.ToObjectValue(un) == null)
if (ret) CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id }); {
cmd.CommandText = "Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, datetime('now', 'localtime'), @approvedBy, datetime('now', 'localtime'), @description)";
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@password", p.Password));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@passSalt", p.PassSalt));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@displayName", p.DisplayName));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@approvedBy", DbAdapterManager.ToDBValue(p.ApprovedBy)));
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@description", p.Description));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
cmd.CommandText = $"insert into UserRole (UserID, RoleID) select ID, (select ID from Roles where RoleName = 'Default') RoleId from Users where UserName = '{p.UserName}'";
cmd.Parameters.Clear();
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? new List<int>() : new List<int>() { p.Id });
ret = true;
}
}
}
catch (Exception ex)
{
transaction.RollbackTransaction();
throw ex;
}
} }
return ret; return ret;
} }
@ -88,50 +130,67 @@ namespace Bootstrap.DataAccess.SQLite
public override bool RejectUser(int id, string rejectBy) public override bool RejectUser(int id, string rejectBy)
{ {
var ret = false; var ret = false;
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_RejectUsers")) using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@id", id)); try
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@rejectedBy", rejectBy)); {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@rejectedReason", "未填写")); using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, $"insert into RejectUsers (UserName, DisplayName, RegisterTime, RejectedBy, RejectedTime, RejectedReason) select UserName, DisplayName, Registertime, '{rejectBy}', datetime('now', 'localtime'), '未填写' from Users where ID = {id}"))
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == -1; {
if (ret) CacheCleanUtility.ClearCache(userIds: new List<int>() { id }); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
cmd.CommandText = $"delete from UserRole where UserId = {id}";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
cmd.CommandText = $"delete from UserGroup where UserId = {id}";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
cmd.CommandText = $"delete from users where ID = {id}";
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
transaction.CommitTransaction();
CacheCleanUtility.ClearCache(userIds: new List<int>() { id });
ret = true;
}
}
catch (Exception ex)
{
transaction.RollbackTransaction();
throw ex;
}
} }
return ret; return ret;
} }
/// <summary> /// <summary>
/// 通过角色ID保存当前授权用户插入 /// 通过角色ID保存当前授权用户插入
/// </summary> /// </summary>
/// <param name="id">角色ID</param> /// <param name="roleId">角色ID</param>
/// <param name="userIds">用户ID数组</param> /// <param name="userIds">用户ID数组</param>
/// <returns></returns> /// <returns></returns>
public override bool SaveUsersByRoleId(int id, IEnumerable<int> userIds) public override bool SaveUsersByRoleId(int roleId, IEnumerable<int> userIds)
{ {
bool ret = false; bool ret = false;
DataTable dt = new DataTable(); DataTable dt = new DataTable();
dt.Columns.Add("RoleID", typeof(int)); dt.Columns.Add("RoleID", typeof(int));
dt.Columns.Add("UserID", typeof(int)); dt.Columns.Add("UserID", typeof(int));
userIds.ToList().ForEach(userId => dt.Rows.Add(id, userId)); userIds.ToList().ForEach(userId => dt.Rows.Add(roleId, userId));
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
try try
{ {
//删除用户角色表该角色所有的用户 //删除用户角色表该角色所有的用户
string sql = "delete from UserRole where RoleID=@RoleID"; string sql = $"delete from UserRole where RoleID = {roleId}";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", id));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
//批插入用户角色表 //批插入用户角色表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) userIds.ToList().ForEach(uId =>
{ {
bulk.DestinationTableName = "UserRole"; cmd.CommandText = $"Insert Into UserRole (UserID, RoleID) Values ( {uId}, {roleId})";
bulk.ColumnMappings.Add("RoleID", "RoleID"); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
bulk.ColumnMappings.Add("UserID", "UserID"); });
bulk.WriteToServer(dt); transaction.CommitTransaction();
transaction.CommitTransaction();
}
} }
CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<int>() { id }); CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<int>() { roleId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -145,37 +204,30 @@ namespace Bootstrap.DataAccess.SQLite
/// <summary> /// <summary>
/// 通过部门ID保存当前授权用户插入 /// 通过部门ID保存当前授权用户插入
/// </summary> /// </summary>
/// <param name="id">GroupID</param> /// <param name="groupId">GroupID</param>
/// <param name="userIds">用户ID数组</param> /// <param name="userIds">用户ID数组</param>
/// <returns></returns> /// <returns></returns>
public override bool SaveUsersByGroupId(int id, IEnumerable<int> userIds) public override bool SaveUsersByGroupId(int groupId, IEnumerable<int> userIds)
{ {
bool ret = false; bool ret = false;
DataTable dt = new DataTable();
dt.Columns.Add("UserID", typeof(int));
dt.Columns.Add("GroupID", typeof(int));
userIds.ToList().ForEach(userId => dt.Rows.Add(userId, id));
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
try try
{ {
//删除用户角色表该角色所有的用户 //删除用户角色表该角色所有的用户
string sql = "delete from UserGroup where GroupID = @GroupID"; string sql = $"delete from UserGroup where GroupID = {groupId}";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", id));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
//批插入用户角色表 //批插入用户角色表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) userIds.ToList().ForEach(uId =>
{ {
bulk.DestinationTableName = "UserGroup"; cmd.CommandText = $"Insert Into UserGroup (UserID, GroupID) Values ( {uId}, {groupId})";
bulk.ColumnMappings.Add("UserID", "UserID"); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
bulk.ColumnMappings.Add("GroupID", "GroupID"); });
bulk.WriteToServer(dt); transaction.CommitTransaction();
transaction.CommitTransaction();
}
} }
CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<int>() { id }); CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<int>() { groupId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -314,25 +314,24 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// 通过角色ID保存当前授权用户插入 /// 通过角色ID保存当前授权用户插入
/// </summary> /// </summary>
/// <param name="id">角色ID</param> /// <param name="roleId">角色ID</param>
/// <param name="userIds">用户ID数组</param> /// <param name="userIds">用户ID数组</param>
/// <returns></returns> /// <returns></returns>
public virtual bool SaveUsersByRoleId(int id, IEnumerable<int> userIds) public virtual bool SaveUsersByRoleId(int roleId, IEnumerable<int> userIds)
{ {
bool ret = false; bool ret = false;
DataTable dt = new DataTable(); DataTable dt = new DataTable();
dt.Columns.Add("RoleID", typeof(int)); dt.Columns.Add("RoleID", typeof(int));
dt.Columns.Add("UserID", typeof(int)); dt.Columns.Add("UserID", typeof(int));
userIds.ToList().ForEach(userId => dt.Rows.Add(id, userId)); userIds.ToList().ForEach(userId => dt.Rows.Add(roleId, userId));
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
try try
{ {
//删除用户角色表该角色所有的用户 //删除用户角色表该角色所有的用户
string sql = "delete from UserRole where RoleID=@RoleID"; string sql = $"delete from UserRole where RoleID = {roleId}";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RoleID", id));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
//批插入用户角色表 //批插入用户角色表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
@ -344,7 +343,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
} }
CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<int>() { id }); CacheCleanUtility.ClearCache(userIds: userIds, roleIds: new List<int>() { roleId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)
@ -388,16 +387,16 @@ namespace Bootstrap.DataAccess
/// <summary> /// <summary>
/// 通过部门ID保存当前授权用户插入 /// 通过部门ID保存当前授权用户插入
/// </summary> /// </summary>
/// <param name="id">GroupID</param> /// <param name="groupId">GroupID</param>
/// <param name="userIds">用户ID数组</param> /// <param name="userIds">用户ID数组</param>
/// <returns></returns> /// <returns></returns>
public virtual bool SaveUsersByGroupId(int id, IEnumerable<int> userIds) public virtual bool SaveUsersByGroupId(int groupId, IEnumerable<int> userIds)
{ {
bool ret = false; bool ret = false;
DataTable dt = new DataTable(); DataTable dt = new DataTable();
dt.Columns.Add("UserID", typeof(int)); dt.Columns.Add("UserID", typeof(int));
dt.Columns.Add("GroupID", typeof(int)); dt.Columns.Add("GroupID", typeof(int));
userIds.ToList().ForEach(userId => dt.Rows.Add(userId, id)); userIds.ToList().ForEach(userId => dt.Rows.Add(userId, groupId));
using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction()) using (TransactionPackage transaction = DbAccessManager.DBAccess.BeginTransaction())
{ {
try try
@ -406,7 +405,7 @@ namespace Bootstrap.DataAccess
string sql = "delete from UserGroup where GroupID = @GroupID"; string sql = "delete from UserGroup where GroupID = @GroupID";
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql)) using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{ {
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", id)); cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@GroupID", groupId));
DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction); DbAccessManager.DBAccess.ExecuteNonQuery(cmd, transaction);
//批插入用户角色表 //批插入用户角色表
using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction))
@ -418,7 +417,7 @@ namespace Bootstrap.DataAccess
transaction.CommitTransaction(); transaction.CommitTransaction();
} }
} }
CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<int>() { id }); CacheCleanUtility.ClearCache(userIds: userIds, groupIds: new List<int>() { groupId });
ret = true; ret = true;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -147,7 +147,7 @@ BEGIN
if(not exists (select 1 from Users Where UserName = @userName)) if(not exists (select 1 from Users Where UserName = @userName))
begin begin
begin tran begin tran
Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, GETDATE(), @approvedBy, @approvedTime, @description) Insert Into Users (UserName, [Password], PassSalt, DisplayName, RegisterTime, ApprovedBy, ApprovedTime, [Description]) values (@userName, @password, @passSalt, @displayName, GETDATE(), @approvedBy, GETDATE(), @description)
insert into UserRole (UserID, RoleID) select @@IDENTITY, ID from Roles where RoleName = N'Default' insert into UserRole (UserID, RoleID) select @@IDENTITY, ID from Roles where RoleName = N'Default'
commit tran commit tran
end end
@ -186,6 +186,7 @@ BEGIN
begin tran begin tran
insert into RejectUsers (UserName, DisplayName, RegisterTime, RejectedBy, RejectedTime, RejectedReason) values (@userName, @displayName, @registerTime, @rejectedBy, GETDATE(), @rejectedReason) insert into RejectUsers (UserName, DisplayName, RegisterTime, RejectedBy, RejectedTime, RejectedReason) values (@userName, @displayName, @registerTime, @rejectedBy, GETDATE(), @rejectedReason)
delete from UserRole where UserId = @id delete from UserRole where UserId = @id
delete from UserGroup where UserId = @id
delete from users where ID = @id delete from users where ID = @id
commit tran commit tran
end end