From ca1209c31020533d458c56fe3efaeff8c835135d Mon Sep 17 00:00:00 2001
From: falcon <9504402@qq.com>
Date: Tue, 29 Nov 2022 15:42:38 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=89=E5=85=A8=E9=AA=8C?=
=?UTF-8?q?=E8=AF=81=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../JWT/ApiAuthorizationAttribute.cs | 18 ++++++++++++++++--
Falcon.SugarApi/JWT/IUserLogin.cs | 2 +-
2 files changed, 17 insertions(+), 3 deletions(-)
diff --git a/Falcon.SugarApi/JWT/ApiAuthorizationAttribute.cs b/Falcon.SugarApi/JWT/ApiAuthorizationAttribute.cs
index 2f0639e..9a908ea 100644
--- a/Falcon.SugarApi/JWT/ApiAuthorizationAttribute.cs
+++ b/Falcon.SugarApi/JWT/ApiAuthorizationAttribute.cs
@@ -18,7 +18,21 @@ namespace Falcon.SugarApi.JWT
///
/// 用户需要具有的角色
///
- public List Roles { get; set; } = new List();
+ public string[] Roles { get; set; }
+
+ ///
+ /// 只需要登录即可通过验证
+ ///
+ public ApiAuthorizationAttribute() {
+ this.Roles = new string[] { };
+ }
+ ///
+ /// 需要登录并且具有一定的角色
+ ///
+ /// 角色组
+ public ApiAuthorizationAttribute(params string[] roles) {
+ this.Roles = roles;
+ }
///
public void OnAuthorization(AuthorizationFilterContext context) {
@@ -41,7 +55,7 @@ namespace Falcon.SugarApi.JWT
Unauthorized(context);
return;
}
- if (this.Roles != null && this.Roles.Count > 0 && !userLogin.UserInRoles(user, this.Roles)) {
+ if (this.Roles != null && this.Roles.Length > 0 && !userLogin.UserInRoles(user, this.Roles)) {
Unauthorized(context);
return;
}
diff --git a/Falcon.SugarApi/JWT/IUserLogin.cs b/Falcon.SugarApi/JWT/IUserLogin.cs
index c335415..a8118db 100644
--- a/Falcon.SugarApi/JWT/IUserLogin.cs
+++ b/Falcon.SugarApi/JWT/IUserLogin.cs
@@ -34,6 +34,6 @@ namespace Falcon.SugarApi.JWT
/// 用户信息
/// 需要具有的角色组
/// True具有,False不具有
- bool UserInRoles(LoginUserInfo userInfo, List roles);
+ bool UserInRoles(LoginUserInfo userInfo, params string[] roles);
}
}