diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 4091d0af..ca963fba 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -23,7 +23,11 @@ + + + + diff --git a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj index 883ff9d6..37529f81 100644 --- a/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj +++ b/Bootstrap.DataAccess/Bootstrap.DataAccess.csproj @@ -63,6 +63,8 @@ + + diff --git a/Bootstrap.DataAccess/User.cs b/Bootstrap.DataAccess/User.cs new file mode 100644 index 00000000..1d036039 --- /dev/null +++ b/Bootstrap.DataAccess/User.cs @@ -0,0 +1,22 @@ +namespace Bootstrap.DataAccess +{ + public class User + { + /// + /// 获得/设置 用户主键ID + /// + public int ID { get; set; } + /// + /// 获得/设置 用户名称 + /// + public string UserName { get; set; } + /// + /// 获取/设置 密码 + /// + public string Password { get; set; } + /// + /// 获取/设置 验证信息 + /// + public string PassSalt { get; set; } + } +} diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs new file mode 100644 index 00000000..0807a949 --- /dev/null +++ b/Bootstrap.DataAccess/UserHelper.cs @@ -0,0 +1,108 @@ +using Longbow.Caching; +using Longbow.Caching.Configuration; +using Longbow.ExceptionManagement; +using System; +using System.Collections.Generic; +using System.Data; +using System.Data.Common; +using System.Globalization; +using System.Linq; + +namespace Bootstrap.DataAccess +{ + public static class UserHelper + { + private const string UserDataKey = "UserData-CodeUserHelper"; + + /// + /// 查询所有用户 + /// + /// + /// + public static IEnumerable RetrieveUsers(string tId = null) + { + string sql = "select * from Users"; + var ret = CacheManager.GetOrAdd(UserDataKey, CacheSection.RetrieveIntervalByKey(UserDataKey), key => + { + List Users = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + Users.Add(new User() + { + ID = (int)reader[0], + UserName = (string)reader[1], + Password = (string)reader[2], + PassSalt = (string)reader[3], + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return Users; + }, CacheSection.RetrieveDescByKey(UserDataKey)); + return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); + } + + /// + /// 删除用户 + /// + /// + public static void DeleteUser(string ids) + { + if (string.IsNullOrEmpty(ids) || ids.Contains("'")) return; + string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Users where ID in ({0})", ids); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + { + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + ClearCache(); + } + } + + /// + /// 保存新建/更新的用户信息 + /// + /// + /// + public static bool SaveUser(User p) + { + if (p == null) throw new ArgumentNullException("p"); + bool ret = false; + if (p.UserName.Length > 50) p.UserName.Substring(0, 50); + if (p.Password.Length > 50) p.Password.Substring(0, 50); + if (p.PassSalt.Length > 50) p.PassSalt.Substring(0, 50); + string sql = p.ID == 0 ? + "Insert Into Users (UserName, Password, PassSalt) Values (@UserName, @Password, @PassSalt)" : + "Update Users set UserName = @UserName, Password = @Password, PassSalt = @PassSalt where ID = @ID"; + 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("@UserName", p.UserName, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Password", p.Password, ParameterDirection.Input)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@PassSalt", p.PassSalt, ParameterDirection.Input)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + } + ret = true; + ClearCache(); + } + catch (DbException ex) + { + ExceptionManager.Publish(ex); + } + return ret; + } + + + // 更新缓存 + private static void ClearCache() + { + CacheManager.Clear(key => key.Contains("TerminalData-")); + } + } +} diff --git a/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj b/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj index 063f696c..aaa90f16 100644 --- a/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj +++ b/Bootstrap.DataAccessTests/Bootstrap.DataAccessTests.csproj @@ -54,7 +54,11 @@ + + + + diff --git a/Bootstrap.DataAccessTests/UserHelperTests.cs b/Bootstrap.DataAccessTests/UserHelperTests.cs new file mode 100644 index 00000000..1fd8760a --- /dev/null +++ b/Bootstrap.DataAccessTests/UserHelperTests.cs @@ -0,0 +1,55 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System.Linq; + +namespace Bootstrap.DataAccess.Tests +{ + [TestClass] + public class UserHelperTests + { + [TestMethod] + public void RetrieveUsersTest() + { + var result = UserHelper.RetrieveUsers("1"); + Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的UserHelper.RetrieveUsers方法调用失败,请检查数据库连接或者数据库SQL语句"); + result = UserHelper.RetrieveUsers(); + Assert.IsTrue(result.Count() >= 0, "不带参数的UserHelper.RetrieveUsers方法调用失败,请检查数据库连接或者数据库SQL语句"); + } + + [TestMethod] + public void DeleteUserTest() + { + string ids = "2"; + try + { + UserHelper.DeleteUser(ids); + Assert.IsTrue(true); + } + catch + { + Assert.IsTrue(false,"带有参数的UserHelper.DeleteUserTest方法调用失败,请检查数据库连接或者数据库SQL语句"); + } + + } + + [TestMethod] + public void SaveUserTest() + { + User users = new User(); + users.ID = 0; + users.UserName = "liqi"; + users.Password = "123"; + users.PassSalt = "123"; + var result = UserHelper.SaveUser(users); + Assert.IsTrue(result == true, "带有参数的UserHelper.SaveUser方法中新建用户信息失败,请检查数据库连接或者数据库SQL语句"); + + User users1 = new User(); + users1.ID = 1; + users1.UserName = "Lily"; + users1.Password = "123456"; + users1.PassSalt = "123456"; + result = UserHelper.SaveUser(users1); + Assert.IsTrue(result == true, "带有参数的UserHelper.SaveUser方法中更新用户信息失败,请检查数据库连接或者数据库SQL语句"); + } + + } +} diff --git a/BootstrapAdmin.sln b/BootstrapAdmin.sln index ca1616e3..7903d1af 100644 --- a/BootstrapAdmin.sln +++ b/BootstrapAdmin.sln @@ -7,6 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Bootstrap.DataAccess", "Boo EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sql", "sql", "{87319AF5-7C40-4362-B67C-35F9DD737DB4}" ProjectSection(SolutionItems) = preProject + DatabaseScripts\ExtendedChecker.sql = DatabaseScripts\ExtendedChecker.sql DatabaseScripts\Install.sql = DatabaseScripts\Install.sql DatabaseScripts\Proc_StartTerminal.sql = DatabaseScripts\Proc_StartTerminal.sql DatabaseScripts\Proc_StopTerminal.sql = DatabaseScripts\Proc_StopTerminal.sql diff --git a/DatabaseScripts/ExtendedChecker.sql b/DatabaseScripts/ExtendedChecker.sql new file mode 100644 index 00000000..a108aca5 Binary files /dev/null and b/DatabaseScripts/ExtendedChecker.sql differ