From 4ea469a1fa8e33614ff4a5adaab7777bc1441271 Mon Sep 17 00:00:00 2001 From: falcon <9504402@qq.com> Date: Wed, 6 May 2020 15:57:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=84=E8=8C=83api=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=EF=BC=8C=E7=BB=9F=E4=B8=80=E6=94=B9=E7=94=A8?= =?UTF-8?q?400=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- FAuth/Controllers/api/ApiControllerBase.cs | 2 + FAuth/Controllers/api/AppController.cs | 11 ++-- .../api/App_RoleGroupController.cs | 9 ++-- FAuth/Controllers/api/RoleController.cs | 9 ++-- FAuth/Controllers/api/RoleGroupController.cs | 7 +-- .../api/RoleGroup_RoleController.cs | 9 ++-- FAuth/Controllers/api/Role_UserController.cs | 9 ++-- FAuth/Controllers/api/UserController.cs | 53 +++++++++---------- FAuth/Extensions/ApiArgumentNullException.cs | 11 ++++ FAuth/Extensions/ApiException.cs | 16 ++++++ .../Extensions/ApiExceptionFilterAttribute.cs | 6 ++- 11 files changed, 89 insertions(+), 53 deletions(-) create mode 100644 FAuth/Extensions/ApiArgumentNullException.cs create mode 100644 FAuth/Extensions/ApiException.cs diff --git a/FAuth/Controllers/api/ApiControllerBase.cs b/FAuth/Controllers/api/ApiControllerBase.cs index f0700f9..e09673c 100644 --- a/FAuth/Controllers/api/ApiControllerBase.cs +++ b/FAuth/Controllers/api/ApiControllerBase.cs @@ -1,6 +1,7 @@ using System; using FAuth.Extensions; using FAuth.Models; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; @@ -12,6 +13,7 @@ namespace FAuth.Controllers.api [ApiController, Route("api/[Controller]/[Action]")] [ServiceFilter(typeof(ApiExceptionFilterAttribute))] [ProducesResponseType(typeof(ApiErrorResult),500)] + [ProducesResponseType(typeof(ApiErrorResult),400)] public abstract class ApiControllerBase:ControllerBase { public ApiControllerBase(ILogger logger,IServiceProvider service) : base(logger,service) { diff --git a/FAuth/Controllers/api/AppController.cs b/FAuth/Controllers/api/AppController.cs index 00c4817..4589448 100644 --- a/FAuth/Controllers/api/AppController.cs +++ b/FAuth/Controllers/api/AppController.cs @@ -8,6 +8,7 @@ using FAuth.DataBase; using System.Linq; using FAuth.DataBase.Tables; using Microsoft.EntityFrameworkCore; +using FAuth.Extensions; namespace FAuth.Controllers.api { @@ -29,11 +30,11 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(Apps),200)] public Apps AddNewApp(string appName,string description) { if(appName.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(appName)); + throw new ApiArgumentNullException(nameof(appName)); } var qu = this.Db.Apps.Where(m => m.Name == appName); if(qu.Any()) { - throw new Exception($"应用{appName}已经存在,不可以重复创建!"); + throw new ApiException($"应用{appName}已经存在,不可以重复创建!"); } var newApp = new Apps { Name = appName, @@ -47,7 +48,7 @@ namespace FAuth.Controllers.api [HttpPost] public bool RemoveApp(string appName) { if(appName.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(appName)); + throw new ApiArgumentNullException(nameof(appName)); } var qu = this.Db.Apps.Where(m => m.Name == appName); foreach(var item in qu) { @@ -66,12 +67,12 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(Apps),200)] public Apps GetApps(string appName) { if(string.IsNullOrEmpty(appName)) - throw new ArgumentException("message",nameof(appName)); + throw new ApiArgumentNullException(nameof(appName)); var qu = this.Db.Apps.Where(m => m.Name == appName); if(qu.Any()) { return qu.First(); } else { - throw new Exception("没有找到该应用"); + throw new ApiException("没有找到该应用"); } } diff --git a/FAuth/Controllers/api/App_RoleGroupController.cs b/FAuth/Controllers/api/App_RoleGroupController.cs index 38789bf..6cb24ef 100644 --- a/FAuth/Controllers/api/App_RoleGroupController.cs +++ b/FAuth/Controllers/api/App_RoleGroupController.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using FAuth.DataBase.Tables; +using FAuth.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -24,11 +25,11 @@ namespace FAuth.Controllers.api public bool Binding(int appId,int roleGroupId) { var aQu = this.Db.Apps.Where(m => m.Id == appId); if(!aQu.Any()) { - throw new Exception("没有找到指定的应用"); + throw new ApiException("没有找到指定的应用"); } var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId); if(!rQu.Any()) { - throw new Exception("没有找到指定的角色组"); + throw new ApiException("没有找到指定的角色组"); } var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId); if(!arQu.Any()) { @@ -51,11 +52,11 @@ namespace FAuth.Controllers.api public bool Unbind(int appId,int roleGroupId) { var aQu = this.Db.Apps.Where(m => m.Id == appId); if(!aQu.Any()) { - throw new Exception("没有找到指定的应用"); + throw new ApiException("没有找到指定的应用"); } var rQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId); if(!rQu.Any()) { - throw new Exception("没有找到指定的角色组"); + throw new ApiException("没有找到指定的角色组"); } var arQu = this.Db.App_RoleGroups.Where(m => m.AppId == appId && m.RoleGroupId == roleGroupId); if(arQu.Any()) { diff --git a/FAuth/Controllers/api/RoleController.cs b/FAuth/Controllers/api/RoleController.cs index b74e94c..e5d2111 100644 --- a/FAuth/Controllers/api/RoleController.cs +++ b/FAuth/Controllers/api/RoleController.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using Falcon.Extend; using FAuth.DataBase.Tables; +using FAuth.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -28,11 +29,11 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(Roles),200)] public Roles AddNewRole(string roleName,string description) { if(roleName.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(roleName)); + throw new ApiArgumentNullException(nameof(roleName)); } var qu = this.Db.Roles.Where(m => m.Name == roleName); if(qu.Any()) { - throw new Exception($"角色{roleName}已经存在,不可以重复创建!"); + throw new ApiException($"角色{roleName}已经存在,不可以重复创建!"); } var newRole = new Roles { Name = roleName, @@ -51,13 +52,11 @@ namespace FAuth.Controllers.api [HttpPost] [ProducesResponseType(typeof(Roles),200)] public Roles GetApps(string roleName) { - if(string.IsNullOrEmpty(roleName)) - throw new ArgumentException("message",nameof(roleName)); var qu = this.Db.Roles.Where(m => m.Name == roleName); if(qu.Any()) { return qu.First(); } else { - throw new Exception("没有找到该角色"); + throw new ApiException("没有找到该角色"); } } diff --git a/FAuth/Controllers/api/RoleGroupController.cs b/FAuth/Controllers/api/RoleGroupController.cs index 8abf9bb..e15703f 100644 --- a/FAuth/Controllers/api/RoleGroupController.cs +++ b/FAuth/Controllers/api/RoleGroupController.cs @@ -2,6 +2,7 @@ using System.Linq; using Falcon.Extend; using FAuth.DataBase.Tables; +using FAuth.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -26,11 +27,11 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(RoleGroup),200)] public RoleGroup AddNew(string name,string description) { if(name.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(name)); + throw new ApiArgumentNullException(nameof(name)); } var qu = this.Db.RoleGroups.Where(m => m.Name == name); if(qu.Any()) { - throw new Exception($"角色组{name}已经存在,不可以重复创建!"); + throw new ApiException($"角色组{name}已经存在,不可以重复创建!"); } var model = new RoleGroup { Name = name, @@ -50,7 +51,7 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(RoleGroup),200)] public RoleGroup GetApps(string name) { if(string.IsNullOrEmpty(name)) - throw new ArgumentException("message",nameof(name)); + throw new ApiArgumentNullException(nameof(name)); var qu = this.Db.RoleGroups.Where(m => m.Name == name); if(qu.Any()) { return qu.First(); diff --git a/FAuth/Controllers/api/RoleGroup_RoleController.cs b/FAuth/Controllers/api/RoleGroup_RoleController.cs index 3163520..5fdc013 100644 --- a/FAuth/Controllers/api/RoleGroup_RoleController.cs +++ b/FAuth/Controllers/api/RoleGroup_RoleController.cs @@ -1,6 +1,7 @@ using System; using System.Linq; using FAuth.DataBase.Tables; +using FAuth.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -25,11 +26,11 @@ namespace FAuth.Controllers.api public bool Binding(int roleGroupId,int roleId) { var rgQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId); if(!rgQu.Any()) { - throw new Exception("没有找到指定的角色组"); + throw new ApiException("没有找到指定的角色组"); } var rQu = this.Db.Roles.Where(m => m.Id == roleId); if(!rQu.Any()) { - throw new Exception("没有找到指定的角色"); + throw new ApiException("没有找到指定的角色"); } var rgr = this.Db.RoleGroup_Roles.Where(m => m.RoleGroupId == roleGroupId && m.RoleId == roleId); if(!rgr.Any()) { @@ -52,11 +53,11 @@ namespace FAuth.Controllers.api public bool Unbind(int roleGroupId,int roleId) { var rgQu = this.Db.RoleGroups.Where(m => m.Id == roleGroupId); if(!rgQu.Any()) { - throw new Exception("没有找到指定的角色组"); + throw new ApiException("没有找到指定的角色组"); } var rQu = this.Db.Roles.Where(m => m.Id == roleId); if(!rQu.Any()) { - throw new Exception("没有找到指定的角色"); + throw new ApiException("没有找到指定的角色"); } var rgr = this.Db.RoleGroup_Roles.Where(m => m.RoleGroupId == roleGroupId && m.RoleId == roleId); if(rgr.Any()) { diff --git a/FAuth/Controllers/api/Role_UserController.cs b/FAuth/Controllers/api/Role_UserController.cs index 01aa78b..780ea0c 100644 --- a/FAuth/Controllers/api/Role_UserController.cs +++ b/FAuth/Controllers/api/Role_UserController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using FAuth.DataBase.Tables; +using FAuth.Extensions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; @@ -26,11 +27,11 @@ namespace FAuth.Controllers.api public bool Binding(int roleId,int userId) { var rQu = this.Db.Roles.Where(m => m.Id == roleId); if(!rQu.Any()) { - throw new Exception("没有找到指定的角色"); + throw new ApiException("没有找到指定的角色"); } var uQu = this.Db.Users.Where(m => m.Id == userId); if(!uQu.Any()) { - throw new Exception("没有找到指定的用户"); + throw new ApiException("没有找到指定的用户"); } var ruQu = this.Db.Role_Users.Where(m => m.RoleId == roleId && m.UserId == userId); if(!ruQu.Any()) { @@ -53,11 +54,11 @@ namespace FAuth.Controllers.api public bool Unbind(int roleId,int userId) { var rQu = this.Db.Roles.Where(m => m.Id == roleId); if(!rQu.Any()) { - throw new Exception("没有找到指定的角色"); + throw new ApiException("没有找到指定的角色"); } var uQu = this.Db.Users.Where(m => m.Id == userId); if(!uQu.Any()) { - throw new Exception("没有找到指定的用户"); + throw new ApiException("没有找到指定的用户"); } var ruQu = this.Db.Role_Users.Where(m => m.RoleId == roleId && m.UserId == userId); if(ruQu.Any()) { diff --git a/FAuth/Controllers/api/UserController.cs b/FAuth/Controllers/api/UserController.cs index 43edbea..5c15773 100644 --- a/FAuth/Controllers/api/UserController.cs +++ b/FAuth/Controllers/api/UserController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Falcon.Extend; using FAuth.DataBase.Tables; +using FAuth.Extensions; using FAuth.Extensions.Account; using FAuth.Extensions.Decryptor; using FAuth.Models; @@ -38,11 +39,11 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(CheckUserResult),200)] public CheckUserResult Login(string userName,string password) { if(userName.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(userName)); + throw new ApiArgumentNullException(nameof(userName)); } var qu = this.Db.Users.Where(m => m.UserName == userName && m.Password == password); if(!qu.Any()) { - throw new Exception("提供的用户名或密码不正确"); + throw new ApiException("提供的用户名或密码不正确"); } var fir = qu.First(); var now = DateTimeOffset.Now; @@ -66,12 +67,12 @@ namespace FAuth.Controllers.api [HttpPost] public bool Logout(string ticket) { if(ticket.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(ticket)); + throw new ApiArgumentNullException(nameof(ticket)); } var userTicketModel = this.UserTicketDryptor.Decrypt(ticket); var qu = this.Db.Users.Where(m => m.Id == userTicketModel.Id); if(!qu.Any()) { - throw new Exception("提交的票据信息错误"); + throw new ApiException("提交的票据信息错误"); } var fir = qu.First(); fir.Status &= ~FUserStatusEnum.Login; @@ -90,16 +91,16 @@ namespace FAuth.Controllers.api [ProducesResponseType(typeof(UserInfo),200)] public UserInfo GetUserByTicket([BindRequired]string ticket) { if(ticket.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(ticket)); + throw new ApiArgumentNullException(nameof(ticket)); } var userTicketModel = this.UserTicketDryptor.Decrypt(ticket); var qu = this.Db.Users.Where(m => m.Id == userTicketModel.Id); if(!qu.Any()) { - throw new Exception("提交的票据信息错误"); + throw new ApiException("提交的票据信息错误"); } var fir = qu.First(); if(fir.LastLogoutDatetime.HasValue && userTicketModel.LoginDatetime < fir.LastLogoutDatetime.Value) { - throw new Exception("用户已登出"); + throw new ApiException("用户已登出"); } return new UserInfo { Id = fir.Id, @@ -137,12 +138,12 @@ namespace FAuth.Controllers.api [HttpPost] public bool ChangePassword(string ticket,string nPassword) { if(ticket.IsNullOrEmpty()) { - throw new ArgumentNullException(nameof(ticket)); + throw new ApiArgumentNullException(nameof(ticket)); } var userTicketModel = this.UserTicketDryptor.Decrypt(ticket); var qu = this.Db.Users.Where(m => m.Id == userTicketModel.Id); if(!qu.Any()) { - throw new Exception("没找到票据对应用户"); + throw new ApiException("没找到票据对应用户"); } foreach(var item in qu) { item.Password = nPassword; @@ -162,21 +163,19 @@ namespace FAuth.Controllers.api /// 是否成功 [HttpPost] public bool AddNewUser(string adminTicket,string userName,string name,string password) { - if(string.IsNullOrEmpty(adminTicket)) - throw new ArgumentException("必须提供管理票据",nameof(adminTicket)); - if(string.IsNullOrEmpty(userName)) - throw new ArgumentException("新用户登录名不能为空",nameof(userName)); - if(string.IsNullOrEmpty(name)) - throw new ArgumentException("用户姓名不能为空",nameof(name)); - if(string.IsNullOrEmpty(password)) - throw new ArgumentException("密码不能为空",nameof(password)); + if(adminTicket is null) + throw new ApiArgumentNullException(nameof(adminTicket)); + if(userName is null) + throw new ApiArgumentNullException(nameof(userName)); + if(password is null) + throw new ApiArgumentNullException(nameof(password)); if(this.Account.IsNotSystemAdmin(adminTicket)) { - throw new Exception($"用户必须在应用FAuth中具有Admin角色!"); + throw new ApiException($"用户必须在应用FAuth中具有Admin角色!"); } var qu = this.Db.Users.Where(m => m.UserName == userName); if(qu.Any()) { - throw new Exception("用户登录名已经存在,不能重复添加"); + throw new ApiException("用户登录名已经存在,不能重复添加"); } var nUser = new FUser { Name = name, @@ -198,19 +197,19 @@ namespace FAuth.Controllers.api /// 是否成功。成功True 否则返回False [HttpPost] public bool ResetUserPassword(string adminTicket,string userName,string newPassword) { - if(string.IsNullOrEmpty(adminTicket)) - throw new ArgumentException("必须提供管理票据",nameof(adminTicket)); - if(string.IsNullOrEmpty(userName)) - throw new ArgumentException("新用户登录名不能为空",nameof(userName)); - if(string.IsNullOrEmpty(newPassword)) - throw new ArgumentException("密码不能为空",nameof(newPassword)); + if(adminTicket is null) + throw new ApiArgumentNullException(nameof(adminTicket)); + if(userName is null) + throw new ApiArgumentNullException(nameof(userName)); + if(newPassword is null) + throw new ApiArgumentNullException(nameof(newPassword)); if(this.Account.IsNotSystemAdmin(adminTicket)) { - throw new Exception($"用户必须在应用FAuth中具有Admin角色!"); + throw new ApiException($"用户必须在应用FAuth中具有Admin角色!"); } var qu = this.Db.Users.Where(m => m.UserName == userName); if(!qu.Any()) { - throw new Exception("用户登录名不存在"); + throw new ApiException("用户登录名不存在"); } foreach(var item in qu) { item.Password = newPassword; diff --git a/FAuth/Extensions/ApiArgumentNullException.cs b/FAuth/Extensions/ApiArgumentNullException.cs new file mode 100644 index 0000000..3f9de7a --- /dev/null +++ b/FAuth/Extensions/ApiArgumentNullException.cs @@ -0,0 +1,11 @@ +namespace FAuth.Extensions +{ + /// + /// 表示请求的参数为空错误 + /// + public class ApiArgumentNullException:ApiException + { + public ApiArgumentNullException(string msg) : base($"{msg} 参数不能为空") { + } + } +} diff --git a/FAuth/Extensions/ApiException.cs b/FAuth/Extensions/ApiException.cs new file mode 100644 index 0000000..62038b7 --- /dev/null +++ b/FAuth/Extensions/ApiException.cs @@ -0,0 +1,16 @@ +using System; + +namespace FAuth.Extensions +{ + /// + /// 表示请求错误,服务器无法处理 + /// + public class ApiException:Exception + { + /// + /// 通过提供异常信息,返回api错误异常 + /// + /// + public ApiException(string msg) : base(msg) { } + } +} diff --git a/FAuth/Extensions/ApiExceptionFilterAttribute.cs b/FAuth/Extensions/ApiExceptionFilterAttribute.cs index 3402755..3fa93bf 100644 --- a/FAuth/Extensions/ApiExceptionFilterAttribute.cs +++ b/FAuth/Extensions/ApiExceptionFilterAttribute.cs @@ -25,7 +25,11 @@ namespace FAuth.Extensions Message = context.Exception.Message, Id = id, }; - context.Result = new JsonResult(result) { StatusCode = StatusCodes.Status500InternalServerError }; + var sc = + context.Exception is ApiException ? StatusCodes.Status400BadRequest : + StatusCodes.Status500InternalServerError; + + context.Result = new JsonResult(result) { StatusCode = sc }; } }