Skip to content

Commit

Permalink
revert: 加回must_install中间件
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Jun 17, 2024
1 parent 634a9a3 commit 0be4ce9
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 6 deletions.
93 changes: 93 additions & 0 deletions app/http/middleware/must_install.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
8 changes: 7 additions & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion lang/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,13 @@
}
},
"errors": {
"internal": "系统内部错误"
"internal": "系统内部错误",
"plugin": {
"notExist": "插件不存在",
"notInstalled": "插件 :slug 未安装",
"dependent": "插件 :slug 需要依赖 :dependency 插件",
"incompatible": "插件 :slug 不兼容 :exclude 插件"
}
},
"messages": {
"mistake": "错误"
Expand Down
6 changes: 3 additions & 3 deletions routes/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ 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)
r.Get("backupList", websiteController.BackupList)
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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion routes/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0be4ce9

Please sign in to comment.