Files
EdgeAdmin/internal/web/helpers/user_must_auth.go

355 lines
8.5 KiB
Go
Raw Normal View History

2020-07-22 22:19:39 +08:00
package helpers
import (
"github.com/TeaOSLab/EdgeAdmin/internal/configloaders"
2020-07-22 22:19:39 +08:00
teaconst "github.com/TeaOSLab/EdgeAdmin/internal/const"
2020-10-13 20:05:29 +08:00
"github.com/TeaOSLab/EdgeAdmin/internal/setup"
2020-07-22 22:19:39 +08:00
"github.com/iwind/TeaGo/actions"
2020-11-10 15:40:22 +08:00
"github.com/iwind/TeaGo/maps"
2020-07-22 22:19:39 +08:00
"net/http"
"reflect"
"strings"
2020-07-22 22:19:39 +08:00
)
// 认证拦截
2020-12-03 11:03:12 +08:00
type userMustAuth struct {
AdminId int64
2020-12-03 11:03:12 +08:00
module string
2020-07-22 22:19:39 +08:00
}
2020-12-03 11:03:12 +08:00
func NewUserMustAuth(module string) *userMustAuth {
return &userMustAuth{module: module}
2020-07-22 22:19:39 +08:00
}
2020-12-03 11:03:12 +08:00
func (this *userMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramName string) (goNext bool) {
var action = actionPtr.Object()
// 恢复模式
2021-07-20 17:15:17 +08:00
if teaconst.IsRecoverMode {
action.RedirectURL("/recover")
2021-07-20 17:15:17 +08:00
return false
}
// DEMO模式
if teaconst.IsDemoMode {
if action.Request.Method == http.MethodPost {
var actionName = action.Spec.ClassName[strings.LastIndex(action.Spec.ClassName, ".")+1:]
2021-07-22 08:25:14 +08:00
var denyPrefixes = []string{"Update", "Create", "Delete", "Truncate", "Clean", "Clear", "Reset", "Add", "Remove", "Sync"}
for _, prefix := range denyPrefixes {
if strings.HasPrefix(actionName, prefix) {
action.Fail(teaconst.ErrorDemoOperation)
return false
}
}
if strings.Index(action.Spec.PkgPath, "settings") > 0 || strings.Index(action.Spec.PkgPath, "delete") > 0 || strings.Index(action.Spec.PkgPath, "update") > 0 {
action.Fail(teaconst.ErrorDemoOperation)
return false
}
}
}
2020-07-22 22:19:39 +08:00
2020-11-10 12:47:24 +08:00
// 安全相关
securityConfig, _ := configloaders.LoadSecurityConfig()
2020-11-20 18:06:54 +08:00
if securityConfig == nil {
2020-11-10 12:47:24 +08:00
action.AddHeader("X-Frame-Options", "SAMEORIGIN")
2020-11-20 18:06:54 +08:00
} else if len(securityConfig.Frame) > 0 {
action.AddHeader("X-Frame-Options", securityConfig.Frame)
2020-11-10 12:47:24 +08:00
}
action.AddHeader("Content-Security-Policy", "default-src 'self' data:; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'")
// 检查IP
if !checkIP(securityConfig, action.RequestRemoteIP()) {
action.ResponseWriter.WriteHeader(http.StatusForbidden)
return false
}
2020-10-13 20:05:29 +08:00
// 检查系统是否已经配置过
if !setup.IsConfigured() {
action.RedirectURL("/setup")
return
}
2020-07-22 22:19:39 +08:00
var session = action.Session()
var adminId = session.GetInt64("adminId")
2020-12-07 11:45:45 +08:00
2020-07-22 22:19:39 +08:00
if adminId <= 0 {
this.login(action)
return false
}
// 检查用户是否存在
2020-12-07 11:45:45 +08:00
if !configloaders.CheckAdmin(adminId) {
session.Delete()
2020-07-22 22:19:39 +08:00
2020-12-07 11:45:45 +08:00
this.login(action)
2020-07-22 22:19:39 +08:00
return false
}
2020-12-07 11:45:45 +08:00
// 检查用户权限
if len(this.module) > 0 && !configloaders.AllowModule(adminId, this.module) {
action.ResponseWriter.WriteHeader(http.StatusForbidden)
action.WriteString("Permission Denied.")
2020-07-22 22:19:39 +08:00
return false
}
this.AdminId = adminId
action.Context.Set("adminId", this.AdminId)
if action.Request.Method != http.MethodGet {
return true
}
config, err := configloaders.LoadAdminUIConfig()
if err != nil {
action.WriteString(err.Error())
return false
}
2020-07-22 22:19:39 +08:00
// 初始化内置方法
action.ViewFunc("teaTitle", func() string {
return action.Data["teaTitle"].(string)
})
action.Data["teaShowVersion"] = config.ShowVersion
action.Data["teaTitle"] = config.AdminSystemName
action.Data["teaName"] = config.ProductName
action.Data["teaFaviconFileId"] = config.FaviconFileId
action.Data["teaLogoFileId"] = config.LogoFileId
2020-12-07 11:45:45 +08:00
action.Data["teaUsername"] = configloaders.FindAdminFullname(adminId)
2021-07-12 10:21:17 +08:00
action.Data["teaTheme"] = configloaders.FindAdminTheme(adminId)
action.Data["teaUserAvatar"] = ""
2020-11-27 15:18:32 +08:00
if !action.Data.Has("teaMenu") {
action.Data["teaMenu"] = ""
}
action.Data["teaModules"] = this.modules(adminId)
action.Data["teaSubMenus"] = []map[string]interface{}{}
action.Data["teaTabbar"] = []map[string]interface{}{}
if len(config.Version) == 0 {
action.Data["teaVersion"] = teaconst.Version
} else {
action.Data["teaVersion"] = config.Version
}
action.Data["teaShowOpenSourceInfo"] = config.ShowOpenSourceInfo
action.Data["teaIsSuper"] = false
2021-04-29 16:47:45 +08:00
action.Data["teaIsPlus"] = teaconst.IsPlus
action.Data["teaDemoEnabled"] = teaconst.IsDemoMode
action.Data["teaShowFinance"] = configloaders.ShowFinance()
if !action.Data.Has("teaSubMenu") {
action.Data["teaSubMenu"] = ""
}
2021-01-27 22:59:46 +08:00
action.Data["teaCheckNodeTasks"] = configloaders.AllowModule(adminId, configloaders.AdminModuleCodeNode)
action.Data["teaCheckDNSTasks"] = configloaders.AllowModule(adminId, configloaders.AdminModuleCodeDNS)
// 菜单
action.Data["firstMenuItem"] = ""
// 未读消息数
action.Data["teaBadge"] = 0
// 调用Init
initMethod := reflect.ValueOf(actionPtr).MethodByName("Init")
if initMethod.IsValid() {
initMethod.Call([]reflect.Value{})
}
return true
}
// 菜单配置
2020-12-03 11:03:12 +08:00
func (this *userMustAuth) modules(adminId int64) []maps.Map {
allMaps := []maps.Map{
2021-01-21 18:55:53 +08:00
{
"code": "dashboard",
"module": configloaders.AdminModuleCodeDashboard,
2021-01-21 19:22:06 +08:00
"name": "数据看板",
2021-01-21 18:55:53 +08:00
"icon": "dashboard",
},
2020-07-22 22:19:39 +08:00
{
2021-03-28 14:47:21 +08:00
"code": "servers",
"module": configloaders.AdminModuleCodeServer,
"name": "网站服务",
"subtitle": "服务列表",
"icon": "clone outsize",
2020-11-10 15:40:22 +08:00
"subItems": []maps.Map{
{
"name": "通用设置",
2020-11-10 15:40:22 +08:00
"url": "/servers/components",
2020-11-27 15:18:32 +08:00
"code": "global",
},
{
"name": "服务分组",
"url": "/servers/components/groups",
"code": "group",
},
{
"name": "缓存策略",
"url": "/servers/components/cache",
"code": "cache",
},
{
"name": "WAF策略",
"url": "/servers/components/waf",
"code": "waf",
2020-11-10 15:40:22 +08:00
},
2021-06-23 13:12:33 +08:00
{
"name": "IP名单",
"url": "/servers/iplists",
"code": "iplist",
},
2020-11-24 17:36:42 +08:00
{
"name": "证书管理",
"url": "/servers/certs",
"code": "cert",
},
2021-06-30 19:59:59 +08:00
{
2021-06-27 21:59:06 +08:00
"name": "统计指标",
"url": "/servers/metrics",
"code": "metric",
2021-06-30 19:59:59 +08:00
},
2020-11-10 15:40:22 +08:00
},
2020-07-22 22:19:39 +08:00
},
{
2021-03-28 14:47:21 +08:00
"code": "clusters",
"module": configloaders.AdminModuleCodeNode,
"name": "边缘节点",
"subtitle": "集群列表",
"icon": "cloud",
2020-11-10 15:45:48 +08:00
"subItems": []maps.Map{
2021-05-19 19:03:03 +08:00
{
2021-06-02 11:53:08 +08:00
"name": "运行日志",
2021-05-19 19:03:03 +08:00
"url": "/clusters/logs",
"code": "log",
},
2020-11-10 15:45:48 +08:00
{
"name": "SSH认证",
"url": "/clusters/grants",
"code": "grant",
},
2020-12-10 15:02:55 +08:00
{
"name": "区域设置",
"url": "/clusters/regions",
"code": "region",
},
2020-11-10 15:45:48 +08:00
},
2020-09-06 16:19:34 +08:00
},
2020-09-13 20:37:07 +08:00
{
2021-03-28 14:47:21 +08:00
"code": "dns",
"module": configloaders.AdminModuleCodeDNS,
"name": "域名解析",
"subtitle": "集群列表",
"icon": "globe",
2020-11-10 21:37:48 +08:00
"subItems": []maps.Map{
{
"name": "问题修复",
"url": "/dns/issues",
"code": "issue",
},
2020-11-10 21:37:48 +08:00
{
2020-11-11 21:32:19 +08:00
"name": "DNS服务商",
2020-11-10 21:37:48 +08:00
"url": "/dns/providers",
"code": "provider",
},
},
2020-09-13 20:37:07 +08:00
},
2021-05-25 15:47:40 +08:00
{
"code": "ns",
"module": configloaders.AdminModuleCodeNS,
2021-05-27 17:09:53 +08:00
"name": "域名服务",
2021-05-25 15:47:40 +08:00
"subtitle": "域名列表",
"icon": "cubes",
"isOn": teaconst.IsPlus,
"subItems": []maps.Map{
{
"name": "集群管理",
"url": "/ns/clusters",
"code": "cluster",
},
{
"name": "线路管理",
"url": "/ns/routes",
"code": "route",
},
2021-05-25 15:47:40 +08:00
{
2021-06-02 11:53:08 +08:00
"name": "访问日志",
"url": "/ns/clusters/accessLogs",
"code": "accessLog",
},
{
"name": "运行日志",
2021-05-27 17:09:53 +08:00
"url": "/ns/clusters/logs",
2021-05-25 15:47:40 +08:00
"code": "log",
},
},
},
{
"code": "users",
"module": configloaders.AdminModuleCodeUser,
"name": "平台用户",
"icon": "users",
},
{
"code": "finance",
"module": configloaders.AdminModuleCodeFinance,
"name": "财务管理",
"icon": "yen sign",
"isOn": teaconst.IsPlus,
},
{
2021-04-05 20:48:13 +08:00
"code": "admins",
"module": configloaders.AdminModuleCodeAdmin,
"name": "系统用户",
"subtitle": "用户列表",
"icon": "user secret",
"subItems": []maps.Map{
{
"name": "通知媒介",
"url": "/admins/recipients",
"code": "recipients",
2021-04-13 20:01:43 +08:00
"isOn": teaconst.IsPlus,
2021-04-05 20:48:13 +08:00
},
},
},
{
"code": "log",
"module": configloaders.AdminModuleCodeLog,
"name": "日志审计",
"icon": "history",
},
2020-10-10 20:28:36 +08:00
{
2021-03-28 14:47:21 +08:00
"code": "settings",
"module": configloaders.AdminModuleCodeSetting,
"name": "系统设置",
"subtitle": "基本设置",
"icon": "setting",
2020-12-14 21:24:21 +08:00
"subItems": []maps.Map{
{
"name": "高级设置",
"url": "/settings/advanced",
"code": "advanced",
},
},
2020-10-10 20:28:36 +08:00
},
2020-07-22 22:19:39 +08:00
}
result := []maps.Map{}
for _, m := range allMaps {
if m.GetString("code") == "finance" && !configloaders.ShowFinance() {
continue
}
module := m.GetString("module")
if configloaders.AllowModule(adminId, module) {
result = append(result, m)
}
}
return result
2020-07-22 22:19:39 +08:00
}
// 跳转到登录页
2020-12-03 11:03:12 +08:00
func (this *userMustAuth) login(action *actions.ActionObject) {
2020-07-22 22:19:39 +08:00
action.RedirectURL("/")
}