2021-09-28 11:47:19 +08:00
|
|
|
|
package router
|
|
|
|
|
|
|
|
|
|
import (
|
2021-11-24 11:12:46 +08:00
|
|
|
|
"SciencesServer/app/logic"
|
2021-09-28 11:47:19 +08:00
|
|
|
|
"SciencesServer/app/service"
|
2022-01-11 10:41:46 +08:00
|
|
|
|
"SciencesServer/app/session"
|
2021-09-28 11:47:19 +08:00
|
|
|
|
"SciencesServer/config"
|
|
|
|
|
"SciencesServer/utils"
|
2022-01-15 11:54:05 +08:00
|
|
|
|
"fmt"
|
2021-09-28 11:47:19 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2021-11-24 09:59:29 +08:00
|
|
|
|
"net/http"
|
2021-09-28 11:47:19 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// SkipperURL 跳过验证
|
|
|
|
|
type SkipperURL func(*gin.Context) bool
|
|
|
|
|
|
|
|
|
|
// PermissionHandle 权限验证
|
|
|
|
|
type PermissionHandle func(key string) gin.HandlerFunc
|
|
|
|
|
|
|
|
|
|
// AddSkipperURL 添加路由
|
|
|
|
|
func AddSkipperURL(url ...string) SkipperURL {
|
|
|
|
|
return func(c *gin.Context) bool {
|
|
|
|
|
path := c.Request.URL.Path
|
|
|
|
|
return utils.InArray(path, url)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NeedLogin 需要登录
|
2022-01-04 11:59:58 +08:00
|
|
|
|
func NeedLogin(key string, session logic.ISession, skipperURL ...SkipperURL) gin.HandlerFunc {
|
2021-09-28 11:47:19 +08:00
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
if len(skipperURL) > 0 && skipperURL[0](c) {
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
token := c.GetHeader(config.APIRequestToken)
|
|
|
|
|
|
|
|
|
|
if token == "" {
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"message": "Token异常"})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-01-04 11:59:58 +08:00
|
|
|
|
err := service.NewAuthToken(token).Auth(key, session)
|
2021-09-28 11:47:19 +08:00
|
|
|
|
|
2021-11-24 09:59:29 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{"message": err.Error()})
|
2021-09-28 11:47:19 +08:00
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Set(config.TokenForSession, session)
|
|
|
|
|
c.Next()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 11:54:05 +08:00
|
|
|
|
// NeedPermission 需要权限验证
|
|
|
|
|
func NeedPermission(skipperURL ...SkipperURL) gin.HandlerFunc {
|
2021-12-03 14:18:06 +08:00
|
|
|
|
return func(c *gin.Context) {
|
2022-01-15 11:54:05 +08:00
|
|
|
|
if len(skipperURL) > 0 && skipperURL[0](c) {
|
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
value, _ := c.Get(config.TokenForSession)
|
|
|
|
|
_session := value.(*session.Admin)
|
2021-12-03 14:18:06 +08:00
|
|
|
|
|
2022-03-05 15:31:22 +08:00
|
|
|
|
if _session.IsAdmin || _session.IsSystemAdmin {
|
2022-01-15 11:54:05 +08:00
|
|
|
|
c.Next()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
pass, err := service.NewPermission(
|
|
|
|
|
service.WithAuthTenant(_session.TenantIDFormat()),
|
2022-01-27 14:50:52 +08:00
|
|
|
|
service.WithAuthUser(_session.GetStringUID()),
|
2022-01-15 11:54:05 +08:00
|
|
|
|
service.WithAuthRequest([]*service.AuthRequest{
|
|
|
|
|
&service.AuthRequest{
|
|
|
|
|
Url: c.Request.URL.Path,
|
|
|
|
|
Method: c.Request.Method,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
).Enforce()
|
|
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
|
|
|
"message": fmt.Sprintf("权限验证错误【%v】,请联系管理员!", err),
|
|
|
|
|
})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
} else if !pass {
|
|
|
|
|
c.JSON(http.StatusForbidden, gin.H{"message": "无权限访问!"})
|
|
|
|
|
c.Abort()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
c.Next()
|
2021-12-03 14:18:06 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-15 11:54:05 +08:00
|
|
|
|
func NeedAuthIdentity() gin.HandlerFunc {
|
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
|
c.Next()
|
2021-09-28 11:47:19 +08:00
|
|
|
|
}
|
|
|
|
|
}
|