From e6849053eaeeeba27091c8391aa0b6a6ae134c29 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Tue, 12 Jun 2018 11:17:21 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8A=9F=E8=83=BD=EF=BC=9A?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E6=97=A5=E5=BF=97=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Startup.cs | 9 +- Bootstrap.DataAccess/ExceptionHelper.cs | 5 +- Bootstrap.DataAccess/UserHelper.cs | 195 +++++++++--------------- 3 files changed, 83 insertions(+), 126 deletions(-) diff --git a/Bootstrap.Admin/Startup.cs b/Bootstrap.Admin/Startup.cs index 5bf8620e..767c2ddf 100644 --- a/Bootstrap.Admin/Startup.cs +++ b/Bootstrap.Admin/Startup.cs @@ -1,10 +1,12 @@ -using Bootstrap.Security.Filter; +using Bootstrap.DataAccess; +using Bootstrap.Security.Filter; using Bootstrap.Security.Middleware; using Longbow.Cache; using Longbow.Cache.Middleware; using Longbow.Configuration; using Longbow.Data; using Longbow.Logging; +using Longbow.Web; using Longbow.Web.WebSockets; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Builder; @@ -15,6 +17,7 @@ using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using Newtonsoft.Json.Serialization; using System; +using System.Collections.Specialized; using System.IO; namespace Bootstrap.Admin @@ -32,7 +35,7 @@ namespace Bootstrap.Admin public void ConfigureServices(IServiceCollection services) { services.AddCors(); - services.AddLogging(builder => builder.AddFileLogger()); + services.AddLogging(builder => builder.AddFileLogger().AddDBLogger(ExceptionHelper.Log)); services.AddConfigurationManager(); services.AddCacheManager(); services.AddDBAccessFactory(); @@ -43,7 +46,7 @@ namespace Bootstrap.Admin services.AddMvc(options => { options.Filters.Add(); - options.Filters.Add(); + options.Filters.Add(); }).AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); diff --git a/Bootstrap.DataAccess/ExceptionHelper.cs b/Bootstrap.DataAccess/ExceptionHelper.cs index efe904bb..986aa77d 100644 --- a/Bootstrap.DataAccess/ExceptionHelper.cs +++ b/Bootstrap.DataAccess/ExceptionHelper.cs @@ -24,9 +24,8 @@ namespace Bootstrap.DataAccess /// /// /// - public static bool Log(Exception ex, NameValueCollection additionalInfo) + public static void Log(Exception ex, NameValueCollection additionalInfo) { - bool ret = false; try { var sql = "insert into Exceptions (AppDomainName, ErrorPage, UserID, UserIp, ExceptionType, Message, StackTrace, LogTime) values (@AppDomainName, @ErrorPage, @UserID, @UserIp, @ExceptionType, @Message, @StackTrace, GetDate())"; @@ -41,13 +40,11 @@ namespace Bootstrap.DataAccess cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@StackTrace", DBAccessFactory.ToDBValue(ex.StackTrace))); DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); } - ret = true; } catch (Exception e) { throw new Exception("Excetion Log Error", e); } - return ret; } /// /// 查询所有异常 diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs index 35b5792e..d11cee38 100644 --- a/Bootstrap.DataAccess/UserHelper.cs +++ b/Bootstrap.DataAccess/UserHelper.cs @@ -33,26 +33,23 @@ namespace Bootstrap.DataAccess { List users = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, "select ID, UserName, DisplayName, RegisterTime, ApprovedTime, ApprovedBy, Description from Users Where ApprovedTime is not null"); - try + + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + users.Add(new User() { - users.Add(new User() - { - Id = (int)reader[0], - UserName = (string)reader[1], - DisplayName = (string)reader[2], - RegisterTime = (DateTime)reader[3], - ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue), - ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5], - Description = (string)reader[6] - }); - } + Id = (int)reader[0], + UserName = (string)reader[1], + DisplayName = (string)reader[2], + RegisterTime = (DateTime)reader[3], + ApprovedTime = LgbConvert.ReadValue(reader[4], DateTime.MinValue), + ApprovedBy = reader.IsDBNull(5) ? string.Empty : (string)reader[5], + Description = (string)reader[6] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return users; }); } @@ -67,24 +64,20 @@ namespace Bootstrap.DataAccess string sql = "select ID, UserName, DisplayName, RegisterTime, [Description] from Users Where ApprovedTime is null and RejectedTime is null order by RegisterTime desc"; List users = new List(); DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + users.Add(new User() { - users.Add(new User() - { - Id = (int)reader[0], - UserName = (string)reader[1], - DisplayName = (string)reader[2], - RegisterTime = (DateTime)reader[3], - Description = (string)reader[4] - }); - } + Id = (int)reader[0], + UserName = (string)reader[1], + DisplayName = (string)reader[2], + RegisterTime = (DateTime)reader[3], + Description = (string)reader[4] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return users; }); } @@ -95,21 +88,14 @@ namespace Bootstrap.DataAccess public static bool DeleteUser(IEnumerable value) { bool ret = false; - try + var ids = string.Join(",", value); + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers")) { - var ids = string.Join(",", value); - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_DeleteUsers")) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(userIds: ids); - ret = true; - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ids", ids)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); } + CacheCleanUtility.ClearCache(userIds: ids); + ret = true; return ret; } /// @@ -127,30 +113,23 @@ namespace Bootstrap.DataAccess p.Password = LgbCryptography.ComputeHash(p.Password, p.PassSalt); } bool ret = false; - try + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers")) { - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.StoredProcedure, "Proc_SaveUsers")) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", p.Id)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", DBAccessFactory.ToDBValue(p.UserName))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@password", DBAccessFactory.ToDBValue(p.Password))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@passSalt", DBAccessFactory.ToDBValue(p.PassSalt))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@displayName", DBAccessFactory.ToDBValue(p.DisplayName))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", DBAccessFactory.ToDBValue(p.Description))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", DBAccessFactory.ToDBValue(p.ApprovedBy))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", DBAccessFactory.ToDBValue(p.RejectedBy))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", DBAccessFactory.ToDBValue(p.RejectedReason))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userStatus", p.UserStatus)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - } - CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? string.Empty : p.Id.ToString()); - ret = true; - if (p.UserStatus == 1) NotificationHelper.MessagePool.Add(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) }); - } - catch (DbException ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@id", p.Id)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", DBAccessFactory.ToDBValue(p.UserName))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@password", DBAccessFactory.ToDBValue(p.Password))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@passSalt", DBAccessFactory.ToDBValue(p.PassSalt))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@displayName", DBAccessFactory.ToDBValue(p.DisplayName))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@description", DBAccessFactory.ToDBValue(p.Description))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@approvedBy", DBAccessFactory.ToDBValue(p.ApprovedBy))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedBy", DBAccessFactory.ToDBValue(p.RejectedBy))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@rejectedReason", DBAccessFactory.ToDBValue(p.RejectedReason))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userStatus", p.UserStatus)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); } + CacheCleanUtility.ClearCache(userIds: p.Id == 0 ? string.Empty : p.Id.ToString()); + ret = true; + if (p.UserStatus == 1) NotificationHelper.MessagePool.Add(new MessageBody() { Category = "Users", Message = string.Format("{0}-{1}", p.UserName, p.Description) }); return ret; } /// @@ -167,23 +146,19 @@ namespace Bootstrap.DataAccess string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID = ur.UserID and RoleID = @RoleID where u.ApprovedTime is not null"; DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId)); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + users.Add(new User() { - users.Add(new User() - { - Id = (int)reader[0], - UserName = (string)reader[1], - DisplayName = (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + UserName = (string)reader[1], + DisplayName = (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return users; }, RetrieveUsersByRoleIdDataKey); } @@ -225,8 +200,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; @@ -245,23 +220,19 @@ namespace Bootstrap.DataAccess string sql = "select u.ID, u.UserName, u.DisplayName, case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserGroup ur on u.ID = ur.UserID and GroupID =@groupId where u.ApprovedTime is not null"; DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupId)); - try + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { - using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + while (reader.Read()) { - while (reader.Read()) + users.Add(new User() { - users.Add(new User() - { - Id = (int)reader[0], - UserName = (string)reader[1], - DisplayName = (string)reader[2], - Checked = (string)reader[3] - }); - } + Id = (int)reader[0], + UserName = (string)reader[1], + DisplayName = (string)reader[2], + Checked = (string)reader[3] + }); } } - catch (Exception ex) { ExceptionManager.Publish(ex); } return users; }, RetrieveUsersByRoleIdDataKey); } @@ -303,8 +274,8 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { - ExceptionManager.Publish(ex); transaction.RollbackTransaction(); + throw ex; } } return ret; @@ -318,22 +289,15 @@ namespace Bootstrap.DataAccess public static bool SaveUserIconByName(string userName, string iconName) { bool ret = false; - try + string sql = "Update Users set Icon = @iconName where UserName = @userName"; + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - string sql = "Update Users set Icon = @iconName where UserName = @userName"; - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName)); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName); - CacheCleanUtility.ClearCache(cacheKey: key); - ret = true; - } - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@iconName", iconName)); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName); + CacheCleanUtility.ClearCache(cacheKey: key); + ret = true; } return ret; } @@ -346,22 +310,15 @@ namespace Bootstrap.DataAccess public static bool SaveUserCssByName(string userName, string cssName) { bool ret = false; - try + string sql = "Update Users set Css = @cssName where UserName = @userName"; + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - string sql = "Update Users set Css = @cssName where UserName = @userName"; - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) - { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cssName", DBAccessFactory.ToDBValue(cssName))); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); - string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName); - CacheCleanUtility.ClearCache(cacheKey: key); - ret = true; - } - } - catch (Exception ex) - { - ExceptionManager.Publish(ex); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@cssName", DBAccessFactory.ToDBValue(cssName))); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@userName", userName)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd); + string key = string.Format("{0}-{1}", RetrieveUsersByNameDataKey, userName); + CacheCleanUtility.ClearCache(cacheKey: key); + ret = true; } return ret; }