mirror of
https://github.com/acepanel/panel.git
synced 2026-02-04 10:17:17 +08:00
revert: 加回must_install中间件
This commit is contained in:
93
app/http/middleware/must_install.go
Normal file
93
app/http/middleware/must_install.go
Normal file
@@ -0,0 +1,93 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/goravel/framework/contracts/http"
|
||||
"github.com/goravel/framework/contracts/translation"
|
||||
"github.com/goravel/framework/facades"
|
||||
|
||||
"github.com/TheTNB/panel/internal/services"
|
||||
)
|
||||
|
||||
// MustInstall 确保已安装插件
|
||||
func MustInstall() http.Middleware {
|
||||
return func(ctx http.Context) {
|
||||
path := ctx.Request().Path()
|
||||
translate := facades.Lang(ctx)
|
||||
var slug string
|
||||
if strings.HasPrefix(path, "/api/panel/website") {
|
||||
slug = "openresty"
|
||||
} else if strings.HasPrefix(path, "/api/panel/container") {
|
||||
slug = "podman"
|
||||
} else {
|
||||
pathArr := strings.Split(path, "/")
|
||||
if len(pathArr) < 4 {
|
||||
ctx.Request().AbortWithStatusJson(http.StatusForbidden, http.Json{
|
||||
"message": translate.Get("errors.plugin.notExist"),
|
||||
})
|
||||
return
|
||||
}
|
||||
slug = pathArr[3]
|
||||
}
|
||||
|
||||
plugin := services.NewPluginImpl().GetBySlug(slug)
|
||||
installedPlugin := services.NewPluginImpl().GetInstalledBySlug(slug)
|
||||
installedPlugins, err := services.NewPluginImpl().AllInstalled()
|
||||
if err != nil {
|
||||
ctx.Request().AbortWithStatusJson(http.StatusInternalServerError, http.Json{
|
||||
"message": translate.Get("errors.internal"),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if installedPlugin.Slug != plugin.Slug {
|
||||
ctx.Request().AbortWithStatusJson(http.StatusForbidden, http.Json{
|
||||
"message": translate.Get("errors.plugin.notInstalled", translation.Option{
|
||||
Replace: map[string]string{
|
||||
"slug": slug,
|
||||
},
|
||||
}),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
pluginsMap := make(map[string]bool)
|
||||
|
||||
for _, p := range installedPlugins {
|
||||
pluginsMap[p.Slug] = true
|
||||
}
|
||||
|
||||
for _, require := range plugin.Requires {
|
||||
_, requireFound := pluginsMap[require]
|
||||
if !requireFound {
|
||||
ctx.Request().AbortWithStatusJson(http.StatusForbidden, http.Json{
|
||||
"message": translate.Get("errors.plugin.dependent", translation.Option{
|
||||
Replace: map[string]string{
|
||||
"slug": slug,
|
||||
"dependency": require,
|
||||
},
|
||||
}),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
for _, exclude := range plugin.Excludes {
|
||||
_, excludeFound := pluginsMap[exclude]
|
||||
if excludeFound {
|
||||
ctx.Request().AbortWithStatusJson(http.StatusForbidden, http.Json{
|
||||
"message": translate.Get("errors.plugin.incompatible", translation.Option{
|
||||
Replace: map[string]string{
|
||||
"slug": slug,
|
||||
"exclude": exclude,
|
||||
},
|
||||
}),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Request().Next()
|
||||
}
|
||||
}
|
||||
@@ -186,7 +186,13 @@
|
||||
}
|
||||
},
|
||||
"errors": {
|
||||
"internal": "internal system error"
|
||||
"internal": "internal system error",
|
||||
"plugin": {
|
||||
"notExist": "plugin does not exist",
|
||||
"notInstalled": "plugin :slug is not installed",
|
||||
"dependent": "plugin :slug requires dependency :dependency",
|
||||
"incompatible": "plugin :slug is incompatible with :exclude plugin"
|
||||
}
|
||||
},
|
||||
"messages": {
|
||||
"mistake": "mistake"
|
||||
|
||||
@@ -186,7 +186,13 @@
|
||||
}
|
||||
},
|
||||
"errors": {
|
||||
"internal": "系统内部错误"
|
||||
"internal": "系统内部错误",
|
||||
"plugin": {
|
||||
"notExist": "插件不存在",
|
||||
"notInstalled": "插件 :slug 未安装",
|
||||
"dependent": "插件 :slug 需要依赖 :dependency 插件",
|
||||
"incompatible": "插件 :slug 不兼容 :exclude 插件"
|
||||
}
|
||||
},
|
||||
"messages": {
|
||||
"mistake": "错误"
|
||||
|
||||
@@ -35,7 +35,7 @@ func Api() {
|
||||
r.Get("log", taskController.Log)
|
||||
r.Post("delete", taskController.Delete)
|
||||
})
|
||||
r.Prefix("website").Middleware(middleware.Jwt()).Group(func(r route.Router) {
|
||||
r.Prefix("website").Middleware(middleware.Jwt(), middleware.MustInstall()).Group(func(r route.Router) {
|
||||
websiteController := controllers.NewWebsiteController()
|
||||
r.Get("defaultConfig", websiteController.GetDefaultConfig)
|
||||
r.Post("defaultConfig", websiteController.SaveDefaultConfig)
|
||||
@@ -43,7 +43,7 @@ func Api() {
|
||||
r.Put("uploadBackup", websiteController.UploadBackup)
|
||||
r.Delete("deleteBackup", websiteController.DeleteBackup)
|
||||
})
|
||||
r.Prefix("websites").Middleware(middleware.Jwt()).Group(func(r route.Router) {
|
||||
r.Prefix("websites").Middleware(middleware.Jwt(), middleware.MustInstall()).Group(func(r route.Router) {
|
||||
websiteController := controllers.NewWebsiteController()
|
||||
r.Get("/", websiteController.List)
|
||||
r.Post("/", websiteController.Add)
|
||||
@@ -115,7 +115,7 @@ func Api() {
|
||||
r.Get("pingStatus", safeController.GetPingStatus)
|
||||
r.Post("pingStatus", safeController.SetPingStatus)
|
||||
})
|
||||
r.Prefix("container").Middleware(middleware.Jwt()).Group(func(r route.Router) {
|
||||
r.Prefix("container").Middleware(middleware.Jwt(), middleware.MustInstall()).Group(func(r route.Router) {
|
||||
containerController := controllers.NewContainerController()
|
||||
r.Get("list", containerController.ContainerList)
|
||||
r.Get("search", containerController.ContainerSearch)
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
// Plugin 加载插件路由
|
||||
func Plugin() {
|
||||
facades.Route().Prefix("api/plugins").Middleware(middleware.Jwt()).Group(func(r route.Router) {
|
||||
facades.Route().Prefix("api/plugins").Middleware(middleware.Jwt(), middleware.MustInstall()).Group(func(r route.Router) {
|
||||
r.Prefix("openresty").Group(func(route route.Router) {
|
||||
openRestyController := plugins.NewOpenrestyController()
|
||||
route.Get("load", openRestyController.Load)
|
||||
|
||||
Reference in New Issue
Block a user